permission.inc
12 KB
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
<?php
//require_once("$default->owl_fs_root/lib/documentmanagement/documentLib.inc");
/**
* Class Permission
*
* Contains static functions used to determine whether the current user:
* o has permission to perform certain actions
* o has a certain role
* o is assigned to a certain group
* o has read/write access for a specific folder/directory
*
* @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
* @date 14 January 2003
*/
class Permission {
/**
* Checks if the current user has write permission for a specific document.
* To have document write permission the user must satisfy ONE of the following conditions:
* o have write permission for the folder in which the document resides
* o be assigned a role which has write permission for the document
*
* @param $iDocumentID Primary key of document to check
*
* @return boolean true if the current user has document write permission, false otherwise and set $_SESSION["errorMessage"]
*/
function userHasDocumentWritePermission($iDocumentID) {
if (Permission::userHasFolderWritePermission(DocumentLib::getDocumentFolderID($iDocumentID)) ||
Permission::userHasWriteRoleForFolder($iDocumentID)) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_doc_write . "id " . $iDocumentID;
return false;
}
/**
* Checks if the current user has read permission for a specific document.
* To have document read permission the user must satisfy ONE of the following conditions:
* o have read permission for the folder in which the document resides
* o be assigned a role which has read permission for the document
*
* @param $iDocumentID Primary key of document to check
*
* @return boolean true if the current user has document read permission, false otherwise and set $_SESSION["errorMessage"]
*/
function userHasDocumentReadPermission($iDocumentID) {
if (Permission::userHasFolderReadPermission(DocumentLib::getDocumentFolderID($iDocumentID)) ||
Permission::userHasReadRoleForFolder($iDocumentID)) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_doc_read . "id " . $iDocumentID;
return false;
}
/**
* Checks if the current user has write permission for a specific folder
* To have write permission on a folder the user must satisfy ONE of the following conditions:
* o be in the system administrator group
* o be in the unit administrator group for the unit to which the folder belongs
* o belong to a group that has write access to the folder
*
* @param $iFolderID Primary key of folder to check
*
* @return boolean true if the user has folder write permission, false otherwise and set $_SESSION["errorMessage"]
*/
function userHasFolderWritePermission($iFolderID) {
global $lang_err_user_folder_write;
if (Permission::userHasGroupWritePermissionForFolder($iFolderID) ||
Permission::userIsInGroupName("System Administrators") ||
Permission::userIsInUnitAdministratorGroup($iFolderID)) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_folder_write . "id " . $iFolderID;
return false;
}
/**
* Checks if the current user has read permission for a specific folder
* To have read permission on a folder the user must satisfy ONE of the following conditions
* o have write permission for the folder
* o belong to a group that has read access to the folder
* o the folder is a public folder
*
* @param $iFolderID Primary key of folder to check
*
* @return boolean true if the user has folder write permission, false otherwise and set $_SESSION["errorMessage"]
*/
function userHasFolderReadPermission($iFolderID) {
global $lang_err_user_folder_write;
if (Permission::folderIsPublic($iFolderID) ||
Permission::userHasFolderWritePermission($iFolderID) ||
Permission::userHasGroupReadPermissionForFolder($iFolderID)) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_folder_write . "id " . $iFolderID;
return false;
}
/**
* Checks if a folder is public
*
* @param $iFolderID Primary key of folder to check
*
* @return boolean true if the folder is public, false otherwise and set $_SESSION["errorMessage"]
*/
function folderIsPublic($iFolderID) {
global $default, $lang_err_folder_not_public;
$sql = new Owl_DB();
$sql->query("SELECT * FROM " . $default->owl_folders_table . " WHERE id = " . $iFolderID . " AND is_public = 1");
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_folder_not_public . "id " . $iFolderID;
return false;
}
/**
* Checks if the current user is in the unit administrator group for the unit
* to which the folder belongs
*
* @param $iFolderID Primary key of folder to check
*
* @return boolean true if the user has folder write permission, false otherwise and set $_SESSION["errorMessage"]
*
* @todo Remove hardcoding of 'Unit Administrators'
*/
function userIsInUnitAdministratorGroup($iFolderID) {
global $lang_err_user_unitadmin_group, $default;
$sql = new Owl_DB();
$sql->query("SELECT * FROM " . $default->owl_group_folders_table ." AS GFL INNER JOIN " . $default->owl_groups_users_link_table . " as GUL ON GFL.group_id = GUL.group_id " .
"INNER JOIN " . $default->owl_groups_table . " AS G ON G.ID = GFL.group_id " .
"WHERE GFL.folder_id = " . $iFolderID . " " .
"AND GUL.user_id = " . $_SESSION["userID"] . " " .
"AND G.Name = 'Unit Administrators' ");
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_unitadmin_group . " id = " . $iFolderID;
return false;
}
/**
* Checks if the current user has write permission through group membership for a particular folder
*
* @param $iFolderID Primary key of folder to check
*
* @return boolean true if the user has folder write permission, false otherwise and set $_SESSION["errorMessage"]
*/
function userHasGroupWritePermissionForFolder($iFolderID) {
global $default, $lang_err_user_folder_write;
$sql = new Owl_DB();
//$sql->query("SELECT * FROM " . $default->owl_groups_folders_table . " WHERE folder_id = " . $iFolderID . " AND user_id = " . $_SESSION["userID"] . " AND can_write = 1");
$sql->query("SELECT * FROM " . $default->owl_groups_folders_table . " GLF, $default->owl_users_groups_table GUL " .
" WHERE GLF.folder_id = " . $iFolderID .
" AND GUL.user_id = " . $_SESSION["userID"] .
" AND GLF.group_id = GUL.group_id " .
" AND can_write = 1");
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_folder_write;
return false;
}
/**
* Checks if the current user has read permission through group membership for a particular folder
*
* @param $iFolderID Primary key of folder to check
*
* @return boolean true if the user has folder write permission, false otherwise and set $_SESSION["errorMessage"]
*/
function userHasGroupReadPermissionForFolder($iFolderID) {
global $default, $lang_err_user_folder_read;
$sql = new Owl_DB();
//$sql->query("SELECT * FROM " . $default->owl_groups_folders_table = "groups_folders_link" . " WHERE folder_id = " . $iFolderID . " AND user_id = " . $_SESSION["userID"] . " AND can_read = 1");
$sql->query("SELECT * FROM " . $default->owl_groups_folders_table . " GLF, $default->owl_users_groups_table GUL " .
" WHERE GLF.folder_id = " . $iFolderID .
" AND GUL.user_id = " . $_SESSION["userID"] .
" AND GLF.group_id = GUL.group_id " .
" AND can_read = 1");
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_folder_read;
return false;
}
/**
* Checks if the current user is in the specified group using the group id
*
* @param $iGroupID Primary key of group to check
*
* @return boolean true if the user is in the group, false otherwise and sets $_SESSION["errorMessage"]
*/
function userIsInGroupID($iGroupID) {
global $default, $lang_err_user_group;
$sql = new Owl_DB();
$sql->query("SELECT id FROM " . $default->owl_groups_users_table . " WHERE id = " . $iGroupID . " AND user_id = " . $_SESSION["userID"]);
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_group . "group id = " . $iGroupID;
return false;
}
/**
* Checks if the current user is in the specified group using the group name
*
* @param $sGroupName Name of group to check
*
* @return boolean true if the user is in the group, false otherwise and sets $_SESSION["errorMessage"]
*/
function userIsInGroupName($sGroupName) {
global $default, $lang_err_user_group;
$sql = new Owl_DB();
$sql->query("SELECT GULT.id FROM " . $default->owl_users_groups_table . " AS GULT INNER JOIN " . $default->owl_groups_table . " AS G ON GULT.group_id = G.ID WHERE G.name = '" . $sGroupName . "' AND user_id = " . $_SESSION["userID"]);
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_group . "group name " . $sGroupName;
return false;
}
/**
* Check is the user is assigned a specific role that has write permission for a folder
*
* @param $iFolderID Primary key of folder to check
*
* @return boolean true is the user has the role assigned, false otherwise and set $_SESSION["errorMessage"]
*/
function userHasWriteRoleForFolder($iFolderID) {
global $default, $lang_err_user_role;
$sql = new Owl_DB();
$sql->query("SELECT * FROM " . $default->owl_folders_user_table . " AS FURL INNER JOIN " . $default->owl_role_table . " AS R ON FURL.role_id = R.id WHERE folder_id = " . $iFolderID . " AND user_id = " . $_SESSION["userID"] . " AND R.can_write = 1");
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_role;
return false;
}
/**
* Check is the user is assigned a specific role that has read permission for a folder
*
* @param $iFolderID Primary key of folder to check
*
* @return boolean true is the user has the role assigned, false otherwise and set $_SESSION["errorMessage"]
*/
function userHasReadRoleForFolder($iFolderID) {
global $default, $lang_err_user_role;
$sql = new Owl_DB();
$sql->query("SELECT * FROM " . $default->owl_folders_user_table . " AS FURL INNER JOIN " . $default->owl_role_table . " AS R ON FURL.role_id = R.id WHERE folder_id = " . $iFolderID . " AND user_id = " . $_SESSION["userID"] . " AND R.can_read = 1");
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_user_role;
return false;
}
/**
* Checks if a given role exists using the role primary key
*
* @param $iRoleID Primary key of role to check for
*
* @return boolean true if role exists, false otherwise and set $_SESSION["errorMessage"]
*/
function roleIDExists($iRoleID) {
global $default, $lang_err_role_not_exist;
$sql = new Owl_DB();
$sql->query("SELECT id FROM " . $default->owl_roles_table . " WHERE id = " . $iRoleID);
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_role_not_exist . $sRoleName;
return false;
}
/**
* Checks if a given role exists using the role name
*
* @param $sRoleName Name of role to check for
*
* @return boolean true if role exists, false otherwise and set $_SESSION["errorMessage"]
*/
function roleNameExists($sRoleName) {
global $default, $lang_err_role_not_exist;
$sql = new Owl_DB();
$sql->query("SELECT id FROM " . $default->owl_roles_table . " WHERE name = '" . $sRoleName . "'");
if ($sql->next_record()) {
return true;
}
$_SESSION["errorMessage"] = $lang_err_role_not_exist . $sRoleName;
return false;
}
/**
* Get the primary key for a role
*
* @param $sRoleName Name of role to get primary key for
*
* @return ID if role exists, false otherwise and set $_SESSION["errorMessage"]
*/
function getRoleID($sRoleName) {
global $default, $lang_database_error;
if (roleExists($sRoleName)) {
$sql = new Owl_DB();
$sql->query("SELECT id FROM " . $default->owl_roles_table . " WHERE name = '" . $sRoleName . "'");
$sql->next_record();
return $sql->f("id");
}
$_SESSION["errorMessage"] = $lang_database_error;
return false;
}
}
?>