This commit is contained in:
2026-11-21 00:08:36 +03:30
parent c2b2dd27c5
commit 4e114aa85a
6 changed files with 688 additions and 44 deletions
+169 -2
View File
@@ -1,7 +1,11 @@
using System;
using System.Collections.Generic;
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
{
@@ -9,6 +13,169 @@ namespace xIdentityService.Providers
{
//
#region Role ...
public async Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
CancellationToken cancellationToken = default
)
{
//
var result = await roles
.GetAllAsync(cancellationToken: cancellationToken);
//
return result;
}
public async Task<XRoleDefinitionDto> GetRoleAsync(
string name,
CancellationToken cancellationToken = default
)
{
//
var result = await FindRoleOrThrowAsync(name, cancellationToken);
//
return result;
}
public async Task<XRoleDefinitionDto> 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<IdentityRole>) ...
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<XRoleDefinitionDto> 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<bool> 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<int> 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
}
}