1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\rbac\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\web\Controller; |
7
|
|
|
use yii\web\NotFoundHttpException; |
8
|
|
|
use yii\web\Response; |
9
|
|
|
use yii2mod\rbac\models\AssignmentModel; |
10
|
|
|
use yii2mod\rbac\models\search\AssignmentSearch; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AssignmentController |
14
|
|
|
* |
15
|
|
|
* @package yii2mod\rbac\controllers |
16
|
|
|
*/ |
17
|
|
|
class AssignmentController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \yii\web\IdentityInterface the class name of the [[identity]] object |
21
|
|
|
*/ |
22
|
|
|
public $userIdentityClass; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string search class name for assignments search |
26
|
|
|
*/ |
27
|
|
|
public $searchClass = [ |
28
|
|
|
'class' => AssignmentSearch::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string id column name |
33
|
|
|
*/ |
34
|
|
|
public $idField = 'id'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string username column name |
38
|
|
|
*/ |
39
|
|
|
public $usernameField = 'username'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array assignments GridView columns |
43
|
|
|
*/ |
44
|
|
|
public $gridViewColumns = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function init() |
50
|
|
|
{ |
51
|
|
|
parent::init(); |
52
|
|
|
|
53
|
|
|
if ($this->userIdentityClass === null) { |
54
|
|
|
$this->userIdentityClass = Yii::$app->user->identityClass; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (empty($this->gridViewColumns)) { |
58
|
|
|
$this->gridViewColumns = [ |
59
|
|
|
$this->idField, |
60
|
|
|
$this->usernameField, |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function behaviors(): array |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
'verbs' => [ |
72
|
|
|
'class' => 'yii\filters\VerbFilter', |
73
|
|
|
'actions' => [ |
74
|
|
|
'index' => ['get'], |
75
|
|
|
'view' => ['get'], |
76
|
|
|
'assign' => ['post'], |
77
|
|
|
'remove' => ['post'], |
78
|
|
|
], |
79
|
|
|
], |
80
|
|
|
'contentNegotiator' => [ |
81
|
|
|
'class' => 'yii\filters\ContentNegotiator', |
82
|
|
|
'only' => ['assign', 'remove'], |
83
|
|
|
'formats' => [ |
84
|
|
|
'application/json' => Response::FORMAT_JSON, |
85
|
|
|
], |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* List of all assignments |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function actionIndex() |
96
|
|
|
{ |
97
|
|
|
/* @var AssignmentSearch */ |
98
|
|
|
$searchModel = Yii::createObject($this->searchClass); |
99
|
|
|
|
100
|
|
|
if ($searchModel instanceof AssignmentSearch) { |
101
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $this->userIdentityClass, $this->idField, $this->usernameField); |
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->render('index', [ |
107
|
|
|
'dataProvider' => $dataProvider, |
108
|
|
|
'searchModel' => $searchModel, |
109
|
|
|
'gridViewColumns' => $this->gridViewColumns, |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Displays a single Assignment model. |
115
|
|
|
* |
116
|
|
|
* @param int $id |
117
|
|
|
* |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function actionView(int $id) |
121
|
|
|
{ |
122
|
|
|
$model = $this->findModel($id); |
123
|
|
|
|
124
|
|
|
return $this->render('view', [ |
125
|
|
|
'model' => $model, |
126
|
|
|
'usernameField' => $this->usernameField, |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Assign items |
132
|
|
|
* |
133
|
|
|
* @param int $id |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function actionAssign(int $id) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$items = Yii::$app->getRequest()->post('items', []); |
140
|
|
|
$assignmentModel = $this->findModel($id); |
141
|
|
|
$assignmentModel->assign($items); |
142
|
|
|
|
143
|
|
|
return $assignmentModel->getItems(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Remove items |
148
|
|
|
* |
149
|
|
|
* @param int $id |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
public function actionRemove(int $id) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$items = Yii::$app->getRequest()->post('items', []); |
156
|
|
|
$assignmentModel = $this->findModel($id); |
157
|
|
|
$assignmentModel->revoke($items); |
158
|
|
|
|
159
|
|
|
return $assignmentModel->getItems(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Finds the Assignment model based on its primary key value. |
164
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
165
|
|
|
* |
166
|
|
|
* @param int $id |
167
|
|
|
* |
168
|
|
|
* @return AssignmentModel the loaded model |
169
|
|
|
* |
170
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
171
|
|
|
*/ |
172
|
|
|
protected function findModel(int $id) |
173
|
|
|
{ |
174
|
|
|
$class = $this->userIdentityClass; |
175
|
|
|
|
176
|
|
|
if (($user = $class::findIdentity($id)) !== null) { |
177
|
|
|
return new AssignmentModel($user); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.')); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: