|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* EGroupware API - ACL |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://www.egroupware.org |
|
6
|
|
|
* @author Dan Kuykendall <[email protected]> |
|
7
|
|
|
* Copyright (C) 2000, 2001 Dan Kuykendall |
|
8
|
|
|
* @license http://opensource.org/licenses/lgpl-license.php LGPL - GNU Lesser General Public License |
|
9
|
|
|
* @package api |
|
10
|
|
|
* @subpackage acl |
|
11
|
|
|
* @version $Id$ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace EGroupware\Api; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Access Control List System |
|
18
|
|
|
* |
|
19
|
|
|
* This class provides an ACL security scheme. |
|
20
|
|
|
* This can manage rights to 'run' applications, and limit certain features within an application. |
|
21
|
|
|
* It is also used for granting a user "membership" to a group, or making a user have the security equivilance of another user. |
|
22
|
|
|
* It is also used for granting a user or group rights to various records, such as todo or calendar items of another user. |
|
23
|
|
|
* |
|
24
|
|
|
* $acl = new acl(5); // 5 is the user id |
|
25
|
|
|
*/ |
|
26
|
|
|
class Acl |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var int $account_id the account-id this class is instanciated for |
|
30
|
|
|
*/ |
|
31
|
|
|
var $account_id = 0; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array $data internal repository with acl rows for the given app and account-id (incl. memberships) |
|
34
|
|
|
*/ |
|
35
|
|
|
var $data = Array(); |
|
36
|
|
|
/** |
|
37
|
|
|
* internal reference to global db-object |
|
38
|
|
|
* |
|
39
|
|
|
* @var Db |
|
40
|
|
|
*/ |
|
41
|
|
|
var $db; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var string $table_name name of the acl_table |
|
44
|
|
|
*/ |
|
45
|
|
|
const TABLE = 'egw_acl'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constants for acl rights, like old EGW_ACL_* defines |
|
49
|
|
|
*/ |
|
50
|
|
|
const READ = 1; // EGW_ACL_READ |
|
51
|
|
|
const ADD = 2; // EGW_ACL_ADD |
|
52
|
|
|
const EDIT = 4; // EGW_ACL_EDIT |
|
53
|
|
|
const DELETE = 8; // EGW_ACL_DELETE |
|
54
|
|
|
const PRIVAT = 16; // EGW_ACL_PRIVATE can NOT use PRIVATE as it is a PHP keyword, using German PRIVAT instead! |
|
55
|
|
|
const GROUPMGRS = 32; // EGW_ACL_GROUP_MANAGERS |
|
56
|
|
|
const CUSTOM1 = 64; // EGW_ACL_CUSTOM_1 |
|
57
|
|
|
const CUSTOM2 = 128; // EGW_ACL_CUSTOM_2 |
|
58
|
|
|
const CUSTOM3 = 256; // EGW_ACL_CUSTOM_3 |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* ACL constructor for setting account id |
|
62
|
|
|
* |
|
63
|
|
|
* Sets the ID for $acl->account_id. Can be used to change a current instances id as well. |
|
64
|
|
|
* Some functions are specific to this account, and others are generic. |
|
65
|
|
|
* |
|
66
|
|
|
* @example acl->acl(5); // 5 is the user id |
|
67
|
|
|
* @param int $account_id = null user id or default null to use current user from $GLOBALS['egw_info']['user']['account_id'] |
|
68
|
|
|
*/ |
|
69
|
|
|
function __construct($account_id = null) |
|
70
|
|
|
{ |
|
71
|
|
View Code Duplication |
if (is_object($GLOBALS['egw_setup']->db)) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->db = $GLOBALS['egw_setup']->db; |
|
74
|
|
|
} |
|
75
|
|
|
else |
|
76
|
|
|
{ |
|
77
|
|
|
$this->db = $GLOBALS['egw']->db; |
|
78
|
|
|
} |
|
79
|
|
|
if ((int)$this->account_id != (int)$account_id) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->account_id = get_account_id((int)$account_id,@$GLOBALS['egw_info']['user']['account_id']); |
|
82
|
|
|
} |
|
83
|
|
|
$this->data = array(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Magic method called before object get serialized |
|
88
|
|
|
* |
|
89
|
|
|
* We only store account_id class is constructed for (not data, which can be huge!) and |
|
90
|
|
|
* get_rights calls read_repository automatic, if data is empty. |
|
91
|
|
|
*/ |
|
92
|
|
|
function __sleep() |
|
93
|
|
|
{ |
|
94
|
|
|
return array('account_id','db'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/**************************************************************************\ |
|
98
|
|
|
* These are the standard $this->account_id specific functions * |
|
99
|
|
|
\**************************************************************************/ |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Read acl records for $acl->account_id from reposity |
|
103
|
|
|
* |
|
104
|
|
|
* @param boolean|array $no_groups = false if true, do not use memberships, if array do not use given groups |
|
105
|
|
|
* @return array along with storing it in $acl->data. <br> |
|
106
|
|
|
*/ |
|
107
|
|
|
function read_repository($no_groups=false) |
|
108
|
|
|
{ |
|
109
|
|
|
// For some reason, calling this via XML-RPC doesn't call the constructor. |
|
110
|
|
|
// Here is yet another work around(tm) (jengo) |
|
111
|
|
|
if (!$this->account_id) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->__construct(); |
|
114
|
|
|
} |
|
115
|
|
|
if ($no_groups === true || !(int)$this->account_id) |
|
116
|
|
|
{ |
|
117
|
|
|
$acl_acc_list = $this->account_id; |
|
118
|
|
|
} |
|
119
|
|
|
else |
|
120
|
|
|
{ |
|
121
|
|
|
$acl_acc_list = (array)$GLOBALS['egw']->accounts->memberships($this->account_id, true); |
|
122
|
|
|
if (is_array($no_groups)) $acl_acc_list = array_diff($acl_acc_list,$no_groups); |
|
123
|
|
|
array_unshift($acl_acc_list,$this->account_id); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->data = Array(); |
|
127
|
|
|
foreach($this->db->select(self::TABLE,'*',array('acl_account' => $acl_acc_list ),__LINE__,__FILE__) as $row) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->data[$row['acl_appname'].'-'.$row['acl_location'].'-'.$row['acl_account']] = Db::strip_array_keys($row,'acl_'); |
|
130
|
|
|
} |
|
131
|
|
|
return $this->data; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Read acl records from $acl->data |
|
136
|
|
|
* |
|
137
|
|
|
* @return array all ACL records from $this->data. |
|
138
|
|
|
*/ |
|
139
|
|
|
function read() |
|
140
|
|
|
{ |
|
141
|
|
|
if (!count($this->data)) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->read_repository(); |
|
144
|
|
|
} |
|
145
|
|
|
return $this->data; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Adds ACL record to the repository of the class |
|
150
|
|
|
* |
|
151
|
|
|
* Adds ACL record to $this->data. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $appname default False derives value from $GLOBALS['egw_info']['flags']['currentapp'] |
|
154
|
|
|
* @param string $location location |
|
155
|
|
|
* @param int $rights rights |
|
156
|
|
|
* @return array all ACL records from $this->data. |
|
157
|
|
|
*/ |
|
158
|
|
|
function add($appname,$location,$rights) |
|
159
|
|
|
{ |
|
160
|
|
|
if (!$appname) $appname = $GLOBALS['egw_info']['flags']['currentapp']; |
|
161
|
|
|
|
|
162
|
|
|
$row = array( |
|
163
|
|
|
'appname' => $appname, |
|
164
|
|
|
'location' => $location, |
|
165
|
|
|
'account' => (int) $this->account_id, |
|
166
|
|
|
'rights' => (int) $rights |
|
167
|
|
|
); |
|
168
|
|
|
$this->data[$row['appname'].'-'.$row['location'].'-'.$row['account']] = $row; |
|
169
|
|
|
|
|
170
|
|
|
return $this->data; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Delete ACL record in the repository of the class |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $appname appname or '' for $GLOBALS['egw_info']['flags']['currentapp'] |
|
177
|
|
|
* @param string/boolean $location location or false for all locations |
|
178
|
|
|
* @return array all ACL records from $this->data. |
|
179
|
|
|
*/ |
|
180
|
|
|
function delete($appname,$location) |
|
181
|
|
|
{ |
|
182
|
|
|
if (!$appname) $appname = $GLOBALS['egw_info']['flags']['currentapp']; |
|
183
|
|
|
|
|
184
|
|
|
foreach($this->data as $idx => $value) |
|
185
|
|
|
{ |
|
186
|
|
|
if ($value['appname'] == $appname && |
|
187
|
|
|
($location === false || $value['location'] == $location) && |
|
188
|
|
|
$value['account'] == $this->account_id) |
|
189
|
|
|
{ |
|
190
|
|
|
unset($this->data[$idx]); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
return $this->data; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* save the internal repository or the class |
|
198
|
|
|
* |
|
199
|
|
|
* @return array all ACL records from $this->data. |
|
200
|
|
|
*/ |
|
201
|
|
|
function save_repository() |
|
202
|
|
|
{ |
|
203
|
|
|
$this->db->delete(self::TABLE,array( |
|
204
|
|
|
'acl_account' => $this->account_id, |
|
205
|
|
|
),__LINE__,__FILE__); |
|
206
|
|
|
|
|
207
|
|
|
foreach($this->data as $value) |
|
208
|
|
|
{ |
|
209
|
|
|
if ($value['account'] == $this->account_id) |
|
210
|
|
|
{ |
|
211
|
|
|
$this->db->insert(self::TABLE,array( |
|
212
|
|
|
'acl_appname' => $value['appname'], |
|
213
|
|
|
'acl_location' => $value['location'], |
|
214
|
|
|
'acl_account' => $this->account_id, |
|
215
|
|
|
'acl_rights' => $value['rights'], |
|
216
|
|
|
),false,__LINE__,__FILE__); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
View Code Duplication |
if ($this->account_id == $GLOBALS['egw_info']['user']['account_id'] && |
|
220
|
|
|
method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited |
|
221
|
|
|
{ |
|
222
|
|
|
$GLOBALS['egw']->invalidate_session_cache(); |
|
223
|
|
|
} |
|
224
|
|
|
return $this->data; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/**************************************************************************\ |
|
228
|
|
|
* These are the non-standard $this->account_id specific functions * |
|
229
|
|
|
\**************************************************************************/ |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* get rights from the class repository (included rights of $this->account_id and all it's memberships) |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $location app location to get rights from |
|
235
|
|
|
* @param string $appname optional defaults to $GLOBALS['egw_info']['flags']['currentapp']; |
|
236
|
|
|
* @return int all rights or'ed together |
|
237
|
|
|
*/ |
|
238
|
|
|
function get_rights($location,$appname = '') |
|
239
|
|
|
{ |
|
240
|
|
|
// For XML-RPC, change this once its working correctly for passing parameters (jengo) |
|
241
|
|
|
if (is_array($location)) |
|
242
|
|
|
{ |
|
243
|
|
|
$appname = $location['appname']; |
|
244
|
|
|
$location = $location['location']; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
if (!count($this->data)) |
|
248
|
|
|
{ |
|
249
|
|
|
$this->read_repository(); |
|
250
|
|
|
} |
|
251
|
|
|
if (!$appname) $appname = $GLOBALS['egw_info']['flags']['currentapp']; |
|
252
|
|
|
|
|
253
|
|
View Code Duplication |
if (!count($this->data) && $GLOBALS['egw_info']['server']['acl_default'] != 'deny') |
|
254
|
|
|
{ |
|
255
|
|
|
return True; |
|
256
|
|
|
} |
|
257
|
|
|
$rights = 0; |
|
258
|
|
|
foreach($this->data as $value) |
|
259
|
|
|
{ |
|
260
|
|
|
if ($value['appname'] == $appname) |
|
261
|
|
|
{ |
|
262
|
|
|
if ($value['location'] == $location || $value['location'] == 'everywhere') |
|
263
|
|
|
{ |
|
264
|
|
|
if ($value['rights'] == 0) |
|
265
|
|
|
{ |
|
266
|
|
|
return False; |
|
267
|
|
|
} |
|
268
|
|
|
$rights |= $value['rights']; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
return $rights; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* check required rights agains the internal repository (included rights of $this->account_id and all it's memberships) |
|
277
|
|
|
* |
|
278
|
|
|
* @param $location app location |
|
279
|
|
|
* @param $required required right to check against |
|
280
|
|
|
* @param $appname optional defaults to currentapp |
|
281
|
|
|
* @return boolean |
|
282
|
|
|
*/ |
|
283
|
|
|
function check($location, $required, $appname = False) |
|
284
|
|
|
{ |
|
285
|
|
|
$rights = $this->get_rights($location,$appname); |
|
286
|
|
|
|
|
287
|
|
|
return !!($rights & $required); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* get specific rights for this->account_id for an app location |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $location app location |
|
294
|
|
|
* @param string $appname = '' optional defaults to currentapp |
|
295
|
|
|
* @param array $memberships = array() additional account_id, eg. memberships to match beside $this->account_id, default none |
|
296
|
|
|
* @return int $rights |
|
297
|
|
|
*/ |
|
298
|
|
|
function get_specific_rights($location, $appname = '', $memberships=array()) |
|
299
|
|
|
{ |
|
300
|
|
|
if (!$appname) $appname = $GLOBALS['egw_info']['flags']['currentapp']; |
|
301
|
|
|
|
|
302
|
|
View Code Duplication |
if (!count($this->data) && $GLOBALS['egw_info']['server']['acl_default'] != 'deny') |
|
303
|
|
|
{ |
|
304
|
|
|
return True; |
|
305
|
|
|
} |
|
306
|
|
|
$rights = 0; |
|
307
|
|
|
|
|
308
|
|
|
foreach($this->data as $value) |
|
309
|
|
|
{ |
|
310
|
|
|
if ($value['appname'] == $appname && |
|
311
|
|
|
($value['location'] == $location || $value['location'] == 'everywhere') && |
|
312
|
|
|
($value['account'] == $this->account_id || $memberships && in_array($value['account'], $memberships))) |
|
313
|
|
|
{ |
|
314
|
|
|
if ($value['rights'] == 0) |
|
315
|
|
|
{ |
|
316
|
|
|
return False; |
|
317
|
|
|
} |
|
318
|
|
|
$rights |= $value['rights']; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
return $rights; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* check specific rights |
|
326
|
|
|
* |
|
327
|
|
|
* @param string $location app location |
|
328
|
|
|
* @param int $required required rights |
|
329
|
|
|
* @param string $appname optional defaults to currentapp |
|
330
|
|
|
* @return boolean |
|
331
|
|
|
*/ |
|
332
|
|
|
function check_specific($location, $required, $appname = '') |
|
333
|
|
|
{ |
|
334
|
|
|
$rights = $this->get_specific_rights($location,$appname); |
|
335
|
|
|
|
|
336
|
|
|
return !!($rights & $required); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/**************************************************************************\ |
|
340
|
|
|
* These are the generic functions. Not specific to $this->account_id * |
|
341
|
|
|
\**************************************************************************/ |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* add repository information / rights for app/location/account_id to the database |
|
345
|
|
|
* |
|
346
|
|
|
* @param string $app appname |
|
347
|
|
|
* @param string $location location |
|
348
|
|
|
* @param int $account_id account id |
|
349
|
|
|
* @param int $rights rights |
|
350
|
|
|
* @return boolean allways true |
|
351
|
|
|
*/ |
|
352
|
|
|
function add_repository($app, $location, $account_id, $rights) |
|
353
|
|
|
{ |
|
354
|
|
|
//echo "<p>self::add_repository('$app','$location',$account_id,$rights);</p>\n"; |
|
355
|
|
|
$this->db->insert(self::TABLE,array( |
|
356
|
|
|
'acl_rights' => $rights, |
|
357
|
|
|
),array( |
|
358
|
|
|
'acl_appname' => $app, |
|
359
|
|
|
'acl_location' => $location, |
|
360
|
|
|
'acl_account' => $account_id, |
|
361
|
|
|
),__LINE__,__FILE__); |
|
362
|
|
|
|
|
363
|
|
View Code Duplication |
if ($account_id == $GLOBALS['egw_info']['user']['account_id'] && |
|
364
|
|
|
method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited |
|
365
|
|
|
{ |
|
366
|
|
|
$GLOBALS['egw']->invalidate_session_cache(); |
|
367
|
|
|
} |
|
368
|
|
|
return True; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* delete repository information / rights for app/location[/account_id] from the DB |
|
373
|
|
|
* |
|
374
|
|
|
* @param string $app appname |
|
375
|
|
|
* @param string $location location |
|
376
|
|
|
* @param int/boolean $accountid = '' account id, default 0=$this->account_id, or false to delete all entries for $app/$location |
|
377
|
|
|
* @return int number of rows deleted |
|
378
|
|
|
*/ |
|
379
|
|
|
function delete_repository($app, $location, $accountid='') |
|
380
|
|
|
{ |
|
381
|
|
|
static $cache_accountid = array(); |
|
382
|
|
|
|
|
383
|
|
|
$where = array( |
|
384
|
|
|
'acl_appname' => $app, |
|
385
|
|
|
'acl_location' => $location, |
|
386
|
|
|
); |
|
387
|
|
|
if ($accountid !== false) |
|
388
|
|
|
{ |
|
389
|
|
|
if(isset($cache_accountid[$accountid]) && $cache_accountid[$accountid]) |
|
390
|
|
|
{ |
|
391
|
|
|
$where['acl_account'] = $cache_accountid[$accountid]; |
|
392
|
|
|
} |
|
393
|
|
|
else |
|
394
|
|
|
{ |
|
395
|
|
|
$where['acl_account'] = $cache_accountid[$accountid] = get_account_id($accountid,$this->account_id); |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
if (method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited |
|
399
|
|
|
{ |
|
400
|
|
|
$GLOBALS['egw']->invalidate_session_cache(); |
|
401
|
|
|
} |
|
402
|
|
|
if ($app == '%' || $app == '%%') unset($where['acl_appname']); |
|
403
|
|
|
|
|
404
|
|
|
$this->db->delete(self::TABLE,$where,__LINE__,__FILE__); |
|
405
|
|
|
|
|
406
|
|
|
return $this->db->affected_rows(); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* Get rights for a given account, location and application |
|
411
|
|
|
* |
|
412
|
|
|
* @param int $account_id |
|
413
|
|
|
* @param string $location |
|
414
|
|
|
* @param string $appname = '' defaults to current app |
|
415
|
|
|
* @return int/boolean rights or false if none exist |
|
416
|
|
|
*/ |
|
417
|
|
|
function get_specific_rights_for_account($account_id,$location,$appname='') |
|
418
|
|
|
{ |
|
419
|
|
|
if (!$appname) $appname = $GLOBALS['egw_info']['flags']['currentapp']; |
|
420
|
|
|
|
|
421
|
|
|
return $this->db->select(self::TABLE,'acl_rights',array( |
|
422
|
|
|
'acl_location' => $location, |
|
423
|
|
|
'acl_account' => $account_id, |
|
424
|
|
|
'acl_appname' => $appname, |
|
425
|
|
|
),__LINE__,__FILE__)->fetchColumn(); |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* Get all rights for a given location and application |
|
430
|
|
|
* |
|
431
|
|
|
* @param string $location |
|
432
|
|
|
* @param string $appname = '' defaults to current app |
|
433
|
|
|
* @return array with account => rights pairs |
|
434
|
|
|
*/ |
|
435
|
|
View Code Duplication |
function get_all_rights($location,$appname='') |
|
436
|
|
|
{ |
|
437
|
|
|
if (!$appname) $appname = $GLOBALS['egw_info']['flags']['currentapp']; |
|
438
|
|
|
|
|
439
|
|
|
$rights = array(); |
|
440
|
|
|
foreach($this->db->select(self::TABLE,'acl_account,acl_rights',array( |
|
441
|
|
|
'acl_location' => $location, |
|
442
|
|
|
'acl_appname' => $appname, |
|
443
|
|
|
),__LINE__,__FILE__) as $row) |
|
444
|
|
|
{ |
|
445
|
|
|
$rights[$row['acl_account']] = $row['acl_rights']; |
|
446
|
|
|
} |
|
447
|
|
|
return $rights; |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
/** |
|
451
|
|
|
* Get the rights for all locations |
|
452
|
|
|
* |
|
453
|
|
|
* @param int $account_id |
|
454
|
|
|
* @param string $appname = '' defaults to current app |
|
455
|
|
|
* @param boolean $use_memberships = true |
|
456
|
|
|
* @return array with location => rights pairs |
|
457
|
|
|
*/ |
|
458
|
|
|
function get_all_location_rights($account_id,$appname='',$use_memberships=true) |
|
459
|
|
|
{ |
|
460
|
|
|
if (!$appname) $appname = $GLOBALS['egw_info']['flags']['currentapp']; |
|
461
|
|
|
|
|
462
|
|
|
$accounts = array($account_id); |
|
463
|
|
View Code Duplication |
if ($use_memberships && (int)$account_id > 0) |
|
464
|
|
|
{ |
|
465
|
|
|
$accounts = $GLOBALS['egw']->accounts->memberships($account_id, true); |
|
466
|
|
|
$accounts[] = $account_id; |
|
467
|
|
|
} |
|
468
|
|
|
$rights = array(); |
|
469
|
|
|
foreach($this->db->select(self::TABLE,'acl_location,acl_rights',array( |
|
470
|
|
|
'acl_account' => $accounts, |
|
471
|
|
|
'acl_appname' => $appname, |
|
472
|
|
|
),__LINE__,__FILE__) as $row) |
|
473
|
|
|
{ |
|
474
|
|
|
$rights[$row['acl_location']] |= $row['acl_rights']; |
|
475
|
|
|
} |
|
476
|
|
|
return $rights; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
/** |
|
480
|
|
|
* get application list for an account id |
|
481
|
|
|
* |
|
482
|
|
|
* @param string $location location |
|
483
|
|
|
* @param int $required required rights |
|
484
|
|
|
* @param int $accountid account id defaults to $GLOBALS['egw_info']['user']['account_id']; |
|
485
|
|
|
* @return array/boolean false if there are no matching row in the db, else array with app-names |
|
486
|
|
|
*/ |
|
487
|
|
|
function get_app_list_for_id($location, $required, $accountid = '') |
|
488
|
|
|
{ |
|
489
|
|
|
static $cache_accountid = array(); |
|
490
|
|
|
|
|
491
|
|
View Code Duplication |
if(isset($cache_accountid[$accountid])) |
|
492
|
|
|
{ |
|
493
|
|
|
$account_id = $cache_accountid[$accountid]; |
|
494
|
|
|
} |
|
495
|
|
|
else |
|
496
|
|
|
{ |
|
497
|
|
|
$account_id = get_account_id($accountid,$this->account_id); |
|
498
|
|
|
$cache_accountid[$accountid] = $account_id; |
|
499
|
|
|
} |
|
500
|
|
|
$rights = 0; |
|
501
|
|
|
$apps = false; |
|
502
|
|
|
foreach($this->db->select(self::TABLE,array('acl_appname','acl_rights'),array( |
|
503
|
|
|
'acl_location' => $location, |
|
504
|
|
|
'acl_account' => $account_id, |
|
505
|
|
|
),__LINE__,__FILE__) as $row) |
|
506
|
|
|
{ |
|
507
|
|
|
if ($row['acl_rights'] == 0) |
|
508
|
|
|
{ |
|
509
|
|
|
return False; |
|
510
|
|
|
} |
|
511
|
|
|
$rights |= $row['acl_rights']; |
|
512
|
|
|
if (!!($rights & $required)) |
|
513
|
|
|
{ |
|
514
|
|
|
$apps[] = $row['acl_appname']; |
|
515
|
|
|
} |
|
516
|
|
|
} |
|
517
|
|
|
return $apps; |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
/** |
|
521
|
|
|
* get location list for id |
|
522
|
|
|
* |
|
523
|
|
|
* @param string $app app |
|
524
|
|
|
* @param int $required required rights |
|
525
|
|
|
* @param int $accountid optional defaults to $GLOBALS['egw_info']['user']['account_id']; |
|
526
|
|
|
* @return array/boolean false if there are no matching rows in the db or array with location-strings |
|
527
|
|
|
*/ |
|
528
|
|
|
function get_location_list_for_id($app, $required, $accountid = '') |
|
529
|
|
|
{ |
|
530
|
|
|
static $cache_accountid = array(); |
|
531
|
|
|
|
|
532
|
|
View Code Duplication |
if(isset($cache_accountid[$accountid])) |
|
533
|
|
|
{ |
|
534
|
|
|
$accountid = $cache_accountid[$accountid]; |
|
535
|
|
|
} |
|
536
|
|
|
else |
|
537
|
|
|
{ |
|
538
|
|
|
$accountid = $cache_accountid[$accountid] = get_account_id($accountid,$this->account_id); |
|
539
|
|
|
} |
|
540
|
|
|
$locations = false; |
|
541
|
|
|
foreach($this->db->select(self::TABLE,'acl_location,acl_rights',array( |
|
542
|
|
|
'acl_appname' => $app, |
|
543
|
|
|
'acl_account' => $accountid, |
|
544
|
|
|
),__LINE__,__FILE__) as $row) |
|
545
|
|
|
{ |
|
546
|
|
|
if ($row['acl_rights'] & $required) |
|
547
|
|
|
{ |
|
548
|
|
|
$locations[] = $row['acl_location']; |
|
549
|
|
|
} |
|
550
|
|
|
} |
|
551
|
|
|
return $locations; |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
|
|
/** |
|
555
|
|
|
* get ids for location |
|
556
|
|
|
* |
|
557
|
|
|
* @param string $location location |
|
558
|
|
|
* @param int $required required rights |
|
559
|
|
|
* @param string $app app optional defaults to $GLOBALS['egw_info']['flags']['currentapp']; |
|
560
|
|
|
* @return boolean/array false if there are no matching rows in the db or array of account-ids |
|
561
|
|
|
*/ |
|
562
|
|
|
function get_ids_for_location($location, $required, $app = '') |
|
563
|
|
|
{ |
|
564
|
|
|
if (!$app) $app = $GLOBALS['egw_info']['flags']['currentapp']; |
|
565
|
|
|
|
|
566
|
|
|
$accounts = false; |
|
567
|
|
|
foreach($this->db->select(self::TABLE,array('acl_account','acl_rights'),array( |
|
568
|
|
|
'acl_appname' => $app, |
|
569
|
|
|
'acl_location' => $location, |
|
570
|
|
|
),__LINE__,__FILE__) as $row) |
|
571
|
|
|
{ |
|
572
|
|
|
if (!!($row['acl_rights'] & $required)) |
|
573
|
|
|
{ |
|
574
|
|
|
$accounts[] = (int) $row['acl_account']; |
|
575
|
|
|
} |
|
576
|
|
|
} |
|
577
|
|
|
return $accounts; |
|
578
|
|
|
} |
|
579
|
|
|
|
|
580
|
|
|
/** |
|
581
|
|
|
* get the locations for an app (excluding the run location !!!) |
|
582
|
|
|
* |
|
583
|
|
|
* @param string $app app optional defaults to $GLOBALS['egw_info']['flags']['currentapp']; |
|
584
|
|
|
* @return boolean/array false if there are no matching location in the db or array of locations |
|
585
|
|
|
*/ |
|
586
|
|
View Code Duplication |
function get_locations_for_app($app='') |
|
587
|
|
|
{ |
|
588
|
|
|
if (!$app) $app = $GLOBALS['egw_info']['flags']['currentapp']; |
|
589
|
|
|
|
|
590
|
|
|
$locations = false; |
|
591
|
|
|
foreach($this->db->select(self::TABLE,'DISTINCT '.'acl_location',array( |
|
592
|
|
|
'acl_appname' => $app, |
|
593
|
|
|
),__LINE__,__FILE__) as $row) |
|
594
|
|
|
{ |
|
595
|
|
|
if (($location = $row['acl_location']) != 'run') |
|
596
|
|
|
{ |
|
597
|
|
|
$locations[] = $location; |
|
598
|
|
|
} |
|
599
|
|
|
} |
|
600
|
|
|
return $locations; |
|
601
|
|
|
} |
|
602
|
|
|
|
|
603
|
|
|
/** |
|
604
|
|
|
* get a list of applications a user has rights to |
|
605
|
|
|
* |
|
606
|
|
|
* @param int $accountid = '' optional defaults to $GLOBALS['egw_info']['user']['account_id']; |
|
607
|
|
|
* @param boolean $use_memberships = true true: use memberships too, false: only use given account |
|
608
|
|
|
* @param boolean $add_implicit_apps = true true: add apps every user has implicit rights |
|
609
|
|
|
* @return array containing list of apps |
|
610
|
|
|
*/ |
|
611
|
|
|
function get_user_applications($accountid = '', $use_memberships=true, $add_implicit_apps=true) |
|
612
|
|
|
{ |
|
613
|
|
|
static $cache_accountid = array(); |
|
614
|
|
|
|
|
615
|
|
View Code Duplication |
if(isset($cache_accountid[$accountid])) |
|
616
|
|
|
{ |
|
617
|
|
|
$account_id = $cache_accountid[$accountid]; |
|
618
|
|
|
} |
|
619
|
|
|
else |
|
620
|
|
|
{ |
|
621
|
|
|
$account_id = get_account_id($accountid,$this->account_id); |
|
622
|
|
|
$cache_accountid[$accountid] = $account_id; |
|
623
|
|
|
} |
|
624
|
|
View Code Duplication |
if ($use_memberships && (int)$account_id > 0) $memberships = $GLOBALS['egw']->accounts->memberships($account_id, true); |
|
625
|
|
|
$memberships[] = (int)$account_id; |
|
626
|
|
|
|
|
627
|
|
|
$apps = array(); |
|
628
|
|
|
foreach($this->db->select(self::TABLE,array('acl_appname','acl_rights'),array( |
|
629
|
|
|
'acl_location' => 'run', |
|
630
|
|
|
'acl_account' => $memberships, |
|
631
|
|
|
),__LINE__,__FILE__) as $row) |
|
632
|
|
|
{ |
|
633
|
|
|
$app = $row['acl_appname']; |
|
634
|
|
|
if(!isset($apps[$app])) |
|
635
|
|
|
{ |
|
636
|
|
|
$apps[$app] = 0; |
|
637
|
|
|
} |
|
638
|
|
|
$apps[$app] |= (int) $row['acl_rights']; |
|
639
|
|
|
} |
|
640
|
|
|
if ($add_implicit_apps) |
|
641
|
|
|
{ |
|
642
|
|
|
$apps['api'] = 1; // give everyone implicit rights for the home app |
|
643
|
|
|
} |
|
644
|
|
|
return $apps; |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
/** |
|
648
|
|
|
* Read the grants other users gave $this->account_id for $app, group ACL is taken into account |
|
649
|
|
|
* |
|
650
|
|
|
* @param string $app optional defaults to $GLOBALS['egw_info']['flags']['currentapp'] |
|
651
|
|
|
* @param boolean/array $enum_group_acls = true should group acls be returned for all members of that group, default yes |
|
652
|
|
|
* if an array of group-id's is given, that id's will NOT be enumerated! |
|
653
|
|
|
* @param int $user = null user whos grants to return, default current user |
|
654
|
|
|
* @return array with account-ids (of owners) and granted rights as values |
|
655
|
|
|
*/ |
|
656
|
|
|
function get_grants($app='',$enum_group_acls=true,$user=null) |
|
657
|
|
|
{ |
|
658
|
|
|
if (!$app) $app = $GLOBALS['egw_info']['flags']['currentapp']; |
|
659
|
|
|
if (!$user) $user = $this->account_id; |
|
660
|
|
|
|
|
661
|
|
|
static $cache = array(); // some caching withing the request |
|
662
|
|
|
|
|
663
|
|
|
$grants =& $cache[$app][$user]; |
|
664
|
|
|
if (!isset($grants)) |
|
665
|
|
|
{ |
|
666
|
|
|
if ((int)$user > 0) $memberships = $GLOBALS['egw']->accounts->memberships($user, true); |
|
667
|
|
|
$memberships[] = $user; |
|
668
|
|
|
|
|
669
|
|
|
$grants = $accounts = Array(); |
|
670
|
|
|
foreach($this->db->select(self::TABLE,array('acl_account','acl_rights','acl_location'),array( |
|
671
|
|
|
'acl_appname' => $app, |
|
672
|
|
|
'acl_location' => $memberships, |
|
673
|
|
|
),__LINE__,__FILE__) as $row) |
|
674
|
|
|
{ |
|
675
|
|
|
$grantor = $row['acl_account']; |
|
676
|
|
|
$rights = $row['acl_rights']; |
|
677
|
|
|
|
|
678
|
|
|
if(!isset($grants[$grantor])) |
|
679
|
|
|
{ |
|
680
|
|
|
$grants[$grantor] = 0; |
|
681
|
|
|
} |
|
682
|
|
|
$grants[$grantor] |= $rights; |
|
683
|
|
|
|
|
684
|
|
|
// if the right is granted from a group and we enummerated group ACL's |
|
685
|
|
|
if ($GLOBALS['egw']->accounts->get_type($grantor) == 'g' && $enum_group_acls && |
|
686
|
|
|
(!is_array($enum_group_acls) || !in_array($grantor,$enum_group_acls))) |
|
687
|
|
|
{ |
|
688
|
|
|
// return the grant for each member of the group (false = also for no longer active users) |
|
689
|
|
|
foreach((array)$GLOBALS['egw']->accounts->members($grantor, true, false) as $grantor) |
|
690
|
|
|
{ |
|
691
|
|
|
if (!$grantor) continue; // can happen if group has no members |
|
692
|
|
|
|
|
693
|
|
|
// Don't allow to override private with group ACL's! |
|
694
|
|
|
$rights &= ~self::PRIVAT; |
|
695
|
|
|
|
|
696
|
|
|
if(!isset($grants[$grantor])) |
|
697
|
|
|
{ |
|
698
|
|
|
$grants[$grantor] = 0; |
|
699
|
|
|
} |
|
700
|
|
|
$grants[$grantor] |= $rights; |
|
701
|
|
|
} |
|
702
|
|
|
} |
|
703
|
|
|
} |
|
704
|
|
|
// user has implizit all rights on own data |
|
705
|
|
|
$grants[$user] = ~0; |
|
706
|
|
|
} |
|
707
|
|
|
//echo "self::get_grants('$app',$enum_group_acls) ".function_backtrace(); _debug_array($grants); |
|
708
|
|
|
return $grants; |
|
709
|
|
|
} |
|
710
|
|
|
|
|
711
|
|
|
/** |
|
712
|
|
|
* Deletes all ACL entries for an account (user or group) |
|
713
|
|
|
* |
|
714
|
|
|
* @param int $account_id acount-id |
|
715
|
|
|
*/ |
|
716
|
|
|
function delete_account($account_id) |
|
717
|
|
|
{ |
|
718
|
|
|
if ((int) $account_id) |
|
719
|
|
|
{ |
|
720
|
|
|
// Delete all grants from this account |
|
721
|
|
|
$this->db->delete(self::TABLE,array( |
|
722
|
|
|
'acl_account' => $account_id |
|
723
|
|
|
),__LINE__,__FILE__); |
|
724
|
|
|
// Delete all grants to this account |
|
725
|
|
|
$this->db->delete(self::TABLE,array( |
|
726
|
|
|
'acl_location' => $account_id |
|
727
|
|
|
),__LINE__, __FILE__); |
|
728
|
|
|
// delete all memberships in account_id (if it is a group) |
|
729
|
|
|
$this->db->delete(self::TABLE,array( |
|
730
|
|
|
'acl_appname' => 'phpgw_group', |
|
731
|
|
|
'acl_location' => $account_id, |
|
732
|
|
|
),__LINE__,__FILE__); |
|
733
|
|
|
} |
|
734
|
|
|
} |
|
735
|
|
|
|
|
736
|
|
|
/** |
|
737
|
|
|
* get the locations for an app (excluding the run location !!!) |
|
738
|
|
|
* |
|
739
|
|
|
* @param string $location location, can contain wildcards % or ? |
|
740
|
|
|
* @param string $app app optional defaults to $GLOBALS['egw_info']['flags']['currentapp']; |
|
741
|
|
|
* @return array with location => array(account => rights) pairs |
|
742
|
|
|
*/ |
|
743
|
|
|
function get_location_grants($location,$app='') |
|
744
|
|
|
{ |
|
745
|
|
|
if (!$app) $app = $GLOBALS['egw_info']['flags']['currentapp']; |
|
746
|
|
|
|
|
747
|
|
|
$locations = array(); |
|
748
|
|
|
foreach($this->db->select(self::TABLE,'acl_location,acl_account,acl_rights',array( |
|
749
|
|
|
'acl_appname' => $app, |
|
750
|
|
|
'acl_location LIKE '.$this->db->quote($location), |
|
751
|
|
|
),__LINE__,__FILE__) as $row) |
|
752
|
|
|
{ |
|
753
|
|
|
if (($location = $row['acl_location']) != 'run') |
|
754
|
|
|
{ |
|
755
|
|
|
$locations[$location][$row['acl_account']] = $row['acl_rights']; |
|
756
|
|
|
} |
|
757
|
|
|
} |
|
758
|
|
|
return $locations; |
|
759
|
|
|
} |
|
760
|
|
|
} |
|
761
|
|
|
|