|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\admin\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\web\NotFoundHttpException; |
|
7
|
|
|
use app\controllers\ControllerTrait; |
|
8
|
|
|
use app\models\entity\AuthItem; |
|
9
|
|
|
use app\modules\admin\models\forms\AuthItemForm; |
|
10
|
|
|
use app\modules\admin\models\search\AuthItemSearch; |
|
11
|
|
|
|
|
12
|
|
|
class RolesController extends \yii\web\Controller |
|
13
|
|
|
{ |
|
14
|
|
|
use ControllerTrait; |
|
15
|
|
|
|
|
16
|
4 |
|
public function behaviors() |
|
17
|
|
|
{ |
|
18
|
|
|
return [ |
|
19
|
4 |
|
'verbs' => [ |
|
20
|
|
|
'class' => 'yii\filters\VerbFilter', |
|
21
|
|
|
'actions' => [ |
|
22
|
|
|
'delete' => ['post'], |
|
23
|
|
|
'batch' => ['post'], |
|
24
|
|
|
], |
|
25
|
|
|
], |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
public function actions() |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
4 |
|
'batch' => [ |
|
33
|
|
|
'class' => 'app\modules\admin\actions\BatchAction', |
|
34
|
|
|
'modelClass' => 'app\models\entity\AuthItem', |
|
35
|
|
|
'actions' => [ |
|
36
|
|
|
'delete' => [], |
|
37
|
|
|
] |
|
38
|
|
|
], |
|
39
|
|
|
'delete' => [ |
|
40
|
|
|
'class' => 'app\modules\admin\actions\DeleteAction', |
|
41
|
|
|
'modelClass' => 'app\models\entity\AuthItem', |
|
42
|
|
|
], |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
public function actionIndex() |
|
47
|
|
|
{ |
|
48
|
4 |
|
$search = new AuthItemSearch(); |
|
49
|
4 |
|
$provider = $search->search(Yii::$app->request->get()); |
|
50
|
|
|
|
|
51
|
4 |
|
return $this->render('index', [ |
|
52
|
4 |
|
'search' => $search, |
|
53
|
4 |
|
'provider' => $provider, |
|
54
|
|
|
]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
public function actionEdit($name = null) |
|
58
|
|
|
{ |
|
59
|
3 |
|
$model = new AuthItemForm(); |
|
60
|
|
|
|
|
61
|
3 |
|
if ($name) { |
|
62
|
2 |
|
$model->setModel($this->findModel($name)); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
if (Yii::$app->request->isPost) { |
|
66
|
|
|
Yii::$app->response->format = 'json'; |
|
67
|
|
|
|
|
68
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
|
69
|
|
|
$model->save(); |
|
70
|
|
|
|
|
71
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully')); |
|
72
|
|
|
return $this->redirect(['edit', 'name' => $model->name]); |
|
73
|
|
|
} |
|
74
|
|
|
return $this->asJsonModelErrors($model); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return $this->render('edit', [ |
|
78
|
2 |
|
'model' => $model, |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Find the model. |
|
84
|
|
|
* If the model is not found, then 404 HTTP exception will be thrown. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $name |
|
87
|
|
|
* @return Model |
|
88
|
|
|
* @throws NotFoundHttpException |
|
89
|
|
|
*/ |
|
90
|
|
|
private function findModel($name): yii\base\Model |
|
91
|
|
|
{ |
|
92
|
|
|
$model = AuthItem::findOne($name); |
|
93
|
|
|
|
|
94
|
|
|
if ($model === null) { |
|
95
|
|
|
throw new NotFoundHttpException(Yii::t('app', 'Page not found')); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $model; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.