Skip to content

Commit

Permalink
add demo and update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangmzsx committed Nov 8, 2019
1 parent cdf41bf commit 565080a
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 11 deletions.
6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,80 @@ g, alice, data_group_admin

## casbin v2
```go
package main

import (
"fmt"

scas "github.com/qiangmzsx/string-adapter/v2"

"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/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()
}

```
75 changes: 75 additions & 0 deletions demo/v1/v1_demo.go
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()
}
16 changes: 11 additions & 5 deletions demo/v2_demo.go → demo/v2/v2_demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@ r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
g2 = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == r.obj.Owner`
m = g(r.sub, p.sub) && g2(r.obj, p.obj) && r.act == p.act`

m := model.Model{}

m.LoadModelFromText(modelText)

line := `
p, alice, /alice_data/*, (GET)|(POST)
p, alice, /alice_data/resource1, POST
p, data_group_admin, /admin/*, POST
p, data_group_admin, /bob_data/*, POST
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)

Expand Down

0 comments on commit 565080a

Please sign in to comment.