-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from shink/master
feat: add support for custom function
- Loading branch information
Showing
14 changed files
with
210 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[request_definition] | ||
r = sub, obj, act | ||
|
||
[policy_definition] | ||
p = sub_rule, obj, act | ||
|
||
[policy_effect] | ||
e = some(where (p.eft == allow)) | ||
|
||
[matchers] | ||
m = eval(p.sub_rule) && r.act == p.act |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
p, r.sub.name == 'alice' && custom(r.obj), r.obj, GET | ||
p, r.sub.age >= 18 && custom(r.obj), r.obj, GET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
p, r.domain.equals("domain1"), admin, domain1, data1, read | ||
p, r.domain.equals("domain1"), admin, domain1, data1, write | ||
p, r.domain.equals("domain2"), admin, domain2, data2, read | ||
p, r.domain.equals("domain2"), admin, domain2, data2, write | ||
p, r.domain == 'domain1', admin, domain1, data1, read | ||
p, r.domain == 'domain1', admin, domain1, data1, write | ||
p, r.domain == 'domain2', admin, domain2, data2, read | ||
p, r.domain == 'domain2', admin, domain2, data2, write | ||
g, alice, admin, domain1 | ||
g, bob, admin, domain2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/org/casbin/jcasbin/util/function/CustomFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2020 The casbin 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.jcasbin.util.function; | ||
|
||
import com.googlecode.aviator.AviatorEvaluatorInstance; | ||
import com.googlecode.aviator.runtime.function.AbstractFunction; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author: shink | ||
*/ | ||
public abstract class CustomFunction extends AbstractFunction { | ||
|
||
private AviatorEvaluatorInstance aviatorEval; | ||
|
||
public String replaceTargets(String exp, Map<String, Object> env) { | ||
for (String key : env.keySet()) { | ||
int index; | ||
if ((index = key.indexOf('_')) != -1) { | ||
String s = key.substring(index + 1); | ||
exp = exp.replace("." + s, "_" + s); | ||
} | ||
} | ||
return exp; | ||
} | ||
|
||
public AviatorEvaluatorInstance getAviatorEval() { | ||
return aviatorEval; | ||
} | ||
|
||
public void setAviatorEval(AviatorEvaluatorInstance aviatorEval) { | ||
this.aviatorEval = aviatorEval; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.