From 18991680016f40840fc94e7976636ec0ba49a353 Mon Sep 17 00:00:00 2001 From: sagilio Date: Sun, 2 Apr 2023 20:27:33 +0800 Subject: [PATCH] chore: Clean up the sample case Signed-off-by: sagilio --- .../WebApplicationSample/CasbinConfigs/basic_policy.csv | 2 +- samples/WebApplicationSample/Startup.cs | 4 ---- samples/WebApplicationSample/Views/Home/Index.cshtml | 4 ++-- .../CasbinConfigs/basic_policy.csv | 9 +++------ .../Controllers/HomeController.cs | 9 ++++----- samples/WebApplicationWithEfcoreSample/Startup.cs | 3 --- .../Views/Home/AttribRouteTest.cshtml | 8 -------- .../Views/Home/AttributeTest.cshtml | 0 .../Views/Home/Index.cshtml | 4 ++-- 9 files changed, 12 insertions(+), 31 deletions(-) delete mode 100644 samples/WebApplicationWithEfcoreSample/Views/Home/AttribRouteTest.cshtml create mode 100644 samples/WebApplicationWithEfcoreSample/Views/Home/AttributeTest.cshtml diff --git a/samples/WebApplicationSample/CasbinConfigs/basic_policy.csv b/samples/WebApplicationSample/CasbinConfigs/basic_policy.csv index e8d07b7..b726eaf 100644 --- a/samples/WebApplicationSample/CasbinConfigs/basic_policy.csv +++ b/samples/WebApplicationSample/CasbinConfigs/basic_policy.csv @@ -1,4 +1,4 @@ -# Policy below will only match using the BasicRequestTransformer +# Policy below will only match if using the BasicRequestTransformer p, alice@example.com, BasicTest, Get # Policy below will only match if using the KeyMatchRequestTransformer diff --git a/samples/WebApplicationSample/Startup.cs b/samples/WebApplicationSample/Startup.cs index cf21f62..d45650b 100644 --- a/samples/WebApplicationSample/Startup.cs +++ b/samples/WebApplicationSample/Startup.cs @@ -41,10 +41,6 @@ public void ConfigureServices(IServiceCollection services) options.PreferSubClaimType = ClaimTypes.Name; options.DefaultModelPath = Path.Combine("CasbinConfigs", "basic_model.conf"); options.DefaultPolicyPath = Path.Combine("CasbinConfigs", "basic_policy.csv"); - - // Comment line below to use the default BasicRequestTransformer - // Note: Commenting the line means that the action methods MUST have [CasbinAuthorize()] attribute which explicitly specifies obj and policy. Otherwise authorization will be denied - options.DefaultRequestTransformerType = typeof(KeyMatchRequestTransformer); }); } diff --git a/samples/WebApplicationSample/Views/Home/Index.cshtml b/samples/WebApplicationSample/Views/Home/Index.cshtml index 9346082..38f5715 100644 --- a/samples/WebApplicationSample/Views/Home/Index.cshtml +++ b/samples/WebApplicationSample/Views/Home/Index.cshtml @@ -4,6 +4,6 @@

Welcome

-

Basic Model : Policy is "p, alice@example.com, BasicTest, GET" - Basic Test.

-

Attrib Route : Policy is "p, alice@example.com, /Attribute, GET" - Attribute Test.

+

Basic Transformer: Policy is "p, alice@example.com, BasicTest, Get" - Basic Test.

+

KeyMatch Transformer: Policy is "p, alice@example.com, /Attribute, GET" - Attribute Test.

