forked from motoko-bootcamp/awesome-motoko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess-control-multi.mo
44 lines (36 loc) · 1.22 KB
/
access-control-multi.mo
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
import Array "mo:base/Array";
import Option "mo:base/Option";
shared ({ caller = creator }) actor class MyCanister() = {
stable var admins : [Principal] = [creator];
private func isAdmin (
principal : Principal,
) : Bool {
switch (Array.find<Principal>(admins, func (x) { x == principal })) {
case (?a) true;
case _ false;
};
};
public shared func getAdmins () : async [Principal] {
admins;
};
public shared ({ caller }) func removeAdmins (
removals : [Principal],
) : async () {
assert(isAdmin(caller));
admins := Array.filter<Principal>(admins, func (admin) {
Option.isNull(Array.find<Principal>(removals, func (x) { x == admin }));
});
};
public shared ({ caller }) func addAdmins (
newAdmins : [Principal]
) : async () {
assert(isAdmin(caller));
admins := Array.append(admins, Array.filter<Principal>(newAdmins, func (x) {
Option.isNull(Array.find<Principal>(admins, func (y) { x == y }));
}));
};
public shared ({ caller }) func mySecret () : async Text {
assert(isAdmin(caller));
"Grandma's secret lasagne recipe...";
};
};