Documentation
¶
Overview ¶
goRBAC provides a lightweight role-based access control implementation in Golang.
For the purposes of this package:
- an identity has one or more roles.
- a role requests access to a permission.
- a permission is given to a role.
Thus, RBAC has the following model:
- many to many relationship between identities and roles.
- many to many relationship between roles and permissions.
- roles can have parent roles.
Index ¶
- Constants
- type AssertionFunc
- type Map
- type Rbac
- func (rbac *Rbac) Add(rank int, name string, permissions, parents []string, description string)
- func (rbac *Rbac) Dump() Map
- func (rbac *Rbac) Get(name string) Role
- func (rbac *Rbac) IsGranted(name, permission string, assert AssertionFunc) bool
- func (rbac *Rbac) Remove(name string)
- func (rbac *Rbac) Set(rank int, name string, permissions, parents []string, description string)
- type Role
- type RoleFactoryFunc
- type RoleMap
Examples ¶
Constants ¶
const ( ParentKey = "parents" PermissionKey = "permissions" NameKey = "name" RankKey = "rank" DescriptionKey = "description" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AssertionFunc ¶
Assertion function supplies more fine-grained permission controls.
type Rbac ¶
type Rbac struct {
// contains filtered or unexported fields
}
RBAC
Example ¶
Suppose:
The role-c is inheriting from role-a, role-b. The role-d is individual. The role-e is inheriting from role-c, role-d. Every roles have thire own permissions.
Thus:
The role-c has been granted permis-a/b/c. The role-e has been granted permis-a/b/c/d.
package main
import (
"fmt"
"github.com/mikespook/gorbac"
)
func main() {
testingcases := map[string]map[string][]string{
"role-a": map[string][]string{
"permissions": []string{"permis-a"},
"parents": nil,
},
"role-b": map[string][]string{
"permissions": []string{"permis-b"},
"parents": nil,
},
"role-c": map[string][]string{
"permissions": []string{"permis-c"},
"parents": []string{"role-a", "role-b"},
},
"role-d": map[string][]string{
"permissions": []string{"permis-d"},
"parents": nil,
},
"role-e": map[string][]string{
"permissions": nil,
"parents": []string{"role-c", "role-d"},
},
}
rbac := gorbac.New()
for role, testingcase := range testingcases {
rbac.Add(role, testingcase["permissions"], testingcase["parents"])
}
if rbac.IsGranted("role-c", "permis-a", nil) &&
rbac.IsGranted("role-c", "permis-b", nil) &&
rbac.IsGranted("role-c", "permis-c", nil) {
fmt.Println("The role-c has been granted permis-a/b/c.")
}
if rbac.IsGranted("role-e", "permis-a", nil) &&
rbac.IsGranted("role-e", "permis-b", nil) &&
rbac.IsGranted("role-e", "permis-c", nil) &&
rbac.IsGranted("role-e", "permis-d", nil) {
fmt.Println("The role-e has been granted permis-a/b/c/d.")
}
}
Output: The role-c has been granted permis-a/b/c. The role-e has been granted permis-a/b/c/d.
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 RestoreWithFactory ¶
func RestoreWithFactory(data Map, factory RoleFactoryFunc) *Rbac
Restore rbac from a map, use factory for your own data structure
func (*Rbac) Add ¶
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.
type Role ¶
type Role interface {
Rank() int
Name() string
Description() string
AddRank(int)
AddDescription(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 ¶
Sometimes, a custom role structure is needed by projects. You should define your own role factory function for this purpuse.

