User.inc
14.7 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
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
<?php
/**
* $Id$
*
* Represents a user as per the users table in the database.
*
* The contents of this file are subject to the KnowledgeTree Public
* License Version 1.1.2 ("License"); You may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.knowledgetree.com/KPL
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights and
* limitations under the License.
*
* All copies of the Covered Code must include on each user interface screen:
* (i) the "Powered by KnowledgeTree" logo and
* (ii) the KnowledgeTree copyright notice
* in the same form as they appear in the distribution. See the License for
* requirements.
*
* The Original Code is: KnowledgeTree Open Source
*
* The Initial Developer of the Original Code is The Jam Warehouse Software
* (Pty) Ltd, trading as KnowledgeTree.
* Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
* (C) 2007 The Jam Warehouse Software (Pty) Ltd;
* All Rights Reserved.
* Contributor( s): ______________________________________
*/
require_once(KT_LIB_DIR . '/database/dbutil.inc');
require_once(KT_LIB_DIR . '/ktentity.inc');
define('ADMIN_USER_ID', 1);
class User extends KTEntity {
/** user's login name */
var $sUserName;
/** user's name (first and last) */
var $sName;
/** user's password */
var $sPassword;
/** user's maximum allowed file storage quota in bytes */
var $iQuotaMax = 1234567890;
/** user's current file storage quota in bytes */
var $iQuotaCurrent = 0;
/** user's email address */
var $sEmail = "";
/** user's mobile phone number */
var $sMobile = "";
/** notify user by mail status */
var $bEmailNotification = false;
/** notify user via sms (mobile phone) status */
var $bSmsNotification = false;
/** maxiumum concurrent sessions user may have */
var $iMaxSessions = 5;
/** primary key of language preferred by user */
var $iLanguageID;
/** internal variable used to determine if the password has changed or not */
var $bPasswordChanged = false;
/** authentication source for this user (built-in if null) */
var $iAuthenticationSourceId = null;
/** authentication details so that the source knows who this user is */
var $sAuthenticationDetails = null;
var $sAuthenticationDetails2 = null;
var $iAuthenticationDetailsInt1 = null;
var $iAuthenticationDetailsInt2 = null;
var $dAuthenticationDetailsDate1 = null;
var $dAuthenticationDetailsDate2 = null;
var $bAuthenticationDetailsBool1 = null;
var $bAuthenticationDetailsBool2 = null;
var $dLastLogin = null;
var $bDisabled = false;
var $_aFieldToSelect = array(
'iId' => 'id',
'sUserName' => 'username',
'sName' => 'name',
'sPassword' => 'password',
'iQuotaMax' => 'quota_max',
'iQuotaCurrent' => 'quota_current',
'sEmail' => 'email',
'sMobile' => 'mobile',
'bEmailNotification' => 'email_notification',
'bSmsNotification' => 'sms_notification',
'iMaxSessions' => 'max_sessions',
'iLanguageID' => 'language_id',
'iAuthenticationSourceId' => 'authentication_source_id',
'sAuthenticationDetails' => 'authentication_details_s1',
'sAuthenticationDetails2' => 'authentication_details_s2',
'iAuthenticationDetailsInt1' => 'authentication_details_i1',
'iAuthenticationDetailsInt2' => 'authentication_details_i2',
'dAuthenticationDetailsDate1' => 'authentication_details_d1',
'dAuthenticationDetailsDate2' => 'authentication_details_d2',
'bAuthenticationDetailsBool1' => 'authentication_details_b1',
'bAuthenticationDetailsBool2' => 'authentication_details_b2',
'dLastLogin' => 'last_login',
'bDisabled' => 'disabled',
);
var $_bUsePearError = true;
function _table() {
return KTUtil::getTableName("users");
}
// STATIC
function _ktentityOptions() {
return array(
'orderby' => 'name',
);
}
function getUserName() { return $this->sUserName; }
function setUserName($sNewValue) { $this->sUserName = $sNewValue; }
function getPassword() { return $this->sPassword; }
function setPassword($sNewValue) { $this->sPassword = $sNewValue; $this->bPasswordChanged = true; }
function getQuotaMax() { return $this->iQuotaMax; }
function setQuotaMax($iNewValue) { $this->iQuotaMax = $iNewValue; }
function setName($sNewValue) { $this->sName = $sNewValue; }
function getName() { return $this->sName; }
function getQuotaCurrent() { return $this->iQuotaCurrent; }
function getEmail() { return $this->sEmail; }
function setEmail($sNewValue) { $this->sEmail = $sNewValue; }
function getMobile() { return $this->sMobile; }
function setMobile($sNewValue) { $this->sMobile = $sNewValue; }
function getEmailNotification() { return $this->bEmailNotification; }
function setEmailNotification($bNewValue) { $this->bEmailNotification = KTUtil::anyToBool($bNewValue); }
function getSmsNotification() { return $this->bSmsNotification; }
function setSmsNotification($bNewValue) { $this->bSmsNotification = $bNewValue; }
function getMaxSessions() { return $this->iMaxSessions; }
function setMaxSessions($iNewValue) { $this->iMaxSessions = $iNewValue; }
function getLanguageID() { return $this->iLanguageIDID; }
function setLanguageID($iNewValue) { $this->iLanguageIDID = $iNewValue; }
function getAuthenticationSourceId() { return $this->iAuthenticationSourceId; }
function setAuthenticationSourceId($iNewValue) { $this->iAuthenticationSourceId = $iNewValue; }
function getAuthenticationDetails() { return $this->sAuthenticationDetails; }
function setAuthenticationDetails($sNewValue) { $this->sAuthenticationDetails = $sNewValue; }
function getAuthenticationDetails2() { return $this->sAuthenticationDetails2; }
function setAuthenticationDetails2($sNewValue) { $this->sAuthenticationDetails2 = $sNewValue; }
function getAuthenticationDetailsInt1() { return $this->iAuthenticationDetailsInt1; }
function setAuthenticationDetailsInt1($mValue) { $this->iAuthenticationDetailsInt1 = $mValue; }
function getAuthenticationDetailsInt2() { return $this->iAuthenticationDetailsInt2; }
function setAuthenticationDetailsInt2($mValue) { $this->iAuthenticationDetailsInt2 = $mValue; }
function getAuthenticationDetailsDate1() { return $this->dAuthenticationDetailsDate1; }
function setAuthenticationDetailsDate1($mValue) { $this->dAuthenticationDetailsDate1 = $mValue; }
function getAuthenticationDetailsDate2() { return $this->dAuthenticationDetailsDate2; }
function setAuthenticationDetailsDate2($mValue) { $this->dAuthenticationDetailsDate2 = $mValue; }
function getAuthenticationDetailsBool1() { return $this->bAuthenticationDetailsBool1; }
function setAuthenticationDetailsBool1($mValue) { $this->bAuthenticationDetailsBool1 = $mValue; }
function getAuthenticationDetailsBool2() { return $this->bAuthenticationDetailsBool2; }
function setAuthenticationDetailsBool2($mValue) { $this->bAuthenticationDetailsBool2 = $mValue; }
function getLastLogin() { return $this->dLastLogin; }
function setLastLogin($mValue) { $this->dLastLogin = $mValue; }
function getDisabled() { return $this->bDisabled; }
function setDisabled($mValue) { $this->bDisabled = $mValue; }
/* return the key for storing dashboard in system settings */
function _getDashboardStateKey() {
return 'dashboard-state-' . $this->getId();
}
function getDashboardState() {
return KTUtil::getSystemSetting($this->_getDashboardStateKey());
}
function setDashboardState($mValue) {
KTUtil::setSystemSetting($this->_getDashboardStateKey(), $mValue);
}
function refreshDashboadState()
{
require_once(KT_DIR . "/thirdparty/pear/JSON.php");
$dashletRegistry = & KTDashletRegistry::getSingleton();
$aDashlets = $dashletRegistry->getDashlets($this);
$oJSON = new Services_JSON();
$state = $this->getDashboardState();
$dashlets = $oJSON->decode($state);
if (!isset($dashlets->left)) $dashlets->left = array();
if (!isset($dashlets->right)) $dashlets->right = array();
$mergedlist = array_merge($dashlets->left,$dashlets->right);
$knownlist = array();
foreach($mergedlist as $dashlet)
{
array_push($knownlist,$dashlet->id);
}
$update=false;
//if (!isset($dashlets->left)) $dashlets->left=array();
//if (!isset($dashlets->right)) $dashlets->right=array();
$column=1;
foreach($aDashlets as $dashlet)
{
$class = get_class($dashlet);
$column=($column + 1) %2;
if (!in_array($class,$knownlist))
{
$obj = new stdClass();
$obj->id=$class;
$obj->state=0;
if ($column == 0)
array_push($dashlets->left,$obj);
else
array_push($dashlets->right,$obj);
$update=true;
}
}
if ($update)
{
$state = $oJSON->encode($dashlets);
$this->setDashboardState($state);
}
}
function &get($iId) {
return KTEntityUtil::get('User', $iId);
}
/**
* update the datastore, without overwriting the password.
*
* only works for a subset of the db values.
*/
function doLimitedUpdate() {
$sQuery = 'UPDATE ' . $this->_table() . ' SET ';
$aParams = array();
$blacklist = array(
"sPassword" => 1,
);
$aParts = array(); // quick workaround to make the join less hurtful.
foreach ($this->_aFieldToSelect as $attr => $column) {
if (!array_key_exists($attr, $blacklist)) {
$val = $this->$attr;
$aParts[] = $column . ' = ?';
$aParams[] = $val;
}
}
$sQuery .= join(', ', $aParts);
$sQuery .= ' WHERE id = ? ';
$aParams[] = $this->getId();
$res = DBUtil::runQuery(array($sQuery, $aParams));
$group = sprintf("%s/%s", get_class($this), 'id');
$oCache =& KTCache::getSingleton();
$oCache->remove($group, $this->iId);
$this->clearCachedGroups();
return $res;
}
/**
* Static function
* Get a list of users
*
* @param String Where clause (not required)
*
* @return Array array of User objects, false otherwise and set $_SESSION["errorMessage"]
*/
function getList($sWhereClause = null, $aOptions = null) {
if(!is_array($aOptions)) $aOptions = array($aOptions);
$aOptions['orderby'] = KTUtil::arrayGet($aOptions, 'orderby', 'name');
return KTEntityUtil::getList2('User', $sWhereClause, $aOptions);
}
function getEmailUsers($sWhereClause = null) {
$aUsers = array();
foreach (User::getList($sWhereClause) as $oUser) {
if ($oUser->getEmail()) {
$aUsers[] = $oUser;
}
}
return $aUsers;
}
/**
* Return the useID for the user
*
* @return int the unitID, false otherwise and $_SESSION["errorMessage"] set
*/
function getUnitId() {
$ugl = KTUtil::getTableName("users_groups");
$g = KTUtil::getTableName("groups");
$aQuery = array(
"SELECT DISTINCT g.unit_id AS unit_id FROM $ugl AS ugl INNER JOIN $g AS g ON ugl.group_id = g.id WHERE ugl.user_id = ?",
array($this->iId),
);
return DBUtil::getOneResultKey($aQuery, 'unit_id');
}
/**
* static function
*
* gets the id of a user using their username
*
* @param string The username for which we want its ID
*/
function getUserID($sUsername) {
global $default;
$id = lookupID($default->users_table, "username", $sUsername);
$this->iId = $id;
}
/** Static function
* Gets the user's default top level folder for the current user
*/
function getHomeFolderID() {
$iUnitId = $this->getUnitId();
if (empty($iUnitId)) {
return false;
}
$oUnit =& Unit::get($iUnitId);
return $oUnit->getFolderId();
}
function &createFromArray($aOptions) { return KTEntityUtil::createFromArray('User', $aOptions); }
function &getByUserName($sUserName, $aOptions = null) {
return KTEntityUtil::getBy('User', 'username', $sUserName, $aOptions);
}
function getByAuthenticationSource($oSource, $aOptions = null) {
$iSourceId = KTUtil::getId($oSource);
$aOptions = KTUtil::meldOptions($aOptions, array(
'multi' => true,
));
return KTEntityUtil::getByDict('User', array(
'authentication_source_id' => $iSourceId,
), $aOptions);
}
function &getByAuthenticationSourceAndDetails($oSource, $sDetails, $aOptions = null) {
$iSourceId = KTUtil::getId($oSource);
return KTEntityUtil::getByDict('User', array(
'authentication_source_id' => $iSourceId,
'authentication_details_s1' => $sDetails,
), $aOptions);
}
function getByLastLoginBefore($dDateTime) {
return KTEntityUtil::getByDict('User', array(
'last_login' => array('type' => 'before', 'value' => $dDateTime),
), array('multi' => true));
}
function getByLastLoginAfter($dDateTime) {
return KTEntityUtil::getByDict('User', array(
'last_login' => array('type' => 'after', 'value' => $dDateTime),
), array('multi' => true));
}
function getNumberEnabledUsers() {
$sQuery = sprintf('SELECT COUNT(id) AS number FROM %s WHERE disabled = ? AND id > 1', KTUtil::getTableName('users'));
$aParams = array(false);
return DBUtil::getOneResultKey(array($sQuery, $aParams), 'number');
}
function isAnonymous() { return $this->iId == -2; }
function disable() {
$this->setDisabled(true);
$this->update();
if (KTPluginUtil::pluginIsActive('ktdms.wintools')) {
require_once(KT_DIR . '/plugins/wintools/baobabkeyutil.inc.php');
BaobabKeyUtil::deallocateUser($this);
}
return;
}
function enable() {
$this->setDisabled(false);
$this->update();
if (KTPluginUtil::pluginIsActive('ktdms.wintools')) {
require_once(KT_DIR . '/plugins/wintools/baobabkeyutil.inc.php');
BaobabKeyUtil::allocateUser($this);
}
return;
}
function create() {
if (KTPluginUtil::pluginIsActive('ktdms.wintools')) {
require_once(KT_DIR . '/plugins/wintools/baobabkeyutil.inc.php');
$res = BaobabKeyUtil::canAddUser();
if (PEAR::isError($res)) {
return $res;
}
}
return parent::create();
}
}