using Microsoft.AspNetCore.Identity; using Raven.Client.Documents; using Raven.Client.Documents.Linq; using Raven.Client.Exceptions; using System.Security.Claims; namespace zero.Identity; public class RavenRoleStore : EntityStore, IRoleStore, IRoleClaimStore where TRole : ZeroIdentityRole, new() { protected IdentityErrorDescriber ErrorDescriber { get; private set; } protected bool Global { get; private set; } public RavenRoleStore(IStoreContext storeContext, IdentityErrorDescriber describer = null, bool global = false) : base(storeContext) { Global = global; Config.Database = global ? Options.For().Database : null; Config.IncludeInactive = true; ErrorDescriber = describer ?? new IdentityErrorDescriber(); } // /// Scope queries to an optional application or something else /// protected virtual IRavenQueryable ScopeQuery(IRavenQueryable query) => query; /// public async Task CreateAsync(TRole role, CancellationToken cancellationToken) { await Create(role); return IdentityResult.Success; } /// public async Task UpdateAsync(TRole role, CancellationToken cancellationToken) { try { await Update(role); } catch (ConcurrencyException) { return IdentityResult.Failed(ErrorDescriber.ConcurrencyFailure()); } return IdentityResult.Success; } /// public async Task DeleteAsync(TRole role, CancellationToken cancellationToken) { try { await Delete(role); } catch (ConcurrencyException) { return IdentityResult.Failed(ErrorDescriber.ConcurrencyFailure()); } return IdentityResult.Success; } /// public Task GetRoleIdAsync(TRole role, CancellationToken cancellationToken) => Task.FromResult(role.Id); /// public Task GetRoleNameAsync(TRole role, CancellationToken cancellationToken) => Task.FromResult(role.Name); /// public Task SetRoleNameAsync(TRole role, string roleName, CancellationToken cancellationToken) { role.Name = roleName; return Task.CompletedTask; } /// public Task GetNormalizedRoleNameAsync(TRole role, CancellationToken cancellationToken) => Task.FromResult(role.Name); /// public async Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken) { await SetRoleNameAsync(role, normalizedName, cancellationToken); } /// public async Task FindByIdAsync(string roleId, CancellationToken cancellationToken) { return await ScopeQuery(Session.Query()).FirstOrDefaultAsync(x => x.Id == roleId, cancellationToken); } /// public async Task FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) { return await ScopeQuery(Session.Query()).FirstOrDefaultAsync(x => x.Name == normalizedRoleName, cancellationToken); } /// public void Dispose() { } /* * **************************************************** * CLAIM * **************************************************** */ /// public Task AddClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default) { role.Claims.Add(new(claim)); return Task.CompletedTask; } /// public Task> GetClaimsAsync(TRole role, CancellationToken cancellationToken = default) { return Task.FromResult((IList)role.Claims.Select(claim => claim.ToClaim()).ToList()); } /// public Task RemoveClaimAsync(TRole role, Claim claim, CancellationToken cancellationToken = default) { UserClaim userClaim = new(claim); role.Claims = role.Claims.Except(new List() { userClaim }, new UserClaimComparer()).ToList(); return Task.CompletedTask; } }