Skip to content

Commit

Permalink
feat: add policy APIs (#74)
Browse files Browse the repository at this point in the history
* feat:add add-policy feature

* feat: add policy APIs

* feat: change apache header year
  • Loading branch information
zhuying1999 authored Mar 17, 2024
1 parent 4557851 commit 26b92ec
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/org/casbin/casdoor/entity/CasbinRule.java
Original file line number Diff line number Diff line change
@@ -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(){

}

}
2 changes: 2 additions & 0 deletions src/main/java/org/casbin/casdoor/service/EnforcerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,4 +107,5 @@ private <T1, T2> CasdoorResponse<T1, T2> modifyEnforcer(EnforcerOperations metho
payload, new TypeReference<CasdoorResponse<T1, T2>>() {
});
}

}
74 changes: 74 additions & 0 deletions src/main/java/org/casbin/casdoor/service/PolicyService.java
Original file line number Diff line number Diff line change
@@ -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<CasbinRule> getPolicies(String enforcerName, String adapterId) throws IOException {
String id = config.organizationName + "/" + enforcerName;
CasdoorResponse<List<CasbinRule>, Object> resp = doGet("get-policies",
Map.of("id", id, "adapterId", adapterId), new TypeReference<CasdoorResponse<List<CasbinRule>, Object>>() {
});
return resp.getData();
}

public CasdoorResponse<Object, String> addPolicy(Enforcer enforcer, CasbinRule policy) throws IOException {
CasbinRule[] policies = new CasbinRule[1];
policies[0] = policy;
return modifyPolicy(PolicyOperations.ADD_Policy, enforcer, policies);
}

public CasdoorResponse<Object, String> removePolicy(Enforcer enforcer, CasbinRule policy) throws IOException {
CasbinRule[] policies = new CasbinRule[1];
policies[0] = policy;
return modifyPolicy(PolicyOperations.DELETE_Policy, enforcer, policies);
}

public CasdoorResponse<Object, String> 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 <T1, T2> CasdoorResponse<T1, T2> 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<CasdoorResponse<T1, T2>>() {
});
}
}
33 changes: 33 additions & 0 deletions src/main/java/org/casbin/casdoor/util/PolicyOperations.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
1 change: 1 addition & 0 deletions src/test/java/org/casbin/casdoor/EnforcerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
94 changes: 94 additions & 0 deletions src/test/java/org/casbin/casdoor/PolicyTest.java
Original file line number Diff line number Diff line change
@@ -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<CasbinRule> 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");
}
}

0 comments on commit 26b92ec

Please sign in to comment.