1 | <?php |
||
33 | class Admin extends ActiveRecord implements IdentityInterface |
||
34 | { |
||
35 | const STATUS_INACTIVE = 0; |
||
36 | const STATUS_ACTIVE = 10; |
||
37 | |||
38 | public $imageFile; |
||
39 | |||
40 | public $isAdmin = true; |
||
41 | |||
42 | /** |
||
43 | * @var string|null the current password value from form input |
||
44 | */ |
||
45 | protected $_password; |
||
46 | |||
47 | /** |
||
48 | * @inheritdoc |
||
49 | */ |
||
50 | 32 | public static function tableName() |
|
54 | |||
55 | /** |
||
56 | * @return AdminQuery custom query class with user scopes |
||
57 | */ |
||
58 | 30 | public static function find() |
|
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | 14 | public function scenarios() |
|
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | 14 | public function rules() |
|
106 | |||
107 | /** |
||
108 | * @inheritdoc |
||
109 | */ |
||
110 | 2 | public function attributeLabels() |
|
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | 33 | public function behaviors() |
|
138 | |||
139 | /** |
||
140 | * @inheritdoc |
||
141 | */ |
||
142 | 13 | public function beforeSave($insert) |
|
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | 5 | public static function findIdentity($id) |
|
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | */ |
||
168 | 1 | public static function findIdentityByAccessToken($token, $type = null) |
|
172 | |||
173 | /** |
||
174 | * @inheritdoc |
||
175 | */ |
||
176 | 18 | public function getId() |
|
180 | |||
181 | /** |
||
182 | * @inheritdoc |
||
183 | */ |
||
184 | 1 | public function getAuthKey() |
|
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | 1 | public function validateAuthKey($authKey) |
|
196 | |||
197 | /** |
||
198 | * Validates password |
||
199 | * |
||
200 | * @param string $password password to validate |
||
201 | * @return boolean if password provided is valid for current user |
||
202 | */ |
||
203 | 7 | public function validatePassword($password) |
|
207 | |||
208 | /** |
||
209 | * Generates password hash from password and sets it to the model |
||
210 | * |
||
211 | * @param string $password |
||
212 | */ |
||
213 | 6 | public function setPassword($password) |
|
220 | |||
221 | /** |
||
222 | * @return string|null the current password value, if set from form. Null otherwise. |
||
223 | */ |
||
224 | public function getPassword() |
||
228 | |||
229 | /** |
||
230 | * Generates "remember me" authentication key |
||
231 | */ |
||
232 | 1 | public function generateAuthKey() |
|
236 | |||
237 | /** |
||
238 | * Generates new email confirmation token |
||
239 | * @param bool $save whether to save the record. Default is `false`. |
||
240 | * @return bool|null whether the save was successful or null if $save was false. |
||
241 | */ |
||
242 | public function generateEmailConfirmationToken($save = false) |
||
249 | |||
250 | /** |
||
251 | * Generates new password reset token |
||
252 | * @param bool $save whether to save the record. Default is `false`. |
||
253 | * @return bool|null whether the save was successful or null if $save was false. |
||
254 | */ |
||
255 | 5 | public function generatePasswordResetToken($save = false) |
|
262 | |||
263 | /** |
||
264 | * Resets to a new password and deletes the password reset token. |
||
265 | * @param string $password the new password for this user. |
||
266 | * @return bool whether the record was updated successfully |
||
267 | */ |
||
268 | 5 | public function resetPassword($password) |
|
274 | |||
275 | public static function getStatusList() |
||
282 | |||
283 | /** |
||
284 | * 上传头像 |
||
285 | * @return bool |
||
286 | */ |
||
287 | 2 | public function upload() |
|
288 | { |
||
289 | 2 | if ($this->validate()) { |
|
290 | 2 | $path = date('Ymd') . '/'; |
|
291 | 2 | $fullpath = \Yii::getAlias('@app') . '/web/uploads/' . $path; |
|
292 | 2 | if (!file_exists($fullpath)) { |
|
293 | 1 | mkdir($fullpath); |
|
294 | } |
||
295 | 2 | $filename = uniqid() . '.' . $this->imageFile->extension; |
|
296 | 2 | $this->imageFile->saveAs($fullpath . $filename); |
|
297 | 2 | $this->avatar = $path . $filename; |
|
298 | 2 | return true; |
|
299 | } else { |
||
300 | 1 | return false; |
|
301 | } |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * 获取头像地址 |
||
306 | * @return string |
||
307 | */ |
||
308 | 1 | public function getImageUrl() |
|
315 | } |
||
316 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: