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

state_role.gno

1.43 Kb · 76 lines
 1package manager
 2
 3import "strconv"
 4
 5type RoleId uint64
 6
 7func NewRoleId(id uint64) RoleId {
 8	return RoleId(id)
 9}
10
11func (r RoleId) Uint64() uint64 {
12	return uint64(r)
13}
14
15func (r RoleId) String() string {
16	return strconv.FormatUint(uint64(r), 10)
17}
18
19type RoleConfig struct {
20	Members    map[address]Access
21	Admin      RoleId
22	GrantDelay Delay
23}
24
25func NewRoleConfig(admin RoleId, grantDelay Delay) *RoleConfig {
26	return &RoleConfig{
27		Members:    make(map[address]Access),
28		Admin:      admin,
29		GrantDelay: grantDelay,
30	}
31}
32
33func (roleConfig *RoleConfig) grant(account address, now TimePoint) bool {
34	_, found := roleConfig.Members[account]
35	if found {
36		return false
37	}
38
39	roleConfig.Members[account] = NewAccessWithDelay(now, roleConfig.GrantDelay)
40
41	return true
42}
43
44func (roleConfig *RoleConfig) revoke(account address) bool {
45	_, found := roleConfig.Members[account]
46	if found {
47		delete(roleConfig.Members, account)
48	}
49
50	return found
51}
52
53func (roleConfig *RoleConfig) hasMember(account address, now TimePoint) bool {
54	access, found := roleConfig.Members[account]
55	if !found {
56		return false
57	}
58
59	return access.isActiveAt(now)
60}
61
62func (roleConfig *RoleConfig) setAdmin(admin RoleId) {
63	roleConfig.Admin = admin
64}
65
66func (roleConfig *RoleConfig) adminRole() RoleId {
67	return roleConfig.Admin
68}
69
70func (roleConfig *RoleConfig) setGrantDelay(delay Delay) {
71	roleConfig.GrantDelay = delay
72}
73
74func (roleConfig *RoleConfig) grantDelay() Delay {
75	return roleConfig.GrantDelay
76}