250 lines
7.3 KiB
C#
250 lines
7.3 KiB
C#
using xModels.Dtos;
|
|
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using xIdentityModels.Dtos;
|
|
using xExceptions.Constants;
|
|
using xIdentityModels.Models;
|
|
using System.Threading.Tasks;
|
|
using xIdentityService.Models;
|
|
using System.Collections.Generic;
|
|
|
|
namespace xIds.Providers
|
|
{
|
|
public partial class XAuthorizationProvider
|
|
{
|
|
//
|
|
#region Policy ...
|
|
public async Task<bool> RemovePolicyAsync(
|
|
string name,
|
|
XUserClaimsInfoDto userInfo,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var existing = await FindPolicyOrThrowAsync(name, cancellationToken);
|
|
if (existing.IsSystem)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
await policies.RemoveAsync(
|
|
id: existing.Id,
|
|
userInfo: userInfo,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
await InvalidateAsync(existing.Name);
|
|
return true;
|
|
}
|
|
|
|
public async Task<XPolicyDefinitionDto> GetPolicyAsync(
|
|
string name,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await FindPolicyOrThrowAsync(name, cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
public async Task<XPolicyDefinitionDto> AddPolicyAsync(
|
|
XPolicyDefinitionDto item,
|
|
XUserClaimsInfoDto userInfo,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
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();
|
|
}
|
|
|
|
//
|
|
// ⚠️ CRITICAL: مقصد همه Role/Scopeها وجود داشته باشند
|
|
await ValidateReferencesAsync(item, cancellationToken);
|
|
|
|
//
|
|
item.IsSystem = false;
|
|
var result = await policies.AddAsync(
|
|
item: item,
|
|
userInfo: userInfo,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
await InvalidateAsync();
|
|
return result;
|
|
}
|
|
|
|
public async Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
|
string name,
|
|
XPolicyDefinitionDto item,
|
|
XUserClaimsInfoDto userInfo,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
if (name.IsNullOrEmpty() || item.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var existing = await FindPolicyOrThrowAsync(name, cancellationToken);
|
|
if (existing.IsSystem)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
// Reference validation BEFORE update — same as Add
|
|
await ValidateReferencesAsync(item, cancellationToken);
|
|
|
|
//
|
|
var updated = await policies.UpdateAsync(
|
|
id: existing.Id,
|
|
item: item,
|
|
userInfo: userInfo,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
await InvalidateAsync(existing.Name);
|
|
return updated;
|
|
}
|
|
|
|
public async Task<XPolicyEvaluationResultDto> EvaluatePolicyAsync(
|
|
string name,
|
|
XUserClaimsInfoDto target,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Load the policy (throws NotFound when missing) ...
|
|
var dto = await FindPolicyOrThrowAsync(name, cancellationToken);
|
|
|
|
//
|
|
var result = new XPolicyEvaluationResultDto
|
|
{
|
|
Passed = true,
|
|
Policy = dto.Name,
|
|
FailedRequirements = []
|
|
};
|
|
|
|
//
|
|
// Build the policy's effective authorization via the shared helper
|
|
// (resolver + projection onto XEffectiveAuthorizationDto) ...
|
|
var auth = await ToDtoWithEffectiveRolesAsync(dto, cancellationToken);
|
|
if (auth.IsNull())
|
|
{
|
|
//
|
|
result.Passed = false;
|
|
result.FailedRequirements.Add("PolicyNotBuilt");
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Anonymous user fails any policy that requires authentication ...
|
|
if (target.IsNull())
|
|
{
|
|
//
|
|
if (auth.RequireAuthenticatedUser)
|
|
{
|
|
//
|
|
result.Passed = false;
|
|
result.FailedRequirements.Add("RequireAuthenticatedUser");
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Effective Roles: the user must hold at least one ...
|
|
if (auth.EffectiveRoles.HasChild())
|
|
{
|
|
// //
|
|
// var userSelectByParam = target.GetUserSelectByParam();
|
|
|
|
// //
|
|
// var userRoles = await roles.FindManyAsync(
|
|
// predicate: x => x.
|
|
// cancellationToken: cancellationToken
|
|
// );
|
|
|
|
// //
|
|
// var matched = auth.EffectiveRoles.Any(er =>
|
|
// userRoles.Any(ur =>
|
|
// string.Equals(ur, er, StringComparison.OrdinalIgnoreCase)
|
|
// )
|
|
// );
|
|
|
|
//
|
|
// if (!matched)
|
|
// {
|
|
// //
|
|
// result.Passed = false;
|
|
// result.FailedRequirements.Add("RequiredRoles");
|
|
// }
|
|
}
|
|
|
|
//
|
|
// NOTE: RequireEnabledUser (IsEnabled / !IsBanned), EffectiveScopes and
|
|
// EffectiveClaims checks are layered on here once the matching flags /
|
|
// collections on XUserClaimsInfoDto (xIdentityModels) are wired.
|
|
return result;
|
|
}
|
|
|
|
public async Task<IEnumerable<XPolicyDefinitionDto>> GetPoliciesAsync(
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await policies
|
|
.GetAllAsync(cancellationToken: cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
public async Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
|
XQuery query,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (query.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await policies.QueryAsync(
|
|
query: query,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
} |