161 lines
4.7 KiB
C#
161 lines
4.7 KiB
C#
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xIdentityModels.Dtos;
|
|
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;
|
|
}
|
|
|
|
// GetPolicyAsync / QueryPoliciesAsync / UpdatePolicyAsync follow same style ...
|
|
#endregion
|
|
|
|
//
|
|
#region Private ...
|
|
private async Task<XPolicyDefinition> FindPolicyOrThrowAsync(
|
|
string name,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
//
|
|
if (name.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var normalized = name.ToNormalString();
|
|
var result = await policies.Repository.FindOneAsync(
|
|
predicate: p => p.Name.ToLower() == normalized,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
if (result.IsNull())
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
private async Task ValidateReferencesAsync(
|
|
XPolicyDefinitionDto item,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
//
|
|
// Referenced Roles must exist ...
|
|
if (item.RequiredRoles.HasChild())
|
|
{
|
|
var all = (await roles.Repository.GetAllAsync(cancellationToken: cancellationToken))
|
|
.Select(r => r.Name.ToNormalString()).ToList();
|
|
if (item.RequiredRoles.Any(r => !all.Contains(r.ToNormalString())))
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
}
|
|
|
|
//
|
|
// Referenced Scopes must exist ...
|
|
if (item.RequiredScopes.HasChild())
|
|
{
|
|
var all = (await scopes.Repository.GetAllAsync(cancellationToken: cancellationToken))
|
|
.Select(s => s.Name.ToNormalString()).ToList();
|
|
if (item.RequiredScopes.Any(s => !all.Contains(s.ToNormalString())))
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
|
|
XPolicyDefinition entity,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
//
|
|
var dto = entity.ToDto();
|
|
dto.EffectiveRoles = (await resolver.ResolveEffectiveRolesAsync(
|
|
explicitRoles: entity.RequiredRoles.ToCsvList(),
|
|
minRoleWeight: entity.MinRoleWeight,
|
|
cancellationToken: cancellationToken
|
|
)).ToList();
|
|
|
|
//
|
|
return dto;
|
|
}
|
|
#endregion
|
|
}
|
|
} |