All Projects → casbin → casbin-pg-adapter

casbin / casbin-pg-adapter

Licence: Apache-2.0 license
A go-pg adapter for casbin

Programming Languages

go
31211 projects - #10 most used programming language
Dockerfile
14818 projects
shell
77523 projects

Projects that are alternatives of or similar to casbin-pg-adapter

Gorm Adapter
Gorm adapter for Casbin
Stars: ✭ 373 (+1521.74%)
Mutual labels:  adapter, access-control, casbin
sqlx-adapter
Asynchronous casbin adapter for mysql, postgres, sqlite based on sqlx-rs
Stars: ✭ 27 (+17.39%)
Mutual labels:  adapter, access-control, casbin
Redis Adapter
Redis adapter for Casbin
Stars: ✭ 167 (+626.09%)
Mutual labels:  adapter, access-control, casbin
sequelize-adapter
Sequelize adapter for Casbin
Stars: ✭ 51 (+121.74%)
Mutual labels:  adapter, access-control, casbin
Xorm Adapter
Xorm adapter for Casbin
Stars: ✭ 329 (+1330.43%)
Mutual labels:  adapter, access-control, casbin
Protobuf Adapter
Google Protocol Buffers adapter for Casbin
Stars: ✭ 185 (+704.35%)
Mutual labels:  adapter, access-control, casbin
Mongodb Adapter
MongoDB adapter for Casbin
Stars: ✭ 194 (+743.48%)
Mutual labels:  adapter, access-control, casbin
Beego Authz
Beego's RBAC & ABAC Authorization middleware based on Casbin
Stars: ✭ 208 (+804.35%)
Mutual labels:  access-control, casbin
Caddy Authz
Caddy-authz is a middleware for Caddy that blocks or allows requests based on access control policies.
Stars: ✭ 221 (+860.87%)
Mutual labels:  access-control, casbin
dart-casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Dart/Flutter
Stars: ✭ 30 (+30.43%)
Mutual labels:  access-control, casbin
laravel-casbin
This repository has moved to https://github.com/php-casbin/laravel-authz
Stars: ✭ 42 (+82.61%)
Mutual labels:  adapter, casbin
jdbc-adapter
JDBC adapter for Casbin
Stars: ✭ 26 (+13.04%)
Mutual labels:  adapter, access-control
lua-casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Lua (OpenResty)
Stars: ✭ 43 (+86.96%)
Mutual labels:  access-control, casbin
Casbin Authz Plugin
Docker Authorization Plugin based on Casbin
Stars: ✭ 204 (+786.96%)
Mutual labels:  access-control, casbin
Casbin Server
Casbin as a Service (CaaS)
Stars: ✭ 171 (+643.48%)
Mutual labels:  access-control, casbin
Chi Authz
chi-authz is an authorization middleware for Chi
Stars: ✭ 248 (+978.26%)
Mutual labels:  access-control, casbin
Etcd Watcher
Etcd watcher for Casbin
Stars: ✭ 157 (+582.61%)
Mutual labels:  access-control, casbin
Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (+573.91%)
Mutual labels:  access-control, casbin
Think Casbin
专为ThinkPHP定制的Casbin的扩展包,Casbin是一个功能强大,高效的开源访问控制库。
Stars: ✭ 138 (+500%)
Mutual labels:  access-control, casbin
Negroni Authz
negroni-authz is an authorization middleware for Negroni
Stars: ✭ 152 (+560.87%)
Mutual labels:  access-control, casbin

Go-pg Adapter

Go Coverage Status

Go-pg Adapter is the Go-pg adapter for Casbin. With this library, Casbin can load policy from PostgreSQL or save policy to it.

Installation

go get github.com/casbin/casbin-pg-adapter

Simple Postgres Example

package main

import (
	pgadapter "github.com/casbin/casbin-pg-adapter"
	"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("postgresql://username:password@postgres:5432/database?sslmode=disable") // 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".
	// 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()
}

Support for FilteredAdapter interface

You can load a subset of policies with this adapter:

package main

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

func main() {
	a, _ := pgadapter.NewAdapter("postgresql://username:password@postgres:5432/database?sslmode=disable")
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

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

Custom DB Connection

You can provide a custom table or database name with pgadapter.NewAdapterByDB

package main

import (
	"github.com/casbin/casbin/v2"
	pgadapter "github.com/casbin/casbin-pg-adapter"
	"github.com/go-pg/pg/v9"
)

func main() {
	opts, _ := pg.ParseURL("postgresql://pguser:pgpassword@localhost:5432/pgdb?sslmode=disable")

	db := pg.Connect(opts)
	defer db.Close()

	a, _ := pgadapter.NewAdapterByDB(db, pgadapter.WithTableName("custom_table"))
	e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
    ...
}

Run all tests

docker-compose run --rm go

Debug tests

docker-compose run --rm go dlv test github.com/casbin/casbin-pg-adapter

Getting Help

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].