add support filling policy handler ...
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System.Linq;
|
||||
using xIdentityHelper;
|
||||
using xAiApi.Providers;
|
||||
using xAiApi.Interfaces;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Authorization;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityService.Interfaces;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
@@ -35,6 +37,7 @@ namespace xAiApi.Extensions
|
||||
)
|
||||
{
|
||||
//
|
||||
var policyHandler = services.GetRegisteredService<IXPolicyHandler>();
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
//
|
||||
@@ -48,6 +51,26 @@ namespace xAiApi.Extensions
|
||||
{
|
||||
var policyDescriptor = policiesEnumerator.Current;
|
||||
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value);
|
||||
|
||||
//
|
||||
// Add Policy to Policy Handler ...
|
||||
if (!policyHandler.IsNull())
|
||||
{
|
||||
//
|
||||
// Check Policy is RequiredRole Policy ...
|
||||
var roleRequirements = policyDescriptor.Value.Requirements
|
||||
.OfType<RequiredRolesRequirement>()
|
||||
.FirstOrDefault();
|
||||
if (!roleRequirements.IsNull() &&
|
||||
roleRequirements.Roles.HasChild())
|
||||
{
|
||||
//
|
||||
policyHandler.AddPolicy(
|
||||
policy: policyDescriptor.Key.ToNormalString(),
|
||||
requiredRoles: roleRequirements.Roles.Select(r => r.ToNormalString())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,8 +85,32 @@ namespace xAiApi.Extensions
|
||||
{
|
||||
while (policiesEnumerator.MoveNext())
|
||||
{
|
||||
//
|
||||
var policyDescriptor = policiesEnumerator.Current;
|
||||
|
||||
//
|
||||
// Add Policy to Authorization ...
|
||||
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value);
|
||||
|
||||
//
|
||||
// Add Policy to Policy Handler ...
|
||||
if (!policyHandler.IsNull())
|
||||
{
|
||||
//
|
||||
// Check Policy is RequiredRole Policy ...
|
||||
var roleRequirements = policyDescriptor.Value.Requirements
|
||||
.OfType<RequiredRolesRequirement>()
|
||||
.FirstOrDefault();
|
||||
if (!roleRequirements.IsNull() &&
|
||||
roleRequirements.Roles.HasChild())
|
||||
{
|
||||
//
|
||||
policyHandler.AddPolicy(
|
||||
policy: policyDescriptor.Key.ToNormalString(),
|
||||
requiredRoles: roleRequirements.Roles.Select(r => r.ToNormalString())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user