Files
xSaherElmIds/Providers/XAuthorizationProvider/XAuthorizationProvider+Scope.cs
T
2026-11-27 19:21:47 +03:30

156 lines
4.4 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 xIds.Providers
{
public partial class XAuthorizationProvider
{
//
#region Scope ...
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;
}
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<IEnumerable<XScopeDefinitionDto>> GetScopesAsync(
CancellationToken cancellationToken = default
)
{
return await scopes
.GetAllAsync(cancellationToken: cancellationToken);
}
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;
}
#endregion
}
}