last ...
This commit is contained in:
@@ -15,273 +15,283 @@ 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();
|
||||
}
|
||||
// //
|
||||
// #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();
|
||||
}
|
||||
// //
|
||||
// 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);
|
||||
// //
|
||||
// await ValidateReferencesAsync(item, cancellationToken);
|
||||
|
||||
//
|
||||
item.IsSystem = false;
|
||||
// //
|
||||
// item.IsSystem = false;
|
||||
|
||||
//
|
||||
var result = await policies.AddAsync(
|
||||
item: item,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
// //
|
||||
// var result = await policies.AddAsync(
|
||||
// item: item,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
//
|
||||
await InvalidateAsync(result.Name);
|
||||
// //
|
||||
// await InvalidateAsync(result.Name);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
// //
|
||||
// return result;
|
||||
// }
|
||||
|
||||
public async Task<bool> RemovePolicyAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var entity = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
if (entity.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
// 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 policies.RemoveAsync(
|
||||
// id: entity.Id,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
//
|
||||
await InvalidateAsync(entity.Name);
|
||||
return true;
|
||||
}
|
||||
// //
|
||||
// await InvalidateAsync(entity.Name);
|
||||
// return true;
|
||||
// }
|
||||
|
||||
public async Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
||||
XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (query.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
// 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
|
||||
);
|
||||
// //
|
||||
// var result = await policies.QueryAsync(
|
||||
// query: query,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
// //
|
||||
// return result;
|
||||
// }
|
||||
|
||||
public async Task<XPolicyDefinitionDto> GetPolicyAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
// public async Task<XPolicyDefinitionDto> GetPolicyAsync(
|
||||
// string name,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// var result = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
// //
|
||||
// return result;
|
||||
// }
|
||||
|
||||
public async Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
||||
string name,
|
||||
XPolicyDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (name.IsNullOrEmpty() || item.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
// public async Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
||||
// string name,
|
||||
// XPolicyDefinitionDto item,
|
||||
// XUserClaimsInfoDto userInfo,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Validate ...
|
||||
// if (name.IsNullOrEmpty() || item.IsNull())
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
|
||||
//
|
||||
var exists = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
if (exists.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
// //
|
||||
// var exists = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
// if (exists.IsSystem)
|
||||
// {
|
||||
// XException.NotAllowed.Throw();
|
||||
// }
|
||||
|
||||
//
|
||||
// Referenced Roles & Scopes must exist ...
|
||||
await ValidateReferencesAsync(item, cancellationToken);
|
||||
// //
|
||||
// // Referenced Roles & Scopes must exist ...
|
||||
// await ValidateReferencesAsync(item, cancellationToken);
|
||||
|
||||
//
|
||||
var result = await policies.UpdateAsync(
|
||||
item: item,
|
||||
id: exists.Id,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
// //
|
||||
// var result = await policies.UpdateAsync(
|
||||
// item: item,
|
||||
// id: exists.Id,
|
||||
// userInfo: userInfo,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
//
|
||||
// Drop the cached policy so it rebuilds on next evaluation ...
|
||||
await InvalidateAsync(result.Name);
|
||||
// //
|
||||
// // Drop the cached policy so it rebuilds on next evaluation ...
|
||||
// await InvalidateAsync(result.Name);
|
||||
|
||||
//
|
||||
// Return the updated DTO as-is (EffectiveRoles is not a field on
|
||||
// XPolicyDefinitionDto) ...
|
||||
return result;
|
||||
}
|
||||
// //
|
||||
// // Return the updated DTO as-is (EffectiveRoles is not a field on
|
||||
// // XPolicyDefinitionDto) ...
|
||||
// return result;
|
||||
// }
|
||||
|
||||
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);
|
||||
// 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 = []
|
||||
};
|
||||
// //
|
||||
// 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");
|
||||
// //
|
||||
// // 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;
|
||||
}
|
||||
// //
|
||||
// return result;
|
||||
// }
|
||||
|
||||
//
|
||||
// Anonymous user fails any policy that requires authentication ...
|
||||
if (target.IsNull())
|
||||
{
|
||||
//
|
||||
if (auth.RequireAuthenticatedUser)
|
||||
{
|
||||
result.Passed = false;
|
||||
result.FailedRequirements.Add("RequireAuthenticatedUser");
|
||||
}
|
||||
// //
|
||||
// // Anonymous user fails any policy that requires authentication ...
|
||||
// if (target.IsNull())
|
||||
// {
|
||||
// //
|
||||
// if (auth.RequireAuthenticatedUser)
|
||||
// {
|
||||
// //
|
||||
// result.Passed = false;
|
||||
// result.FailedRequirements.Add("RequireAuthenticatedUser");
|
||||
// }
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
// //
|
||||
// return result;
|
||||
// }
|
||||
|
||||
//
|
||||
// Effective Roles: the user must hold at least one ...
|
||||
if (auth.EffectiveRoles.HasChild())
|
||||
{
|
||||
//
|
||||
var userRoles = await roleSync.GetUserRolesAsync(
|
||||
target.GetUserSelectByParam(),
|
||||
cancellationToken
|
||||
);
|
||||
// //
|
||||
// // Effective Roles: the user must hold at least one ...
|
||||
// if (auth.EffectiveRoles.HasChild())
|
||||
// {
|
||||
// //
|
||||
// // var userRoles = await roles.FindManyAsync(
|
||||
// // target.GetUserSelectByParam(),
|
||||
// // cancellationToken
|
||||
// // );
|
||||
|
||||
//
|
||||
var matched = auth.EffectiveRoles.Any(er =>
|
||||
userRoles.Any(ur =>
|
||||
string.Equals(ur, er, StringComparison.OrdinalIgnoreCase)
|
||||
)
|
||||
);
|
||||
// //
|
||||
// var userSelectByParam = target.GetUserSelectByParam();
|
||||
|
||||
//
|
||||
if (!matched)
|
||||
{
|
||||
//
|
||||
result.Passed = false;
|
||||
result.FailedRequirements.Add("RequiredRoles");
|
||||
}
|
||||
}
|
||||
// //
|
||||
// var userRoles = await roles.FindManyAsync(
|
||||
// predicate: x => x.
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
|
||||
//
|
||||
// NOTE: RequireEnabledUser (IsEnabled / !IsBanned), EffectiveScopes and
|
||||
// EffectiveClaims checks are layered on here once the matching flags /
|
||||
// collections on XUserClaimsInfoDto (xIdentityModels) are wired.
|
||||
return result;
|
||||
}
|
||||
// //
|
||||
// var matched = auth.EffectiveRoles.Any(er =>
|
||||
// userRoles.Any(ur =>
|
||||
// string.Equals(ur, er, StringComparison.OrdinalIgnoreCase)
|
||||
// )
|
||||
// );
|
||||
|
||||
public async Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||
XUserClaimsInfoDto target,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new XEffectiveAuthorizationDto
|
||||
{
|
||||
RequireEnabledUser = !target.IsNull(),
|
||||
RequireAuthenticatedUser = !target.IsNull(),
|
||||
EffectiveClaims = new List<XRequiredClaimDto>(),
|
||||
EffectiveRoles = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
EffectiveScopes = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
};
|
||||
// //
|
||||
// if (!matched)
|
||||
// {
|
||||
// //
|
||||
// result.Passed = false;
|
||||
// result.FailedRequirements.Add("RequiredRoles");
|
||||
// }
|
||||
// }
|
||||
|
||||
//
|
||||
if (target.IsNull())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
// //
|
||||
// // NOTE: RequireEnabledUser (IsEnabled / !IsBanned), EffectiveScopes and
|
||||
// // EffectiveClaims checks are layered on here once the matching flags /
|
||||
// // collections on XUserClaimsInfoDto (xIdentityModels) are wired.
|
||||
// return result;
|
||||
// }
|
||||
|
||||
//
|
||||
// Effective roles = the user's actual roles (from RoleManager) ...
|
||||
var userRoles = await roleSync.GetUserRolesAsync(
|
||||
cancellationToken: cancellationToken,
|
||||
userSelectByParam: target.GetUserSelectByParam()
|
||||
);
|
||||
foreach (var role in userRoles)
|
||||
{
|
||||
result.EffectiveRoles.Add(role);
|
||||
}
|
||||
// public async Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||
// XUserClaimsInfoDto target,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// var result = new XEffectiveAuthorizationDto
|
||||
// {
|
||||
// RequireEnabledUser = !target.IsNull(),
|
||||
// RequireAuthenticatedUser = !target.IsNull(),
|
||||
// EffectiveClaims = new List<XRequiredClaimDto>(),
|
||||
// EffectiveRoles = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
// EffectiveScopes = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
// };
|
||||
|
||||
//
|
||||
// NOTE: EffectiveScopes / EffectiveClaims aggregation is wired here from
|
||||
// the user's scope / claim source on XUserClaimsInfoDto (xIdentityModels).
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
// //
|
||||
// if (target.IsNull())
|
||||
// {
|
||||
// return result;
|
||||
// }
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
#endregion
|
||||
// //
|
||||
// // Effective roles = the user's actual roles (from RoleManager) ...
|
||||
// var userRoles = await roleSync.GetUserRolesAsync(
|
||||
// cancellationToken: cancellationToken,
|
||||
// userSelectByParam: target.GetUserSelectByParam()
|
||||
// );
|
||||
// foreach (var role in userRoles)
|
||||
// {
|
||||
// result.EffectiveRoles.Add(role);
|
||||
// }
|
||||
|
||||
// //
|
||||
// // NOTE: EffectiveScopes / EffectiveClaims aggregation is wired here from
|
||||
// // the user's scope / claim source on XUserClaimsInfoDto (xIdentityModels).
|
||||
// return result;
|
||||
// }
|
||||
// #endregion
|
||||
|
||||
// //
|
||||
// #region Private ...
|
||||
// #endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user