using System; using System.Linq; using xModels.Dtos; using System.Threading; using xCommons.Extensions; using xIdentityModels.Dtos; using xExceptions.Constants; using System.Threading.Tasks; using xIdentityModels.Models; using System.Collections.Generic; namespace xIds.Providers { public partial class XAuthorizationProvider { // #region Role ... public async Task RemoveRoleAsync( string name, XUserClaimsInfoDto userInfo, CancellationToken cancellationToken = default ) { // var existing = await FindRoleOrThrowAsync(name, cancellationToken); if (existing.IsSystem) { XException.NotAllowed.Throw(); } // // ⚠️ CRITICAL: capture affected users BEFORE delete. // After DeleteRoleAsync, AspNetUserRoles rows are gone. var affectedUsers = await manager.UserManager.GetUsersInRoleAsync(existing.Name); var affectedUserIds = affectedUsers.Select(u => u.Id).ToList(); // // Delete from local DB await roles.RemoveAsync( id: existing.Id, userInfo: userInfo, cancellationToken: cancellationToken ); // // Delete from IdentityServer — this cascades AspNetUserRoles await manager.DeleteRoleAsync(existing.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 usersInRole = await manager .UserManager.GetUsersInRoleAsync(name); // var result = usersInRole.Count(); // 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() || userInfo.IsNullOrDefault() ) { XException.InvalidArgs.Throw(); } // // Duplicate check via repository (NOT AsQueryable) ... 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 locally first item.IsSystem = false; var result = await roles.AddAsync( item: item, userInfo: userInfo, cancellationToken: cancellationToken ); // // Sync to IdentityServer — MUST check result var identityResult = await manager.CreateRoleAsync(result.Name); if (!identityResult.Succeeded) { // // Rollback local insert to avoid permanent drift await roles.RemoveAsync( id: result.Id, userInfo: userInfo, cancellationToken: cancellationToken ); // XException.ActionFailed.Throw(); } // await InvalidateAsync(); return result; } public async Task UpdateRoleAsync( string name, XRoleDefinitionDto item, XUserClaimsInfoDto userInfo, CancellationToken cancellationToken = default ) { // 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( item: item, id: existing.Id, userInfo: userInfo, cancellationToken: cancellationToken ); // // Sync to IdentityServer — rename requires delete+create in IdentityServer if (!string.Equals( existing.Name, updated.Name, StringComparison.OrdinalIgnoreCase) ) { // var usersInRole = await manager.UserManager .GetUsersInRoleAsync(existing.Name.ToNormalString()); // await manager.DeleteRoleAsync( existing.Name, cancellationToken ); // var identityResult = await manager.CreateRoleAsync(updated.Name); if (!identityResult.Succeeded) { XException.ActionFailed.Throw(); } // foreach (var user in usersInRole) { // await manager.AddUserToRoleAsync( user: user, roleName: updated.Name ); } } // await InvalidateAsync(); return updated; } public async Task> GetRolesAsync( CancellationToken cancellationToken = default ) { // var result = await roles .GetAllAsync(cancellationToken: cancellationToken); // return result; } public async Task> QueryRolesAsync( XQuery query, CancellationToken cancellationToken = default ) { // // Validate ... if (query.IsNull()) { XException.InvalidArgs.Throw(); } // var result = await roles.QueryAsync( query: query, cancellationToken: cancellationToken ); // return result; } #endregion } }