| | | 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 application 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 | | /// Application contracts have no TenantAgnostic flag, so isolation always applies (EF query filter). |
| | | 17 | | /// </remarks> |
| | | 18 | | public class MemoryApplicationStore : IApplicationStore |
| | | 19 | | { |
| | | 20 | | private readonly MemoryStore<Application> _store; |
| | | 21 | | private readonly ITenantAccessor _tenantAccessor; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="MemoryApplicationStore"/> class. |
| | | 25 | | /// </summary> |
| | 18 | 26 | | public MemoryApplicationStore(MemoryStore<Application> store, ITenantAccessor tenantAccessor) |
| | | 27 | | { |
| | 18 | 28 | | _store = store; |
| | 18 | 29 | | _tenantAccessor = tenantAccessor; |
| | 18 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public Task SaveAsync(Application application, CancellationToken cancellationToken = default) |
| | | 34 | | { |
| | 28 | 35 | | lock (_store.Sync) |
| | | 36 | | { |
| | 28 | 37 | | ApplyCurrentTenant(application); |
| | 61 | 38 | | MemoryIdentityUniqueness.EnsureAvailable(_store, application, x => x.Name, "name"); |
| | 52 | 39 | | MemoryIdentityUniqueness.EnsureAvailable(_store, application, x => x.ClientId, "client id"); |
| | 48 | 40 | | _store.Save(application, x => x.Id); |
| | 24 | 41 | | } |
| | | 42 | | |
| | 24 | 43 | | return Task.CompletedTask; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public Task DeleteAsync(ApplicationFilter filter, CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 2 | 49 | | lock (_store.Sync) |
| | | 50 | | { |
| | 2 | 51 | | _store.DeleteWhere(application => |
| | 6 | 52 | | IsVisible(application) && filter.Apply(new[] { application }.AsQueryable()).Any()); |
| | 2 | 53 | | } |
| | | 54 | | |
| | 2 | 55 | | return Task.CompletedTask; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public Task<Application?> FindAsync(ApplicationFilter filter, CancellationToken cancellationToken = default) |
| | | 60 | | { |
| | 44 | 61 | | var result = _store.Query(query => Filter(query, filter)).Select(Clone).FirstOrDefault(); |
| | 22 | 62 | | return Task.FromResult(result); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | private IQueryable<Application> Filter(IQueryable<Application> queryable, ApplicationFilter filter) => |
| | 22 | 66 | | filter.Apply(queryable.WhereVisibleToTenant(_tenantAccessor.TenantId)); |
| | | 67 | | |
| | 4 | 68 | | private bool IsVisible(Entity entity) => TenantVisibility.IsVisible(entity.TenantId, _tenantAccessor.TenantId); |
| | | 69 | | |
| | | 70 | | private void ApplyCurrentTenant(Entity entity) |
| | | 71 | | { |
| | 28 | 72 | | if (entity.TenantId == Tenant.AgnosticTenantId) |
| | 4 | 73 | | return; |
| | | 74 | | |
| | 24 | 75 | | entity.TenantId ??= _tenantAccessor.TenantId; |
| | 24 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static Application Clone(Application application) => |
| | 18 | 79 | | new() |
| | 18 | 80 | | { |
| | 18 | 81 | | Id = application.Id, |
| | 18 | 82 | | Name = application.Name, |
| | 18 | 83 | | ClientId = application.ClientId, |
| | 18 | 84 | | TenantId = application.TenantId, |
| | 18 | 85 | | HashedApiKey = application.HashedApiKey, |
| | 18 | 86 | | HashedApiKeySalt = application.HashedApiKeySalt, |
| | 18 | 87 | | HashedClientSecret = application.HashedClientSecret, |
| | 18 | 88 | | HashedClientSecretSalt = application.HashedClientSecretSalt, |
| | 18 | 89 | | Roles = application.Roles.ToList() |
| | 18 | 90 | | }; |
| | | 91 | | } |