-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccess_Control_IPFS.sol
executable file
·130 lines (118 loc) · 4.44 KB
/
Access_Control_IPFS.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
pragma solidity >=0.4.22 <0.7.0;
contract AccessControlIPFS {
struct FileData {
address owner;
mapping(address => bool) access;
address[] allowedAddresses;
}
mapping(bytes32 => FileData) fileMapping;
function addBlock(address sender, bytes32 chunk, address[] memory grantees) public {
if (chunk[0] == 0)
return;
FileData storage fileData;
fileData.owner = sender;
fileMapping[chunk] = fileData;
address grantee;
for (uint256 granteeInd = 0; granteeInd < grantees.length; granteeInd++) {
grantee = grantees[granteeInd];
fileData.access[grantee] = true;
fileData.allowedAddresses.push(grantee);
}
}
function addBlockMultiple(address sender, bytes32[] memory chunks, address[] memory grantees) public {
bytes32 chunk;
address grantee;
address[] memory emptyAddresses;
for (uint256 i = 0; i < chunks.length; i++) {
chunk = chunks[i];
if (chunk[0] == 0)
continue;
fileMapping[chunk] = FileData(sender, emptyAddresses);
for (uint256 granteeInd = 0; granteeInd < grantees.length; granteeInd++) {
grantee = grantees[granteeInd];
fileMapping[chunk].access[grantee] = true;
fileMapping[chunk].allowedAddresses.push(grantee);
}
}
}
function grantAccess(address sender, address grantee, bytes32 chunk) public {
if (chunk[0] == 0)
return;
FileData storage fileData = fileMapping[chunk];
if (sender != fileData.owner)
return;
fileData.access[grantee] = true;
fileData.allowedAddresses.push(grantee);
}
function grantAccessMultiple(address sender, address grantee, bytes32[] memory chunks) public {
bytes32 chunk;
for (uint256 i = 0; i < chunks.length; i++) {
chunk = chunks[i];
if (chunk[0] == 0)
continue;
FileData storage fileData = fileMapping[chunk];
if ((i == 0) && (sender != fileData.owner))
return;
fileData.access[grantee] = true;
fileData.allowedAddresses.push(grantee);
}
}
function removeAccess(address sender, address grantee, bytes32 chunk) public {
if (chunk[0] == 0)
return;
FileData storage fileData = fileMapping[chunk];
if (sender != fileData.owner)
return;
fileData.access[grantee] = false;
}
function removeAccessMultiple(address sender, address grantee, bytes32[] memory chunks) public {
bytes32 chunk;
for (uint256 i = 0; i < chunks.length; i++) {
chunk = chunks[i];
if (chunk[0] == 0)
continue;
FileData storage fileData = fileMapping[chunk];
if ((i == 0) && (sender != fileData.owner))
return;
fileData.access[grantee] = false;
}
}
function checkAccess(address grantee, bytes32 chunk) public view returns (bool hasAccess) {
hasAccess = false;
if (chunk[0] == 0)
return hasAccess;
FileData storage fileData = fileMapping[chunk];
if ((fileData.owner == grantee) || (fileData.access[grantee] == true)) {
hasAccess = true;
return hasAccess;
}
}
function checkAccessMultiple(address grantee, bytes32[] memory chunks) public view returns (bool[] memory _checkAccessMultiple) {
_checkAccessMultiple = new bool[](chunks.length);
bytes32 chunk;
for (uint256 i = 0; i < chunks.length; i++) {
chunk = chunks[i];
_checkAccessMultiple[i] = false;
if (chunk[0] == 0)
continue;
FileData storage fileData = fileMapping[chunk];
if ((fileData.owner == grantee) || (fileData.access[grantee] == true)) {
_checkAccessMultiple[i] = true;
}
}
}
function deleteBlock(address sender, bytes32 chunk) public {
if (chunk[0] == 0)
return;
delete fileMapping[chunk];
}
function deleteBlockMultiple(address sender, bytes32[] memory chunks) public {
bytes32 chunk;
for (uint256 i = 0; i < chunks.length; i++) {
chunk = chunks[i];
if (chunk[0] == 0)
continue;
delete fileMapping[chunk];
}
}
}