Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

permissions source pure

Readme View source

Boards Permissions Extension

This is a gno.land/p/gnoland/boards package extension that provides a custom Permissions implementation that uses an underlying DAO to manage users and roles.

It also supports optionally setting validation functions to be triggered by the WithPermission() method before a callback is called. Validators allows adding custom checks and requirements before the callback is called.

Usage Example:

 1package permissions
 2
 3import (
 4	"errors"
 5
 6	"gno.land/p/gnoland/boards"
 7)
 8
 9// Example user account
10const user address = "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"
11
12// Define a role
13const RoleExample boards.Role = "example"
14
15// Define a permission
16const PermissionFoo boards.Permission = 42
17
18func ExamplePermission() {
19	// Define a custom foo permission validation function
20	validateFoo := func(_ boards.Permissions, args boards.Args) error {
21		// Check that the first argument is the string "bob"
22		if name, ok := args[0].(string); !ok || name != "bob" {
23			return errors.New("unauthorized")
24		}
25		return nil
26	}
27
28	// Create a permissions instance and assign the custom validator to it
29	perms := New()
30	perms.ValidateFunc(PermissionFoo, validateFoo)
31
32	// Add foo permission to example role
33	perms.AddRole(RoleExample, PermissionFoo)
34
35	// Add a guest user
36	perms.SetUserRoles(user, RoleExample)
37
38	// Call a permissioned callback
39	args := boards.Args{"bob"}
40	perms.WithPermission(user, PermissionFoo, args, func() {
41		println("Hello Bob!")
42	})
43
44	// Output:
45	// Hello Bob!
46}

Functions 3

func UseSingleUserRole

1func UseSingleUserRole() Option
source

UseSingleUserRole configures permissions to only allow one role per user.

func WithSuperRole

1func WithSuperRole(r boards.Role) Option
source

WithSuperRole configures permissions to have a super role. A super role is the one that have all permissions. This type of role doesn't need to be mapped to any permission.

func New

1func New(options ...Option) *Permissions
source

New creates a new permissions type.

Types 3

type Option

func
1type Option func(*Permissions)
source

Option configures permissions.

type Permissions

struct
1type Permissions struct {
2	superRole      boards.Role
3	dao            *commondao.CommonDAO
4	public         boards.PermissionSet
5	validators     *bptree.BPTree // string(boards.Permission) -> ValidatorFunc
6	singleUserRole bool
7}
source

Permissions manages users, roles and permissions.

This type is a default `gno.land/p/gnoland/boards` package `Permissions` implementation that handles boards users, roles and permissions using an underlying DAO. It also supports optionally setting validation functions to be triggered within `WithPermission()` method before a permissioned callback is called.

No permissions validation is done by default.

Users are allowed to have multiple roles at the same time by default, but permissions can be configured to only allow one role per user.

Methods on Permissions

func AddRole

method on Permissions
1func (ps *Permissions) AddRole(r boards.Role, p boards.Permission, extra ...boards.Permission)
source

AddRole adds a role with one or more assigned permissions. If role exists its permissions are overwritten with the new ones.

func DAO

method on Permissions
1func (ps Permissions) DAO() *commondao.CommonDAO
source

DAO returns the underlying permissions DAO.

func GetUserRoles

method on Permissions
1func (ps Permissions) GetUserRoles(user address) []boards.Role
source

GetUserRoles returns the list of roles assigned to a user.

func HasPermission

method on Permissions
1func (ps Permissions) HasPermission(user address, perm boards.Permission) bool
source

HasPermission checks if a user has a specific permission.

func HasRole

method on Permissions
1func (ps Permissions) HasRole(user address, r boards.Role) bool
source

HasRole checks if a user has a specific role assigned.

func HasUser

method on Permissions
1func (ps Permissions) HasUser(user address) bool
source

HasUser checks if a user exists.

func IterateUsers

method on Permissions
1func (ps Permissions) IterateUsers(start, count int, fn boards.UsersIterFn) (stopped bool)
source

IterateUsers iterates permissions' users.

func RemoveUser

method on Permissions
1func (ps *Permissions) RemoveUser(user address) bool
source

RemoveUser removes a user from permissions.

func RoleExists

method on Permissions
1func (ps Permissions) RoleExists(r boards.Role) bool
source

RoleExists checks if a role exists.

func SetPublicPermissions

method on Permissions
1func (ps *Permissions) SetPublicPermissions(permissions ...boards.Permission)
source

SetPublicPermissions assigns permissions that are available to anyone. It removes previous public permissions and assigns the new ones. By default there are no public permissions.

func SetUserRoles

method on Permissions
1func (ps *Permissions) SetUserRoles(user address, roles ...boards.Role)
source

SetUserRoles adds a new user when it doesn't exist and sets its roles. Method can also be called to change the roles of an existing user. It removes any existing user roles before assigning new ones. All user's roles can be removed by calling this method without roles.

func UsersCount

method on Permissions
1func (ps Permissions) UsersCount() int
source

UsersCount returns the total number of users the permissioner contains.

func ValidateFunc

method on Permissions
1func (ps *Permissions) ValidateFunc(p boards.Permission, fn ValidatorFunc)
source

ValidateFunc adds a custom permission validator function. If an existing permission function exists it's ovewritten by the new one.

func WithPermission

method on Permissions
1func (ps *Permissions) WithPermission(user address, p boards.Permission, args boards.Args, cb func())
source

WithPermission calls a callback when a user has a specific permission. It panics on error or when a permission validator fails. Callbacks are by default called when there is no validator function registered for the permission. If a permission validation function exists it's called before calling the callback.

type ValidatorFunc

func
1type ValidatorFunc func(boards.Permissions, boards.Args) error
source

ValidatorFunc defines a function type for permissions validators.

Imports 4

Source Files 6