| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Entities; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | using Humanizer; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Identity.Endpoints.Roles.Create; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// An endpoint that creates a new role. |
| | | 12 | | /// </summary> |
| | | 13 | | [PublicAPI] |
| | | 14 | | internal class Create : ElsaEndpoint<Request, Response> |
| | | 15 | | { |
| | | 16 | | private readonly IIdentityGenerator _identityGenerator; |
| | | 17 | | private readonly ISecretGenerator _secretGenerator; |
| | | 18 | | private readonly ISecretHasher _secretHasher; |
| | | 19 | | private readonly IUserStore _userStore; |
| | | 20 | | private readonly IRoleStore _roleStore; |
| | | 21 | | |
| | 1 | 22 | | public Create( |
| | 1 | 23 | | IIdentityGenerator identityGenerator, |
| | 1 | 24 | | ISecretGenerator secretGenerator, |
| | 1 | 25 | | ISecretHasher secretHasher, |
| | 1 | 26 | | IUserStore userStore, |
| | 1 | 27 | | IRoleStore roleStore) |
| | | 28 | | { |
| | 1 | 29 | | _identityGenerator = identityGenerator; |
| | 1 | 30 | | _secretGenerator = secretGenerator; |
| | 1 | 31 | | _secretHasher = secretHasher; |
| | 1 | 32 | | _userStore = userStore; |
| | 1 | 33 | | _roleStore = roleStore; |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public override void Configure() |
| | | 38 | | { |
| | 1 | 39 | | Post("/identity/roles"); |
| | 1 | 40 | | ConfigurePermissions("create:role"); |
| | 1 | 41 | | Policies(IdentityPolicyNames.SecurityRoot); |
| | 1 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 46 | | { |
| | 0 | 47 | | var id = request.Id ?? request.Name.Kebaberize(); |
| | | 48 | | |
| | 0 | 49 | | var role = new Role |
| | 0 | 50 | | { |
| | 0 | 51 | | Id = id, |
| | 0 | 52 | | Name = request.Name, |
| | 0 | 53 | | Permissions = request.Permissions ?? new List<string>() |
| | 0 | 54 | | }; |
| | | 55 | | |
| | 0 | 56 | | await _roleStore.SaveAsync(role, cancellationToken); |
| | | 57 | | |
| | 0 | 58 | | var response = new Response( |
| | 0 | 59 | | id, |
| | 0 | 60 | | role.Name, |
| | 0 | 61 | | role.Permissions); |
| | | 62 | | |
| | 0 | 63 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 64 | | } |
| | | 65 | | } |