README.md 2.37 KB
Newer Older
1 2
[![Build Status](https://travis-ci.org/casbin/casbin-pg-adapter.svg?branch=master)](https://travis-ci.org/casbin/casbin-pg-adapter)

khoipham's avatar
khoipham committed
3
# Go-pg Adapter
4 5 6 7 8

Go-pg Adapter is the [Go-pg](https://github.com/go-pg/pg) adapter for [Casbin](https://github.com/casbin/casbin). With this library, Casbin can load policy from PostgreSQL or save policy to it.

## Installation

khoipham's avatar
khoipham committed
9
    go get github.com/casbin/casbin-pg-adapter
10 11 12 13 14 15 16

## Simple Postgres Example

```go
package main

import (
khoipham's avatar
khoipham committed
17
	pgadapter "github.com/casbin/casbin-pg-adapter"
khoipham's avatar
khoipham committed
18
	"github.com/casbin/casbin/v2"
19 20 21 22 23 24
)

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.
khoipham's avatar
khoipham committed
25
	a, _ := pgadapter.NewAdapter("postgresql://username:password@postgres:5432/database?sslmode=disable") // Your driver and data source.
khoipham's avatar
khoipham committed
26 27 28 29 30 31
	// Alternatively, you can construct an adapter instance with *pg.Options:
	// a, _ := pgadapter.NewAdapter(&pg.Options{
	//     Database: "...",
	//     User: "...",
	//     Password: "...",
	// })
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

	// 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.

	e := casbin.NewEnforcer("examples/rbac_model.conf", a)

	// Load the policy from DB.
	e.LoadPolicy()

	// Check the permission.
	e.Enforce("alice", "data1", "read")

	// Modify the policy.
	// e.AddPolicy(...)
	// e.RemovePolicy(...)

	// Save the policy back to DB.
	e.SavePolicy()
}
```

khoipham's avatar
khoipham committed
54 55 56 57 58 59 60 61 62
## 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 (
	"github.com/casbin/casbin/v2"
khoipham's avatar
khoipham committed
63
	pgadapter "github.com/casbin/casbin-pg-adapter"
khoipham's avatar
khoipham committed
64 65 66
)

func main() {
khoipham's avatar
khoipham committed
67
	a, _ := pgadapter.NewAdapter("postgresql://username:password@postgres:5432/database?sslmode=disable")
khoipham's avatar
khoipham committed
68 69 70 71 72 73 74 75 76 77
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

	e.LoadFilteredPolicy(&pgadapter.Filter{
		P: []string{"", "data1"},
		G: []string{"alice"},
	})
	...
}
```

khoipham's avatar
khoipham committed
78 79 80 81 82 83
## Run all tests

    docker-compose run --rm go

## Debug tests

khoipham's avatar
khoipham committed
84
    docker-compose run --rm go dlv test github.com/casbin/casbin-pg-adapter
khoipham's avatar
khoipham committed
85

86 87
## Getting Help

khoipham's avatar
khoipham committed
88
-   [Casbin](https://github.com/casbin/casbin)
89 90 91 92

## License

This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.