|
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\Filter\AccessRuleFilter; |
|
15
|
|
|
use Da\User\Helper\AuthHelper; |
|
16
|
|
|
use Da\User\Model\AbstractAuthItem; |
|
17
|
|
|
use Da\User\Module; |
|
18
|
|
|
use Da\User\Service\AuthItemEditionService; |
|
19
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
|
20
|
|
|
use Da\User\Validator\AjaxRequestModelValidator; |
|
21
|
|
|
use Yii; |
|
22
|
|
|
use yii\filters\AccessControl; |
|
23
|
|
|
use yii\web\Controller; |
|
24
|
|
|
|
|
25
|
|
|
abstract class AbstractAuthItemController extends Controller |
|
26
|
|
|
{ |
|
27
|
|
|
use ContainerAwareTrait; |
|
28
|
|
|
|
|
29
|
|
|
protected $modelClass; |
|
30
|
|
|
protected $searchModelClass; |
|
31
|
|
|
protected $authHelper; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* AbstractAuthItemController constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $id |
|
37
|
|
|
* @param Module $module |
|
38
|
|
|
* @param AuthHelper $authHelper |
|
39
|
|
|
* @param array $config |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($id, Module $module, AuthHelper $authHelper, array $config = []) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->authHelper = $authHelper; |
|
44
|
|
|
parent::__construct($id, $module, $config); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function behaviors() |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
'access' => [ |
|
54
|
|
|
'class' => AccessControl::class, |
|
55
|
|
|
'ruleConfig' => [ |
|
56
|
|
|
'class' => AccessRuleFilter::class, |
|
57
|
|
|
], |
|
58
|
|
|
'rules' => [ |
|
59
|
|
|
[ |
|
60
|
|
|
'allow' => true, |
|
61
|
|
|
'roles' => ['admin'], |
|
62
|
|
|
], |
|
63
|
|
|
], |
|
64
|
|
|
], |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function actionIndex() |
|
69
|
|
|
{ |
|
70
|
|
|
$searchModel = $this->make($this->getSearchModelClass()); |
|
71
|
|
|
|
|
72
|
|
|
return $this->render( |
|
73
|
|
|
'index', |
|
74
|
|
|
[ |
|
75
|
|
|
'searchModel' => $searchModel, |
|
76
|
|
|
'dataProvider' => $searchModel->search(Yii::$app->request->get()), |
|
77
|
|
|
] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function actionCreate() |
|
82
|
|
|
{ |
|
83
|
|
|
/** @var AbstractAuthItem $model */ |
|
84
|
|
|
$model = $this->make($this->getModelClass(), [], ['scenario' => 'create']); |
|
85
|
|
|
|
|
86
|
|
|
$this->make(AjaxRequestModelValidator::class, [$model])->validate(); |
|
87
|
|
|
|
|
88
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
89
|
|
|
if ($this->make(AuthItemEditionService::class, [$model])->run()) { |
|
90
|
|
|
Yii::$app |
|
|
|
|
|
|
91
|
|
|
->getSession() |
|
92
|
|
|
->setFlash('success', Yii::t('usuario', 'Authorization item successfully created.')); |
|
93
|
|
|
|
|
94
|
|
|
return $this->redirect(['index']); |
|
95
|
|
|
} |
|
96
|
|
|
Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'Unable to create authorization item.')); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->render( |
|
100
|
|
|
'create', |
|
101
|
|
|
[ |
|
102
|
|
|
'model' => $model, |
|
103
|
|
|
'unassignedItems' => $this->authHelper->getUnassignedItems($model), |
|
104
|
|
|
] |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function actionUpdate($name) |
|
109
|
|
|
{ |
|
110
|
|
|
$authItem = $this->getItem($name); |
|
111
|
|
|
|
|
112
|
|
|
/** @var AbstractAuthItem $model */ |
|
113
|
|
|
$model = $this->make($this->getModelClass(), [], ['scenario' => 'update', 'item' => $authItem]); |
|
114
|
|
|
|
|
115
|
|
|
$this->make(AjaxRequestModelValidator::class, [$model])->validate(); |
|
116
|
|
|
|
|
117
|
|
|
if ($model->load(Yii::$app->request->post())) { |
|
118
|
|
|
if ($this->make(AuthItemEditionService::class, [$model])->run()) { |
|
119
|
|
|
Yii::$app |
|
|
|
|
|
|
120
|
|
|
->getSession() |
|
121
|
|
|
->setFlash('success', Yii::t('usuario', 'Authorization item successfully updated.')); |
|
122
|
|
|
|
|
123
|
|
|
return $this->redirect(['index']); |
|
124
|
|
|
} |
|
125
|
|
|
Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'Unable to update authorization item.')); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $this->render( |
|
129
|
|
|
'update', |
|
130
|
|
|
[ |
|
131
|
|
|
'model' => $model, |
|
132
|
|
|
'unassignedItems' => $this->authHelper->getUnassignedItems($model), |
|
133
|
|
|
] |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function actionDelete($name) |
|
138
|
|
|
{ |
|
139
|
|
|
$item = $this->getItem($name); |
|
140
|
|
|
|
|
141
|
|
|
if ($this->authHelper->remove($item)) { |
|
142
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Authorization item successfully removed.')); |
|
|
|
|
|
|
143
|
|
|
} else { |
|
144
|
|
|
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Unable to remove authorization item.')); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $this->redirect(['index']); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* The fully qualified class name of the model. |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
abstract protected function getModelClass(); |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* The fully qualified class name of the search model. |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
abstract protected function getSearchModelClass(); |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the an auth item. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $name |
|
168
|
|
|
* |
|
169
|
|
|
* @return \yii\rbac\Role|\yii\rbac\Permission|\yii\rbac\Rule |
|
170
|
|
|
*/ |
|
171
|
|
|
abstract protected function getItem($name); |
|
172
|
|
|
} |
|
173
|
|
|
|
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: