Files
xIdentityService/Partial/XAuthorizationProvider+Role.cs
T
2026-11-21 00:08:36 +03:30

181 lines
5.0 KiB
C#

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<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
}
}