Search Apps Documentation Source Content File Folder Download Copy Actions Download

permissions.gno

1.85 Kb · 68 lines
 1package boards
 2
 3import "strconv"
 4
 5type (
 6	// Role defines the type for user roles.
 7	Role string
 8
 9	// Args is a list of generic arguments.
10	Args []interface{}
11
12	// User contains user info.
13	User struct {
14		Address address
15		Roles   []Role
16	}
17
18	// UsersIterFn defines a function type to iterate users.
19	UsersIterFn func(User) bool
20
21	// Permissions define an interface to for permissioned execution.
22	Permissions interface {
23		// HasRole checks if a user has a specific role assigned.
24		HasRole(address, Role) bool
25
26		// HasPermission checks if a user has a specific permission.
27		HasPermission(address, Permission) bool
28
29		// WithPermission calls a callback when a user has a specific permission.
30		// It panics on error.
31		//
32		// An inline crossing function call can be used by the implementation if
33		// crossing is required to update its internal state, for example to create
34		// proposals that when approved execute the callback:
35		//
36		//  func(realm) {
37		//    // Update internal realm state
38		//    // ...
39		//  }(cross)
40		WithPermission(address, Permission, Args, func())
41
42		// SetUserRoles adds a new user when it doesn't exist and sets its roles.
43		// Method can also be called to change the roles of an existing user.
44		// It panics on error.
45		SetUserRoles(address, ...Role)
46
47		// RemoveUser removes a user from the permissioner.
48		// It panics on error.
49		RemoveUser(address) (removed bool)
50
51		// HasUser checks if a user exists.
52		HasUser(address) bool
53
54		// UsersCount returns the total number of users the permissioner contains.
55		UsersCount() int
56
57		// IterateUsers iterates permissions' users.
58		IterateUsers(start, count int, fn UsersIterFn) bool
59	}
60)
61
62// Permission defines the type for permissions.
63type Permission uint16
64
65// String returns the string representation of a permission value.
66func (p Permission) String() string {
67	return strconv.FormatUint(uint64(p), 10)
68}