| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Identity.Contracts; |
| | | 4 | | using Elsa.Permissions; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Endpoints.Roles.Update; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// An endpoint that updates an existing role. |
| | | 11 | | /// </summary> |
| | | 12 | | [PublicAPI] |
| | 3 | 13 | | internal class Update(IRoleStore roleStore, IRoleAuthorizationService roleAuthorizationService, IPermissionGrantValidato |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public override void Configure() |
| | | 17 | | { |
| | 3 | 18 | | Put("/identity/roles/{id}"); |
| | 3 | 19 | | RequirePermission(Elsa.Identity.Permissions.IdentityPermissions.Roles, CoreVerbs.Update); |
| | 3 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 24 | | { |
| | 0 | 25 | | var id = Route<string>("id")!; |
| | | 26 | | |
| | 0 | 27 | | var role = await roleStore.FindAsync(new() |
| | 0 | 28 | | { |
| | 0 | 29 | | Id = id |
| | 0 | 30 | | }, cancellationToken); |
| | | 31 | | |
| | 0 | 32 | | if (role == null) |
| | | 33 | | { |
| | 0 | 34 | | await Send.NotFoundAsync(cancellationToken); |
| | 0 | 35 | | return; |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | var validation = grantValidator.Validate(request.Permissions); |
| | | 39 | | |
| | 0 | 40 | | if (!validation.IsValid) |
| | | 41 | | { |
| | | 42 | | // Both parts matter: the permission identifies which entry to fix, the reason says how. |
| | 0 | 43 | | foreach (var error in validation.Errors) |
| | 0 | 44 | | AddError($"{error.Permission} — {error.Reason}"); |
| | | 45 | | |
| | 0 | 46 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | if (!roleAuthorizationService.CanMutateRole(User, role, request.Permissions)) |
| | | 51 | | { |
| | 0 | 52 | | await Send.ForbiddenAsync(cancellationToken); |
| | 0 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | if (!string.IsNullOrWhiteSpace(request.Name)) |
| | 0 | 57 | | role.Name = request.Name.Trim(); |
| | | 58 | | |
| | 0 | 59 | | if (request.Permissions != null) |
| | 0 | 60 | | role.Permissions = request.Permissions; |
| | | 61 | | |
| | 0 | 62 | | await roleStore.SaveAsync(role, cancellationToken); |
| | 0 | 63 | | await securityNotifier.RoleChangedAsync(User, "updated", role.Id, role.Name, role.Permissions.ToArray(), cancell |
| | | 64 | | |
| | 0 | 65 | | var response = new Response( |
| | 0 | 66 | | role.Id, |
| | 0 | 67 | | role.Name, |
| | 0 | 68 | | role.Permissions, |
| | 0 | 69 | | role.TenantId); |
| | | 70 | | |
| | 0 | 71 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 72 | | } |
| | | 73 | | } |