-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.rs
368 lines (337 loc) · 11 KB
/
lib.rs
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::LookupMap;
use near_sdk::{env, near_bindgen, PanicOnDefault};
use std::collections::HashMap;
pub type Permission = u32;
const PERMISSION_OWNER: Permission = 0x01; // Can create
const PERMISSION_CONTRIBUTOR: Permission = 0x02;
const PERMISSION_READER: Permission = 0x04;
const PERMISSION_FREE: Permission = 0x08;
static EVERYONE: &str = "EVERYONE";
const REPOSITORY_PERMISSION_KEY: &[u8] = b"rp";
#[near_bindgen]
#[derive(PanicOnDefault, BorshDeserialize, BorshSerialize)]
pub struct RepositoryPermission {
permission: HashMap<String, HashMap<String, Permission>>,
}
#[near_bindgen]
#[derive(PanicOnDefault, BorshDeserialize, BorshSerialize)]
pub struct Contract {
permission: LookupMap<String, HashMap<String, Permission>>
}
#[near_bindgen]
impl Contract {
#[init(ignore_state)]
pub fn init() -> Self {
let mut contract = Self {
permission: LookupMap::new(REPOSITORY_PERMISSION_KEY.to_vec()),
};
let old_state = env::state_read();
if old_state.is_some() {
let old_state_value: RepositoryPermission =
old_state.unwrap_or_else(|| panic!("nothing to migrate"));
for (key, value) in old_state_value.permission {
contract.permission.insert(&key, &value);
}
}
contract
}
#[payable]
pub fn set_permission(
&mut self,
path: String,
account_id: String,
permission: Permission,
) -> bool {
assert_eq!(
10u128.pow(23),
env::attached_deposit(),
"requires attached deposit of 0.1N"
);
let caller_account_id = env::signer_account_id();
let current_permission =
self.get_permission(caller_account_id.to_string(), path.to_string());
if current_permission & (PERMISSION_OWNER) > 0 {
let mut path_users = self.permission.get(&path.to_string()).unwrap().clone();
path_users.insert(account_id, permission);
self.permission.insert(&path.to_string(), &path_users);
return true;
} else if current_permission & (PERMISSION_FREE) > 0 {
let mut path_users: HashMap<String, Permission> = HashMap::new();
path_users.insert(account_id, permission);
self.permission.insert(&path.to_string(), &path_users);
return true;
} else {
panic!("permission denied");
}
}
pub fn get_permission(&self, account_id: String, path: String) -> Permission {
let path_users = self.permission.get(&path.to_string());
if path_users.is_some() {
let path_users_unwrapped = path_users.unwrap();
let permission = path_users_unwrapped.get(&account_id);
if permission.is_some() {
return *permission.unwrap();
} else {
let everyone_permission = path_users_unwrapped.get(EVERYONE);
return if everyone_permission.is_some() {
*everyone_permission.unwrap()
} else {
0
};
}
} else {
return PERMISSION_FREE;
}
}
}
/*
* the rest of this file sets up unit tests
* to run these, the command will be:
* cargo test --package rust-counter-tutorial -- --nocapture
* Note: 'rust-counter-tutorial' comes from cargo.toml's 'name' key
*/
// use the attribute below for unit tests
#[cfg(test)]
mod tests {
use std::convert::TryInto;
use super::*;
use near_sdk::{test_utils::VMContextBuilder, testing_env, VMContext};
const REQUIRED_ATTACHED_DEPOSIT: u128 = 100000000000000000000000;
// part of writing unit tests is setting up a mock context
// in this example, this is only needed for env::log in the contract
fn get_context(
signer_account_id: String,
is_view: bool,
attached_deposit: u128,
) -> VMContext {
VMContextBuilder::new()
.signer_account_id(signer_account_id.parse().unwrap())
.current_account_id("alice_near".parse().unwrap())
.predecessor_account_id("jane_near".parse().unwrap())
.attached_deposit(attached_deposit)
.signer_account_pk(
vec![
00, 66, 211, 21, 84, 20, 241, 129, 29, 118, 83, 184, 41, 215, 240, 117, 106,
56, 29, 69, 103, 43, 191, 167, 199, 102, 3, 16, 194, 250, 138, 198, 78,
]
.try_into()
.unwrap(),
)
.is_view(is_view)
.build()
}
#[test]
fn set_get_permission() {
let context = get_context(
"peter".to_string(),
false,
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = Contract::init();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("peter".to_string(), "testrepo".to_string())
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
"peter".to_string(),
PERMISSION_OWNER
)
);
assert_eq!(
PERMISSION_OWNER,
contract.get_permission("peter".to_string(), "testrepo".to_string())
);
}
#[test]
fn set_get_permission_different_context() {
let context = get_context(
"johan".to_string(),
false,
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = Contract::init();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("johan".to_string(), "testrepo".to_string())
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
"johan".to_string(),
PERMISSION_OWNER
)
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
"peter".to_string(),
PERMISSION_CONTRIBUTOR
)
);
let context2 = get_context("peter".to_string(), false, 0);
testing_env!(context2);
assert_eq!(
PERMISSION_CONTRIBUTOR,
contract.get_permission("peter".to_string(), "testrepo".to_string())
);
let context3 = get_context("johan".to_string(), false, 0);
testing_env!(context3);
assert_eq!(
PERMISSION_OWNER,
contract.get_permission("johan".to_string(), "testrepo".to_string())
);
}
#[test]
fn deny_set_permission() {
let context = get_context(
"johan".to_string(),
false,
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract: Contract = Contract::init();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("johan".to_string(), "testrepo".to_string())
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
"johan".to_string(),
PERMISSION_OWNER
)
);
testing_env!(get_context(
"someotheruser".to_string(),
false,
REQUIRED_ATTACHED_DEPOSIT,
));
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
contract.set_permission(
"testrepo".to_string(),
"someotheruser".to_string(),
PERMISSION_OWNER,
);
}));
assert!(result.is_err());
}
#[test]
fn read_permission_for_everyone() {
let context = get_context(
"peter".to_string(),
false,
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = Contract::init();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("peter".to_string(), "testrepo".to_string())
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
"peter".to_string(),
PERMISSION_OWNER
)
);
assert_eq!(
PERMISSION_OWNER,
contract.get_permission("peter".to_string(), "testrepo".to_string())
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
EVERYONE.to_string(),
PERMISSION_READER
)
);
assert_eq!(
PERMISSION_READER,
contract.get_permission(EVERYONE.to_string(), "testrepo".to_string())
);
assert_eq!(
PERMISSION_READER,
contract.get_permission("randomuser".to_string(), "testrepo".to_string())
);
}
#[test]
fn write_permission_for_everyone_read_for_anonymous() {
let context = get_context(
"peter".to_string(),
false,
REQUIRED_ATTACHED_DEPOSIT,
);
testing_env!(context);
let mut contract = Contract::init();
assert_eq!(
PERMISSION_FREE,
contract.get_permission("peter".to_string(), "testrepo".to_string())
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
"peter".to_string(),
PERMISSION_OWNER
)
);
assert_eq!(
PERMISSION_OWNER,
contract.get_permission("peter".to_string(), "testrepo".to_string())
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
EVERYONE.to_string(),
PERMISSION_CONTRIBUTOR
)
);
assert_eq!(
true,
contract.set_permission(
"testrepo".to_string(),
"ANONYMOUS".to_string(),
PERMISSION_READER
)
);
assert_eq!(
PERMISSION_CONTRIBUTOR,
contract.get_permission(EVERYONE.to_string(), "testrepo".to_string())
);
assert_eq!(
PERMISSION_CONTRIBUTOR,
contract.get_permission("ANNONYMOUS".to_string(), "testrepo".to_string())
);
assert_eq!(
PERMISSION_READER,
contract.get_permission("ANONYMOUS".to_string(), "testrepo".to_string())
);
}
#[test]
fn require_attached_deposits() {
let context = get_context("peter".to_string(), false, 2);
testing_env!(context);
let mut contract = Contract::init();
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
return contract.set_permission(
"testrepo".to_string(),
"peter".to_string(),
PERMISSION_OWNER,
);
}));
assert!(result.is_err());
}
}