1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Controller; |
13
|
|
|
|
14
|
|
|
use Da\User\Model\Rule; |
15
|
|
|
use Da\User\Search\RuleSearch; |
16
|
|
|
use Da\User\Service\AuthRuleEditionService; |
17
|
|
|
use Da\User\Traits\AuthManagerAwareTrait; |
18
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
19
|
|
|
use Da\User\Validator\AjaxRequestModelValidator; |
20
|
|
|
use Yii; |
21
|
|
|
use yii\filters\VerbFilter; |
22
|
|
|
use yii\web\Controller; |
23
|
|
|
use yii\web\NotFoundHttpException; |
24
|
|
|
|
25
|
|
|
class RuleController extends Controller |
26
|
|
|
{ |
27
|
|
|
use AuthManagerAwareTrait; |
28
|
|
|
use ContainerAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function behaviors() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
[ |
37
|
|
|
'class' => VerbFilter::className(), |
38
|
|
|
'actions' => [ |
39
|
|
|
'delete' => ['POST'], |
40
|
|
|
], |
41
|
|
|
] |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function actionIndex() |
46
|
|
|
{ |
47
|
|
|
/** @var RuleSearch $searchModel */ |
48
|
|
|
$searchModel = $this->make(RuleSearch::class); |
49
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
50
|
|
|
|
51
|
|
|
return $this->render( |
52
|
|
|
'index', |
53
|
|
|
[ |
54
|
|
|
'searchModel' => $searchModel, |
55
|
|
|
'dataProvider' => $dataProvider, |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function actionCreate() |
61
|
|
|
{ |
62
|
|
|
$model = $this->make(Rule::class, [], ['scenario' => 'create', 'className' => \yii\rbac\Rule::class]); |
63
|
|
|
|
64
|
|
|
$this->make(AjaxRequestModelValidator::class, [$model])->validate(); |
65
|
|
|
|
66
|
|
|
if ($model->load(Yii::$app->request->post())) { |
67
|
|
|
if ($this->make(AuthRuleEditionService::class, [$model])->run()) { |
68
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Authorization rule has been added.')); |
|
|
|
|
69
|
|
|
|
70
|
|
|
return $this->redirect(['index']); |
71
|
|
|
} |
72
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Unable to create new authorization rule.')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->render( |
76
|
|
|
'create', |
77
|
|
|
[ |
78
|
|
|
'model' => $model |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function actionUpdate($name) |
84
|
|
|
{ |
85
|
|
|
/** @var Rule $model */ |
86
|
|
|
$model = $this->make(Rule::class, [], ['scenario' => 'update']); |
87
|
|
|
$rule = $this->findRule($name); |
88
|
|
|
|
89
|
|
|
$model->setAttributes( |
90
|
|
|
[ |
91
|
|
|
'previousName' => $name, |
92
|
|
|
'name' => $rule->name, |
93
|
|
|
'className' => get_class($rule) |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$this->make(AjaxRequestModelValidator::class, [$model])->validate(); |
98
|
|
|
|
99
|
|
|
if ($model->load(Yii::$app->request->post())) { |
100
|
|
|
if ($this->make(AuthRuleEditionService::class, [$model])->run()) { |
101
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Authorization rule has been updated.')); |
|
|
|
|
102
|
|
|
|
103
|
|
|
return $this->redirect(['index']); |
104
|
|
|
} |
105
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Unable to update authorization rule.')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->render( |
109
|
|
|
'update', |
110
|
|
|
[ |
111
|
|
|
'model' => $model, |
112
|
|
|
] |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function actionDelete($name) |
117
|
|
|
{ |
118
|
|
|
$rule = $this->findRule($name); |
119
|
|
|
|
120
|
|
|
$this->getAuthManager()->remove($rule); |
121
|
|
|
$this->getAuthManager()->invalidateCache(); |
|
|
|
|
122
|
|
|
|
123
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Authorization rule has been removed.')); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $name |
128
|
|
|
* |
129
|
|
|
* @throws NotFoundHttpException |
130
|
|
|
* @return mixed|null|\yii\rbac\Rule |
131
|
|
|
*/ |
132
|
|
|
protected function findRule($name) |
133
|
|
|
{ |
134
|
|
|
$rule = $this->getAuthManager()->getRule($name); |
135
|
|
|
|
136
|
|
|
if (!($rule instanceof \yii\rbac\Rule)) { |
137
|
|
|
throw new NotFoundHttpException(Yii::t('usuario', 'Rule {0} not found.', $name)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $rule; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: