1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\user\controllers; |
4
|
|
|
|
5
|
|
|
use app\modules\user\models\forms\UserForm; |
6
|
|
|
use app\modules\user\models\User; |
7
|
|
|
use Yii; |
8
|
|
|
use app\modules\user\models\SearchUser; |
9
|
|
|
use yii\filters\AccessControl; |
10
|
|
|
use yii\web\Controller; |
11
|
|
|
use yii\web\NotFoundHttpException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ManagementController implements the CRU actions for Users model. |
15
|
|
|
*/ |
16
|
|
|
class ManagementController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
7 |
|
public function behaviors() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'access' => [ |
25
|
7 |
|
'class' => AccessControl::className(), |
26
|
|
|
'rules' => [ |
27
|
|
|
[ |
28
|
|
|
'allow' => true, |
29
|
|
|
'actions' => ['index', 'view', 'update'], |
30
|
|
|
'roles' => [User::ROLE_ADMIN], |
31
|
|
|
], |
32
|
|
|
], |
33
|
7 |
|
], |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Lists all Users models. |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
1 |
View Code Duplication |
public function actionIndex() |
|
|
|
|
43
|
|
|
{ |
44
|
1 |
|
$searchModel = new SearchUser(); |
45
|
1 |
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
46
|
|
|
|
47
|
1 |
|
return $this->render('index', [ |
48
|
1 |
|
'searchModel' => $searchModel, |
49
|
1 |
|
'dataProvider' => $dataProvider, |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Displays a single Users model. |
55
|
|
|
* |
56
|
|
|
* @param integer $id |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
3 |
|
public function actionView($id) |
60
|
|
|
{ |
61
|
3 |
|
return $this->render('view', [ |
62
|
3 |
|
'model' => $this->findModel($id), |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Updates an existing Users model. |
68
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
69
|
|
|
* |
70
|
|
|
* @param integer $id |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
3 |
|
public function actionUpdate($id) |
74
|
|
|
{ |
75
|
3 |
|
$user = $this->findModel($id); |
76
|
3 |
|
$userForm = new UserForm(); |
77
|
3 |
|
$userForm->setAttributes($user->attributes); |
78
|
3 |
|
$userForm->role = $user->getRoleName(); |
79
|
|
|
|
80
|
3 |
|
if ($userForm->load(Yii::$app->request->post()) && $userForm->validate()) { |
81
|
1 |
|
$user->setAttributes($userForm->attributes); |
82
|
1 |
|
$user->update(false); |
83
|
1 |
|
$user->setRole($userForm->role); |
84
|
|
|
|
85
|
1 |
|
Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Information saved.')); |
|
|
|
|
86
|
1 |
|
return $this->redirect(['view', 'id' => $user->id]); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
return $this->render('update', [ |
90
|
3 |
|
'userForm' => $userForm, |
91
|
3 |
|
'id' => $user->id, |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Finds the Users model based on its primary key value. |
97
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
98
|
|
|
* |
99
|
|
|
* @param integer $id |
100
|
|
|
* @return User the loaded model |
101
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
102
|
|
|
*/ |
103
|
5 |
View Code Duplication |
protected function findModel($id) |
|
|
|
|
104
|
|
|
{ |
105
|
5 |
|
$model = User::findOne($id); |
106
|
5 |
|
if (null === $model) { |
107
|
|
|
throw new NotFoundHttpException('The requested page does not exist.'); |
108
|
|
|
} |
109
|
5 |
|
return $model; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.