This commit is contained in:
2026-11-26 19:36:49 +03:30
parent 4e114aa85a
commit bdc8791822
8 changed files with 752 additions and 921 deletions
+145 -145
View File
@@ -12,170 +12,170 @@ 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);
// #region Role ...
// public async Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
// CancellationToken cancellationToken = default
// )
// {
// //
// var result = await roles
// .GetAllAsync(cancellationToken: cancellationToken);
//
return result;
}
// //
// return result;
// }
public async Task<XRoleDefinitionDto> GetRoleAsync(
string name,
CancellationToken cancellationToken = default
)
{
//
var result = await FindRoleOrThrowAsync(name, cancellationToken);
// public async Task<XRoleDefinitionDto> GetRoleAsync(
// string name,
// CancellationToken cancellationToken = default
// )
// {
// //
// var result = await FindRoleOrThrowAsync(name, cancellationToken);
//
return result;
}
// //
// return result;
// }
public async Task<XRoleDefinitionDto> AddRoleAsync(
XRoleDefinitionDto item,
XUserClaimsInfoDto userInfo,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
if (item.IsNull() || item.Name.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
// 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();
}
// //
// // 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
);
// //
// // 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);
}
// //
// // 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;
}
// //
// 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();
}
// 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 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
);
// //
// 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);
}
// //
// // 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;
}
// //
// 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();
}
// 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
);
// //
// await roles.RemoveAsync(
// id: exists.Id,
// userInfo: userInfo,
// cancellationToken: cancellationToken
// );
//
// Remove from IdentityServer ...
await roleSync.RemoveAsync(name, cancellationToken);
// //
// // Remove from IdentityServer ...
// await roleSync.RemoveAsync(name, cancellationToken);
//
await InvalidateAsync();
return true;
}
// //
// 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);
// 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
);
// //
// var result = await roleSync
// .CountUsersInRoleAsync(
// name,
// cancellationToken
// );
//
return result;
}
#endregion
// //
// return result;
// }
// #endregion
}
}