Skip to content
PostgreSQL adapter for Casbin
Go Makefile
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
pkg
LICENSE Initial commit Dec 9, 2019
Makefile
README.md
adapter.go Rollback failed database operation for starting up Dec 12, 2019
adapter_test.go Need caller pass DB to create adapter instead of creating ourselve Dec 11, 2019
docker-compose.yml
go.mod
go.sum Conform Adaptor to casbin adaptor interface Dec 10, 2019

README.md

Casbin Postgres Adapter

Casbin Postgres Adapter is the postgres adapter for Casbin

Installation

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

Example

package main

import (
  "database/sql"
  "os"

  "github.com/casbin/casbin/v2"
  "github.com/cychiuae/casbin-pg-adapter"
)

func main() {
  connectionString := "postgresql://postgres:@localhost:5432/postgres?sslmode=disable"
  db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
  if err != nil {
    panic(err)
  }

  tableName := "casbin"
  adapter, err := casbinpgadapter.NewAdapter(db, tableName)
  if err != nil {
    panic(err)
  }

  enforcer, err := casbin.NewEnforcer("./examples/model.conf", adapter)
  if err != nil {
    panic(err)
  }

  // Load stored policy from database
  enforcer.LoadPolicy()

  // Do permission checking
  enforcer.Enforce("alice", "data1", "write")

  // Do some mutations
  enforcer.AddPolicy("alice", "data2", "write")
  enforcer.RemovePolicy("alice", "data1", "write")

  // Persist policy to database
  enforcer.SavePolicy()
}
You can’t perform that action at this time.