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

/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 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}