Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support stream effector (#238)
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
- Loading branch information
Showing
8 changed files
with
250 additions
and
22 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
67 changes: 67 additions & 0 deletions
67
src/main/java/org/casbin/jcasbin/effect/DefaultStreamEffector.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,67 @@ | ||
// Copyright 2021 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.effect; | ||
|
||
public class DefaultStreamEffector implements StreamEffector { | ||
private final String expr; | ||
private boolean done = false; | ||
private boolean effect = false; | ||
|
||
public DefaultStreamEffector(String expr) { | ||
this.expr = expr; | ||
} | ||
|
||
@Override | ||
public StreamEffectorResult current() { | ||
return new DefaultStreamEffectorResult(effect, done); | ||
} | ||
|
||
@Override | ||
public boolean push(Effect eft, int currentIndex, int policySize) { | ||
switch (this.expr) { | ||
case "some(where (p_eft == allow))": | ||
if (eft == Effect.Allow) { | ||
this.effect = true; | ||
this.done = true; | ||
} | ||
break; | ||
case "!some(where (p_eft == deny))": | ||
this.effect = true; | ||
if (eft == Effect.Deny) { | ||
this.effect = false; | ||
this.done = true; | ||
} | ||
break; | ||
case "some(where (p_eft == allow)) && !some(where (p_eft == deny))": | ||
if (eft == Effect.Allow) { | ||
this.effect = true; | ||
} else if (eft == Effect.Deny) { | ||
this.effect = false; | ||
this.done = true; | ||
} | ||
break; | ||
case "priority(p_eft) || deny": | ||
if (eft != Effect.Indeterminate) { | ||
this.effect = eft == Effect.Allow; | ||
this.done = true; | ||
} | ||
break; | ||
default: | ||
throw new UnsupportedOperationException("unsupported effect"); | ||
} | ||
|
||
return this.done; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/casbin/jcasbin/effect/DefaultStreamEffectorResult.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,46 @@ | ||
// Copyright 2021 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.effect; | ||
|
||
public class DefaultStreamEffectorResult implements StreamEffectorResult { | ||
private boolean done; | ||
private boolean effect; | ||
|
||
public DefaultStreamEffectorResult(){ | ||
|
||
} | ||
|
||
public DefaultStreamEffectorResult(boolean effect, boolean done) { | ||
this.effect = effect; | ||
this.done = done; | ||
} | ||
|
||
@Override | ||
public boolean hasEffect() { | ||
return effect; | ||
} | ||
|
||
public boolean isDone() { | ||
return done; | ||
} | ||
|
||
public void setEffect(boolean effect) { | ||
this.effect = effect; | ||
} | ||
|
||
public void setDone(boolean done) { | ||
this.done = done; | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
src/main/java/org/casbin/jcasbin/effect/StreamEffector.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,21 @@ | ||
// Copyright 2021 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.effect; | ||
|
||
// StreamEffector provides a stream interface for effector. | ||
public interface StreamEffector { | ||
StreamEffectorResult current(); | ||
boolean push(Effect effect, int currentIndex, int policySize); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/casbin/jcasbin/effect/StreamEffectorResult.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,20 @@ | ||
// Copyright 2021 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.effect; | ||
|
||
public interface StreamEffectorResult { | ||
boolean hasEffect(); | ||
boolean isDone(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/casbin/jcasbin/exception/CasbinEffectorException.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,21 @@ | ||
// Copyright 2018 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.exception; | ||
|
||
public class CasbinEffectorException extends RuntimeException { | ||
public CasbinEffectorException(Throwable throwable) { | ||
super(throwable); | ||
} | ||
} |
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