-
Notifications
You must be signed in to change notification settings - Fork 75
/
accessControl.ts
118 lines (102 loc) · 3.25 KB
/
accessControl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
generateEvent,
Storage,
createEvent,
Context,
} from '@massalabs/massa-as-sdk';
import { Args, boolToByte } from '@massalabs/as-types';
import { onlyOwner } from './ownership';
import { _hasRole, _members, _roleKey } from './accessControl-internal';
import { _isOwner } from './ownership-internal';
export const ROLES_KEY = '_ROLES';
export const ROLE_GRANTED_EVENT = 'ROLE_GRANTED_EVENT';
/**
* Set the role for account
*
* @param role - role name string
* @param account - account address string
*/
export function grantRole(binaryArgs: StaticArray<u8>): void {
onlyOwner();
const args = new Args(binaryArgs);
const role = args.nextString().expect('role argument is missing or invalid');
const account = args
.nextString()
.expect('account argument is missing or invalid');
assert(!_hasRole(role, account), `Account already has ${role} role`);
const membersList = _members(role);
membersList.push(account);
Storage.set(_roleKey(role), new Args().add(membersList).serialize());
generateEvent(createEvent(ROLE_GRANTED_EVENT, [role, account]));
}
/**
* get the members for a role
*
* @param role - role name string
*/
export function members(binaryArgs: StaticArray<u8>): StaticArray<u8> {
const args = new Args(binaryArgs);
const role = args.nextString().expect('role argument is missing or invalid');
return new Args().add(_members(role)).serialize();
}
/**
* Returns true if the account has the role.
*
* @param role - role name string
* @param account - account address string
* @returns boolean
*/
export function hasRole(binaryArgs: StaticArray<u8>): StaticArray<u8> {
const args = new Args(binaryArgs);
const role = args.nextString().expect('role argument is missing or invalid');
const account = args
.nextString()
.expect('account argument is missing or invalid');
return boolToByte(_hasRole(role, account));
}
/**
* Revoke role for account. Must be called by the role owner or the contract admin.
*
* @param role - role name string
* @param account - account address string
*/
export function revokeRole(binaryArgs: StaticArray<u8>): StaticArray<u8> {
const args = new Args(binaryArgs);
const role = args.nextString().expect('role argument is missing or invalid');
const account = args
.nextString()
.expect('account argument is missing or invalid');
const caller = Context.caller().toString();
if (!_isOwner(caller)) {
assert(
Context.caller().toString() === account,
`Caller is not ${account} or admin`,
);
assert(_hasRole(role, account), `Account does not have ${role} role`);
}
let membersList = _members(role);
if (membersList.length > 1) {
membersList[membersList.indexOf(account)] =
membersList[membersList.length - 1];
membersList.pop();
} else {
membersList = [];
}
Storage.set(_roleKey(role), new Args().add(membersList).serialize());
return [];
}
/**
* Assert that caller has the role.
*
* @param role - role name string
* @returns boolean
*/
export function onlyRole(binaryArgs: StaticArray<u8>): void {
const role = new Args(binaryArgs)
.nextString()
.expect('role argument is missing or invalid');
assert(
_hasRole(role, Context.caller().toString()),
`Caller does not have ${role} role`,
);
}