Back to godoc.org
github.com/reedom/echo-middleware-casbin

Package casbinmw

v1.2.0
Latest Go to latest

The latest major version is .

Published: Jun 10, 2020 | License: MIT | Module: github.com/reedom/echo-middleware-casbin

Index

Variables

var (
	DefaultConfig = Config{
		Skipper: middleware.DefaultSkipper,
	}
)

func Middleware

func Middleware(ce *casbin.Enforcer, ds DataSource) echo.MiddlewareFunc

Middleware returns a Echo middleware.

func MiddlewareWithConfig

func MiddlewareWithConfig(config Config) echo.MiddlewareFunc

MiddlewareWithConfig returns an Echo middleware with config.

type Config

type Config struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper
	// BeforeFunc defines a function which is executed just before the middleware.
	BeforeFunc middleware.BeforeFunc
	// GetURLPathFunc defines a function which return the requested URL.
	GetURLPathFunc func(ctx echo.Context) string
	// SuccessHandler defines a function which is executed for a granted access.
	SuccessHandler func(echo.Context)
	// ErrorHandler defines a function which is executed for a rejected access.
	// It may be used to define a custom error.
	ErrorHandler func(error, echo.Context) error
	// Enforcer instance.
	Enforcer *casbin.Enforcer
	// DataSource is the interface that extract a subject from echo.Context.
	DataSource DataSource
}

Config defines the config for this middleware.

func (*Config) GetSubject

func (a *Config) GetSubject(c echo.Context) string

GetSubject extract a subject from the request.

func (*Config) HasPermission

func (a *Config) HasPermission(c echo.Context, urlPath string) (bool, error)

HasPermission checks a resource access permission against casbin with the subject/method/path combination from the request. Returns true (permission granted) or false (permission forbidden).

type DataSource

type DataSource interface {
	GetSubject(c echo.Context) string
}

DataSource is the interface that extract a subject from echo.Context.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier