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

174 lines
4.7 KiB
C#

using System;
using System.Linq;
using xModels.Dtos;
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 Scope ...
public async Task<XQueryResult<XScopeDefinitionDto>> QueryScopesAsync(
XQuery query,
CancellationToken cancellationToken = default
)
{
//
if (query.IsNull())
{
XException.InvalidArgs.Throw();
}
//
var result = await scopes.QueryAsync(
query: query,
cancellationToken: cancellationToken
);
//
return result;
}
public async Task<XScopeDefinitionDto> GetScopeAsync(
string name,
CancellationToken cancellationToken = default
)
{
//
var result = await FindScopeOrThrowAsync(name, cancellationToken);
//
return result;
}
public async Task<XScopeDefinitionDto> AddScopeAsync(
XScopeDefinitionDto 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 scopes.FindOneAsync(
cancellationToken: cancellationToken,
predicate: s => s.Name.ToLower() == normalized
);
if (!exists.IsNull())
{
XException.Duplicate.Throw();
}
//
// Persist ...
item.IsSystem = false;
var result = await scopes.AddAsync(
item: item,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
// Sync to IdentityServer (ConfigurationDbContext) ...
var entity = scopes.AsQueryable()
.FirstOrDefault(s => s.Name.ToLower() == normalized);
if (!entity.IsNull())
{
//
await scopeSync
.UpsertAsync(entity, cancellationToken);
}
//
await InvalidateAsync();
return result;
}
public async Task<XScopeDefinitionDto> UpdateScopeAsync(
string name,
XScopeDefinitionDto item,
XUserClaimsInfoDto userInfo,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
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(
id: existing.Id,
item: item,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
// Sync to IdentityServer ...
var entity = scopes.AsQueryable()
.FirstOrDefault(s => s.Name.ToLower() == name.ToNormalString());
if (!entity.IsNull())
{
await scopeSync.UpsertAsync(entity, cancellationToken);
}
//
await InvalidateAsync();
return updated;
}
public async Task<bool> RemoveScopeAsync(
string name,
XUserClaimsInfoDto userInfo,
CancellationToken cancellationToken = default
)
{
//
var entity = await FindScopeOrThrowAsync(name, cancellationToken);
if (entity.IsSystem)
{
XException.NotAllowed.Throw();
}
//
await scopes.RemoveAsync(
id: entity.Id,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
// Remove from IdentityServer ...
await scopeSync.RemoveAsync(name, cancellationToken);
//
await InvalidateAsync();
return true;
}
#endregion
}
}