Skip to content

Commit

Permalink
Merge pull request #20 from captt-g/master
Browse files Browse the repository at this point in the history
fix: fix CoreAPI bug
  • Loading branch information
hsluoyz authored Jun 17, 2021
2 parents 9b933e3 + 5e3320a commit 977bc49
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,6 @@ Icon
Network Trash Folder
Temporary Items
.apdisk
# Ignore the Package.resolved

Package.resolved
6 changes: 3 additions & 3 deletions Sources/Casbin/APi/ManagementApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public extension CoreApi {
model.hasPolicy(sec: "g", ptype: ptype, rule: params)
}
func getAllNamedSubjects(ptype:String) -> [String] {
model.getValuesForFieldInPolicy(sec: "p", ptype: ptype, fieldIndex: 0)
model.getValuesForFieldInPolicy(sec: "p", ptype: ptype, fieldIndex: model.getModel()["p"]?["p"]?.tokens.firstIndex(of: "p_sub") ?? 0)
}
func getAllNamedObjects(ptype:String) -> [String] {
model.getValuesForFieldInPolicy(sec: "p", ptype: ptype, fieldIndex: 1)
model.getValuesForFieldInPolicy(sec: "p", ptype: ptype, fieldIndex: model.getModel()["p"]?["p"]?.tokens.firstIndex(of: "p_obj") ?? 1)
}
func getAllNamedActions(ptype:String) -> [String] {
model.getValuesForFieldInPolicy(sec: "p", ptype: ptype, fieldIndex: 2)
model.getValuesForFieldInPolicy(sec: "p", ptype: ptype, fieldIndex: model.getModel()["p"]?["p"]?.tokens.firstIndex(of: "p_act") ?? 2)
}
func getAllNamedRoles(ptype:String) -> [String] {
model.getValuesForFieldInPolicy(sec: "g", ptype: ptype, fieldIndex: 1)
Expand Down
16 changes: 9 additions & 7 deletions Sources/Casbin/Model/DefaultModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,15 @@ extension DefaultModel:Model {
public func hasPolicy(sec:String,ptype:String,rule:[String]) -> Bool {
getPolicy(sec: sec, ptype: ptype).contains(rule)
}
public func getValuesForFieldInPolicy(sec:String,ptype:String,fieldIndex:Int) -> [String] {
getPolicy(sec: sec, ptype: ptype).reduce([]) { acc, x in
var acc = acc
acc.append(x[fieldIndex])
return acc
}
}
public func getValuesForFieldInPolicy(sec:String,ptype:String,fieldIndex:Int) -> [String] {
let policy = getPolicy(sec: sec, ptype: ptype).reduce(Set<String>()) { acc, x in
var acc = acc
acc.insert(x[fieldIndex])
return acc
}
return Array(policy)
}

public func removePolicy(sec:String,ptype:String,rule: [String]) -> Bool {
if let ast = model[sec]?[ptype] {
ast.policy.removeAll {
Expand Down
13 changes: 11 additions & 2 deletions Tests/CasbinTests/RbacApiTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ final class RbacApiTests: XCTestCase {
XCTAssertEqual(["data1_admin", "data2_admin"], e.getRoles(for: "alice", domain: nil).sorted())
XCTAssertEqual([], e.getRoles(for: "bob", domain: nil).sorted())
XCTAssertEqual([], e.getRoles(for: "data2_admin", domain: nil).sorted())


XCTAssertEqual(["read", "write"], e.getAllActions().sorted())
//XCTAssertEqual(["data1", "data2"], e.getAllObjects().sorted())
//XCTAssertEqual(["alice", "bob", "data2_admin"], e.getAllSubjects().sorted())
//XCTAssertEqual(["data1_admin", "data2_admin"], e.getAllRoles().sorted())
}
func testCoreApi_for_RoleApi_with_domain() throws {
let e = try makeEnforer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv")
XCTAssertEqual(["read", "write"], e.getAllActions().sorted())
//XCTAssertEqual(["data1", "data2"], e.getAllObjects())
//XCTAssertEqual(["admin",], e.getAllSubjects())
//XCTAssertEqual(["admin", ], e.getAllRoles())
}
}

0 comments on commit 977bc49

Please sign in to comment.