| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Models; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Endpoints.Roles.Create; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// An endpoint that creates a new role. |
| | | 11 | | /// </summary> |
| | | 12 | | [PublicAPI] |
| | 3 | 13 | | internal class Create(IRoleManager roleManager, IRoleAuthorizationService roleAuthorizationService) : ElsaEndpoint<Reque |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public override void Configure() |
| | | 17 | | { |
| | 3 | 18 | | Post("/identity/roles"); |
| | 3 | 19 | | ConfigurePermissions("create:role"); |
| | 3 | 20 | | Policies(IdentityPolicyNames.SecurityRoot); |
| | 3 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 25 | | { |
| | 0 | 26 | | if (!roleAuthorizationService.CanCreateRoleWithPermissions(User, request.Permissions)) |
| | | 27 | | { |
| | 0 | 28 | | await Send.ForbiddenAsync(cancellationToken); |
| | 0 | 29 | | return; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | CreateRoleResult result; |
| | | 33 | | try |
| | | 34 | | { |
| | 0 | 35 | | result = await roleManager.CreateRoleAsync( |
| | 0 | 36 | | request.Name, |
| | 0 | 37 | | request.Permissions, |
| | 0 | 38 | | request.Id, |
| | 0 | 39 | | cancellationToken); |
| | 0 | 40 | | } |
| | 0 | 41 | | catch (InvalidOperationException ex) when (ex.Message.Contains("already exists", StringComparison.OrdinalIgnoreC |
| | | 42 | | { |
| | 0 | 43 | | await Send.ErrorsAsync(StatusCodes.Status409Conflict, cancellationToken); |
| | 0 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | var response = new Response( |
| | 0 | 48 | | result.Role.Id, |
| | 0 | 49 | | result.Role.Name, |
| | 0 | 50 | | result.Role.Permissions); |
| | | 51 | | |
| | 0 | 52 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 53 | | } |
| | | 54 | | } |