-
Notifications
You must be signed in to change notification settings - Fork 988
/
User.php
267 lines (244 loc) · 6.93 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
<?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.
*
* $Id$
*/
class User extends CActiveRecord
{
/**
* 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, full_name', '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;
}
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;
}
/**
* 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;
$iRecordsAffected = Yii::app()->db->createCommand()->from('{{users}}')->delete('{{users}}', "uid={$iUserID}");
return (bool) $iRecordsAffected;
}
/**
* 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)
{
return Yii::app()->db->createCommand()->select('full_name')->from('{{users}}')->where("uid = :userid")->bindParam(":userid", $userid, PDO::PARAM_INT)->queryAll();
}
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($fullname)
{
$this->db->select('uid');
$this->db->from('users');
$this->db->where(array("full_name"=>Yii::app()->db->quoteValue($fullname)));
$result = $this->db->get();
return $result->row();
}
/**
* Updates user password
*
* @access public
* @return string
*/
public function updatePassword($uid,$password)
{
return $this->updateByPk($uid, array('password' => $password));
}
/**
* 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 {{survey_permissions}} WHERE sid = :surveyid) 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
}
}