1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This is the model class for table "users". |
5
|
|
|
* |
6
|
|
|
* The followings are the available columns in table 'users': |
7
|
|
|
* @property integer $id |
8
|
|
|
* @property string $email |
9
|
|
|
* @property string $password |
10
|
|
|
* @property string $firstName |
11
|
|
|
* @property string $lastName |
12
|
|
|
* @property string $username |
13
|
|
|
* @property string $about |
14
|
|
|
* @property integer $user_role |
15
|
|
|
* @property integer $status |
16
|
|
|
* @property string $created |
17
|
|
|
* @property string $updated |
18
|
|
|
* |
19
|
|
|
* The followings are the available model relations: |
20
|
|
|
* @property Comments[] $comments |
21
|
|
|
* @property Content[] $contents |
22
|
|
|
* @property Tags[] $tags |
23
|
|
|
* @property UserMetadata[] $userMetadatas |
24
|
|
|
* @property UserRoles $userRole |
25
|
|
|
*/ |
26
|
|
|
class Users extends CiiModel |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
const INACTIVE = 0; |
29
|
|
|
const ACTIVE = 1; |
30
|
|
|
const BANNED = 2; |
31
|
|
|
const PENDING_INVITATION = 3; |
32
|
|
|
|
33
|
|
|
public $pageSize = 15; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the static model of the specified AR class. |
37
|
|
|
* @param string $className active record class name. |
38
|
|
|
* @return Users the static model class |
39
|
|
|
*/ |
40
|
|
|
public static function model($className=__CLASS__) |
41
|
|
|
{ |
42
|
|
|
return parent::model($className); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string the associated database table name |
47
|
|
|
*/ |
48
|
|
|
public function tableName() |
49
|
|
|
{ |
50
|
|
|
return 'users'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array validation rules for model attributes. |
55
|
|
|
*/ |
56
|
|
|
public function rules() |
57
|
|
|
{ |
58
|
|
|
// NOTE: you should only define rules for those attributes that |
59
|
|
|
// will receive user inputs. |
60
|
|
|
return array( |
61
|
|
|
array('email, password, username, user_role, status', 'required'), |
62
|
|
|
array('email', 'email'), |
63
|
|
|
array('user_role, status', 'numerical', 'integerOnly'=>true), |
64
|
|
|
array('email, username', 'length', 'max'=>255), |
65
|
|
|
array('password', 'length', 'max'=>64), |
66
|
|
|
// The following rule is used by search(). |
67
|
|
|
array('id, email, password, username, about, user_role, status, created, updated', 'safe', 'on'=>'search'), |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array relational rules. |
73
|
|
|
*/ |
74
|
|
|
public function relations() |
75
|
|
|
{ |
76
|
|
|
// NOTE: you may need to adjust the relation name and the related |
77
|
|
|
// class name for the relations automatically generated below. |
78
|
|
|
return array( |
79
|
|
|
'comments' => array(self::HAS_MANY, 'Comments', 'user_id'), |
80
|
|
|
'content' => array(self::HAS_MANY, 'Content', 'author_id'), |
81
|
|
|
'metadata' => array(self::HAS_MANY, 'UserMetadata', 'user_id', 'condition' => '`metadata`.`entity_type` = 0'), |
82
|
|
|
'role' => array(self::BELONGS_TO, 'UserRoles', 'user_role'), |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array customized attribute labels (name=>label) |
88
|
|
|
*/ |
89
|
|
|
public function attributeLabels() |
90
|
|
|
{ |
91
|
|
|
return array( |
92
|
|
|
'id' => Yii::t('ciims.models.Users', 'ID'), |
93
|
|
|
'email' => Yii::t('ciims.models.Users', 'Email'), |
94
|
|
|
'password' => Yii::t('ciims.models.Users', 'Password'), |
95
|
|
|
'username' => Yii::t('ciims.models.Users', 'User Name'), |
96
|
|
|
'user_role' => Yii::t('ciims.models.Users', 'User Role'), |
97
|
|
|
'status' => Yii::t('ciims.models.Users', 'Active'), |
98
|
|
|
'created' => Yii::t('ciims.models.Users', 'Created'), |
99
|
|
|
'updated' => Yii::t('ciims.models.Users', 'Updated'), |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets the first and last name instead of the username |
105
|
|
|
*/ |
106
|
|
|
public function getName() |
107
|
|
|
{ |
108
|
|
|
return $this->username; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieves the username |
113
|
|
|
*/ |
114
|
|
|
public function getUsername() |
115
|
|
|
{ |
116
|
|
|
return $this->username; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retrieves the username |
121
|
|
|
* @todo This is technical debt. At some point this should be completely depricated |
|
|
|
|
122
|
|
|
*/ |
123
|
|
|
public function getDisplayName() |
124
|
|
|
{ |
125
|
|
|
Yii::log('Users::displayName has been deprecated. Use Users::username moving forward', 'warning', 'models.users'); |
126
|
|
|
return $this->getUsername(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieves the reputation for a given user |
131
|
|
|
* @param boolean $model Whether an instance of UserMetadata should be returned or not |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function getReputation($model=false) |
135
|
|
|
{ |
136
|
|
|
$reputation = UserMetadata::model()->getPrototype('UserMetadata', array('user_id' => $this->id, 'key' => 'reputation'), array('value' => 150)); |
137
|
|
|
|
138
|
|
|
if ($model === true) |
139
|
|
|
return $reputation; |
140
|
|
|
|
141
|
|
|
return $reputation->value; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Updates a user's reputation |
146
|
|
|
* @return boolean |
147
|
|
|
*/ |
148
|
|
|
public function setReputation($rep = 10) |
149
|
|
|
{ |
150
|
|
|
$reputation = $this->getReputation(true); |
151
|
|
|
$reputation->value += $rep; |
152
|
|
|
return $reputation->save(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Retrieves all comments that the user has flagged |
157
|
|
|
* @param boolean $model Whether an instance of UserMetadata should be returned or not |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
|
|
public function getFlaggedComments($model=false) |
161
|
|
|
{ |
162
|
|
|
$flags = UserMetadata::model()->getPrototype('UserMetadata', array('user_id' => $this->id, 'key' => 'flaggedComments'), array('value' => CJSON::encode(array()))); |
163
|
|
|
|
164
|
|
|
if ($model === true) |
165
|
|
|
return $flags; |
166
|
|
|
|
167
|
|
|
return CJSON::decode($flags->value); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Flags a comment with a given ID |
172
|
|
|
* @return boolean |
173
|
|
|
*/ |
174
|
|
|
public function flagComment($id) |
175
|
|
|
{ |
176
|
|
|
$flaggedComments = $this->getFlaggedComments(true); |
177
|
|
|
$flags = CJSON::decode($flaggedComments->value); |
178
|
|
|
|
179
|
|
|
// If the comment has already been flagged, just return true |
180
|
|
|
if (in_array($id, $flags)) |
181
|
|
|
return true; |
182
|
|
|
|
183
|
|
|
$flags[] = $id; |
184
|
|
|
$flaggedComments->value = CJSON::encode($flags); |
185
|
|
|
|
186
|
|
|
return $flaggedComments->save(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Retrieves a list of models based on the current search/filter conditions. |
191
|
|
|
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. |
192
|
|
|
*/ |
193
|
|
|
public function search() |
194
|
|
|
{ |
195
|
|
|
$criteria=new CDbCriteria; |
196
|
|
|
|
197
|
|
|
$criteria->compare('id',$this->id); |
198
|
|
|
$criteria->compare('email',$this->email,true); |
199
|
|
|
$criteria->compare('password',$this->password,true); |
200
|
|
|
$criteria->compare('username',$this->username,true); |
201
|
|
|
$criteria->compare('user_role',$this->user_role); |
202
|
|
|
$criteria->compare('status',$this->status); |
203
|
|
|
$criteria->compare('created',$this->created,true); |
204
|
|
|
$criteria->compare('updated',$this->updated,true); |
205
|
|
|
$criteria->order = "user_role DESC, created DESC"; |
|
|
|
|
206
|
|
|
|
207
|
|
|
return new CActiveDataProvider($this, array( |
208
|
|
|
'criteria' => $criteria, |
209
|
|
|
'pagination' => array( |
210
|
|
|
'pageSize' => $this->pageSize |
211
|
|
|
) |
212
|
|
|
)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Sets some default values for the user record. |
217
|
|
|
* @see CActiveRecord::beforeValidate() |
218
|
|
|
**/ |
219
|
|
|
public function beforeValidate() |
220
|
|
|
{ |
221
|
|
|
// If the password is nulled, or unchanged |
222
|
|
|
if ($this->password == NULL || $this->password == Cii::get($this->_oldAttributes, 'password', false)) |
223
|
|
|
{ |
224
|
|
|
if (!$this->isNewRecord) |
225
|
|
|
$this->password = $this->_oldAttributes['password']; |
226
|
|
|
} |
227
|
|
|
else |
228
|
|
|
{ |
229
|
|
|
$this->password = password_hash($this->password, PASSWORD_BCRYPT, array('cost' => Cii::getBcryptCost())); |
230
|
|
|
|
231
|
|
|
if (!$this->isNewRecord) |
232
|
|
|
{ |
233
|
|
|
$emailSettings = new EmailSettings; |
234
|
|
|
$emailSettings->send( |
235
|
|
|
$this, |
236
|
|
|
Yii::t('ciims.models.Users', 'CiiMS Password Change Notification'), |
237
|
|
|
'base.themes.' . Cii::getConfig('theme', 'default') .'.views.email.passwordchange', |
238
|
|
|
array('user' => $this) |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return parent::beforeValidate(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Lets us know if the user likes a given content post or not |
248
|
|
|
* @param int $id The id of the content we want to know about |
249
|
|
|
* @return bool Whether or not the user likes the post |
250
|
|
|
*/ |
251
|
|
|
public function likesPost($id = NULL) |
252
|
|
|
{ |
253
|
|
|
if ($id === NULL) |
254
|
|
|
return false; |
255
|
|
|
|
256
|
|
|
$likes = UserMetadata::model()->findByAttributes(array('user_id' => $this->id, 'key' => 'likes')); |
257
|
|
|
|
258
|
|
|
if ($likes === NULL) |
259
|
|
|
return false; |
260
|
|
|
|
261
|
|
|
$likesArray = json_decode($likes->value, true); |
262
|
|
|
if (in_array($id, array_values($likesArray))) |
263
|
|
|
return true; |
264
|
|
|
|
265
|
|
|
return false; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Helper method to get the usermetadata object rather than calling getPrototype everywhere |
270
|
|
|
* @param string $key |
271
|
|
|
* @param mixed $value |
272
|
|
|
* @return UserMetadata prototype object |
273
|
|
|
*/ |
274
|
|
|
public function getMetadataObject($key, $value=NULL) |
275
|
|
|
{ |
276
|
|
|
return UserMetadata::model()->getPrototype('UserMetadata', array( |
277
|
|
|
'user_id' => $this->id, |
278
|
|
|
'key' => $key |
279
|
|
|
),array( |
280
|
|
|
'user_id' => $this->id, |
281
|
|
|
'key' => $key, |
282
|
|
|
'value' => $value, |
283
|
|
|
)); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Helper method to set the usermetadata object rather than calling getPrototype everywhere |
288
|
|
|
* @param string $key |
289
|
|
|
* @param mixed $value |
290
|
|
|
* @return UserMetadata prototype object |
291
|
|
|
*/ |
292
|
|
|
public function setMetadataObject($key, $value) |
293
|
|
|
{ |
294
|
|
|
$metadata = $this->getMetadataObject(); |
295
|
|
|
|
296
|
|
|
$metadata->value = $value; |
297
|
|
|
|
298
|
|
|
return $metadata->save(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Determines if two factor authentication code is required |
303
|
|
|
* @return boolean |
304
|
|
|
*/ |
305
|
|
|
public function needsTwoFactorAuth() |
306
|
|
|
{ |
307
|
|
|
$metadata = $this->getMetadataObject('OTPSeed', false); |
308
|
|
|
|
309
|
|
|
if ($metadata->value !== false) |
310
|
|
|
return true; |
311
|
|
|
|
312
|
|
|
return false; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Returns the gravatar image url for a particular user |
317
|
|
|
* The beauty of this is that you can call User::model()->findByPk()->gravatarImage() and not have to do anything else |
318
|
|
|
* Implemention details borrowed from Hypatia Cii User Extensions with permission |
319
|
|
|
* @param integer $size The size of the image we want to display |
320
|
|
|
* @param string $default The default image to be displayed if none is found |
321
|
|
|
* @return string gravatar api image |
322
|
|
|
*/ |
323
|
|
|
public function gravatarImage($size=20, $default=NULL) |
324
|
|
|
{ |
325
|
|
|
return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email))).'?s='.$size; |
|
|
|
|
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.