-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathManagePersonsPermissionCheckerTask.cs
84 lines (70 loc) · 3.32 KB
/
ManagePersonsPermissionCheckerTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Lombiq.TrainingDemo.Permissions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Localization;
using OrchardCore.Users.Models;
using OrchardCore.Users.Services;
using OrchardCore.Workflows.Abstractions.Models;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Models;
using OrchardCore.Workflows.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Lombiq.TrainingDemo.Activities;
// A simple workflow task that accepts a username as a TextField input and checks whether the user has ManagePersons
// Permission or not.
public class ManagePersonsPermissionCheckerTask : TaskActivity
{
private readonly IAuthorizationService _authorizationService;
private readonly IUserService _userService;
private readonly IWorkflowExpressionEvaluator _expressionEvaluator;
private readonly IStringLocalizer S;
public ManagePersonsPermissionCheckerTask(
IAuthorizationService authorizationService,
IUserService userService,
IWorkflowExpressionEvaluator expressionEvaluator,
IStringLocalizer<ManagePersonsPermissionCheckerTask> localizer)
{
_authorizationService = authorizationService;
_userService = userService;
_expressionEvaluator = expressionEvaluator;
S = localizer;
}
// The technical name of the activity. Activities in a workflow definition reference this name.
public override string Name => nameof(ManagePersonsPermissionCheckerTask);
// The displayed name of the activity, so it can use localization.
public override LocalizedString DisplayText => S["Manage Persons Permission Checker Task"];
// The category to which this activity belongs. The activity picker groups activities by this category.
public override LocalizedString Category => S["User"];
// The username to evaluate for ManagePersons permission.
public WorkflowExpression<string> UserName
{
get => GetProperty(() => new WorkflowExpression<string>());
set => SetProperty(value);
}
// Returns the possible outcomes of this activity.
public override IEnumerable<Outcome> GetPossibleOutcomes(
WorkflowExecutionContext workflowContext,
ActivityContext activityContext) =>
Outcomes(S["HasPermission"], S["NoPermission"]);
// This is the heart of the activity and actually performs the work to be done.
public override async Task<ActivityExecutionResult> ExecuteAsync(
WorkflowExecutionContext workflowContext,
ActivityContext activityContext)
{
var userName = await _expressionEvaluator.EvaluateAsync(UserName, workflowContext, encoder: null);
var user = (User)await _userService.GetUserAsync(userName);
if (user != null)
{
var userClaim = await _userService.CreatePrincipalAsync(user);
if (await _authorizationService.AuthorizeAsync(userClaim, PersonPermissions.ManagePersons))
{
return Outcomes("HasPermission");
}
}
return Outcomes("NoPermission");
}
}
// NEXT STATION: Now you have to create a ViewModel for the ActivityDisplayDriver. Check out the following file
// and come back here.
// ViewModels/ManagePersonsPermissionCheckerTaskViewModel.cs
// NEXT STATION: Drivers/ManagePersonsPermissionCheckerTaskDisplayDriver.cs