Table of Contents
This provides jcasbin support to Play Framework.
Policy can be defined either in a .conf
file or through JDBC adapter.
- jcasbin
- jdbc-adapter
- database of your choice (in case you want to use JDBC Adapter)
(versions used for the project -sbt
)
"org.casbin" % "jcasbin" % "1.24.0",
"org.casbin" % "jdbc-adapter" % "2.3.3",
"org.postgresql" % "postgresql" % "42.5.0"
- Default:
casbin {
model = conf/casbin/model.conf
policy = conf/casbin/policy.csv
storeType = jdbc
...
}
- To use model, policy files:
- Make sure to add your model and policy files in
casbin
directory which is under theconf
directory. - Set
storeType = file
Note: If model file is not provided then default model will be used.
- To use JDBC Adapter for defining policy:
- Add your rules in
casbin_rule
table (will be created by default if not present).
Config example:
db.default {
driver = org.someDriver.Driver
url = "jdbc:example:example:play"
username = user
password = password
...
}
You can use enforcer by doing dependency injection in your controller.
@Inject
public PostResourceHandler(... other params, CasbinEnforcer enforcer) {
...
this.enforcer = enforcer;
}
Then everything else stays the same.
if(enforcer.enforce(role, resource, action)) {
// some logic
}