diff --git a/samples/WebApplicationWithEfcoreSample/CasbinConfigs/basic_policy.csv b/samples/WebApplicationWithEfcoreSample/CasbinConfigs/basic_policy.csv index 52f0cf5..b726eaf 100644 --- a/samples/WebApplicationWithEfcoreSample/CasbinConfigs/basic_policy.csv +++ b/samples/WebApplicationWithEfcoreSample/CasbinConfigs/basic_policy.csv @@ -1,8 +1,5 @@ -# Policy below will only match using both BasicRequestTransformer and CustomRequestTransformer +# Policy below will only match if using the BasicRequestTransformer p, alice@example.com, BasicTest, Get -# Policy below will only match if using the CustomRequestTransformer -p, alice@example.com, /attribtest, GET - -# Policy below will only match if using the CustomRequestTransformer -#p, alice@example.com, /home/privacy, GET +# Policy below will only match if using the KeyMatchRequestTransformer +p, alice@example.com, /AttributeTest, GET diff --git a/samples/WebApplicationWithEfcoreSample/Controllers/HomeController.cs b/samples/WebApplicationWithEfcoreSample/Controllers/HomeController.cs index 94b6419..a84247e 100644 --- a/samples/WebApplicationWithEfcoreSample/Controllers/HomeController.cs +++ b/samples/WebApplicationWithEfcoreSample/Controllers/HomeController.cs @@ -22,20 +22,19 @@ public IActionResult Index() return View(); } - [HttpGet("attribtest")] - [CasbinAuthorize("/attribtest", "GET")] - public IActionResult AttribRouteTest(string tenantId) + [HttpGet("AttributeTest")] + [CasbinAuthorize(RequestTransformerType = typeof(KeyMatchRequestTransformer))] + public IActionResult AttributeTest(string tenantId) { return View(); } - [CasbinAuthorize(nameof(BasicTest), nameof(HttpMethod.Get))] + [CasbinAuthorize(nameof(BasicTest), nameof(HttpMethod.Get), RequestTransformerType = typeof(BasicRequestTransformer))] public IActionResult BasicTest() { return View(); } - [CasbinAuthorize] public IActionResult Privacy() { return View(); diff --git a/samples/WebApplicationWithEfcoreSample/Startup.cs b/samples/WebApplicationWithEfcoreSample/Startup.cs index e7cceab..0b2ba2d 100644 --- a/samples/WebApplicationWithEfcoreSample/Startup.cs +++ b/samples/WebApplicationWithEfcoreSample/Startup.cs @@ -35,11 +35,8 @@ public void ConfigureServices(IServiceCollection services) { options.PreferSubClaimType = ClaimTypes.Name; options.DefaultModelPath = Path.Combine("CasbinConfigs", "basic_model.conf"); - //options.DefaultPolicyPath = Path.Combine("CasbinConfigs", "basic_policy.csv"); - options.DefaultEnforcerFactory = (p, m) => new Enforcer(m, new EFCoreAdapter(p.GetRequiredService>())); - options.DefaultRequestTransformerType = typeof(BasicRequestTransformer); }); services.AddDefaultIdentity(options => { diff --git a/samples/WebApplicationWithEfcoreSample/Views/Home/AttribRouteTest.cshtml b/samples/WebApplicationWithEfcoreSample/Views/Home/AttribRouteTest.cshtml deleted file mode 100644 index fff0703..0000000 --- a/samples/WebApplicationWithEfcoreSample/Views/Home/AttribRouteTest.cshtml +++ /dev/null @@ -1,8 +0,0 @@ -@{ - ViewData["Title"] = "Attribute Routed Test Page"; -} - -
-

Welcome

-

Learn about building Web apps with ASP.NET Core.

-
diff --git a/samples/WebApplicationWithEfcoreSample/Views/Home/AttributeTest.cshtml b/samples/WebApplicationWithEfcoreSample/Views/Home/AttributeTest.cshtml new file mode 100644 index 0000000..e69de29 diff --git a/samples/WebApplicationWithEfcoreSample/Views/Home/Index.cshtml b/samples/WebApplicationWithEfcoreSample/Views/Home/Index.cshtml index ee4a12a..38f5715 100644 --- a/samples/WebApplicationWithEfcoreSample/Views/Home/Index.cshtml +++ b/samples/WebApplicationWithEfcoreSample/Views/Home/Index.cshtml @@ -4,6 +4,6 @@

Welcome

-

Basic Model : Policy is "p, alice@example.com, BasicTest, GET" - Test.

-

Attrib Route : Policy is "p, alice@example.com, /attribtest, GET" - Attribute Route Test.

+

Basic Transformer: Policy is "p, alice@example.com, BasicTest, Get" - Basic Test.

+

KeyMatch Transformer: Policy is "p, alice@example.com, /Attribute, GET" - Attribute Test.