using Microsoft.AspNetCore.Identity; using System.Security.Claims; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Finch.Identity; namespace Finch.Identity; public class FinchRoleStore : IRoleStore, IRoleClaimStore where TRole : FinchIdentityRole, new() { protected IdentityErrorDescriber ErrorDescriber { get; private set; } protected virtual IFinchIdentityStoreDbProvider Db { get; set; } public FinchRoleStore(IFinchIdentityStoreDbProvider db, IdentityErrorDescriber describer = null) { Db = db; ErrorDescriber = describer ?? new IdentityErrorDescriber(); } private IdentityResult Fail(Result result) { IdentityError[] errors = new IdentityError[result.Errors.Count]; int index = 0; foreach (ResultError error in result.Errors) { string message = error.Message + "(key: " + error.Property + ")"; errors[index++] = new() { Code = "finch/raven/500", Description = message }; } return IdentityResult.Failed(errors); } /// public async Task CreateAsync(TRole role, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (role == null) { throw new ArgumentNullException(nameof(role)); } try { Result result = await Db.Create(role); if (!result.IsSuccess) { return Fail(result); } } catch (Exception ex) { return IdentityResult.Failed(new IdentityError { Description = ex.Message }); } return IdentityResult.Success; } /// public async Task UpdateAsync(TRole role, CancellationToken cancellationToken) { Result result = await Db.Update(role); if (!result.IsSuccess) { return Fail(result); } return IdentityResult.Success; } /// public async Task DeleteAsync(TRole role, CancellationToken cancellationToken) { Result result = await Db.Delete(role); if (!result.IsSuccess) { return Fail(result); } 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 Task SetNormalizedRoleNameAsync(TRole role, string normalizedName, CancellationToken cancellationToken) => SetRoleNameAsync(role, normalizedName, cancellationToken); /// public async Task FindByIdAsync(string roleId, CancellationToken cancellationToken) { // TODO index return await Db.Load(roleId); } /// public async Task FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) { // TODO index return await Db.Find(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) => 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; } }