181 lines
5.3 KiB
C#
181 lines
5.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using xIdentityModels.Dtos;
|
|
using xExceptions.Constants;
|
|
using System.Threading.Tasks;
|
|
using xIdentityModels.Models;
|
|
|
|
namespace xIds.Providers
|
|
{
|
|
public partial class XAuthorizationProvider
|
|
{
|
|
//
|
|
#region Role ...
|
|
public async Task<XRoleDefinitionDto> 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<XRoleDefinitionDto> 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<bool> 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;
|
|
}
|
|
#endregion
|
|
}
|
|
} |