last ...
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
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 xIds.Providers
|
||||
{
|
||||
public partial class XAuthorizationProvider
|
||||
{
|
||||
//
|
||||
#region Scope ...
|
||||
public async Task<IEnumerable<XScopeDefinitionDto>> GetScopesAsync(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
return await scopes
|
||||
.GetAllAsync(cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<XScopeDefinitionDto> GetScopeAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
return await FindScopeOrThrowAsync(name, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<XScopeDefinitionDto> AddScopeAsync(
|
||||
XScopeDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
if (item.IsNull() || item.Name.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var normalized = item.Name.ToNormalString();
|
||||
var exists = await scopes.FindOneAsync(
|
||||
predicate: s => s.Name.ToLower() == normalized,
|
||||
cancellationToken: cancellationToken);
|
||||
if (!exists.IsNull())
|
||||
{
|
||||
XException.Duplicate.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
item.IsSystem = false;
|
||||
var result = await scopes.AddAsync(
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XScopeDefinitionDto> UpdateScopeAsync(
|
||||
string name,
|
||||
XScopeDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
if (name.IsNullOrEmpty() || item.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var existing = await FindScopeOrThrowAsync(name, cancellationToken);
|
||||
if (existing.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var updated = await scopes.UpdateAsync(
|
||||
item: item,
|
||||
id: existing.Id,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return updated;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveScopeAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var existing = await FindScopeOrThrowAsync(name, cancellationToken);
|
||||
if (existing.IsSystem) {
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var affectedPolicies = (await policies.GetAllAsync(cancellationToken: cancellationToken))
|
||||
.Where(p => !p.RequiredScopes.IsNullOrEmpty()
|
||||
&& p.RequiredScopes.ParseListString<string>()
|
||||
.Any(s => string.Equals(s, existing.Name,
|
||||
StringComparison.OrdinalIgnoreCase)))
|
||||
.ToList();
|
||||
|
||||
//
|
||||
await scopes.RemoveAsync(
|
||||
id: existing.Id,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user