86 lines
2.2 KiB
C#
86 lines
2.2 KiB
C#
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using xIdentityModels.Dtos;
|
|
using xExceptions.Constants;
|
|
using System.Threading.Tasks;
|
|
using xIdentityModels.Models;
|
|
|
|
namespace xIdentityService.Providers
|
|
{
|
|
public partial class XAuthorizationProvider
|
|
{
|
|
//
|
|
#region Policy ...
|
|
public async Task<XPolicyDefinitionDto> AddPolicyAsync(
|
|
XPolicyDefinitionDto item,
|
|
XUserClaimsInfoDto userInfo,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (item.IsNull() || item.Name.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var normalized = item.Name.ToNormalString();
|
|
var exists = await policies.FindOneAsync(
|
|
predicate: p => p.Name.ToLower() == normalized,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (!exists.IsNull())
|
|
{
|
|
XException.Duplicate.Throw();
|
|
}
|
|
|
|
//
|
|
await ValidateReferencesAsync(item, cancellationToken);
|
|
|
|
//
|
|
item.IsSystem = false;
|
|
|
|
//
|
|
var result = await policies.AddAsync(
|
|
item: item,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
await InvalidateAsync(result.Name);
|
|
|
|
//
|
|
return await ToDtoWithEffectiveRolesAsync(result, cancellationToken);
|
|
}
|
|
|
|
public async Task<bool> RemovePolicyAsync(
|
|
string name,
|
|
XUserClaimsInfoDto userInfo,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var entity = await FindPolicyOrThrowAsync(name, cancellationToken);
|
|
if (entity.IsSystem)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
await policies.RemoveAsync(
|
|
id: entity.Id,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
await InvalidateAsync(entity.Name);
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Private ...
|
|
#endregion
|
|
}
|
|
} |