| | | 1 | | using Elsa.Identity.Contracts; |
| | | 2 | | using Elsa.Identity.Entities; |
| | | 3 | | using Elsa.Identity.Models; |
| | | 4 | | using Elsa.Workflows; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Identity.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Default implementation of <see cref="IUserManager"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | public class UserManager : IUserManager |
| | | 12 | | { |
| | | 13 | | private readonly IIdentityGenerator _identityGenerator; |
| | | 14 | | private readonly ISecretGenerator _secretGenerator; |
| | | 15 | | private readonly ISecretHasher _secretHasher; |
| | | 16 | | private readonly IUserStore _userStore; |
| | | 17 | | |
| | 3 | 18 | | public UserManager( |
| | 3 | 19 | | IIdentityGenerator identityGenerator, |
| | 3 | 20 | | ISecretGenerator secretGenerator, |
| | 3 | 21 | | ISecretHasher secretHasher, |
| | 3 | 22 | | IUserStore userStore) |
| | | 23 | | { |
| | 3 | 24 | | _identityGenerator = identityGenerator; |
| | 3 | 25 | | _secretGenerator = secretGenerator; |
| | 3 | 26 | | _secretHasher = secretHasher; |
| | 3 | 27 | | _userStore = userStore; |
| | 3 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public async Task<CreateUserResult> CreateUserAsync( |
| | | 32 | | string name, |
| | | 33 | | string? password = null, |
| | | 34 | | ICollection<string>? roles = null, |
| | | 35 | | CancellationToken cancellationToken = default) |
| | | 36 | | { |
| | 0 | 37 | | var id = _identityGenerator.GenerateId(); |
| | 0 | 38 | | var plainTextPassword = string.IsNullOrWhiteSpace(password) ? _secretGenerator.Generate() : password.Trim(); |
| | 0 | 39 | | var hashedPassword = _secretHasher.HashSecret(plainTextPassword); |
| | | 40 | | |
| | 0 | 41 | | var user = new User |
| | 0 | 42 | | { |
| | 0 | 43 | | Id = id, |
| | 0 | 44 | | Name = name, |
| | 0 | 45 | | Roles = roles ?? new List<string>(), |
| | 0 | 46 | | HashedPassword = hashedPassword.EncodeSecret(), |
| | 0 | 47 | | HashedPasswordSalt = hashedPassword.EncodeSalt() |
| | 0 | 48 | | }; |
| | | 49 | | |
| | 0 | 50 | | await _userStore.SaveAsync(user, cancellationToken); |
| | | 51 | | |
| | 0 | 52 | | return new CreateUserResult(user, plainTextPassword); |
| | 0 | 53 | | } |
| | | 54 | | } |