| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Multitenancy; |
| | | 3 | | using Elsa.Common.Services; |
| | | 4 | | using Elsa.Identity.Contracts; |
| | | 5 | | using Elsa.Identity.Entities; |
| | | 6 | | using Elsa.Identity.Models; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Identity.Services; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents an in-memory user store. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Ambient tenant is applied here rather than in callers. |
| | | 15 | | /// EF owns that via <c>SetTenantIdFilter</c> / <c>ApplyTenantId</c>; Memory must compensate. |
| | | 16 | | /// Null tenant IDs are visible only to the default tenant. |
| | | 17 | | /// </remarks> |
| | | 18 | | public class MemoryUserStore : IUserStore |
| | | 19 | | { |
| | | 20 | | private readonly MemoryStore<User> _store; |
| | | 21 | | private readonly ITenantAccessor _tenantAccessor; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="MemoryUserStore"/> class. |
| | | 25 | | /// </summary> |
| | 20 | 26 | | public MemoryUserStore(MemoryStore<User> store, ITenantAccessor tenantAccessor) |
| | | 27 | | { |
| | 20 | 28 | | _store = store; |
| | 20 | 29 | | _tenantAccessor = tenantAccessor; |
| | 20 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public Task SaveAsync(User user, CancellationToken cancellationToken = default) |
| | | 34 | | { |
| | 29 | 35 | | lock (_store.Sync) |
| | | 36 | | { |
| | 29 | 37 | | ApplyCurrentTenant(user); |
| | 62 | 38 | | MemoryIdentityUniqueness.EnsureAvailable(_store, user, x => x.Name, "name"); |
| | 52 | 39 | | _store.Save(user, x => x.Id); |
| | 26 | 40 | | } |
| | | 41 | | |
| | 26 | 42 | | return Task.CompletedTask; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public Task DeleteAsync(UserFilter filter, CancellationToken cancellationToken = default) |
| | | 47 | | { |
| | 2 | 48 | | lock (_store.Sync) |
| | | 49 | | { |
| | 2 | 50 | | _store.DeleteWhere(user => |
| | 6 | 51 | | IsVisible(user) && filter.Apply(new[] { user }.AsQueryable()).Any()); |
| | 2 | 52 | | } |
| | | 53 | | |
| | 2 | 54 | | return Task.CompletedTask; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public Task<IEnumerable<User>> FindManyAsync(UserFilter filter, CancellationToken cancellationToken = default) |
| | | 59 | | { |
| | 4 | 60 | | var result = _store.Query(query => Filter(query, filter)).Select(Clone).ToList(); |
| | 2 | 61 | | return Task.FromResult<IEnumerable<User>>(result); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | public Task<User?> FindAsync(UserFilter filter, CancellationToken cancellationToken = default) |
| | | 66 | | { |
| | 32 | 67 | | var result = _store.Query(query => Filter(query, filter)).Select(Clone).FirstOrDefault(); |
| | 16 | 68 | | return Task.FromResult(result); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private IQueryable<User> Filter(IQueryable<User> queryable, UserFilter filter) => |
| | 18 | 72 | | filter.Apply(queryable.WhereVisibleToTenant(_tenantAccessor.TenantId)); |
| | | 73 | | |
| | 4 | 74 | | private bool IsVisible(Entity entity) => TenantVisibility.IsVisible(entity.TenantId, _tenantAccessor.TenantId); |
| | | 75 | | |
| | | 76 | | private void ApplyCurrentTenant(Entity entity) |
| | | 77 | | { |
| | 29 | 78 | | if (entity.TenantId == Tenant.AgnosticTenantId) |
| | 5 | 79 | | return; |
| | | 80 | | |
| | 24 | 81 | | entity.TenantId ??= _tenantAccessor.TenantId; |
| | 24 | 82 | | } |
| | | 83 | | |
| | | 84 | | private static User Clone(User user) => |
| | 17 | 85 | | new() |
| | 17 | 86 | | { |
| | 17 | 87 | | Id = user.Id, |
| | 17 | 88 | | Name = user.Name, |
| | 17 | 89 | | TenantId = user.TenantId, |
| | 17 | 90 | | HashedPassword = user.HashedPassword, |
| | 17 | 91 | | HashedPasswordSalt = user.HashedPasswordSalt, |
| | 17 | 92 | | Roles = user.Roles.ToList() |
| | 17 | 93 | | }; |
| | | 94 | | } |