-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess_indexor.sol
408 lines (335 loc) · 15.2 KB
/
access_indexor.sol
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
pragma solidity 0.5.4;
import "./ownable.sol";
import {IUserSpace} from "./base_space_interfaces.sol";
import "./editable.sol";
/* -- Revision history --
AccessIndexor20190423111800ML: First versioned release
AccessIndexor20190510150600ML: Removes debug events
AccessIndexor20190528194200ML: Removes contentSpace is field as it is now inherited from Ownable
AccessIndexor20190605162000ML: Adds cleanUp functions to remove references to dead objects
AccessIndexor20190722214200ML: Fix false positive for group-based rights when object owner match group owner
AccessIndexor20190801141000ML: Adds method to provide ACCESS right to the caller object
AccessIndexor20191113202400ML: Ensures accessor has at least access right to a group to benefit from group rights
AccessIndexor20200110100200ML: Removes debug events
AccessIndexor20200204144400ML: Fixes lookup of group based rights to check group membership vs. visibility only
AccessIndexor20200316121400ML: Replaces entity-specific set and check rights with generic one that uses index-type
AccessIndexor20200410215200ML: Disambiguate setRights by renaming setEntityRights
AccessIndexor20200928110000PO: Replace tx.origin with msg.sender in some cases
*/
contract AccessIndexor is Ownable {
bytes32 public version = "AccessIndexor20200928110000PO";
event RightsChanged(address principal, address entity, uint8 aggregate);
uint8 public CATEGORY_CONTENT_OBJECT = 1;
uint8 public CATEGORY_GROUP = 2;
uint8 public CATEGORY_LIBRARY = 3;
uint8 public CATEGORY_CONTENT_TYPE = 4;
uint8 public CATEGORY_CONTRACT = 5;
uint8 public constant TYPE_SEE = 0;
uint8 public constant TYPE_ACCESS = 1;
uint8 public constant TYPE_EDIT = 2;
uint8 public constant ACCESS_NONE = 0; // No access
uint8 public constant ACCESS_TENTATIVE = 1; //access was granted, but not accepted by recipient
uint8 public constant ACCESS_CONFIRMED = 2; //access was granted and accepted by recipient
uint8[3] ranking = [1,10,100];
struct AccessIndex {
uint8 category;
mapping (address => uint8) rights; // rights map for a given object address
address payable[] list; // All items for which rights are known
uint256 length; // Number of items listed
}
//mapping (address => uint8) libraryRights;
//address[] libraryList;
AccessIndex public libraries; // = AccessIndex(libraryRights, libraryList, 0);
AccessIndex public contentObjects;
AccessIndex public accessGroups;
AccessIndex public contentTypes;
AccessIndex public contracts;
AccessIndex public others;
mapping(uint8 => AccessIndex) private accessIndexes;
constructor() public payable {
libraries.category = CATEGORY_LIBRARY;
accessGroups.category = CATEGORY_GROUP;
contentObjects.category = CATEGORY_CONTENT_OBJECT;
contentTypes.category = CATEGORY_CONTENT_TYPE;
contracts.category = CATEGORY_CONTRACT;
}
function getAccessIndex(uint8 indexCategory) private view returns (AccessIndex storage) {
if (indexCategory == CATEGORY_CONTENT_OBJECT) {
return contentObjects;
}
if (indexCategory == CATEGORY_GROUP) {
return accessGroups;
}
if (indexCategory == CATEGORY_LIBRARY) {
return libraries;
}
if (indexCategory == CATEGORY_CONTRACT) {
return contracts;
}
if (indexCategory == CATEGORY_CONTENT_TYPE) {
return contentTypes;
}
return others;
}
function setContentSpace(address payable content_space) public onlyOwner {
contentSpace = content_space;
}
function setLibraryRights(address payable lib, uint8 access_type, uint8 access) public {
setRightsInternal(libraries, lib, access_type, access);
}
function setAccessGroupRights(address payable group, uint8 access_type, uint8 access) public {
setRightsInternal(accessGroups, group, access_type, access);
}
function setContentObjectRights(address payable obj, uint8 access_type, uint8 access) public {
setRightsInternal(contentObjects, obj, access_type, access);
}
function setContentTypeRights(address payable obj, uint8 access_type, uint8 access) public {
setRightsInternal(getAccessIndex(CATEGORY_CONTENT_TYPE), obj, access_type, access);
}
function setContractRights(address payable obj, uint8 access_type, uint8 access) public {
setRightsInternal(contracts, obj, access_type, access);
}
function checkLibraryRights(address payable lib, uint8 access_type) public view returns(bool) {
return checkRights(CATEGORY_LIBRARY, lib, access_type);
}
function checkAccessGroupRights(address payable group, uint8 access_type) public view returns(bool) {
return checkRights(CATEGORY_GROUP, group, access_type);
}
function checkContentObjectRights(address payable obj, uint8 access_type) public view returns(bool) {
return checkRights(CATEGORY_CONTENT_OBJECT, obj, access_type);
}
function checkContentTypeRights(address payable obj, uint8 access_type) public view returns(bool) {
return checkRights(CATEGORY_CONTENT_TYPE, obj, access_type);
}
function checkContractRights(address payable obj, uint8 access_type) public view returns(bool) {
return checkRights(CATEGORY_CONTRACT, obj, access_type);
}
function getLibraryRights(address lib) public view returns(uint8) {
return libraries.rights[lib];
}
function getLibrariesLength() public view returns(uint256) {
return libraries.length;
}
function getLibrary(uint256 position) public view returns(address) {
return libraries.list[position];
}
function getAccessGroupRights(address group) public view returns(uint8) {
return accessGroups.rights[group];
}
function getAccessGroupsLength() public view returns(uint256) {
return accessGroups.length;
}
function getAccessGroup(uint256 position) public view returns(address) {
return accessGroups.list[position];
}
function getContentObjectRights(address obj) public view returns(uint8) {
return contentObjects.rights[obj];
}
function getContentObjectsLength() public view returns(uint256) {
return contentObjects.length;
}
function getContentObject(uint256 position) public view returns(address) {
return contentObjects.list[position];
}
function getContentTypeRights(address obj) public view returns(uint8) {
return contentTypes.rights[obj];
}
function getContentTypesLength() public view returns(uint256) {
return contentTypes.length;
}
function getContentType(uint256 position) public view returns(address) {
return contentTypes.list[position];
}
function getContractRights(address obj) public view returns(uint8) {
return contracts.rights[obj];
}
function getContractsLength() public view returns(uint256) {
return contracts.length;
}
function getContract(uint256 position) public view returns(address) {
return contracts.list[position];
}
function hasManagerAccess(address candidate) public view returns (bool) {
return (candidate == owner);
}
function checkDirectRights(uint8 index_type, address obj, uint8 access_type) public view returns(bool) {
if (index_type != 0) {
return checkRawRights(getAccessIndex(index_type), obj, access_type);
}
return false;
}
function checkRawRights(AccessIndex storage index, address obj, uint8 access_type) private view returns(bool) {
uint8 currentAggregate = index.rights[obj];
return (currentAggregate >= ranking[access_type]);
}
function checkRights(uint8 index_type, address payable obj, uint8 access_type) public view returns(bool) {
Ownable instance = Ownable(obj);
if (instance.owner() == owner){
return true;
}
bool directRights = checkDirectRights(index_type, obj, access_type);
if (directRights == true) {
return true;
}
if (index_type != CATEGORY_GROUP){
AccessIndexor groupObj;
address payable group;
for (uint i = 0; i < accessGroups.length; i++) {
group = accessGroups.list[i];
if ((group != address(0x0)) && (accessGroups.rights[group] >= 1)) {
groupObj = AccessIndexor(group);
//needs to be at least a member, seeing is not enough (not using hasAccess on group to avoid circular reference)
if (((groupObj.owner() == owner) || (accessGroups.rights[group] >= 10 )) && groupObj.checkDirectRights(index_type, obj, access_type) == true) {
return true;
}
}
}
}
return false;
}
// Provides ACCESS right to the caller object
function setAccessRights() public {
address payable obj = msg.sender;
uint8 currentAggregate = contentObjects.rights[obj];
uint8[3] memory currentRights;
currentRights[0] = currentAggregate % 10;
currentRights[1] = currentAggregate % 100 - currentRights[0];
currentRights[2] = currentAggregate - currentRights[1] - currentRights[0];
currentRights[TYPE_ACCESS] = ACCESS_CONFIRMED * ranking[TYPE_ACCESS]; //Set access to confirmed
uint8 newAggregate = currentRights[0] + currentRights[1] + currentRights[2];
contentObjects.rights[obj] = newAggregate;
//add to array if newly added
if ((newAggregate != 0) && (currentAggregate == 0)) {
addToList(contentObjects, obj);
}
emit RightsChanged(address(this), obj, newAggregate);
}
function setEntityRights(uint8 indexType, address payable obj, uint8 access_type, uint8 access) public {
if (indexType != 0) {
setRightsInternal(getAccessIndex(indexType), obj, access_type, access);
}
}
// Indexor managers can revoke anything
// Indexor managers can confirm tentative
// Object rights holders can grant tentative
// Object rights holders can revoke
// Object rights holders can grant confirm if they are also Wallet manager
//access is either ACCESS_NONE (0), or any uint8 > 0, the current rights and privilege of granter and grantee will drive the new rights values
function setRightsInternal(AccessIndex storage index, address payable obj, uint8 access_type, uint8 access) private {
bool isIndexorManager = false;
bool isObjRightHolder = true;
// if we proxy through an object then it's the job of the calling object to auth the call correctly
if (msg.sender != obj) {
Editable e = Editable(obj);
isObjRightHolder = e.hasEditorRight(msg.sender);
isIndexorManager = hasManagerAccess(msg.sender);
} else {
// if we are being proxied through the target object then it is safe and correct to use tx.origin
isIndexorManager = hasManagerAccess(tx.origin);
}
uint8 currentAggregate = index.rights[obj];
uint8[3] memory currentRights;
currentRights[0] = currentAggregate % 10;
currentRights[1] = currentAggregate % 100 - currentRights[0];
currentRights[2] = currentAggregate - currentRights[1] - currentRights[0];
bool operationAuthorized = false;
uint8 targetAccess = access;
// Indexor managers can revoke anything, Object rights holders can revoke
if ((access == ACCESS_NONE) && (isIndexorManager || isObjRightHolder)) {
operationAuthorized = true;
}
// Indexor managers can confirm tentative
if ((access != ACCESS_NONE) && isIndexorManager && (currentRights[access_type] == ACCESS_TENTATIVE)) {
operationAuthorized = true;
targetAccess = ACCESS_CONFIRMED;
}
// Object rights holders can grant tentative
if ((access != ACCESS_NONE) && isObjRightHolder && (currentRights[access_type] != ACCESS_CONFIRMED)){
operationAuthorized = true;
targetAccess = ACCESS_TENTATIVE;
}
// Object rights holders can grant confirm if they are also Wallet manager
if ((access != ACCESS_NONE) && isIndexorManager && isObjRightHolder) {
operationAuthorized = true;
targetAccess = ACCESS_CONFIRMED;
}
require(operationAuthorized);
currentRights[access_type] = targetAccess * ranking[access_type];
uint8 newAggregate = currentRights[0] + currentRights[1] + currentRights[2];
index.rights[obj] = newAggregate;
//add to array if newly added
if ((newAggregate != 0) && (currentAggregate == 0)) {
addToList(index, obj);
}
//remove from array if removed
if (newAggregate == 0) {
removeFromList(index, obj);
}
emit RightsChanged(address(this), obj, newAggregate);
}
function addToList(AccessIndex storage index, address payable obj) private {
if (index.length < index.list.length) {
index.list[index.length] = obj;
} else {
index.list.push(obj);
}
index.length++;
}
function removeFromList(AccessIndex storage index, address obj) private returns (bool) {
for (uint i = 0; i < index.length; i++) {
if (index.list[i] == obj) {
delete index.list[i];
if (i != (index.length - 1)) {
index.list[i] = index.list[index.length - 1];
delete index.list[index.length - 1];
}
index.length--;
return true;
}
}
return false;
}
function contractExists(address addr) public view returns (bool) {
uint size;
assembly {
size := extcodesize(addr)
}
return (size > 0);
}
//event dbgAddress(string label, uint index, address a);
function cleanUp(AccessIndex storage index) private returns (uint) {
uint cleansedCount = 0;
uint i = 0;
while (i < index.length) {
if (contractExists(index.list[i]) == false) {
//emit dbgAddress("dead", i, index.list[i]);
delete index.list[i];
cleansedCount++;
if (i != (index.length - 1)) {
index.list[i] = index.list[index.length - 1];
delete index.list[index.length - 1];
}
index.length--;
} else {
//emit dbgAddress("alive", i, index.list[i]);
i++;
}
}
return cleansedCount;
}
function cleanUpLibraries() public returns (uint) {
return cleanUp(libraries);
}
function cleanUpAccessGroups() public returns (uint) {
return cleanUp(accessGroups);
}
function cleanUpContentObjects() public returns (uint) {
return cleanUp(contentObjects);
}
function cleanUpContentTypes() public returns (uint) {
return cleanUp(contentTypes);
}
function cleanUpAll() public returns (uint, uint, uint, uint, uint) {
return (cleanUp(libraries), cleanUp(accessGroups), cleanUp(contentObjects), cleanUp(contentTypes), cleanUp(contracts));
}
}