Skip to content

Commit

Permalink
Add a way to Remove User from a Role (Issue OrchardCMS#14632)
Browse files Browse the repository at this point in the history
Update after code review
  • Loading branch information
elaurentin committed Nov 10, 2023
1 parent 45dc46d commit 1e76fef
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
@using OrchardCore.Users.Workflows.ViewModels
@model ActivityViewModel<RemoveUserRoleTask>
<header>
<h4><i class="fa-solid fa-user" aria-hidden="true"></i>@Model.Activity.GetTitleOrDefault(() => T["Remove user from role"])</h4>
<h4><i class="fa-solid fa-user" aria-hidden="true"></i>@Model.Activity.GetTitleOrDefault(() => T["Remove user from a role"])</h4>
</header>
<em>@T["{0} from role {1}", Model.Activity.UserName, Model.Activity.RoleName]</em>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<header>
<h4><i class="fa-solid fa-user" aria-hidden="true"></i>@Model.Activity.GetTitleOrDefault(() => T["Select users in role"])</h4>
</header>
<em>@T["Store users of role {0} to {1}", Model.Activity.RoleName, Model.Activity.PropertyName]</em>
<em>@T["Stores users from role {0} to {1}", Model.Activity.RoleName, Model.Activity.OutputKeyName]</em>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@using OrchardCore.Users.Workflows.ViewModels
@model SelectUsersInRoleTaskViewModel

<div class="mb-3" asp-validation-class-for="PropertyName">
<label asp-for="PropertyName">@T["PropertyName"]</label>
<input type="text" asp-for="PropertyName" class="form-control code" />
<span asp-validation-for="PropertyName"></span>
<span class="hint">@T["The PropertyName to save list. With Liquid support."]</span>
<div class="mb-3" asp-validation-class-for="OutputKeyName">
<label asp-for="OutputKeyName">@T["OutputKeyName"]</label>
<input type="text" asp-for="OutputKeyName" class="form-control code" />
<span asp-validation-for="OutputKeyName"></span>
<span class="hint">@T["The Output Key Name to save list. With Liquid support."]</span>
</div>

<div class="mb-3" asp-validation-class-for="RoleName">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -34,7 +33,7 @@ public SelectUsersInRoleTask(UserManager<IUser> userManager, IUserService userSe

public override LocalizedString Category => S["User"];

public WorkflowExpression<string> PropertyName
public WorkflowExpression<string> OutputKeyName
{
get => GetProperty(() => new WorkflowExpression<string>());
set => SetProperty(value);
Expand All @@ -48,32 +47,22 @@ public WorkflowExpression<string> RoleName

public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
return Outcomes(S["Done"], S["Empty"], S["Failed"]);
return Outcomes(S["Done"], S["Failed"]);
}

public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext)
{
var propName = await _expressionEvaluator.EvaluateAsync(PropertyName, workflowContext, null);
var propKeyName = await _expressionEvaluator.EvaluateAsync(OutputKeyName, workflowContext, null);
var roleName = await _expressionEvaluator.EvaluateAsync(RoleName, workflowContext, null);

if (!string.IsNullOrEmpty(propName) && !string.IsNullOrEmpty(roleName))
if (!string.IsNullOrEmpty(propKeyName) && !string.IsNullOrEmpty(roleName))
{
var usersInRole = await _userManager.GetUsersInRoleAsync(roleName);
if (usersInRole.Count > 0)
{
List<string> output;
if (propName.Contains("email", StringComparison.InvariantCultureIgnoreCase))
{
output = usersInRole.Select((u) => (u as User)?.Email).ToList();
}
else
{
output = usersInRole.Select(u => u.UserName).ToList();
}
workflowContext.Properties[propName] = string.Join(",", output);
workflowContext.Output[propKeyName] = usersInRole.Select(u => (u as User).UserId).ToArray();
return Outcomes("Done");
}
return Outcomes("Empty");
}
return Outcomes("Failed");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public class SelectUsersInRoleTaskDisplayDriver : ActivityDisplayDriver<SelectUs
{
protected override void EditActivity(SelectUsersInRoleTask activity, SelectUsersInRoleTaskViewModel model)
{
model.PropertyName = activity.PropertyName.Expression;
model.OutputKeyName = activity.OutputKeyName.Expression;
model.RoleName = activity.RoleName.Expression;
}

protected override void UpdateActivity(SelectUsersInRoleTaskViewModel model, SelectUsersInRoleTask activity)
{
activity.PropertyName = new WorkflowExpression<string>(model.PropertyName);
activity.OutputKeyName = new WorkflowExpression<string>(model.OutputKeyName);
activity.RoleName = new WorkflowExpression<string>(model.RoleName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OrchardCore.Users.Workflows.ViewModels
public class SelectUsersInRoleTaskViewModel
{
[Required]
public string PropertyName { get; set; }
public string OutputKeyName { get; set; }

[Required]
public string RoleName { get; set; }
Expand Down

0 comments on commit 1e76fef

Please sign in to comment.