1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\backend\controllers; |
4
|
|
|
|
5
|
|
|
use zacksleo\yii2\backend\models\forms\ChangePasswordForm; |
6
|
|
|
use yii; |
7
|
|
|
use zacksleo\yii2\backend\models\Admin; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\filters\AccessControl; |
10
|
|
|
|
11
|
|
|
class MyController extends Controller |
12
|
|
|
{ |
13
|
|
|
public $layout = '@vendor/zacksleo/yii2-backend/src/views/my/layouts'; |
14
|
|
|
|
15
|
|
|
public function behaviors() |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
|
|
'access' => [ |
19
|
|
|
'class' => AccessControl::className(), |
20
|
|
|
'rules' => [ |
21
|
|
|
[ |
22
|
|
|
'allow' => true, |
23
|
|
|
'actions' => ['profile', 'change-password', 'avatar'], |
24
|
|
|
'roles' => ['@'], |
25
|
|
|
] |
26
|
|
|
], |
27
|
|
|
] |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 设置头像 |
33
|
|
|
* @return string|yii\web\Response |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function actionAvatar() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$model = Admin::findOne(Yii::$app->user->id); |
38
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
39
|
|
|
Yii::$app->session->setFlash('success', '修改成功'); |
40
|
|
|
return $this->redirect(['avatar']); |
41
|
|
|
} else { |
42
|
|
|
return $this->render('avatar', [ |
43
|
|
|
'model' => $model |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 修改密码 |
50
|
|
|
* @return string|yii\web\Response |
51
|
|
|
*/ |
52
|
|
|
public function actionChangePassword() |
53
|
|
|
{ |
54
|
|
|
$model = new ChangePasswordForm(); |
55
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { |
56
|
|
|
Yii::$app->session->setFlash('success', '修改成功'); |
57
|
|
|
return $this->redirect(['change-password']); |
58
|
|
|
} else { |
59
|
|
|
return $this->render('change-password', [ |
60
|
|
|
'model' => $model |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* 设置个人信息 |
67
|
|
|
* @return string|yii\web\Response |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function actionProfile() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$model = Admin::findOne(Yii::$app->user->id); |
72
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
73
|
|
|
Yii::$app->session->setFlash('success', '修改成功'); |
74
|
|
|
return $this->redirect(['profile']); |
75
|
|
|
} else { |
76
|
|
|
return $this->render('profile', [ |
77
|
|
|
'model' => $model |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.