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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use super::*;
use frame_support::dispatch::DispatchError;
use pallet_permissions::SpacePermissionsContext;
impl<T: Trait> Module<T> {
pub fn ensure_role_exists(role_id: RoleId) -> DispatchResult {
ensure!(<RoleById<T>>::contains_key(role_id), Error::<T>::RoleNotFound);
Ok(())
}
pub fn require_role(role_id: SpaceId) -> Result<Role<T>, DispatchError> {
Ok(Self::role_by_id(role_id).ok_or(Error::<T>::RoleNotFound)?)
}
pub fn ensure_role_manager(account: T::AccountId, space_id: SpaceId) -> DispatchResult {
ensure!(
T::IsAccountBlocked::is_allowed_account(account.clone(), space_id),
UtilsError::<T>::AccountIsBlocked
);
Self::ensure_user_has_space_permission_with_load_space(
User::Account(account),
space_id,
SpacePermission::ManageRoles,
Error::<T>::NoPermissionToManageRoles.into()
)
}
fn ensure_user_has_space_permission_with_load_space(
user: User<T::AccountId>,
space_id: SpaceId,
permission: SpacePermission,
error: DispatchError,
) -> DispatchResult {
let space = T::Spaces::get_space(space_id)?;
let mut is_owner = false;
let mut is_follower = false;
match &user {
User::Account(account) => {
is_owner = *account == space.owner;
is_follower = is_owner || T::SpaceFollows::is_space_follower(account.clone(), space_id);
}
User::Space(_) => (),
}
Self::ensure_user_has_space_permission(
user,
SpacePermissionsContext {
space_id,
is_space_owner: is_owner,
is_space_follower: is_follower,
space_perms: space.permissions
},
permission,
error
)
}
fn ensure_user_has_space_permission(
user: User<T::AccountId>,
ctx: SpacePermissionsContext,
permission: SpacePermission,
error: DispatchError,
) -> DispatchResult {
match Permissions::<T>::has_user_a_space_permission(
ctx.clone(),
permission.clone()
) {
Some(true) => return Ok(()),
Some(false) => return Err(error),
_ => ()
}
Self::has_permission_in_space_roles(
user,
ctx.space_id,
permission,
error
)
}
fn has_permission_in_space_roles(
user: User<T::AccountId>,
space_id: SpaceId,
permission: SpacePermission,
error: DispatchError,
) -> DispatchResult {
let role_ids = Self::role_ids_by_user_in_space(user, space_id);
for role_id in role_ids {
if let Some(role) = Self::role_by_id(role_id) {
if role.disabled {
continue;
}
let mut is_expired = false;
if let Some(expires_at) = role.expires_at {
if expires_at <= <system::Module<T>>::block_number() {
is_expired = true;
}
}
if !is_expired && role.permissions.contains(&permission) {
return Ok(());
}
}
}
Err(error)
}
}
impl<T: Trait> Role<T> {
pub fn new(
created_by: T::AccountId,
space_id: SpaceId,
time_to_live: Option<T::BlockNumber>,
content: Content,
permissions: BTreeSet<SpacePermission>,
) -> Result<Self, DispatchError> {
let role_id = Module::<T>::next_role_id();
let mut expires_at: Option<T::BlockNumber> = None;
if let Some(ttl) = time_to_live {
expires_at = Some(ttl + <system::Module<T>>::block_number());
}
let new_role = Role::<T> {
created: WhoAndWhen::new(created_by),
updated: None,
id: role_id,
space_id,
disabled: false,
expires_at,
content,
permissions,
};
Ok(new_role)
}
pub fn set_disabled(&mut self, disable: bool) -> DispatchResult {
if self.disabled && disable {
return Err(Error::<T>::RoleAlreadyDisabled.into());
} else if !self.disabled && !disable {
return Err(Error::<T>::RoleAlreadyEnabled.into());
}
self.disabled = disable;
Ok(())
}
pub fn revoke_from_users(&self, users: Vec<User<T::AccountId>>) {
let mut users_by_role = <UsersByRoleId<T>>::take(self.id);
for user in users.iter() {
let role_idx_by_user_opt = Module::<T>::role_ids_by_user_in_space(&user, self.space_id).iter()
.position(|x| { *x == self.id });
if let Some(role_idx) = role_idx_by_user_opt {
<RoleIdsByUserInSpace<T>>::mutate(user, self.space_id, |n| { n.swap_remove(role_idx) });
}
let user_idx_by_role_opt = users_by_role.iter().position(|x| { x == user });
if let Some(user_idx) = user_idx_by_role_opt {
users_by_role.swap_remove(user_idx);
}
}
<UsersByRoleId<T>>::insert(self.id, users_by_role);
}
}
impl<T: Trait> PermissionChecker for Module<T> {
type AccountId = T::AccountId;
fn ensure_user_has_space_permission(
user: User<Self::AccountId>,
ctx: SpacePermissionsContext,
permission: SpacePermission,
error: DispatchError,
) -> DispatchResult {
Self::ensure_user_has_space_permission(
user,
ctx,
permission,
error
)
}
}