z_readme_filetest.gno
1.08 Kb · 47 lines
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!