| | | 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 role store. |
| | | 11 | | /// </summary> |
| | | 12 | | public class MemoryRoleStore : IRoleStore, IRoleStoreWithAtomicDelete |
| | | 13 | | { |
| | | 14 | | private readonly MemoryStore<Role> _store; |
| | | 15 | | private readonly ITenantAccessor _tenantAccessor; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="MemoryRoleStore"/> class. |
| | | 19 | | /// </summary> |
| | 32 | 20 | | public MemoryRoleStore(MemoryStore<Role> store, ITenantAccessor tenantAccessor) |
| | | 21 | | { |
| | 32 | 22 | | _store = store; |
| | 32 | 23 | | _tenantAccessor = tenantAccessor; |
| | 32 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public Task AddAsync(Role role, CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 2 | 29 | | _store.Save(role, GetStorageKey); |
| | 2 | 30 | | return Task.CompletedTask; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public Task DeleteAsync(RoleFilter filter, CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | 4 | 36 | | var roles = _store.Query(query => Filter(query, filter)).ToList(); |
| | 2 | 37 | | _store.DeleteMany(roles, GetStorageKey); |
| | 2 | 38 | | return Task.CompletedTask; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | /// <remarks> |
| | | 43 | | /// The matching role's key is removed through the underlying concurrent dictionary, whose removal is a single |
| | | 44 | | /// compare-and-remove step. Two callers racing on the same role ID therefore see one <see langword="true"/> and |
| | | 45 | | /// one <see langword="false"/>, rather than both concluding they deleted it. |
| | | 46 | | /// </remarks> |
| | | 47 | | public Task<bool> TryDeleteAsync(string roleId, CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 8 | 49 | | var role = _store.Query(query => Filter(query, new RoleFilter { Id = roleId })).FirstOrDefault(); |
| | 4 | 50 | | var deleted = role is not null && _store.Delete(GetStorageKey(role)); |
| | | 51 | | |
| | 4 | 52 | | return Task.FromResult(deleted); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public Task SaveAsync(Role role, CancellationToken cancellationToken = default) |
| | | 57 | | { |
| | 28 | 58 | | _store.Save(role, GetStorageKey); |
| | 28 | 59 | | return Task.CompletedTask; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public Task<Role?> FindAsync(RoleFilter filter, CancellationToken cancellationToken = default) |
| | | 64 | | { |
| | 130 | 65 | | var result = _store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 65 | 66 | | return Task.FromResult(result); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public Task<IEnumerable<Role>> FindManyAsync(RoleFilter filter, CancellationToken cancellationToken = default) |
| | | 71 | | { |
| | 18 | 72 | | var result = _store.Query(query => Filter(query, filter)).ToList().AsEnumerable(); |
| | 9 | 73 | | return Task.FromResult(result); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <remarks> |
| | | 77 | | /// The ambient tenant is applied here rather than left to callers. Isolation previously existed only |
| | | 78 | | /// on the Entity Framework path, and only when multitenancy was enabled, so a deployment running the |
| | | 79 | | /// default in-memory stores had none at all. Null tenant IDs are retained only for the default tenant |
| | | 80 | | /// for backwards compatibility with records created before tenant assignment was added. |
| | | 81 | | /// </remarks> |
| | | 82 | | private IQueryable<Role> Filter(IQueryable<Role> queryable, RoleFilter filter) |
| | | 83 | | { |
| | 80 | 84 | | var tenantId = _tenantAccessor.TenantId; |
| | 80 | 85 | | queryable = queryable.Where(x => x.TenantId == tenantId || x.TenantId == Tenant.AgnosticTenantId || (x.TenantId |
| | | 86 | | |
| | 80 | 87 | | return filter.Apply(queryable); |
| | | 88 | | } |
| | | 89 | | |
| | 36 | 90 | | private static string GetStorageKey(Role role) => GetStorageKey(role.TenantId, role.Id); |
| | | 91 | | |
| | | 92 | | private static string GetStorageKey(string? tenantId, string roleId) => |
| | 36 | 93 | | $"{tenantId?.Length ?? -1}:{tenantId}{roleId.Length}:{roleId}"; |
| | | 94 | | } |