using System.Linq; using System.Threading; using xCommons.Extensions; using xIdentityModels.Dtos; using xExceptions.Constants; using System.Threading.Tasks; using xIdentityModels.Models; using System.Collections.Generic; namespace xIdentityService.Providers { public partial class XAuthorizationProvider { // // #region Role ... // public async Task> GetRolesAsync( // CancellationToken cancellationToken = default // ) // { // // // var result = await roles // .GetAllAsync(cancellationToken: cancellationToken); // // // return result; // } // public async Task GetRoleAsync( // string name, // CancellationToken cancellationToken = default // ) // { // // // var result = await FindRoleOrThrowAsync(name, cancellationToken); // // // return result; // } // public async Task AddRoleAsync( // XRoleDefinitionDto item, // XUserClaimsInfoDto userInfo, // CancellationToken cancellationToken = default // ) // { // // // // Validate ... // if (item.IsNull() || item.Name.IsNullOrEmpty()) // { // XException.InvalidArgs.Throw(); // } // // // // Duplicate check ... // var normalized = item.Name.ToNormalString(); // var exists = await roles.FindOneAsync( // predicate: r => r.Name.ToLower() == normalized, // cancellationToken: cancellationToken // ); // if (!exists.IsNull()) // { // XException.Duplicate.Throw(); // } // // // // Persist ... // item.IsSystem = false; // var result = await roles.AddAsync( // item: item, // userInfo: userInfo, // cancellationToken: cancellationToken // ); // // // // Sync to IdentityServer (RoleManager) ... // var entity = roles.AsQueryable() // .FirstOrDefault(r => r.Name.ToLower() == normalized); // if (!entity.IsNull()) // { // await roleSync.UpsertAsync(entity, cancellationToken); // } // // // await InvalidateAsync(); // return result; // } // public async Task UpdateRoleAsync( // string name, // XRoleDefinitionDto item, // XUserClaimsInfoDto userInfo, // CancellationToken cancellationToken = default // ) // { // // // // Validate ... // if (name.IsNullOrEmpty() || item.IsNull()) // { // XException.InvalidArgs.Throw(); // } // // // var existing = await FindRoleOrThrowAsync(name, cancellationToken); // if (existing.IsSystem) // { // XException.NotAllowed.Throw(); // } // // // var updated = await roles.UpdateAsync( // id: existing.Id, // item: item, // userInfo: userInfo, // cancellationToken: cancellationToken // ); // // // // Sync to IdentityServer ... // var entity = roles.AsQueryable() // .FirstOrDefault(r => r.Name.ToLower() == name.ToNormalString()); // if (!entity.IsNull()) // { // await roleSync.UpsertAsync(entity, cancellationToken); // } // // // await InvalidateAsync(); // return updated; // } // public async Task RemoveRoleAsync( // string name, // XUserClaimsInfoDto userInfo, // CancellationToken cancellationToken = default // ) // { // // // var exists = await FindRoleOrThrowAsync(name, cancellationToken); // if (exists.IsSystem) // { // XException.NotAllowed.Throw(); // } // // // await roles.RemoveAsync( // id: exists.Id, // userInfo: userInfo, // cancellationToken: cancellationToken // ); // // // // Remove from IdentityServer ... // await roleSync.RemoveAsync(name, cancellationToken); // // // await InvalidateAsync(); // return true; // } // public async Task CountUsersInRoleAsync( // string name, // CancellationToken cancellationToken = default // ) // { // // // // Fail fast if the role itself does not exist ... // await FindRoleOrThrowAsync(name, cancellationToken); // // // var result = await roleSync // .CountUsersInRoleAsync( // name, // cancellationToken // ); // // // return result; // } // #endregion } }