Files
mixtape/zero.Core/Identity/RavenRoleStore(TRole).cs
T

150 lines
4.0 KiB
C#
Raw Normal View History

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