diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs
index 08a707b..81a91a1 100644
--- a/DI/XDIHelperExtension.cs
+++ b/DI/XDIHelperExtension.cs
@@ -96,6 +96,17 @@ namespace xIdentityService.DI
Console.WriteLine("IdentityService: Add PersonService ...");
}
+ ///
+ /// Register Policy Handler Service ...
+ ///
+ ///
+ public static void AddXPolicyHandler(
+ this IServiceCollection source
+ )
+ {
+ source.AddSingleton();
+ }
+
///
/// Use Authentication
///
@@ -116,6 +127,10 @@ namespace xIdentityService.DI
//
services.AddSingleton(config);
+ //
+ // Register Policy Handler ...
+ services.AddXPolicyHandler();
+
//
var builder = services
.AddAuthentication(XAuthenticationScheme.XToken.GetStringValue());
diff --git a/Interfaces/IXPolicyHandler.cs b/Interfaces/IXPolicyHandler.cs
new file mode 100644
index 0000000..c90ca50
--- /dev/null
+++ b/Interfaces/IXPolicyHandler.cs
@@ -0,0 +1,43 @@
+using System.Collections.Generic;
+
+namespace xIdentityService.Interfaces
+{
+ public interface IXPolicyHandler
+ {
+ ///
+ /// Add Specified Role ...
+ ///
+ ///
+ ///
+ ///
+ bool AddPolicy(
+ string policy,
+ IEnumerable requiredRoles
+ );
+
+ ///
+ /// Check Specified Role Exists or not ...
+ ///
+ ///
+ ///
+ bool HasPolicy(string policy);
+
+ ///
+ /// Remove Specified Role ...
+ ///
+ ///
+ ///
+ bool RemovePolicy(string policy);
+
+ ///
+ /// Check Specified Policy passed or not ...
+ ///
+ ///
+ ///
+ ///
+ bool IsPolicyPassed(
+ string policy,
+ IEnumerable roles
+ );
+ }
+}
\ No newline at end of file
diff --git a/Providers/XPolicyHandler.cs b/Providers/XPolicyHandler.cs
new file mode 100644
index 0000000..9d12920
--- /dev/null
+++ b/Providers/XPolicyHandler.cs
@@ -0,0 +1,140 @@
+using System;
+using System.Linq;
+using xCommons.Extensions;
+using xExceptions.Constants;
+using System.Collections.Generic;
+using xIdentityService.Interfaces;
+using Microsoft.EntityFrameworkCore.Internal;
+
+namespace xIdentityService.Providers
+{
+ public class XPolicyHandler : IXPolicyHandler
+ {
+ private readonly static Dictionary> policies = [];
+
+ ///
+ /// Add Specified Role ...
+ ///
+ ///
+ ///
+ ///
+ public bool AddPolicy(
+ string policy,
+ IEnumerable requiredRoles
+ )
+ {
+ //
+ // Validate ...
+ if (policy.IsNullOrEmpty() ||
+ !requiredRoles.HasChild()
+ )
+ {
+ XException.InvalidArgs.Throw();
+ }
+
+ //
+ var result = !HasPolicy(policy);
+ if (!result)
+ {
+ return result;
+ }
+
+ //
+ policies[policy] = requiredRoles;
+
+ //
+ return result;
+ }
+
+ ///
+ /// Check Specified Role Exists or not ...
+ ///
+ ///
+ ///
+ public bool HasPolicy(string policy)
+ {
+ //
+ var result =
+ policies.HasChild() &&
+ !policy.IsNullOrEmpty() &&
+ policies.Keys.Any(p => p
+ .Equals(policy, StringComparison.InvariantCultureIgnoreCase));
+
+ //
+ return result;
+ }
+
+ ///
+ /// Remove Specified Role ...
+ ///
+ ///
+ ///
+ public bool RemovePolicy(string policy)
+ {
+ //
+ // Validate ...
+ if (policy.IsNullOrEmpty())
+ {
+ XException.InvalidArgs.Throw();
+ }
+
+ //
+ var result = HasPolicy(policy);
+ if (!result)
+ {
+ return result;
+ }
+
+ //
+ result = policies.Remove(policy);
+
+ //
+ return result;
+ }
+
+ ///
+ /// Check Specified Policy passed or not ...
+ ///
+ ///
+ ///
+ ///
+ public bool IsPolicyPassed(
+ string policy,
+ IEnumerable roles
+ )
+ {
+ //
+ // Validate ...
+ if (!roles.HasChild() ||
+ policy.IsNullOrEmpty())
+ {
+ XException.InvalidArgs.Throw();
+ }
+
+ //
+ // Normalize ...
+ policy = policy.ToNormalString();
+
+ //
+ var result = HasPolicy(policy);
+ if (!result)
+ {
+ return result;
+ }
+
+ //
+ var requiredRoles = policies[policy];
+ result =
+ requiredRoles.HasChild() &&
+ roles.All(r => requiredRoles
+ .Any(rq => r.Equals(rq, StringComparison.InvariantCultureIgnoreCase)));
+
+ //
+ return result;
+ }
+
+ //
+ #region Private ...
+ #endregion
+ }
+}
\ No newline at end of file