From 26b92ecd651ab96d7acf7de29ea3e1a07082e2e5 Mon Sep 17 00:00:00 2001 From: zhuying1999 <57996853+zhuying1999@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:52:10 +0800 Subject: [PATCH] feat: add policy APIs (#74) * feat:add add-policy feature * feat: add policy APIs * feat: change apache header year --- .../org/casbin/casdoor/entity/CasbinRule.java | 30 ++++++ .../casdoor/service/EnforcerService.java | 2 + .../casbin/casdoor/service/PolicyService.java | 74 +++++++++++++++ .../casbin/casdoor/util/PolicyOperations.java | 33 +++++++ .../java/org/casbin/casdoor/EnforcerTest.java | 1 + .../java/org/casbin/casdoor/PolicyTest.java | 94 +++++++++++++++++++ 6 files changed, 234 insertions(+) create mode 100644 src/main/java/org/casbin/casdoor/entity/CasbinRule.java create mode 100644 src/main/java/org/casbin/casdoor/service/PolicyService.java create mode 100644 src/main/java/org/casbin/casdoor/util/PolicyOperations.java create mode 100644 src/test/java/org/casbin/casdoor/PolicyTest.java diff --git a/src/main/java/org/casbin/casdoor/entity/CasbinRule.java b/src/main/java/org/casbin/casdoor/entity/CasbinRule.java new file mode 100644 index 0000000..0998d7e --- /dev/null +++ b/src/main/java/org/casbin/casdoor/entity/CasbinRule.java @@ -0,0 +1,30 @@ +package org.casbin.casdoor.entity; + +public class CasbinRule { + public long id; + public String Ptype; + public String V0; + public String V1; + public String V2; + public String V3; + public String V4; + public String V5; + public String tableName; + + public CasbinRule(long id, String ptype, String v0, String v1, String v2, String v3, String v4, String v5, String tableName) { + this.id = id; + this.Ptype = ptype; + this.V0 = v0; + this.V1 = v1; + this.V2 = v2; + this.V3 = v3; + this.V4 = v4; + this.V5 = v5; + this.tableName = tableName; + } + + public CasbinRule(){ + + } + +} diff --git a/src/main/java/org/casbin/casdoor/service/EnforcerService.java b/src/main/java/org/casbin/casdoor/service/EnforcerService.java index 3947ae1..9a8d7a9 100644 --- a/src/main/java/org/casbin/casdoor/service/EnforcerService.java +++ b/src/main/java/org/casbin/casdoor/service/EnforcerService.java @@ -16,6 +16,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import org.casbin.casdoor.config.Config; +import org.casbin.casdoor.entity.CasbinRule; import org.casbin.casdoor.entity.Enforcer; import org.casbin.casdoor.exception.Exception; import org.casbin.casdoor.util.EnforcerOperations; @@ -106,4 +107,5 @@ private CasdoorResponse modifyEnforcer(EnforcerOperations metho payload, new TypeReference>() { }); } + } diff --git a/src/main/java/org/casbin/casdoor/service/PolicyService.java b/src/main/java/org/casbin/casdoor/service/PolicyService.java new file mode 100644 index 0000000..d12a6c0 --- /dev/null +++ b/src/main/java/org/casbin/casdoor/service/PolicyService.java @@ -0,0 +1,74 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing CasdoorPermissions and +// limitations under the License. + +package org.casbin.casdoor.service; + +import com.fasterxml.jackson.core.type.TypeReference; +import org.casbin.casdoor.config.Config; +import org.casbin.casdoor.entity.CasbinRule; +import org.casbin.casdoor.entity.Enforcer; +import org.casbin.casdoor.util.Map; +import org.casbin.casdoor.util.PolicyOperations; +import org.casbin.casdoor.util.http.CasdoorResponse; + +import java.io.IOException; +import java.util.List; + +public class PolicyService extends Service{ + public PolicyService(Config config) { + super(config); + } + + public List getPolicies(String enforcerName, String adapterId) throws IOException { + String id = config.organizationName + "/" + enforcerName; + CasdoorResponse, Object> resp = doGet("get-policies", + Map.of("id", id, "adapterId", adapterId), new TypeReference, Object>>() { + }); + return resp.getData(); + } + + public CasdoorResponse addPolicy(Enforcer enforcer, CasbinRule policy) throws IOException { + CasbinRule[] policies = new CasbinRule[1]; + policies[0] = policy; + return modifyPolicy(PolicyOperations.ADD_Policy, enforcer, policies); + } + + public CasdoorResponse removePolicy(Enforcer enforcer, CasbinRule policy) throws IOException { + CasbinRule[] policies = new CasbinRule[1]; + policies[0] = policy; + return modifyPolicy(PolicyOperations.DELETE_Policy, enforcer, policies); + } + + public CasdoorResponse updatePolicy(Enforcer enforcer, CasbinRule oldpolicy, CasbinRule newpolicy) throws IOException { + CasbinRule[] policies = new CasbinRule[2]; + policies[0] = oldpolicy; + policies[1] = newpolicy; + return modifyPolicy(PolicyOperations.UPDATE_Policy, enforcer, policies); + } + + private CasdoorResponse modifyPolicy(PolicyOperations method, Enforcer enforcer, CasbinRule[] policies) throws IOException { + enforcer.owner = config.organizationName; + String id = enforcer.owner + "/" + enforcer.name; + String payload = ""; + if (method == PolicyOperations.UPDATE_Policy){ + payload = objectMapper.writeValueAsString(policies); + } else { + payload = objectMapper.writeValueAsString(policies[0]); + } + return doPost(method.getOperation(), + Map.of("id", id), + payload, new TypeReference>() { + }); + } +} diff --git a/src/main/java/org/casbin/casdoor/util/PolicyOperations.java b/src/main/java/org/casbin/casdoor/util/PolicyOperations.java new file mode 100644 index 0000000..bf48876 --- /dev/null +++ b/src/main/java/org/casbin/casdoor/util/PolicyOperations.java @@ -0,0 +1,33 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.casbin.casdoor.util; + +public enum PolicyOperations { + + ADD_Policy("add-policy"), + DELETE_Policy("remove-policy"), + UPDATE_Policy("update-policy"); + + private final String operation; + + PolicyOperations(String op) { + this.operation = op; + } + + public String getOperation() { + return operation; + } + +} diff --git a/src/test/java/org/casbin/casdoor/EnforcerTest.java b/src/test/java/org/casbin/casdoor/EnforcerTest.java index 8919eb0..4b40cbd 100644 --- a/src/test/java/org/casbin/casdoor/EnforcerTest.java +++ b/src/test/java/org/casbin/casdoor/EnforcerTest.java @@ -14,6 +14,7 @@ package org.casbin.casdoor; +import org.casbin.casdoor.entity.CasbinRule; import org.casbin.casdoor.entity.Enforcer; import org.casbin.casdoor.service.EnforcerService; import org.casbin.casdoor.support.TestDefaultConfig; diff --git a/src/test/java/org/casbin/casdoor/PolicyTest.java b/src/test/java/org/casbin/casdoor/PolicyTest.java new file mode 100644 index 0000000..b928b4a --- /dev/null +++ b/src/test/java/org/casbin/casdoor/PolicyTest.java @@ -0,0 +1,94 @@ +// Copyright 2024 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.casbin.casdoor; + +import org.casbin.casdoor.entity.CasbinRule; +import org.casbin.casdoor.entity.Enforcer; +import org.casbin.casdoor.service.EnforcerService; +import org.casbin.casdoor.service.PolicyService; +import org.casbin.casdoor.support.TestDefaultConfig; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class PolicyTest { + + private final EnforcerService enforcerService = new EnforcerService(TestDefaultConfig.InitConfig()); + + private final PolicyService policyService = new PolicyService(TestDefaultConfig.InitConfig()); + + @Test + public void testPolicy() { + String name = TestDefaultConfig.getRandomName("Enforcer"); + + // Add a new object + Enforcer enforcer = new Enforcer( + "admin", + name, + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME), + name, + "built-in/user-model-built-in", + "built-in/user-adapter-built-in", + "Casdoor Website"); + assertDoesNotThrow(() -> enforcerService.addEnforcer(enforcer)); + + CasbinRule policy = new CasbinRule(0,"p","1","2","4","","","",""); + assertDoesNotThrow(() -> policyService.addPolicy(enforcer, policy)); + + // Get all objects, check if our added object is inside the list + List policies; + try { + policies = policyService.getPolicies(name,""); + } catch (IOException e) { + fail("Failed to get objects: " + e.getMessage()); + return; + } + + boolean found = policies.stream().anyMatch(item -> item.Ptype.equals("p") && item.V2.equals("4")); + assertTrue(found, "Added object not found in list"); + + // Update the object + CasbinRule newpolicy = new CasbinRule(0,"p","1","2","5","","","",""); + assertDoesNotThrow(() -> policyService.updatePolicy(enforcer, policy, newpolicy)); + + // Validate the update + try { + policies = policyService.getPolicies(name,""); + } catch (IOException e) { + fail("Failed to get objects: " + e.getMessage()); + return; + } + + found = policies.stream().anyMatch(item -> item.Ptype.equals("p") && item.V2.equals("5")); + assertTrue(found, "Updated object not found in list"); + + // Delete the object + assertDoesNotThrow(() -> policyService.removePolicy(enforcer, newpolicy)); + // Validate the deletion + try { + policies = policyService.getPolicies(name,""); + } catch (IOException e) { + fail("Failed to get objects: " + e.getMessage()); + return; + } + found = policies.stream().anyMatch(item -> item.Ptype.equals("p") && item.V2.equals("5")); + assertFalse(found, "Deleted object not found in list"); + } +}