Search Apps Documentation Source Content File Folder Download Copy Actions Download

/p/gnoland/boards/exts/permissions

Directory · 6 Files
README.md Open

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