| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Identity.Contracts; |
| | | 3 | | using Elsa.Identity.Entities; |
| | | 4 | | using Elsa.Identity.Models; |
| | | 5 | | using Elsa.Workflows; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Default implementation of <see cref="IUserManager"/>. |
| | | 11 | | /// </summary> |
| | 7 | 12 | | public class UserManager( |
| | 7 | 13 | | IIdentityGenerator identityGenerator, |
| | 7 | 14 | | ISecretGenerator secretGenerator, |
| | 7 | 15 | | ISecretHasher secretHasher, |
| | 7 | 16 | | IUserStore userStore, |
| | 7 | 17 | | ITenantAccessor tenantAccessor) |
| | | 18 | | : IUserManager |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public async Task<CreateUserResult> CreateUserAsync( |
| | | 22 | | string name, |
| | | 23 | | string? password = null, |
| | | 24 | | ICollection<string>? roles = null, |
| | | 25 | | CancellationToken cancellationToken = default) |
| | | 26 | | { |
| | 4 | 27 | | var id = identityGenerator.GenerateId(); |
| | 4 | 28 | | var isPasswordGenerated = string.IsNullOrWhiteSpace(password); |
| | 4 | 29 | | var plainTextPassword = isPasswordGenerated ? secretGenerator.Generate() : password!.Trim(); |
| | 4 | 30 | | var hashedPassword = secretHasher.HashSecret(plainTextPassword); |
| | | 31 | | |
| | 4 | 32 | | var user = new User |
| | 4 | 33 | | { |
| | 4 | 34 | | Id = id, |
| | 4 | 35 | | Name = name, |
| | 4 | 36 | | // Set explicitly rather than relying on the Entity Framework saving handler, which does not run |
| | 4 | 37 | | // on the in-memory path and left users unassigned there. |
| | 4 | 38 | | TenantId = tenantAccessor.TenantId, |
| | 4 | 39 | | Roles = roles ?? new List<string>(), |
| | 4 | 40 | | HashedPassword = hashedPassword.EncodeSecret(), |
| | 4 | 41 | | HashedPasswordSalt = hashedPassword.EncodeSalt() |
| | 4 | 42 | | }; |
| | | 43 | | |
| | 4 | 44 | | await userStore.SaveAsync(user, cancellationToken); |
| | | 45 | | |
| | 4 | 46 | | return new CreateUserResult(user, plainTextPassword, isPasswordGenerated); |
| | 4 | 47 | | } |
| | | 48 | | } |