forked from zotero/dataserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermissions.inc.php
278 lines (214 loc) · 6.45 KB
/
Permissions.inc.php
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
<?
/*
***** BEGIN LICENSE BLOCK *****
This file is part of the Zotero Data Server.
Copyright © 2010 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
class Zotero_Permissions {
private $super = false;
private $anonymous = false;
private $userID = null;
private $permissions = array();
private $userPrivacy = array();
private $groupPrivacy = array();
public function __construct($userID=null) {
$this->userID = $userID;
}
public function canAccess($libraryID, $permission='library') {
if ($this->super || $libraryID === 0) {
return true;
}
if (!$libraryID) {
throw new Exception('libraryID not provided');
}
// TEMP: necessary?
$libraryID = (int) $libraryID;
// If requested permission is explicitly set
//
// This assumes that permissions can't be incorrectly set
// (e.g., are properly removed when a user loses group access)
if (!empty($this->permissions[$libraryID][$permission])) {
return true;
}
$libraryType = Zotero_Libraries::getType($libraryID);
switch ($libraryType) {
case 'user':
$userID = Zotero_Users::getUserIDFromLibraryID($libraryID);
$privacy = $this->getUserPrivacy($userID);
break;
case 'publications':
return true;
case 'group':
$groupID = Zotero_Groups::getGroupIDFromLibraryID($libraryID);
// If key has access to all groups, grant access if user
// has read access to group
if (!empty($this->permissions[0]['library'])) {
$group = Zotero_Groups::get($groupID);
// Only members have file access
if ($permission == 'files') {
return !!$group->getUserRole($this->userID);
}
if ($group->userCanRead($this->userID)) {
return true;
}
}
$privacy = $this->getGroupPrivacy($groupID);
break;
default:
throw new Exception("Unsupported library type '$libraryType'");
}
switch ($permission) {
case 'view':
return $privacy['view'];
case 'library':
return $privacy['library'];
case 'notes':
return $privacy['notes'];
default:
return false;
}
}
/**
* This should be called after canAccess()
*/
public function canWrite($libraryID) {
if ($this->super) {
return true;
}
if ($libraryID === 0) {
return false;
}
if (!$libraryID) {
throw new Exception('libraryID not provided');
}
if (!empty($this->permissions[$libraryID]['write'])) {
return true;
}
$libraryType = Zotero_Libraries::getType($libraryID);
switch ($libraryType) {
case 'user':
return false;
// Write permissions match key's write access to user library
case 'publications':
$userLibraryID = Zotero_Users::getLibraryIDFromUserID($this->userID);
return $this->canWrite($userLibraryID);
case 'group':
$groupID = Zotero_Groups::getGroupIDFromLibraryID($libraryID);
// If key has write access to all groups, grant access if user
// has write access to group
if (!empty($this->permissions[0]['write'])) {
$group = Zotero_Groups::get($groupID);
return $group->userCanEdit($this->userID);
}
return false;
default:
throw new Exception("Unsupported library type '$libraryType'");
}
}
public function setPermission($libraryID, $permission, $enabled) {
if ($this->super) {
throw new Exception("Super-user permissions already set");
}
switch ($permission) {
case 'library':
case 'notes':
case 'files':
case 'write':
break;
default:
throw new Exception("Invalid permission '$permission'");
}
$this->permissions[$libraryID][$permission] = $enabled;
}
public function setAnonymous() {
$this->anonymous = true;
}
public function setUser($userID) {
$this->userID = $userID;
}
public function setSuper() {
$this->super = true;
}
public function isSuper() {
return $this->super;
}
private function getUserPrivacy($userID) {
if (isset($this->userPrivacy[$userID])) {
return $this->userPrivacy[$userID];
}
if (Z_ENV_DEV_SITE) {
// Hard-coded test values
$privacy = array();
switch ($userID) {
case 1:
$privacy['library'] = true;
$privacy['notes'] = true;
break;
case 2:
$privacy['library'] = false;
$privacy['notes'] = false;
break;
default:
throw new Exception("External requests disabled on dev site");
}
$this->userPrivacy[$userID] = $privacy;
return $privacy;
}
$sql = "SELECT metaKey, metaValue FROM users_meta WHERE userID=? AND metaKey LIKE 'privacy_publish%'";
try {
$rows = Zotero_WWW_DB_2::query($sql, $userID);
}
catch (Exception $e) {
Z_Core::logError("WARNING: $e -- retrying on primary");
$rows = Zotero_WWW_DB_1::query($sql, $userID);
}
$privacy = array(
'library' => false,
'notes' => false
);
foreach ($rows as $row) {
$privacy[strtolower(substr($row['metaKey'], 15))] = (bool) (int) $row['metaValue'];
}
$this->userPrivacy[$userID] = $privacy;
return $privacy;
}
private function getGroupPrivacy($groupID) {
if (isset($this->groupPrivacy[$groupID])) {
return $this->groupPrivacy[$groupID];
}
$group = Zotero_Groups::get($groupID);
if (!$group) {
throw new Exception("Group $groupID doesn't exist");
}
$privacy = array();
if ($group->isPublic()) {
$privacy['view'] = true;
$privacy['library'] = $group->libraryReading == 'all';
$privacy['notes'] = $group->libraryReading == 'all';
}
else {
$privacy['view'] = false;
$privacy['library'] = false;
$privacy['notes'] = false;
}
$this->groupPrivacy[$groupID] = $privacy;
return $privacy;
}
public function hasPermission($object, $userID=false) {
return false;
}
}
?>