Completed
Pull Request — master (#38)
by
unknown
01:38
created

ItemController::beforeAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace yii2mod\rbac\base;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\rbac\Item;
8
use yii\web\Controller;
9
use yii\web\NotFoundHttpException;
10
use yii\web\Response;
11
use yii2mod\rbac\models\AuthItemModel;
12
use yii2mod\rbac\models\search\AuthItemSearch;
13
14
/**
15
 * Class ItemController
16
 *
17
 * @package yii2mod\rbac\base
18
 */
19
class ItemController extends Controller
20
{
21
	/**
22
	 * @var string search class name for auth items search
23
	 */
24
	public $searchClass = [
25
		'class' => AuthItemSearch::class,
26
	];
27
28
	/**
29
	 * @var int Type of Auth Item
30
	 */
31
	protected $type;
32
33
	/**
34
	 * @var array labels use in view
35
	 */
36
	protected $labels;
37
38
	/**
39
	 * @inheritdoc
40
	 */
41
	public function behaviors(): array
42
	{
43
		return [
44
			'verbs' => [
45
				'class' => VerbFilter::class,
46
				'actions' => [
47
					'index' => ['get'],
48
					'view' => ['get'],
49
					'create' => ['get', 'post'],
50
					'update' => ['get', 'post'],
51
					'delete' => ['post'],
52
					'assign' => ['post'],
53
					'remove' => ['post'],
54
				],
55
			],
56
			'contentNegotiator' => [
57
				'class' => 'yii\filters\ContentNegotiator',
58
				'only' => ['assign', 'remove'],
59
				'formats' => [
60
					'application/json' => Response::FORMAT_JSON,
61
				],
62
			],
63
		];
64
	}
65
66
	/**
67
	 * Lists of all auth items
68
	 *
69
	 * @return mixed
70
	 */
71
	public function actionIndex()
72
	{
73
		$searchModel = Yii::createObject($this->searchClass);
74
		$searchModel->type = $this->type;
75
		$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
76
77
		return $this->render('index', [
78
			'dataProvider' => $dataProvider,
79
			'searchModel' => $searchModel,
80
		]);
81
	}
82
83
	/**
84
	 * Displays a single AuthItem model.
85
	 *
86
	 * @param string $id
87
	 *
88
	 * @return mixed
89
	 */
90
	public function actionView(string $id)
91
	{
92
		$model = $this->findModel($id);
93
94
		return $this->render('view', ['model' => $model]);
95
	}
96
97
	/**
98
	 * Creates a new AuthItem model.
99
	 *
100
	 * If creation is successful, the browser will be redirected to the 'view' page.
101
	 *
102
	 * @return mixed
103
	 */
104 View Code Duplication
	public function actionCreate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
105
	{
106
		$model = new AuthItemModel();
107
		$model->type = $this->type;
108
109
		if ($model->load(Yii::$app->request->post()) && $model->save()) {
110
			Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Item has been saved.'));
111
112
			return $this->redirect(['view', 'id' => $model->name]);
113
		}
114
115
		return $this->render('create', ['model' => $model]);
116
	}
117
118
	/**
119
	 * Updates an existing AuthItem model.
120
	 *
121
	 * If update is successful, the browser will be redirected to the 'view' page.
122
	 *
123
	 * @param string $id
124
	 *
125
	 * @return mixed
126
	 */
127 View Code Duplication
	public function actionUpdate(string $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
128
	{
129
		$model = $this->findModel($id);
130
131
		if ($model->load(Yii::$app->request->post()) && $model->save()) {
132
			Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Item has been saved.'));
133
134
			return $this->redirect(['view', 'id' => $model->name]);
135
		}
136
137
		return $this->render('update', ['model' => $model]);
138
	}
139
140
	/**
141
	 * Deletes an existing AuthItem model.
142
	 *
143
	 * If deletion is successful, the browser will be redirected to the 'index' page.
144
	 *
145
	 * @param string $id
146
	 *
147
	 * @return mixed
148
	 */
149 View Code Duplication
	public function actionDelete(string $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
150
	{
151
		$model = $this->findModel($id);
152
		Yii::$app->getAuthManager()->remove($model->item);
153
		Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Item has been removed.'));
154
155
		return $this->redirect(['index']);
156
	}
157
158
	/**
159
	 * Assign items
160
	 *
161
	 * @param string $id
162
	 *
163
	 * @return array
164
	 */
165 View Code Duplication
	public function actionAssign(string $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
166
	{
167
		$items = Yii::$app->getRequest()->post('items', []);
168
		$model = $this->findModel($id);
169
		$model->addChildren($items);
170
171
		return array_merge($model->getItems());
172
	}
173
174
	/**
175
	 * Remove items
176
	 *
177
	 * @param string $id
178
	 *
179
	 * @return array
180
	 */
181 View Code Duplication
	public function actionRemove(string $id): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
182
	{
183
		$items = Yii::$app->getRequest()->post('items', []);
184
		$model = $this->findModel($id);
185
		$model->removeChildren($items);
186
187
		return array_merge($model->getItems());
188
	}
189
190
	/**
191
	 * @inheritdoc
192
	 */
193
	public function getViewPath(): string
194
	{
195
		return $this->module->getViewPath() . DIRECTORY_SEPARATOR . 'item';
196
	}
197
198
	/**
199
	 * @return int
200
	 */
201
	public function getType(): int
202
	{
203
		return $this->type;
204
	}
205
206
	/**
207
	 * @return array
208
	 */
209
	public function getLabels(): array
210
	{
211
		return $this->labels;
212
	}
213
214
	/**
215
	 * Finds the AuthItem model based on its primary key value.
216
	 *
217
	 * If the model is not found, a 404 HTTP exception will be thrown.
218
	 *
219
	 * @param string $id
220
	 *
221
	 * @return AuthItemModel the loaded model
222
	 *
223
	 * @throws NotFoundHttpException if the model cannot be found
224
	 */
225
	protected function findModel(string $id): AuthItemModel
226
	{
227
		$auth = Yii::$app->getAuthManager();
228
		$item = $this->type === Item::TYPE_ROLE ? $auth->getRole($id) : $auth->getPermission($id);
229
230
		if (empty($item)) {
231
			throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.'));
232
		}
233
234
		return new AuthItemModel($item);
235
	}
236
237 View Code Duplication
	public function beforeAction($action)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
238
	{
239
		if (!parent::beforeAction($action)) {
240
			return false;
241
		}
242
243
		if (!\app\controllers\UsersController::test('admin')) {
244
			throw new \yii\web\ForbiddenHttpException('You are not allowed to access this page.');
245
		}
246
247
		return true;
248
	}
249
}
250