Commit 38a27228 authored by khoipham's avatar khoipham

add documentation

parent e91692c9
...@@ -15,7 +15,7 @@ import ( ...@@ -15,7 +15,7 @@ import (
"os" "os"
pgadapter "github.com/pckhoi/casbin-pg-adapter" pgadapter "github.com/pckhoi/casbin-pg-adapter"
"github.com/casbin/casbin" "github.com/casbin/casbin/v2"
) )
func main() { func main() {
...@@ -23,6 +23,12 @@ func main() { ...@@ -23,6 +23,12 @@ func main() {
// The adapter will use the Postgres database named "casbin". // The adapter will use the Postgres database named "casbin".
// If it doesn't exist, the adapter will create it automatically. // If it doesn't exist, the adapter will create it automatically.
a, _ := pgadapter.NewAdapter(os.Getenv("PG_CONN")) // Your driver and data source. a, _ := pgadapter.NewAdapter(os.Getenv("PG_CONN")) // Your driver and data source.
// Alternatively, you can construct an adapter instance with *pg.Options:
// a, _ := pgadapter.NewAdapter(&pg.Options{
// Database: "...",
// User: "...",
// Password: "...",
// })
// Or you can use an existing DB "abc" like this: // Or you can use an existing DB "abc" like this:
// The adapter will use the table named "casbin_rule". // The adapter will use the table named "casbin_rule".
...@@ -45,6 +51,32 @@ func main() { ...@@ -45,6 +51,32 @@ func main() {
} }
``` ```
## Support for FilteredAdapter interface
You can [load a subset of policies](https://casbin.org/docs/en/policy-subset-loading) with this adapter:
```go
package main
import (
"os"
"github.com/casbin/casbin/v2"
pgadapter "github.com/pckhoi/casbin-pg-adapter"
)
func main() {
a, _ := pgadapter.NewAdapter(os.Getenv("PG_CONN"))
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
e.LoadFilteredPolicy(&pgadapter.Filter{
P: []string{"", "data1"},
G: []string{"alice"},
})
...
}
```
## Run all tests ## Run all tests
docker-compose run --rm go docker-compose run --rm go
......
...@@ -8,5 +8,4 @@ require ( ...@@ -8,5 +8,4 @@ require (
github.com/go-pg/pg/v9 v9.1.0 github.com/go-pg/pg/v9 v9.1.0
github.com/mmcloughlin/meow v0.0.0-20181112033425-871e50784daf github.com/mmcloughlin/meow v0.0.0-20181112033425-871e50784daf
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413
) )
This diff is collapsed.
...@@ -3,7 +3,7 @@ package main ...@@ -3,7 +3,7 @@ package main
import ( import (
"os" "os"
"github.com/casbin/casbin" "github.com/casbin/casbin/v2"
pgadapter "github.com/pckhoi/casbin-pg-adapter" pgadapter "github.com/pckhoi/casbin-pg-adapter"
) )
...@@ -12,16 +12,28 @@ func main() { ...@@ -12,16 +12,28 @@ func main() {
// The adapter will use the Postgres database named "casbin". // The adapter will use the Postgres database named "casbin".
// If it doesn't exist, the adapter will create it automatically. // If it doesn't exist, the adapter will create it automatically.
a, _ := pgadapter.NewAdapter(os.Getenv("PG_CONN")) // Your driver and data source. a, _ := pgadapter.NewAdapter(os.Getenv("PG_CONN")) // Your driver and data source.
// Alternatively, you can construct an adapter instance with *pg.Options:
// a, _ := pgadapter.NewAdapter(&pg.Options{
// Database: "...",
// User: "...",
// Password: "...",
// })
// Or you can use an existing DB "abc" like this: // Or you can use an existing DB "abc" like this:
// The adapter will use the table named "casbin_rule". // The adapter will use the table named "casbin_rule".
// If it doesn't exist, the adapter will create it automatically. // If it doesn't exist, the adapter will create it automatically.
e := casbin.NewEnforcer("examples/rbac_model.conf", a) e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
// Load the policy from DB. // Load the policy from DB.
e.LoadPolicy() e.LoadPolicy()
// Alternatively load a subset of policy as demonstrated at https://casbin.org/docs/en/policy-subset-loading
// e.LoadFilteredPolicy(&pgadapter.Filter{
// P: []string{"", "data1"},
// G: []string{"alice"},
// })
// Check the permission. // Check the permission.
e.Enforce("alice", "data1", "read") e.Enforce("alice", "data1", "read")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment