forked from tine20/tine20
/
User.php
471 lines (413 loc) · 14.4 KB
/
User.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
/**
* Tine 2.0
*
* @package Tinebase
* @subpackage User
* @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
* @copyright Copyright (c) 2007-2014 Metaways Infosystems GmbH (http://www.metaways.de)
* @author Lars Kneschke <l.kneschke@metaways.de>
*/
/**
* defines the datatype for simple user object
*
* this user object contains only public informations
* its primary usecase are user selection interfaces
*
* @package Tinebase
* @subpackage User
*
* @property string accountId
* @property string contact_id
* @property string accountEmailAddress email address of user
* @property string accountDisplayName
* @property string accountLastName
* @property string accountFirstName
*/
class Tinebase_Model_User extends Tinebase_Record_Abstract
{
/**
* const to describe current account accountId independent
*
* @var string
*/
const CURRENTACCOUNT = 'currentAccount';
/**
* hidden from addressbook
*
* @var string
*/
const VISIBILITY_HIDDEN = 'hidden';
/**
* visible in addressbook
*
* @var string
*/
const VISIBILITY_DISPLAYED = 'displayed';
/**
* account is enabled
*
* @var string
*/
const ACCOUNT_STATUS_ENABLED = 'enabled';
/**
* account is disabled
*
* @var string
*/
const ACCOUNT_STATUS_DISABLED = 'disabled';
/**
* account is expired
*
* @var string
*/
const ACCOUNT_STATUS_EXPIRED = 'expired';
/**
* account is blocked
*
* @var string
*/
const ACCOUNT_STATUS_BLOCKED = 'blocked';
/**
* name of fields containing datetime or or an array of datetime
* information
*
* @var array list of datetime fields
*/
protected $_datetimeFields = array(
'creation_time',
'last_modified_time',
'deleted_time',
);
/**
* if foreign Id fields should be resolved on search and get from json
* should have this format:
* array('Calendar_Model_Contact' => 'contact_id', ...)
* or for more fields:
* array('Calendar_Model_Contact' => array('contact_id', 'customer_id), ...)
* (e.g. resolves contact_id with the corresponding Model)
*
* @var array
*/
protected static $_resolveForeignIdFields = array(
'Tinebase_Model_User' => array('created_by', 'last_modified_by')
);
/**
* list of zend inputfilter
*
* this filter get used when validating user generated content with Zend_Input_Filter
*
* @var array
*/
protected $_filters = array(
'accountId' => 'StringTrim',
//'accountLoginName' => 'StringTrim',
'accountDisplayName' => 'StringTrim',
'accountLastName' => 'StringTrim',
'accountFirstName' => 'StringTrim',
'accountFullName' => 'StringTrim',
);
/**
* list of zend validator
*
* this validators get used when validating user generated content with Zend_Input_Filter
*
* @var array
*/
protected $_validators = array(
'accountId' => array('presence' => 'required'),
//'accountLoginName' => array('presence' => 'required'),
'accountDisplayName' => array('presence' => 'required'),
'accountLastName' => array('presence' => 'required'),
'accountFirstName' => array('allowEmpty' => true),
'accountEmailAddress' => array('allowEmpty' => true),
'accountFullName' => array('presence' => 'required'),
'contact_id' => array('allowEmpty' => true),
// @todo do we need this information in this model?
'created_by' => array('allowEmpty' => true),
'creation_time' => array('allowEmpty' => true),
'last_modified_by' => array('allowEmpty' => true),
'last_modified_time' => array('allowEmpty' => true),
'is_deleted' => array('allowEmpty' => true),
'deleted_time' => array('allowEmpty' => true),
'deleted_by' => array('allowEmpty' => true),
'seq' => array('allowEmpty' => true),
'xprops' => array('allowEmpty' => true),
);
protected static $_replicable = true;
protected static $_forceSuperUser = false;
/**
* (non-PHPdoc)
* @see Tinebase/Record/Tinebase_Record_Abstract#setFromArray($_data)
*
* @todo need to discuss if this is the right place to do this. perhaps the client should send the fullname (and displayname), too.
*/
public function setFromArray(array $_data)
{
// always update accountDisplayName and accountFullName
if (isset($_data['accountLastName'])) {
$_data['accountDisplayName'] = $_data['accountLastName'];
if (!empty($_data['accountFirstName'])) {
$_data['accountDisplayName'] .= ', ' . $_data['accountFirstName'];
}
if (! (isset($_data['accountFullName']) || array_key_exists('accountFullName', $_data))) {
$_data['accountFullName'] = $_data['accountLastName'];
if (!empty($_data['accountFirstName'])) {
$_data['accountFullName'] = $_data['accountFirstName'] . ' ' . $_data['accountLastName'];
}
}
}
parent::setFromArray($_data);
}
/**
* key in $_validators/$_properties array for the filed which
* represents the identifier
*
* @var string
*/
protected $_identifier = 'accountId';
/**
* check if current user has a given right for a given application
*
* @param string|Tinebase_Model_Application $_application the application (one of: app name, id or record)
* @param int $_right the right to check for
* @return bool
*/
public function hasRight($_application, $_right)
{
if (true === static::$_forceSuperUser) {
return true;
}
$roles = Tinebase_Acl_Roles::getInstance();
$result = $roles->hasRight($_application, $this->accountId, $_right);
return $result;
}
/**
* returns a bitmask of rights for current user and given application
*
* @param string $_application the name of the application
* @return int bitmask of rights
*/
public function getRights($_application)
{
$roles = Tinebase_Acl_Roles::getInstance();
$result = $roles->getApplicationRights($_application, $this->accountId);
return $result;
}
/**
* return the group ids current user is member of
*
* @return array list of group ids
*/
public function getGroupMemberships()
{
$backend = Tinebase_Group::getInstance();
$result = $backend->getGroupMemberships($this->accountId);
return $result;
}
/**
* update the lastlogin time of current user
*
* @param string $_ipAddress
* @return void
* @todo write test for that
*/
public function setLoginTime($_ipAddress)
{
$backend = Tinebase_User::getInstance();
$result = $backend->setLoginTime($this->accountId, $_ipAddress);
return $result;
}
/**
* set the password for current user
*
* @param string $_password
* @return void
*/
public function setPassword($_password)
{
$backend = Tinebase_User::getInstance();
$backend->setPassword($this->accountId, $_password);
}
/**
* returns list of applications the current user is able to use
*
* this function takes group memberships into user. Applications the user is able to use
* must have the 'run' right set
*
* @param boolean $_anyRight is any right enough to geht app?
* @return array list of enabled applications for this user
*/
public function getApplications($_anyRight = FALSE)
{
$roles = Tinebase_Acl_Roles::getInstance();
$result = $roles->getApplications($this->accountId, $_anyRight);
if (Tinebase_Controller::getInstance()->userAccountChanged()) {
// TODO this information should be saved in application table
$disabledAppsForChangedUserAccounts = array('Felamimail');
foreach ($result as $key => $app) {
if (in_array($app, $disabledAppsForChangedUserAccounts)) {
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
. ' Skipping ' . $app . ' because app is disabled for changed user accounts');
}
unset($result[$key]);
}
}
}
return $result;
}
/**
* return all container, which the user has the requested right for
*
* used to get a list of all containers accesssible by the current user
*
* @param string $_application the application name
* @param int $_right the required right
* @param bool $_onlyIds return only ids
* @return Tinebase_Record_RecordSet|array
* @todo write test for that
*/
public function getContainerByACL($_application, $_right, $_onlyIds = FALSE)
{
$container = Tinebase_Container::getInstance();
$result = $container->getContainerByACL($this->accountId, $_application, $_right, $_onlyIds);
return $result;
}
/**
* return all personal container of the current user
*
* used to get a list of all personal containers accesssible by the current user
*
* @param string $_application the application name
* @return Tinebase_Record_RecordSet
* @todo write test for that
*/
public function getPersonalContainer($_application, $_owner, $_grant)
{
$container = Tinebase_Container::getInstance();
$result = $container->getPersonalContainer($this, $_application, $_owner, $_grant);
return $result;
}
/**
* get shared containers
*
* @param string|Tinebase_Model_Application $_application
* @param array|string $_grant
* @return Tinebase_Record_RecordSet set of Tinebase_Model_Container
*/
public function getSharedContainer($_application, $_grant)
{
$container = Tinebase_Container::getInstance();
$result = $container->getSharedContainer($this, $_application, $_grant);
return $result;
}
/**
* get containers of other users
*
* @param string|Tinebase_Model_Application $_application
* @param array|string $_grant
* @return Tinebase_Record_RecordSet set of Tinebase_Model_Container
*/
public function getOtherUsersContainer($_application, $_grant)
{
$container = Tinebase_Container::getInstance();
$result = $container->getOtherUsersContainer($this, $_application, $_grant);
return $result;
}
/**
* check if the current user has a given grant
*
* @param mixed $_containerId
* @param string $_grant
* @param string $_aclModel
* @return boolean
* @throws Tinebase_Exception_InvalidArgument
*
* TODO improve handling of different acl models
*/
public function hasGrant($_containerId, $_grant, $_aclModel = 'Tinebase_Model_Container')
{
if (true === static::$_forceSuperUser) {
return true;
}
if ($_containerId instanceof Tinebase_Record_Abstract) {
$aclModel = get_class($_containerId);
if (! in_array($aclModel, array('Tinebase_Model_Container', 'Tinebase_Model_Tree_Node'))) {
// fall back to param
$aclModel = $_aclModel;
}
} else {
$aclModel = $_aclModel;
}
switch ($aclModel) {
case 'Tinebase_Model_Container':
$result = Tinebase_Container::getInstance()->hasGrant($this->accountId, $_containerId, $_grant);
break;
case 'Tinebase_Model_Tree_Node':
$result = Tinebase_FileSystem::getInstance()->hasGrant($this->accountId, $_containerId, $_grant);
break;
default:
throw new Tinebase_Exception_InvalidArgument('ACL model not supported ');
}
return $result;
}
/**
* converts a int, string or Tinebase_Model_User to an accountid
*
* @param int|string|Tinebase_Model_User $_accountId the accountid to convert
* @return string
* @throws Tinebase_Exception_InvalidArgument
*
* TODO completely replace with TRA::convertId
*/
static public function convertUserIdToInt($_accountId)
{
return (string) self::convertId($_accountId, 'Tinebase_Model_User');
}
/**
* sanitizes account primary group and returns primary group id
*
* @return string
*/
public function sanitizeAccountPrimaryGroup()
{
try {
Tinebase_Group::getInstance()->getGroupById($this->accountPrimaryGroup);
} catch (Tinebase_Exception_Record_NotDefined $e) {
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ .' Could not resolve accountPrimaryGroupgroup (' . $this->accountPrimaryGroup . '): ' . $e->getMessage() . ' => set default user group id as accountPrimaryGroup for account ' . $this->getId());
$this->accountPrimaryGroup = Tinebase_Group::getInstance()->getDefaultGroup()->getId();
}
return $this->accountPrimaryGroup;
}
/**
* returns true if this record should be replicated
*
* @return boolean
*/
public function isReplicable()
{
return static::$_replicable;
}
/**
* @param boolean $isReplicable
*/
public static function setReplicable($isReplicable)
{
static::$_replicable = (bool)$isReplicable;
}
/**
* @param bool $bool
*/
public static function forceSuperUser($bool = true)
{
static::$_forceSuperUser = (bool)$bool;
}
/**
* @return string
*/
public function getTitle()
{
return $this->accountDisplayName;
}
}