casbin / jcasbin

An authorization library that supports access control models like ACL, RBAC, ABAC in Java
https://casbin.org
Apache License 2.0
2.4k stars 464 forks source link

Question: Is there a way to persist policy and model? #35

Closed DemianTinkiel closed 5 years ago

DemianTinkiel commented 5 years ago

Use case is that I have a program that takes a model and a policy path and to test it I'd like to be able to do something like

Model m = newModel();
m.addDef("r", "r", "sub, obj, act");
m.addDef("p", "p", "sub, obj, act");
m.addDef("g", "g", "_, _");
m.addDef("e", "e", "some(where (p.eft == allow))");
m.addDef("m", "m", "g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act");

Enforcer e = new Enforcer(m);

e.addPermissionForUser("alice", "data1", "read");
Path modelURI = e.persistModel();
Path policyURI = e.persistPolicy();

assertTrue(new myProgram(modelURI, policyURI).isAuth("alice", "read"))
hsluoyz commented 5 years ago

Hi @DemianTinkiel , model can be loaded in 3 ways: https://casbin.org/docs/en/model-storage. But Casbin doesn't provide a way to save it to somewhere. But It's very easy for you to write a util function to do that by accessing the Model object.

The policy can be saved or loaded with adapters. I don't know what's policyURI. But you need to implement an adapter to support your URI.

DemianTinkiel commented 5 years ago

Hi @hsluoyz, thanks for that. But I was thinking more in the lines of Model and Policy providing a toString method for their content. The adapters could the build on top of that?

hsluoyz commented 5 years ago

You can implement a toString() method for model.

DemianTinkiel commented 5 years ago

excellent. I'll do that + policy too

hsluoyz commented 5 years ago

I think here will be another PR, right?

DemianTinkiel commented 5 years ago

Yep. There will be one tomorrow morning.

hsluoyz commented 5 years ago

BTW, you can link your Email in this commit with your GitHub account for more accurate contribution calculation.

DemianTinkiel commented 5 years ago

@hsluoyz thanks, I'm just happy to help :smile: sorry for PR spam, merge conflict resolution in github is weird to me. #40 is ready when you are

DemianTinkiel commented 5 years ago

@hsluoyz You closed the PR. Is that intentional? Do you want a new one?

hsluoyz commented 5 years ago

Fixed in: https://github.com/casbin/jcasbin/commit/e3a28951550a184f2561da5c78156386f5364c28