middleware

package module
v0.0.0-...-8092743 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2018 License: BSD-3-Clause Imports: 14 Imported by: 0

README

martini-middleware

martini-middleware

Documentation

Overview

 	rbac := middleware.NewRbac()
 	m.Map(rbac)

	m.Get("/article/edit", func(c *middleware.Context, rbac *middleware.Rbac) {
		ok := rbac.IsGranted("editor", "edit.article",
			func(role, permission string, rbac *middleware.Rbac) bool {
				return article.Owner == User.Id
			})
		if ok {
			fmt.Println("have permission....")
		} else {
			fmt.Println("not have permission....")
		}
		c.HTML(200, "index", c.Data)
	})

Index

Constants

View Source
const (
	ParentKey     = "parents"
	PermissionKey = "permissions"
	NameKey       = "name"
)

Variables

View Source
var Locale *i18n.Locale

Functions

func AuthRequest

func AuthRequest(disUrls ...string) martini.Handler

setup not need check authorize URL

 	urls := []string{"/login", "/users", "/firewall"}
	    m.Use(middleware.AuthRequest(urls...))

func EncodeMsg

func EncodeMsg(msg Msg) (body []byte, err error)

func I18n

func I18n(options ...I18nOptions) martini.Handler

I18n is a middleware provides localization layer for your application. Paramenter langs must be in the form of "en-US", "zh-CN", etc. Otherwise it may not recognize browser input.

func InitContext

func InitContext() martini.Handler

func InitContextNoSess

func InitContextNoSess() martini.Handler

func InitSession

func InitSession(name, key string) martini.Handler

func Tr

func Tr(format string, args ...interface{}) string

func Version

func Version() string

Types

type AssertionFunc

type AssertionFunc func(string, string, *Rbac) bool

Assertion function supplies more fine-grained permission controls.

type Context

type Context struct {
	render.Render
	C        martini.Context
	P        martini.Params
	S        sessions.Session
	R        *http.Request
	W        http.ResponseWriter
	FormErr  binding.Errors
	Messages []string
	Errors   []string
	Data     map[string]interface{}
	// contains filtered or unexported fields
}

func (*Context) AddMessage

func (self *Context) AddMessage(message string)

func (*Context) Clear

func (self *Context) Clear()

func (*Context) ClearMessages

func (self *Context) ClearMessages()

func (*Context) Delete

func (self *Context) Delete(key string)

func (*Context) Get

func (self *Context) Get(key string) interface{}

func (*Context) GetSession

func (self *Context) GetSession(name string, value interface{}) (err error)

Parse session value

func (*Context) HMessage

func (self *Context) HMessage(code int, url, message string, v ...interface{})

Render HTML message

func (*Context) HTML

func (self *Context) HTML(code int, name string, binding interface{}, htmlOpt ...HTMLOptions)

Parse HTML code

func (*Context) HasMessage

func (self *Context) HasMessage() bool

func (*Context) JMessage

func (self *Context) JMessage(code int, url, message string, v ...interface{})

Render JSON message

func (*Context) PostForm

func (self *Context) PostForm(key string, def ...string) (value string)

func (*Context) Query

func (self *Context) Query(key string, def ...string) (value string)

func (*Context) Set

func (self *Context) Set(key string, val interface{})

type HTMLOptions

type HTMLOptions struct {
	Layout   string
	Template string
}

type I18nOptions

type I18nOptions struct {
	Domain      string
	Directory   string
	ZipData     []byte
	DefaultLang string
	// Suburl of path. Default is empty.
	SubURL     string
	CookieName string
	// Name of language parameter name in URL. Default is "lang".
	Parameter string
	// Redirect when user uses get parameter to specify language.
	Redirect bool
	// Name that maps into template variable. Default is "i18n".
	TmplName string
	Inited   bool
}

Options represents a struct for specifying configuration options for the i18n middleware.

type Map

type Map map[string]RoleMap

Export RBAC to a structure data

type Msg

type Msg struct {
	Code    int    // status code
	Message string // status message
	Url     string // redirect url
}

func DecodeMsg

func DecodeMsg(body []byte) (msg Msg, err error)

type Rbac

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

RBAC

func NewRbac

func NewRbac() *Rbac

Return a RBAC structure. The default role structure will be used.

func NewWithFactory

func NewWithFactory(factory RoleFactoryFunc) *Rbac

Return a RBAC structure with a specific factory function. Role structure will be generated by the function.

func Restore

func Restore(data Map) *Rbac

Restore rbac from a map, a default role implamentation used

func RestoreWithFactory

func RestoreWithFactory(data Map, factory RoleFactoryFunc) *Rbac

Restore rbac from a map, use factory for your own data structure

func (*Rbac) Add

func (rbac *Rbac) Add(name string, permissions []string, parents []string)

Add a role with `name`. It has `permissions` and `parents`. If the role is not existing, a new one will be created. This function will add new permissions and parents to the role, and keep orignals.

func (*Rbac) Dump

func (rbac *Rbac) Dump() Map

Dump RBAC

func (*Rbac) Get

func (rbac *Rbac) Get(name string) Role

Return a role or nil if not exists.

func (*Rbac) IsGranted

func (rbac *Rbac) IsGranted(name, permission string,
	assert AssertionFunc) bool

Test if the `name` has `permission` in the `assert` condition.

func (*Rbac) Remove

func (rbac *Rbac) Remove(name string)

Remove a role.

func (*Rbac) Set

func (rbac *Rbac) Set(name string, permissions []string, parents []string)

Set a role with `name`. It has `permissions` and `parents`. If the role is not existing, a new one will be created. This function will cover role's orignal permissions and parents.

type Role

type Role interface {
	Name() string
	AddPermission(string)
	HasPermission(string) bool
	RevokePermission(string)
	Permissions() []string
	AddParent(string)
	RemoveParent(string)
	Parents() []string
	Reset()
}

Implement this interface for your own role structure.

type RoleFactoryFunc

type RoleFactoryFunc func(*Rbac, string) Role

Sometimes, a custom role structure is needed by projects. You should define your own role factory function for this purpuse.

type RoleMap

type RoleMap map[string][]string

An exportable data structure

func RoleToMap

func RoleToMap(role Role) RoleMap

An interface can't export directly. But you can convert it into a map.

Jump to

Keyboard shortcuts

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