-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
161 additions
and
11 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
scas "github.com/qiangmzsx/string-adapter" | ||
|
||
"github.com/casbin/casbin" | ||
"github.com/casbin/casbin/model" | ||
) | ||
|
||
func main() { | ||
|
||
modelText := ` | ||
[request_definition] | ||
r = sub, obj, act | ||
[policy_definition] | ||
p = sub, obj, act | ||
[role_definition] | ||
g = _, _ | ||
g2 = _, _ | ||
[policy_effect] | ||
e = some(where (p.eft == allow)) | ||
[matchers] | ||
m = g(r.sub, p.sub) && g2(r.obj, p.obj) && r.act == p.act` | ||
|
||
m := model.Model{} | ||
|
||
m.LoadModelFromText(modelText) | ||
|
||
line := ` | ||
p, alice, data1, read | ||
p, bob, data2, write | ||
p, data_group_admin, data_group, write | ||
g, alice, data_group_admin | ||
g2, data1, data_group | ||
g2, data2, data_group | ||
` | ||
sa := scas.NewAdapter(line) | ||
|
||
// Initialize a Gorm adapter and use it in a Casbin enforcer: | ||
// The adapter will use the MySQL database named "casbin". | ||
// If it doesn't exist, the adapter will create it automatically. | ||
// You can also use an already existing gorm instance with gormadapter.NewAdapterByDB(gormInstance) | ||
//a, _ := gormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") // Your driver and data source. | ||
// e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a) | ||
e := casbin.NewEnforcer(m, sa) | ||
|
||
// Or you can use an existing DB "abc" like this: | ||
// The adapter will use the table named "casbin_rule". | ||
// If it doesn't exist, the adapter will create it automatically. | ||
// a := gormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/abc", true) | ||
|
||
// Load the policy from DB. | ||
e.LoadPolicy() | ||
|
||
// Check the permission. | ||
if res := e.Enforce("alice", "data1", "read"); res { | ||
fmt.Println("permitted") | ||
} else { | ||
fmt.Println("rejected") | ||
} | ||
|
||
// Modify the policy. | ||
// e.AddPolicy(...) | ||
// e.RemovePolicy(...) | ||
|
||
// Save the policy back to DB. | ||
e.SavePolicy() | ||
} |
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