| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Common.Services; |
| | | 3 | | using Elsa.Identity.Contracts; |
| | | 4 | | using Elsa.Identity.Entities; |
| | | 5 | | using Elsa.Identity.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Identity.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents an in-memory user store. |
| | | 11 | | /// </summary> |
| | | 12 | | public class MemoryUserStore : IUserStore |
| | | 13 | | { |
| | | 14 | | private readonly MemoryStore<User> _store; |
| | | 15 | | private readonly ITenantAccessor _tenantAccessor; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="MemoryUserStore"/> class. |
| | | 19 | | /// </summary> |
| | 3 | 20 | | public MemoryUserStore(MemoryStore<User> store, ITenantAccessor tenantAccessor) |
| | | 21 | | { |
| | 3 | 22 | | _store = store; |
| | 3 | 23 | | _tenantAccessor = tenantAccessor; |
| | 3 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public Task SaveAsync(User user, CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 4 | 29 | | _store.Save(user, x => x.Id); |
| | 2 | 30 | | return Task.CompletedTask; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public Task DeleteAsync(UserFilter filter, CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | 0 | 36 | | var ids = _store.Query(query => Filter(query, filter)).Select(x => x.Id).Distinct().ToList(); |
| | 0 | 37 | | _store.DeleteWhere(x => ids.Contains(x.Id)); |
| | 0 | 38 | | return Task.CompletedTask; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public Task<IEnumerable<User>> FindManyAsync(UserFilter filter, CancellationToken cancellationToken = default) |
| | | 43 | | { |
| | 0 | 44 | | var result = _store.Query(query => Filter(query, filter)).ToList(); |
| | 0 | 45 | | return Task.FromResult<IEnumerable<User>>(result); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public Task<User?> FindAsync(UserFilter filter, CancellationToken cancellationToken = default) |
| | | 50 | | { |
| | 4 | 51 | | var result = _store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 2 | 52 | | return Task.FromResult(result); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <remarks> |
| | | 56 | | /// The ambient tenant is applied here rather than left to callers. Isolation previously existed only |
| | | 57 | | /// on the Entity Framework path, and only when multitenancy was enabled, so a deployment running the |
| | | 58 | | /// default in-memory stores had none at all. |
| | | 59 | | /// </remarks> |
| | | 60 | | private IQueryable<User> Filter(IQueryable<User> queryable, UserFilter filter) |
| | | 61 | | { |
| | 2 | 62 | | var tenantId = _tenantAccessor.TenantId; |
| | 2 | 63 | | queryable = queryable.Where(x => x.TenantId == tenantId || x.TenantId == Tenant.AgnosticTenantId || x.TenantId = |
| | | 64 | | |
| | 2 | 65 | | return filter.Apply(queryable); |
| | | 66 | | } |
| | | 67 | | } |