README.md 2.54 KB
Newer Older
khoipham's avatar
khoipham committed
1
# Go-pg Adapter
2

khoipham's avatar
khoipham committed
3 4 5
[![Build Status](https://travis-ci.org/casbin/casbin-pg-adapter.svg?branch=master)](https://travis-ci.org/casbin/casbin-pg-adapter)
[![Coverage Status](https://coveralls.io/repos/github/casbin/casbin-pg-adapter/badge.svg?branch=master)](https://coveralls.io/github/casbin/casbin-pg-adapter?branch=master)

6 7 8 9
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
10
    go get github.com/casbin/casbin-pg-adapter
11 12 13 14 15 16 17

## Simple Postgres Example

```go
package main

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

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

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

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

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

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

    docker-compose run --rm go

## Debug tests

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

87 88
## Getting Help

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

## License

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