Skip to content

Commit

Permalink
add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pckhoi committed Dec 27, 2019
1 parent e91692c commit 38a2722
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 139 deletions.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ import (
"os"

pgadapter "github.com/pckhoi/casbin-pg-adapter"
"github.com/casbin/casbin"
"github.com/casbin/casbin/v2"
)

func main() {
// Initialize a Go-pg adapter and use it in a Casbin enforcer:
// The adapter will use the Postgres database named "casbin".
// If it doesn't exist, the adapter will create it automatically.
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:
// The adapter will use the table named "casbin_rule".
Expand All @@ -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

docker-compose run --rm go
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ require (
github.com/go-pg/pg/v9 v9.1.0
github.com/mmcloughlin/meow v0.0.0-20181112033425-871e50784daf
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413
)
Loading

0 comments on commit 38a2722

Please sign in to comment.