rbac

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

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

Go to latest
Published: May 26, 2017 License: BSD-3-Clause Imports: 6 Imported by: 0

README

rbac Build Status

Rbac is a rbac middleware for Tango, it's based on https://github.com/mikespook/gorbac.

Installation

go get github.com/tango-contrib/rbac

Simple Example

package main

import (
	"github.com/lunny/tango"
	"github.com/mikespook/gorbac"
	"github.com/tango-contrib/rbac"
	"github.com/tango-contrib/session"
)

type LoginAction struct {
	session.Session
	rbac.Manager
}

func (l *LoginAction) Post() {
	l.SetRBACRole("writer")
}

type RBACPermAction struct {
	rbac.Perm `write`
}

func (a *RBACPermAction) Get() string {
	return "You have write permission"
}

func main() {
	t := tango.Classic()

	// init session middleware to store roles
	sessions := session.New()
	t.Use(sessions)

	// init rbac middleware
	goRBAC := gorbac.New()
	rA := gorbac.NewStdRole("writer")
	pA := gorbac.NewStdPermission("write")
	rA.Assign(pA)
	goRBAC.Add(rA)

	t.Use(rbac.RBAC(goRBAC, sessions))

	// define the routers
	t.Post("/login", new(LoginAction))
	t.Any("/perm_write", new(RBACPermAction))
	t.Run()
}
  • If you want to give difference perm for GET and POST, then you can give a tag
type Action struct {
	Perm `GET:"read" POST:"write"`
}
type Action struct {
	Role `GET:"reader" POST:"writer"`
}
  • If you want to dynamic perm, you can use rbac.PermTager and rbac.RolesTager interfaces.
type Action struct {
}

func (a *Action) PermTag() string {
	return `GET:"read" POST:"write"`
}
type Action struct {
}

func (a *Action) RolesTag() string {
	return `GET:"reader" POST:"writer"`
}

Getting Help

License

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultRoleSessionKey = "rbac_roles"
	DefaultNoPermString   = "You have no permission to visit this page"
)

Functions

func RBAC

func RBAC(rbac *gorbac.RBAC, sessions *session.Sessions, opts ...Options) tango.HandlerFunc

RBAC return a rbac handler.

Types

type Manager

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

func (*Manager) SetRBACRole

func (m *Manager) SetRBACRole(roles ...string)

func (*Manager) SetRBACSession

func (m *Manager) SetRBACSession(opt Options, sessions *session.Sessions, ctx *tango.Context)

type Options

type Options struct {
	RoleSessionKey string
	OnNoPerm       tango.HandlerFunc
}

type Perm

type Perm struct{}

type PermTager

type PermTager interface {
	PermTag() string
}

type Role

type Role struct{}

type RolesTager

type RolesTager interface {
	RolesTag() string
}

Jump to

Keyboard shortcuts

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