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
19a90c4
commit eb04287
Showing
22 changed files
with
251 additions
and
256 deletions.
There are no files selected for viewing
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
6 changes: 3 additions & 3 deletions
6
.../SelectUsersInRoleTask.Fields.Edit.cshtml → ...ems/GetUsersByRoleTask.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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
@using OrchardCore.Roles.Workflows.ViewModels | ||
@model SelectUsersInRoleTaskViewModel | ||
@model GetUsersByRoleTaskViewModel | ||
|
||
<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> | ||
<span class="hint">@T["The designated key name for storing the users' list in the workflow output. This key enables later access to the list within the workflow.With Liquid support."]</span> | ||
</div> | ||
|
||
<div class="mb-3" asp-validation-class-for="Roles"> | ||
<label asp-for="Roles">@T["Roles"]</label> | ||
@await Component.InvokeAsync("SelectRoles", new { selectedRoles = Model.Roles, htmlName = Html.NameFor(m => m.Roles), except = new[] { "Anonymous", "Authenticated" } }) | ||
<span asp-validation-for="Roles"></span> | ||
<span class="hint">@T["The Roles to search for. With Liquid support."]</span> | ||
<span class="hint">@T["Choose the roles used to identify users during list generation."]</span> | ||
</div> |
2 changes: 2 additions & 0 deletions
2
...hardCore.Modules/OrchardCore.Roles/Views/Items/GetUsersByRoleTask.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["Get users in roles"]</h4> | ||
<p>@T["Get users in roles."]</p> |
16 changes: 0 additions & 16 deletions
16
src/OrchardCore.Modules/OrchardCore.Roles/Views/Items/RemoveUserRoleTask.Fields.Edit.cshtml
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
...hardCore.Modules/OrchardCore.Roles/Views/Items/RemoveUserRoleTask.Fields.Thumbnail.cshtml
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
...dCore.Modules/OrchardCore.Roles/Views/Items/SelectUsersInRoleTask.Fields.Thumbnail.cshtml
This file was deleted.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
...OrchardCore.Modules/OrchardCore.Roles/Views/Items/UnassignUserRoleTask.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.Roles.Workflows.ViewModels | ||
@model UnassignUserRoleTaskViewModel | ||
|
||
<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="Roles"> | ||
<label asp-for="Roles">@T["Roles"]</label> | ||
@await Component.InvokeAsync("SelectRoles", new { selectedRoles = Model.Roles, htmlName = Html.NameFor(m => m.Roles), except = new[] { "Anonymous", "Authenticated" } }) | ||
<span asp-validation-for="Roles"></span> | ||
<span class="hint">@T["The Roles to remove."]</span> | ||
</div> |
2 changes: 2 additions & 0 deletions
2
...rdCore.Modules/OrchardCore.Roles/Views/Items/UnassignUserRoleTask.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["Unassign user from roles"]</h4> | ||
<p>@T["Unassign a user from roles."]</p> |
73 changes: 73 additions & 0 deletions
73
src/OrchardCore.Modules/OrchardCore.Roles/Views/Workflows/Activities/GetUsersByRoleTask.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,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.Users; | ||
using OrchardCore.Users.Models; | ||
using OrchardCore.Workflows.Abstractions.Models; | ||
using OrchardCore.Workflows.Activities; | ||
using OrchardCore.Workflows.Models; | ||
using OrchardCore.Workflows.Services; | ||
|
||
namespace OrchardCore.Roles.Workflows.Activities; | ||
|
||
public class GetUsersByRoleTask : TaskActivity | ||
{ | ||
private readonly UserManager<IUser> _userManager; | ||
private readonly IWorkflowExpressionEvaluator _expressionEvaluator; | ||
protected readonly IStringLocalizer S; | ||
|
||
public GetUsersByRoleTask(UserManager<IUser> userManager, IWorkflowExpressionEvaluator expressionvaluator, IStringLocalizer<GetUsersByRoleTask> localizer) | ||
{ | ||
_userManager = userManager; | ||
_expressionEvaluator = expressionvaluator; | ||
S = localizer; | ||
} | ||
|
||
public override string Name => nameof(GetUsersByRoleTask); | ||
|
||
public override LocalizedString DisplayText => S["Get Users by Role Task"]; | ||
|
||
public override LocalizedString Category => S["User"]; | ||
|
||
public WorkflowExpression<string> OutputKeyName | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<string>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public IEnumerable<string> Roles | ||
{ | ||
get => GetProperty(() => new List<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 propKeyName = await _expressionEvaluator.EvaluateAsync(OutputKeyName, workflowContext, null); | ||
|
||
if (!string.IsNullOrEmpty(propKeyName)) | ||
{ | ||
if (Roles.Any()) | ||
{ | ||
HashSet<IUser> usersInRole = new HashSet<IUser>(); | ||
foreach (var role in Roles) | ||
{ | ||
usersInRole.UnionWith(await _userManager.GetUsersInRoleAsync(role)); | ||
} | ||
if (usersInRole.Any()) | ||
{ | ||
workflowContext.Output[propKeyName] = usersInRole.Select(u => (u as User).UserId).ToArray(); | ||
return Outcomes("Done"); | ||
} | ||
} | ||
} | ||
return Outcomes("Failed"); | ||
} | ||
} |
75 changes: 0 additions & 75 deletions
75
src/OrchardCore.Modules/OrchardCore.Roles/Views/Workflows/Activities/RemoveUserRoleTask.cs
This file was deleted.
Oops, something went wrong.
74 changes: 0 additions & 74 deletions
74
...OrchardCore.Modules/OrchardCore.Roles/Views/Workflows/Activities/SelectUsersInRoleTask.cs
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
src/OrchardCore.Modules/OrchardCore.Roles/Views/Workflows/Activities/UnassignUserRoleTask.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,76 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.Users; | ||
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.Roles.Workflows.Activities; | ||
|
||
public class UnassignUserRoleTask : TaskActivity | ||
{ | ||
private readonly UserManager<IUser> _userManager; | ||
private readonly IUserService _userService; | ||
private readonly IWorkflowExpressionEvaluator _expressionEvaluator; | ||
protected readonly IStringLocalizer S; | ||
|
||
public UnassignUserRoleTask(UserManager<IUser> userManager, IUserService userService, IWorkflowExpressionEvaluator expressionvaluator, IStringLocalizer<UnassignUserRoleTask> localizer) | ||
{ | ||
_userManager = userManager; | ||
_userService = userService; | ||
_expressionEvaluator = expressionvaluator; | ||
S = localizer; | ||
} | ||
|
||
public override string Name => nameof(UnassignUserRoleTask); | ||
|
||
public override LocalizedString DisplayText => S["Unassign User Role Task"]; | ||
|
||
public override LocalizedString Category => S["User"]; | ||
|
||
public WorkflowExpression<string> UserName | ||
{ | ||
get => GetProperty(() => new WorkflowExpression<string>()); | ||
set => SetProperty(value); | ||
} | ||
|
||
public IEnumerable<string> Roles | ||
{ | ||
get => GetProperty(() => new List<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 usr = await _userService.GetUserAsync(userName); | ||
|
||
if (usr is User user) | ||
{ | ||
if (Roles.Any()) | ||
{ | ||
foreach(var role in Roles) | ||
{ | ||
if (user.RoleNames.Contains(role)) | ||
{ | ||
await _userManager.RemoveFromRoleAsync(user, role); | ||
} | ||
} | ||
return Outcomes("Done"); | ||
} | ||
} | ||
return Outcomes("Failed"); | ||
} | ||
} |
Oops, something went wrong.