casbinpgadapter

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2020 License: Apache-2.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Casbin Postgres Adapter

Casbin Postgres Adapter is the postgres adapter for Casbin

Installation

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

Example

package main

import (
  "database/sql"
  "os"

  "github.com/casbin/casbin/v2"
  "github.com/CarbonFactory/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()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter is a postgresql adaptor for casbin

func NewAdapter

func NewAdapter(db *pg.DB, tableName string) (*Adapter, error)

NewAdapter returns a new casbin postgresql adapter

func (*Adapter) AddPolicy

func (adapter *Adapter) AddPolicy(sec string, ptype string, rule []string) error

AddPolicy adds a policy rule to the storage. This is part of the Auto-Save feature.

func (*Adapter) LoadPolicy

func (adapter *Adapter) LoadPolicy(cmodel cModel.Model) error

LoadPolicy loads all policy rules from the storage.

func (*Adapter) RemoveFilteredPolicy

func (adapter *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error

RemoveFilteredPolicy removes policy rules that match the filter from the storage. This is part of the Auto-Save feature.

func (*Adapter) RemovePolicy

func (adapter *Adapter) RemovePolicy(sec string, ptype string, rule []string) error

RemovePolicy removes a policy rule from the storage. This is part of the Auto-Save feature.

func (*Adapter) SavePolicy

func (adapter *Adapter) SavePolicy(cmodel cModel.Model) error

SavePolicy saves all policy rules to the storage.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址