forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to Remove User from a Role (Issue OrchardCMS#14632)
- Loading branch information
1 parent
d27de16
commit ad9768c
Showing
12 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...OrchardCore.Modules/OrchardCore.Users/Views/Items/RemoveUserRoleTask.Fields.Design.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@using OrchardCore.Workflows.ViewModels | ||
@using OrchardCore.Workflows.Helpers | ||
@using OrchardCore.Users.Workflows.Activities | ||
@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> | ||
</header> | ||
<em>@T["{0} from role {1}", Model.Activity.UserName, Model.Activity.RoleName]</em> |
16 changes: 16 additions & 0 deletions
16
src/OrchardCore.Modules/OrchardCore.Users/Views/Items/RemoveUserRoleTask.Fields.Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@using OrchardCore.Users.Workflows.ViewModels | ||
@model RemoveUserRoleTaskViewModel | ||
|
||
<div class="mb-3" asp-validation-class-for="UserName"> | ||
<label asp-for="UserName">@T["UserName"]</label> | ||
<input type="text" asp-for="UserName" class="form-control code" /> | ||
<span asp-validation-for="UserName"></span> | ||
<span class="hint">@T["The User to update. With Liquid support."]</span> | ||
</div> | ||
|
||
<div class="mb-3" asp-validation-class-for="RoleName"> | ||
<label asp-for="RoleName">@T["RoleName"]</label> | ||
<input type="text" asp-for="RoleName" class="form-control code" /> | ||
<span asp-validation-for="RoleName"></span> | ||
<span class="hint">@T["The Role to remove. With Liquid support."]</span> | ||
</div> |
2 changes: 2 additions & 0 deletions
2
...hardCore.Modules/OrchardCore.Users/Views/Items/RemoveUserRoleTask.Fields.Thumbnail.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h4 class="card-title"><i class="fa-solid fa-user" aria-hidden="true"></i>@T["Remove user from role"]</h4> | ||
<p>@T["Removes a user from a role."]</p> |
9 changes: 9 additions & 0 deletions
9
...hardCore.Modules/OrchardCore.Users/Views/Items/SelectUsersInRoleTask.Fields.Design.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@using OrchardCore.Workflows.ViewModels | ||
@using OrchardCore.Workflows.Helpers | ||
@using OrchardCore.Users.Workflows.Activities | ||
@using OrchardCore.Users.Workflows.ViewModels | ||
@model ActivityViewModel<SelectUsersInRoleTask> | ||
<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> |
16 changes: 16 additions & 0 deletions
16
...rchardCore.Modules/OrchardCore.Users/Views/Items/SelectUsersInRoleTask.Fields.Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@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> | ||
|
||
<div class="mb-3" asp-validation-class-for="RoleName"> | ||
<label asp-for="RoleName">@T["RoleName"]</label> | ||
<input type="text" asp-for="RoleName" class="form-control code" /> | ||
<span asp-validation-for="RoleName"></span> | ||
<span class="hint">@T["The Role to search for. With Liquid support."]</span> | ||
</div> |
2 changes: 2 additions & 0 deletions
2
...dCore.Modules/OrchardCore.Users/Views/Items/SelectUsersInRoleTask.Fields.Thumbnail.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h4 class="card-title"><i class="fa-solid fa-user" aria-hidden="true"></i>@T["Select users in role"]</h4> | ||
<p>@T["Select users in role."]</p> |
74 changes: 74 additions & 0 deletions
74
src/OrchardCore.Modules/OrchardCore.Users/Workflows/Activities/RemoveUserRoleTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
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; | ||
|
||
namespace OrchardCore.Users.Workflows.Activities | ||
{ | ||
public class RemoveUserRoleTask : TaskActivity | ||
{ | ||
private readonly UserManager<IUser> _userManager; | ||
private readonly IUserService _userService; | ||
private readonly IWorkflowExpressionEvaluator _expressionEvaluator; | ||
protected readonly IStringLocalizer S; | ||
|
||
public RemoveUserRoleTask(UserManager<IUser> userManager, IUserService userService, IWorkflowExpressionEvaluator expressionvaluator, IStringLocalizer<RemoveUserRoleTask> localizer) | ||
{ | ||
_userManager = userManager; | ||
_userService = userService; | ||
_expressionEvaluator = expressionvaluator; | ||
S = localizer; | ||
} | ||
|
||
public override string Name => nameof(RemoveUserRoleTask); | ||
|
||
public override LocalizedString DisplayText => S["Remove User Role Task"]; | ||
|
||
public override LocalizedString Category => S["User"]; | ||
|
||
public WorkflowExpression<string> UserName | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<string>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public WorkflowExpression<string> RoleName | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<string>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext) | ||
{ | ||
return Outcomes(S["Done"], S["Failed"]); | ||
} | ||
|
||
public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext) | ||
{ | ||
var userName = await _expressionEvaluator.EvaluateAsync(UserName, workflowContext, null); | ||
var roleName = await _expressionEvaluator.EvaluateAsync(RoleName, workflowContext, null); | ||
|
||
var user = (User)await _userService.GetUserAsync(userName); | ||
|
||
if (user != null) | ||
{ | ||
if (user.RoleNames.Contains(roleName)) | ||
{ | ||
await _userManager.RemoveFromRoleAsync(user, roleName); | ||
} | ||
|
||
return Outcomes("Done"); | ||
} | ||
else | ||
{ | ||
return Outcomes("Failed"); | ||
} | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/OrchardCore.Modules/OrchardCore.Users/Workflows/Activities/SelectUsersInRoleTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
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; | ||
|
||
namespace OrchardCore.Users.Workflows.Activities | ||
{ | ||
public class SelectUsersInRoleTask : TaskActivity | ||
{ | ||
private readonly UserManager<IUser> _userManager; | ||
private readonly IUserService _userService; | ||
private readonly IWorkflowExpressionEvaluator _expressionEvaluator; | ||
protected readonly IStringLocalizer S; | ||
|
||
public SelectUsersInRoleTask(UserManager<IUser> userManager, IUserService userService, IWorkflowExpressionEvaluator expressionvaluator, IStringLocalizer<SelectUsersInRoleTask> localizer) | ||
{ | ||
_userManager = userManager; | ||
_userService = userService; | ||
_expressionEvaluator = expressionvaluator; | ||
S = localizer; | ||
} | ||
|
||
public override string Name => nameof(SelectUsersInRoleTask); | ||
|
||
public override LocalizedString DisplayText => S["Select Users in Role Task"]; | ||
|
||
public override LocalizedString Category => S["User"]; | ||
|
||
public WorkflowExpression<string> PropertyName | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<string>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public WorkflowExpression<string> RoleName | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<string>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContext workflowContext, ActivityContext activityContext) | ||
{ | ||
return Outcomes(S["Done"], S["Empty"], S["Failed"]); | ||
} | ||
|
||
public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecutionContext workflowContext, ActivityContext activityContext) | ||
{ | ||
var propName = await _expressionEvaluator.EvaluateAsync(PropertyName, workflowContext, null); | ||
var roleName = await _expressionEvaluator.EvaluateAsync(RoleName, workflowContext, null); | ||
|
||
if (!string.IsNullOrEmpty(propName) && !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); | ||
return Outcomes("Done"); | ||
} | ||
return Outcomes("Empty"); | ||
} | ||
return Outcomes("Failed"); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...rchardCore.Modules/OrchardCore.Users/Workflows/Drivers/RemoveUserRoleTaskDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using OrchardCore.Workflows.Display; | ||
using OrchardCore.Workflows.Models; | ||
using OrchardCore.Users.Workflows.Activities; | ||
using OrchardCore.Users.Workflows.ViewModels; | ||
|
||
namespace OrchardCore.Users.Workflows.Drivers | ||
{ | ||
public class RemoveUserRoleTaskDisplayDriver : ActivityDisplayDriver<RemoveUserRoleTask, RemoveUserRoleTaskViewModel> | ||
{ | ||
protected override void EditActivity(RemoveUserRoleTask activity, RemoveUserRoleTaskViewModel model) | ||
{ | ||
model.UserName = activity.UserName.Expression; | ||
model.RoleName = activity.RoleName.Expression; | ||
} | ||
|
||
protected override void UpdateActivity(RemoveUserRoleTaskViewModel model, RemoveUserRoleTask activity) | ||
{ | ||
activity.UserName = new WorkflowExpression<string>(model.UserName); | ||
activity.RoleName = new WorkflowExpression<string>(model.RoleName); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ardCore.Modules/OrchardCore.Users/Workflows/Drivers/SelectUsersInRoleTaskDisplayDriver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using OrchardCore.Users.Workflows.Activities; | ||
using OrchardCore.Users.Workflows.ViewModels; | ||
using OrchardCore.Workflows.Display; | ||
using OrchardCore.Workflows.Models; | ||
|
||
namespace OrchardCore.Users.Workflows.Drivers | ||
{ | ||
public class SelectUsersInRoleTaskDisplayDriver : ActivityDisplayDriver<SelectUsersInRoleTask, SelectUsersInRoleTaskViewModel> | ||
{ | ||
protected override void EditActivity(SelectUsersInRoleTask activity, SelectUsersInRoleTaskViewModel model) | ||
{ | ||
model.PropertyName = activity.PropertyName.Expression; | ||
model.RoleName = activity.RoleName.Expression; | ||
} | ||
|
||
protected override void UpdateActivity(SelectUsersInRoleTaskViewModel model, SelectUsersInRoleTask activity) | ||
{ | ||
activity.PropertyName = new WorkflowExpression<string>(model.PropertyName); | ||
activity.RoleName = new WorkflowExpression<string>(model.RoleName); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...OrchardCore.Modules/OrchardCore.Users/Workflows/ViewModels/RemoveUserRoleTaskViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace OrchardCore.Users.Workflows.ViewModels | ||
{ | ||
public class RemoveUserRoleTaskViewModel | ||
{ | ||
[Required] | ||
public string UserName { get; set; } | ||
|
||
[Required] | ||
public string RoleName { get; set; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...hardCore.Modules/OrchardCore.Users/Workflows/ViewModels/SelectUsersInRoleTaskViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace OrchardCore.Users.Workflows.ViewModels | ||
{ | ||
public class SelectUsersInRoleTaskViewModel | ||
{ | ||
[Required] | ||
public string PropertyName { get; set; } | ||
|
||
[Required] | ||
public string RoleName { get; set; } | ||
} | ||
} |