/
User.php
500 lines (461 loc) · 17.3 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<?php
/*
* LimeSurvey
* Copyright (C) 2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
*/
class User extends LSActiveRecord
{
/**
* @var string Default value for user language
*/
public $lang='auto';
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param string $class
* @return User
*/
public static function model($class = __CLASS__)
{
return parent::model($class);
}
/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{users}}';
}
/**
* Returns the primary key of this table
*
* @access public
* @return string
*/
public function primaryKey()
{
return 'uid';
}
/**
* Defines several rules for this table
*
* @access public
* @return array
*/
public function rules()
{
return array(
array('users_name, password, email', 'required'),
array('email', 'email'),
);
}
/**
* Returns all users
*
* @access public
* @return string
*/
public function getAllRecords($condition=FALSE)
{
$criteria = new CDbCriteria;
if ($condition != FALSE)
{
foreach ($condition as $item => $value)
{
$criteria->addCondition($item.'='.Yii::app()->db->quoteValue($value));
}
}
$data = $this->findAll($criteria);
return $data;
}
/**
*
*
* @param mixed $postuserid
*/
function parentAndUser($postuserid)
{
$user = Yii::app()->db->createCommand()
->select('a.users_name, a.full_name, a.email, a.uid, b.users_name AS parent')
->limit(1)
->where('a.uid = :postuserid')
->from("{{users}} a")
->leftJoin('{{users}} AS b', 'a.parent_id = b.uid')
->bindParam(":postuserid", $postuserid, PDO::PARAM_INT)
->queryRow();
return $user;
}
public function getParentUser(){
$parent_user = $this->parentAndUser( $this->uid );
return $parent_user['parent'];
}
public function getSurveysCreated(){
$noofsurveys = Survey::model()->countByAttributes(array("owner_id" =>$this->uid));
return $noofsurveys;
}
/**
* Returns onetime password
*
* @access public
* @return string
*/
public function getOTPwd($user)
{
$this->db->select('uid, users_name, password, one_time_pw, dateformat, full_name, htmleditormode');
$this->db->where('users_name',$user);
$data = $this->db->get('users',1);
return $data;
}
/**
* Deletes onetime password
*
* @access public
* @return string
*/
public function deleteOTPwd($user)
{
$data = array(
'one_time_pw' => ''
);
$this->db->where('users_name',$user);
$this->db->update('users',$data);
}
/**
* Creates new user
*
* @access public
* @return string
*/
public static function insertUser($new_user, $new_pass,$new_full_name,$parent_user,$new_email)
{
$oUser = new self;
$oUser->users_name = $new_user;
$oUser->password = hash('sha256', $new_pass);
$oUser->full_name = $new_full_name;
$oUser->parent_id = $parent_user;
$oUser->lang = 'auto';
$oUser->email = $new_email;
if ($oUser->save())
{
return $oUser->uid;
}
else{
return false;
}
}
/**
* This method is invoked before saving a record (after validation, if any).
* The default implementation raises the {@link onBeforeSave} event.
* You may override this method to do any preparation work for record saving.
* Use {@link isNewRecord} to determine whether the saving is
* for inserting or updating record.
* Make sure you call the parent implementation so that the event is raised properly.
* @return boolean whether the saving should be executed. Defaults to true.
*/
public function beforeSave()
{
// Postgres delivers bytea fields as streams :-o - if this is not done it looks like Postgres saves something unexpected
if (gettype($this->password)=='resource')
{
$this->password=stream_get_contents($this->password,-1,0);
}
return parent::beforeSave();
}
/**
* Delete user
*
* @param int $iUserID The User ID to delete
* @return mixed
*/
function deleteUser($iUserID)
{
$iUserID= (int)$iUserID;
$oUser=$this->findByPk($iUserID);
return (bool) $oUser->delete();
}
/**
* Returns user share settings
*
* @access public
* @return string
*/
public function getShareSetting()
{
$this->db->where(array("uid"=>$this->session->userdata('loginID')));
$result= $this->db->get('users');
return $result->row();
}
/**
* Returns full name of user
*
* @access public
* @return string
*/
public function getName($userid)
{
static $aOwnerCache = array();
if (array_key_exists($userid, $aOwnerCache)) {
$result = $aOwnerCache[$userid];
} else {
$result = Yii::app()->db->createCommand()->select('full_name')->from('{{users}}')->where("uid = :userid")->bindParam(":userid", $userid, PDO::PARAM_INT)->queryAll();
$aOwnerCache[$userid] = $result;
}
return $result;
}
public function getuidfromparentid($parentid)
{
return Yii::app()->db->createCommand()->select('uid')->from('{{users}}')->where('parent_id = :parent_id')->bindParam(":parent_id", $parentid, PDO::PARAM_INT)->queryRow();
}
/**
* Returns id of user
*
* @access public
* @return string
*/
public function getID($sUserName)
{
$oUser = User::model()->findByAttributes(array(
'users_name' => $sUserName
));
if ($oUser)
{
return $oUser->uid;
}
}
/**
* Updates user password hash
*
* @param int $iUserID The User ID
* @param string $sPassword The clear text password
*/
public function updatePassword($iUserID, $sPassword)
{
return $this->updateByPk($iUserID, array('password' => hash('sha256', $sPassword)));
}
/**
* Adds user record
*
* @access public
* @return string
*/
public function insertRecords($data)
{
return $this->db->insert('users',$data);
}
/**
* Returns User ID common in Survey_Permissions and User_in_groups
*
* @access public
* @return CDbDataReader Object
*/
public function getCommonUID($surveyid, $postusergroupid)
{
$query2 = "SELECT b.uid FROM (SELECT uid FROM {{permissions}} WHERE entity_id = :surveyid AND entity = 'survey') AS c RIGHT JOIN {{user_in_groups}} AS b ON b.uid = c.uid WHERE c.uid IS NULL AND b.ugid = :postugid";
return Yii::app()->db->createCommand($query2)->bindParam(":surveyid", $surveyid, PDO::PARAM_INT)->bindParam(":postugid", $postusergroupid, PDO::PARAM_INT)->query(); //Checked
}
public function relations()
{
return array(
'permissions' => array(self::HAS_MANY, 'Permission', 'uid')
);
}
/**
* Return all super admins in the system
* @return User[]
*/
public function getSuperAdmins()
{
$criteria = new CDbCriteria();
$criteria->join = ' JOIN {{permissions}} AS p ON p.uid = t.uid';
$criteria->addCondition('p.permission = \'superadmin\'');
$users = $this->findAll($criteria);
return $users;
}
/**
* Gets the buttons for the GridView
*/
public function getButtons(){
$editUser = "";
$deleteUser = "";
$setPermissionsUser = "";
$setTemplatePermissionUser = "";
$changeOwnership = "";
$editUrl = Yii::app()->getController()->createUrl('admin/user/sa/modifyuser');
$setPermissionsUrl = Yii::app()->getController()->createUrl('admin/user/sa/setuserpermissions');
$setTemplatePermissionsUrl = Yii::app()->getController()->createUrl('admin/user/sa/setusertemplates');
$changeOwnershipUrl = Yii::app()->getController()->createUrl('admin/user/sa/setasadminchild');
if($this->uid == Yii::app()->user->getId())
{
$editUser = "<button
data-toggle='tooltip'
title='".gT("Edit this user")."'
data-url='".$editUrl."'
data-uid='".$this->uid."'
data-user='".$this->getName($this->uid)[0]['full_name']."'
data-action='modifyuser'
class='btn btn-default btn-xs action_usercontrol_button'>
<span class='fa fa-pencil text-success'></span>
</button>";
if ($this->parent_id != 0 && Permission::model()->hasGlobalPermission('users','delete') )
{
$deleteUser = "<button
data-toggle='modal'
data-href='#'
data-onclick='$.post(".$deleteUrl.",
{action: \"deluser\", uid:\"".$this->uid."\", user: \"".htmlspecialchars($this->getName($this->uid)[0]['full_name'])."\"});'
data-target='#confirmation-modal'
data-uid='".$this->uid."'
data-action='deluser'
data-message='".gT("Delete this user")."'
class='btn btn-default btn-xs'>
<span class='fa fa-trash text-danger'></span>
</button>";
}
} else {
if (Permission::model()->hasGlobalPermission('superadmin','read')
|| $this->uid == Yii::app()->session['loginID']
|| (Permission::model()->hasGlobalPermission('users','update')
&& $this->parent_id == Yii::app()->session['loginID']))
{
$editUser = "<button data-toggle='tooltip' data-url='".$editUrl."' data-user='".$this->getName($this->uid)[0]['full_name']."' data-uid='".$this->uid."' data-action='modifyuser' title='".gT("Edit this user")."' type='submit' class='btn btn-default btn-xs action_usercontrol_button'><span class='fa fa-pencil text-success'></span></button>";
}
if (((Permission::model()->hasGlobalPermission('superadmin','read') &&
$this->uid != Yii::app()->session['loginID'] ) ||
(Permission::model()->hasGlobalPermission('users','update') &&
$this->parent_id == Yii::app()->session['loginID'])) && $this->uid!=1)
{
//'admin/user/sa/setuserpermissions'
$setPermissionsUser = "<button data-toggle='tooltip' data-user='".$this->getName($this->uid)[0]['full_name']."' data-url='".$setPermissionsUrl."' data-uid='".$this->uid."' data-action='setuserpermissions' title='".gT("Set global permissions for this user")."' type='submit' class='btn btn-default btn-xs action_usercontrol_button'><span class='icon-security text-success'></span></button>";
}
if ((Permission::model()->hasGlobalPermission('superadmin','read')
|| Permission::model()->hasGlobalPermission('templates','read'))
&& $this->uid!=1)
{
//'admin/user/sa/setusertemplates')
$setTemplatePermissionUser = "<button type='submit' data-user='".$this->getName($this->uid)[0]['full_name']."' data-url='".$setTemplatePermissionsUrl."' data-uid='".$this->uid."' data-action='setusertemplates' data-toggle='tooltip' title='".gT("Set template permissions for this user")."' class='btn btn-default btn-xs action_usercontrol_button'><span class='icon-templatepermissions text-success'></span></button>";
}
if ((Permission::model()->hasGlobalPermission('superadmin','read')
|| (Permission::model()->hasGlobalPermission('users','delete')
&& $this->parent_id == Yii::app()->session['loginID'])) && $this->uid!=1)
{
$deleteUrl = Yii::app()->getController()->createUrl('admin/user/sa/deluser', array(
"action"=> "deluser",
"uid"=>$this->uid,
"user" => htmlspecialchars(Yii::app()->user->getId())
));
//'admin/user/sa/deluser'
$deleteUser = "<button
id='delete_user_".$this->uid."'
data-toggle='modal'
data-target='#confirmation-modal'
data-url='".$deleteUrl."'
data-uid='".$this->uid."'
data-user='".$this->getName($this->uid)[0]['full_name']."'
data-action='deluser'
data-onclick='triggerRunAction($(\"#delete_user_".$this->uid."\"))'
data-message='".gT("Delete this user")."'
class='btn btn-default btn-xs '>
<span class='fa fa-trash text-danger'></span>
</button>";
}
if (Yii::app()->session['loginID'] == "1" && $this->parent_id !=1 ) {
//'admin/user/sa/setasadminchild'
$changeOwnership = "<button data-toggle='tooltip' data-url='".$changeOwnershipUrl."' data-user='".$this->getName($this->uid)[0]['full_name']."' data-uid='".$this->uid."' data-action='setasadminchild' title='".gT("Take ownership")."' class='btn btn-default btn-sm action_usercontrol_button' type='submit'><span class='icon-takeownership text-success'></span></button>";
}
}
return "<div>"
. $editUser
. $deleteUser
. $setPermissionsUser
. $setTemplatePermissionUser
. $changeOwnership
. "</div>";
}
public function getColums(){
$cols = array(
array(
"name" => 'buttons',
"type" => 'raw',
"header" => gT("Action")
),
array(
"name" => 'uid',
"header" => gT("User ID")
),
array(
"name" => 'users_name',
"header" => gT("Username")
),
array(
"name" => 'email',
"header" => gT("Email")
),
array(
"name" => 'full_name',
"header" => gT("Full name")
)
);
if(Permission::model()->hasGlobalPermission('superadmin','read')) {
$cols[] = array(
"name" => 'surveysCreated',
"header" => gT("No of surveys")
);
}
$cols[] = array(
"name" => 'parentUser',
"header" => gT("Created by")
);
return $cols;
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$pageSize = Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);
$criteria=new CDbCriteria;
// $criteria->compare('uid',$this->uid);
// $criteria->compare('users_name',$this->users_name,true);
// $criteria->compare('password',$this->password,true);
// $criteria->compare('full_name',$this->full_name,true);
// $criteria->compare('parent_id',$this->parent_id);
// $criteria->compare('lang',$this->lang,true);
// $criteria->compare('email',$this->email,true);
// $criteria->compare('htmleditormode',$this->htmleditormode,true);
// $criteria->compare('templateeditormode',$this->templateeditormode,true);
// $criteria->compare('questionselectormode',$this->questionselectormode,true);
// $criteria->compare('one_time_pw',$this->one_time_pw,true);
// $criteria->compare('dateformat',$this->dateformat);
// $criteria->compare('created',$this->created,true);
// $criteria->compare('modified',$this->modified,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => array(
'pageSize' => $pageSize
)
));
}
}