@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | public function addNew(array $routes): bool |
| 65 | 65 | { |
| 66 | 66 | foreach ($routes as $route) { |
| 67 | - $this->manager->add($this->manager->createPermission('/' . trim($route, ' /'))); |
|
| 67 | + $this->manager->add($this->manager->createPermission('/'.trim($route, ' /'))); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | $this->invalidate(); |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | public function remove(array $routes): bool |
| 83 | 83 | { |
| 84 | 84 | foreach ($routes as $route) { |
| 85 | - $item = $this->manager->createPermission('/' . trim($route, '/')); |
|
| 85 | + $item = $this->manager->createPermission('/'.trim($route, '/')); |
|
| 86 | 86 | $this->manager->remove($item); |
| 87 | 87 | } |
| 88 | 88 | $this->invalidate(); |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | protected function getRouteRecursive(Module $module, &$result) |
| 165 | 165 | { |
| 166 | 166 | if (!in_array($module->id, $this->excludeModules)) { |
| 167 | - $token = "Get Route of '" . get_class($module) . "' with id '" . $module->uniqueId . "'"; |
|
| 167 | + $token = "Get Route of '".get_class($module)."' with id '".$module->uniqueId."'"; |
|
| 168 | 168 | Yii::beginProfile($token, __METHOD__); |
| 169 | 169 | |
| 170 | 170 | try { |
@@ -178,9 +178,9 @@ discard block |
||
| 178 | 178 | $this->getControllerActions($type, $id, $module, $result); |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - $namespace = trim($module->controllerNamespace, '\\') . '\\'; |
|
| 181 | + $namespace = trim($module->controllerNamespace, '\\').'\\'; |
|
| 182 | 182 | $this->getControllerFiles($module, $namespace, '', $result); |
| 183 | - $all = '/' . ltrim($module->uniqueId . '/*', '/'); |
|
| 183 | + $all = '/'.ltrim($module->uniqueId.'/*', '/'); |
|
| 184 | 184 | $result[$all] = $all; |
| 185 | 185 | } catch (\Exception $exc) { |
| 186 | 186 | Yii::error($exc->getMessage(), __METHOD__); |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | */ |
| 201 | 201 | protected function getControllerFiles(Module $module, string $namespace, string $prefix, &$result) |
| 202 | 202 | { |
| 203 | - $path = Yii::getAlias('@' . str_replace('\\', '/', $namespace), false); |
|
| 203 | + $path = Yii::getAlias('@'.str_replace('\\', '/', $namespace), false); |
|
| 204 | 204 | $token = "Get controllers from '$path'"; |
| 205 | 205 | Yii::beginProfile($token, __METHOD__); |
| 206 | 206 | |
@@ -213,15 +213,15 @@ discard block |
||
| 213 | 213 | if ($file == '.' || $file == '..') { |
| 214 | 214 | continue; |
| 215 | 215 | } |
| 216 | - if (is_dir($path . '/' . $file) && preg_match('%^[a-z0-9_/]+$%i', $file . '/')) { |
|
| 217 | - $this->getControllerFiles($module, $namespace . $file . '\\', $prefix . $file . '/', $result); |
|
| 216 | + if (is_dir($path.'/'.$file) && preg_match('%^[a-z0-9_/]+$%i', $file.'/')) { |
|
| 217 | + $this->getControllerFiles($module, $namespace.$file.'\\', $prefix.$file.'/', $result); |
|
| 218 | 218 | } elseif (strcmp(substr($file, -14), 'Controller.php') === 0) { |
| 219 | 219 | $baseName = substr(basename($file), 0, -14); |
| 220 | 220 | $name = strtolower(preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $baseName)); |
| 221 | 221 | $id = ltrim(str_replace(' ', '-', $name), '-'); |
| 222 | - $className = $namespace . $baseName . 'Controller'; |
|
| 222 | + $className = $namespace.$baseName.'Controller'; |
|
| 223 | 223 | if (strpos($className, '-') === false && class_exists($className) && is_subclass_of($className, 'yii\base\Controller')) { |
| 224 | - $this->getControllerActions($className, $prefix . $id, $module, $result); |
|
| 224 | + $this->getControllerActions($className, $prefix.$id, $module, $result); |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | } |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | */ |
| 243 | 243 | protected function getControllerActions($type, $id, Module $module, &$result) |
| 244 | 244 | { |
| 245 | - $token = 'Create controller with config=' . VarDumper::dumpAsString($type) . " and id='$id'"; |
|
| 245 | + $token = 'Create controller with config='.VarDumper::dumpAsString($type)." and id='$id'"; |
|
| 246 | 246 | Yii::beginProfile($token, __METHOD__); |
| 247 | 247 | |
| 248 | 248 | try { |
@@ -266,13 +266,13 @@ discard block |
||
| 266 | 266 | */ |
| 267 | 267 | protected function getActionRoutes(Controller $controller, &$result) |
| 268 | 268 | { |
| 269 | - $token = "Get actions of controller '" . $controller->uniqueId . "'"; |
|
| 269 | + $token = "Get actions of controller '".$controller->uniqueId."'"; |
|
| 270 | 270 | Yii::beginProfile($token, __METHOD__); |
| 271 | 271 | |
| 272 | 272 | try { |
| 273 | - $prefix = '/' . $controller->uniqueId . '/'; |
|
| 273 | + $prefix = '/'.$controller->uniqueId.'/'; |
|
| 274 | 274 | foreach ($controller->actions() as $id => $value) { |
| 275 | - $result[$prefix . $id] = $prefix . $id; |
|
| 275 | + $result[$prefix.$id] = $prefix.$id; |
|
| 276 | 276 | } |
| 277 | 277 | $class = new \ReflectionClass($controller); |
| 278 | 278 | |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | $name = $method->getName(); |
| 281 | 281 | if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0 && $name !== 'actions') { |
| 282 | 282 | $name = strtolower(preg_replace('/(?<![A-Z])[A-Z]/', ' \0', substr($name, 6))); |
| 283 | - $id = $prefix . ltrim(str_replace(' ', '-', $name), '-'); |
|
| 283 | + $id = $prefix.ltrim(str_replace(' ', '-', $name), '-'); |
|
| 284 | 284 | $result[$id] = $id; |
| 285 | 285 | } |
| 286 | 286 | } |
@@ -6,10 +6,10 @@ |
||
| 6 | 6 | define('YII_ENABLE_ERROR_HANDLER', false); |
| 7 | 7 | define('YII_DEBUG', true); |
| 8 | 8 | |
| 9 | -$_SERVER['SCRIPT_NAME'] = '/' . __DIR__; |
|
| 9 | +$_SERVER['SCRIPT_NAME'] = '/'.__DIR__; |
|
| 10 | 10 | $_SERVER['SCRIPT_FILENAME'] = __FILE__; |
| 11 | 11 | |
| 12 | -require_once(__DIR__ . '/../vendor/autoload.php'); |
|
| 13 | -require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); |
|
| 12 | +require_once(__DIR__.'/../vendor/autoload.php'); |
|
| 13 | +require_once(__DIR__.'/../vendor/yiisoft/yii2/Yii.php'); |
|
| 14 | 14 | |
| 15 | 15 | Yii::setAlias('@yii2mod/rbac', dirname(__DIR__)); |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | /** |
| 67 | 67 | * Lists of all auth items |
| 68 | 68 | * |
| 69 | - * @return mixed |
|
| 69 | + * @return string |
|
| 70 | 70 | */ |
| 71 | 71 | public function actionIndex() |
| 72 | 72 | { |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | * |
| 86 | 86 | * @param string $id |
| 87 | 87 | * |
| 88 | - * @return mixed |
|
| 88 | + * @return string |
|
| 89 | 89 | */ |
| 90 | 90 | public function actionView(string $id) |
| 91 | 91 | { |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @param string $id |
| 146 | 146 | * |
| 147 | - * @return mixed |
|
| 147 | + * @return Response |
|
| 148 | 148 | */ |
| 149 | 149 | public function actionDelete(string $id) |
| 150 | 150 | { |
@@ -18,232 +18,232 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class ItemController extends Controller |
| 20 | 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 | - public function actionCreate() |
|
| 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 | - public function actionUpdate(string $id) |
|
| 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 | - public function actionDelete(string $id) |
|
| 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 | - public function actionAssign(string $id) |
|
| 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 | - public function actionRemove(string $id): array |
|
| 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 | - public function beforeAction($action) |
|
| 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 | - } |
|
| 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 | + public function actionCreate() |
|
| 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 | + public function actionUpdate(string $id) |
|
| 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 | + public function actionDelete(string $id) |
|
| 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 | + public function actionAssign(string $id) |
|
| 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 | + public function actionRemove(string $id): array |
|
| 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 | + public function beforeAction($action) |
|
| 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 | 249 | } |
@@ -192,7 +192,7 @@ |
||
| 192 | 192 | */ |
| 193 | 193 | public function getViewPath(): string |
| 194 | 194 | { |
| 195 | - return $this->module->getViewPath() . DIRECTORY_SEPARATOR . 'item'; |
|
| 195 | + return $this->module->getViewPath().DIRECTORY_SEPARATOR.'item'; |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -115,7 +115,7 @@ |
||
| 115 | 115 | * |
| 116 | 116 | * @param $id |
| 117 | 117 | * |
| 118 | - * @return mixed |
|
| 118 | + * @return string |
|
| 119 | 119 | */ |
| 120 | 120 | public function actionView($id) |
| 121 | 121 | { |
@@ -16,180 +16,180 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class AssignmentController extends Controller |
| 18 | 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 $id |
|
| 117 | - * |
|
| 118 | - * @return mixed |
|
| 119 | - */ |
|
| 120 | - public function actionView($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 $id |
|
| 134 | - * |
|
| 135 | - * @return array |
|
| 136 | - */ |
|
| 137 | - public function actionAssign($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 $id |
|
| 150 | - * |
|
| 151 | - * @return array |
|
| 152 | - */ |
|
| 153 | - public function actionRemove($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 $id |
|
| 167 | - * |
|
| 168 | - * @return AssignmentModel the loaded model |
|
| 169 | - * |
|
| 170 | - * @throws NotFoundHttpException if the model cannot be found |
|
| 171 | - */ |
|
| 172 | - protected function findModel($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 | - public function beforeAction($action) |
|
| 184 | - { |
|
| 185 | - if (!parent::beforeAction($action)) { |
|
| 186 | - return false; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - if (!\app\controllers\UsersController::test('admin')) { |
|
| 190 | - throw new \yii\web\ForbiddenHttpException('You are not allowed to access this page.'); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - return true; |
|
| 194 | - } |
|
| 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 $id |
|
| 117 | + * |
|
| 118 | + * @return mixed |
|
| 119 | + */ |
|
| 120 | + public function actionView($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 $id |
|
| 134 | + * |
|
| 135 | + * @return array |
|
| 136 | + */ |
|
| 137 | + public function actionAssign($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 $id |
|
| 150 | + * |
|
| 151 | + * @return array |
|
| 152 | + */ |
|
| 153 | + public function actionRemove($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 $id |
|
| 167 | + * |
|
| 168 | + * @return AssignmentModel the loaded model |
|
| 169 | + * |
|
| 170 | + * @throws NotFoundHttpException if the model cannot be found |
|
| 171 | + */ |
|
| 172 | + protected function findModel($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 | + public function beforeAction($action) |
|
| 184 | + { |
|
| 185 | + if (!parent::beforeAction($action)) { |
|
| 186 | + return false; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + if (!\app\controllers\UsersController::test('admin')) { |
|
| 190 | + throw new \yii\web\ForbiddenHttpException('You are not allowed to access this page.'); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + return true; |
|
| 194 | + } |
|
| 195 | 195 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | /** |
| 48 | 48 | * List of all rules |
| 49 | 49 | * |
| 50 | - * @return mixed |
|
| 50 | + * @return string |
|
| 51 | 51 | */ |
| 52 | 52 | public function actionIndex() |
| 53 | 53 | { |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | * |
| 66 | 66 | * @param string $id |
| 67 | 67 | * |
| 68 | - * @return mixed |
|
| 68 | + * @return string |
|
| 69 | 69 | */ |
| 70 | 70 | public function actionView(string $id) |
| 71 | 71 | { |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | * |
| 124 | 124 | * @param string $id |
| 125 | 125 | * |
| 126 | - * @return mixed |
|
| 126 | + * @return \yii\web\Response |
|
| 127 | 127 | */ |
| 128 | 128 | public function actionDelete(string $id) |
| 129 | 129 | { |
@@ -16,156 +16,156 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class RuleController extends Controller |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @var string search class name for rules search |
|
| 21 | - */ |
|
| 22 | - public $searchClass = [ |
|
| 23 | - 'class' => BizRuleSearch::class, |
|
| 24 | - ]; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Returns a list of behaviors that this component should behave as. |
|
| 28 | - * |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public function behaviors(): array |
|
| 32 | - { |
|
| 33 | - return [ |
|
| 34 | - 'verbs' => [ |
|
| 35 | - 'class' => VerbFilter::class, |
|
| 36 | - 'actions' => [ |
|
| 37 | - 'index' => ['get'], |
|
| 38 | - 'view' => ['get'], |
|
| 39 | - 'create' => ['get', 'post'], |
|
| 40 | - 'update' => ['get', 'post'], |
|
| 41 | - 'delete' => ['post'], |
|
| 42 | - ], |
|
| 43 | - ], |
|
| 44 | - ]; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * List of all rules |
|
| 49 | - * |
|
| 50 | - * @return mixed |
|
| 51 | - */ |
|
| 52 | - public function actionIndex() |
|
| 53 | - { |
|
| 54 | - $searchModel = Yii::createObject($this->searchClass); |
|
| 55 | - $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); |
|
| 56 | - |
|
| 57 | - return $this->render('index', [ |
|
| 58 | - 'dataProvider' => $dataProvider, |
|
| 59 | - 'searchModel' => $searchModel, |
|
| 60 | - ]); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Displays a single Rule item. |
|
| 65 | - * |
|
| 66 | - * @param string $id |
|
| 67 | - * |
|
| 68 | - * @return mixed |
|
| 69 | - */ |
|
| 70 | - public function actionView(string $id) |
|
| 71 | - { |
|
| 72 | - $model = $this->findModel($id); |
|
| 73 | - |
|
| 74 | - return $this->render('view', ['model' => $model]); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Creates a new Rule item. |
|
| 79 | - * |
|
| 80 | - * If creation is successful, the browser will be redirected to the 'view' page. |
|
| 81 | - * |
|
| 82 | - * @return mixed |
|
| 83 | - */ |
|
| 84 | - public function actionCreate() |
|
| 85 | - { |
|
| 86 | - $model = new BizRuleModel(); |
|
| 87 | - |
|
| 88 | - if ($model->load(Yii::$app->request->post()) && $model->save()) { |
|
| 89 | - Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.')); |
|
| 90 | - |
|
| 91 | - return $this->redirect(['view', 'id' => $model->name]); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - return $this->render('create', ['model' => $model]); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Updates an existing Rule item. |
|
| 99 | - * |
|
| 100 | - * If update is successful, the browser will be redirected to the 'view' page. |
|
| 101 | - * |
|
| 102 | - * @param string $id |
|
| 103 | - * |
|
| 104 | - * @return mixed |
|
| 105 | - */ |
|
| 106 | - public function actionUpdate(string $id) |
|
| 107 | - { |
|
| 108 | - $model = $this->findModel($id); |
|
| 109 | - |
|
| 110 | - if ($model->load(Yii::$app->request->post()) && $model->save()) { |
|
| 111 | - Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.')); |
|
| 112 | - |
|
| 113 | - return $this->redirect(['view', 'id' => $model->name]); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return $this->render('update', ['model' => $model]); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Deletes an existing Rule item. |
|
| 121 | - * |
|
| 122 | - * If deletion is successful, the browser will be redirected to the 'index' page. |
|
| 123 | - * |
|
| 124 | - * @param string $id |
|
| 125 | - * |
|
| 126 | - * @return mixed |
|
| 127 | - */ |
|
| 128 | - public function actionDelete(string $id) |
|
| 129 | - { |
|
| 130 | - $model = $this->findModel($id); |
|
| 131 | - Yii::$app->authManager->remove($model->item); |
|
| 132 | - Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been deleted.')); |
|
| 133 | - |
|
| 134 | - return $this->redirect(['index']); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Finds the BizRuleModel based on its primary key value. |
|
| 139 | - * |
|
| 140 | - * If the model is not found, a 404 HTTP exception will be thrown. |
|
| 141 | - * |
|
| 142 | - * @param string $id |
|
| 143 | - * |
|
| 144 | - * @return BizRuleModel the loaded model |
|
| 145 | - * |
|
| 146 | - * @throws \yii\web\NotFoundHttpException |
|
| 147 | - */ |
|
| 148 | - protected function findModel(string $id) |
|
| 149 | - { |
|
| 150 | - $item = Yii::$app->authManager->getRule($id); |
|
| 151 | - |
|
| 152 | - if (!empty($item)) { |
|
| 153 | - return new BizRuleModel($item); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.')); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - public function beforeAction($action) |
|
| 160 | - { |
|
| 161 | - if (!parent::beforeAction($action)) { |
|
| 162 | - return false; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if (!\app\controllers\UsersController::test('admin')) { |
|
| 166 | - throw new \yii\web\ForbiddenHttpException('You are not allowed to access this page.'); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return true; |
|
| 170 | - } |
|
| 19 | + /** |
|
| 20 | + * @var string search class name for rules search |
|
| 21 | + */ |
|
| 22 | + public $searchClass = [ |
|
| 23 | + 'class' => BizRuleSearch::class, |
|
| 24 | + ]; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Returns a list of behaviors that this component should behave as. |
|
| 28 | + * |
|
| 29 | + * @return array |
|
| 30 | + */ |
|
| 31 | + public function behaviors(): array |
|
| 32 | + { |
|
| 33 | + return [ |
|
| 34 | + 'verbs' => [ |
|
| 35 | + 'class' => VerbFilter::class, |
|
| 36 | + 'actions' => [ |
|
| 37 | + 'index' => ['get'], |
|
| 38 | + 'view' => ['get'], |
|
| 39 | + 'create' => ['get', 'post'], |
|
| 40 | + 'update' => ['get', 'post'], |
|
| 41 | + 'delete' => ['post'], |
|
| 42 | + ], |
|
| 43 | + ], |
|
| 44 | + ]; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * List of all rules |
|
| 49 | + * |
|
| 50 | + * @return mixed |
|
| 51 | + */ |
|
| 52 | + public function actionIndex() |
|
| 53 | + { |
|
| 54 | + $searchModel = Yii::createObject($this->searchClass); |
|
| 55 | + $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); |
|
| 56 | + |
|
| 57 | + return $this->render('index', [ |
|
| 58 | + 'dataProvider' => $dataProvider, |
|
| 59 | + 'searchModel' => $searchModel, |
|
| 60 | + ]); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Displays a single Rule item. |
|
| 65 | + * |
|
| 66 | + * @param string $id |
|
| 67 | + * |
|
| 68 | + * @return mixed |
|
| 69 | + */ |
|
| 70 | + public function actionView(string $id) |
|
| 71 | + { |
|
| 72 | + $model = $this->findModel($id); |
|
| 73 | + |
|
| 74 | + return $this->render('view', ['model' => $model]); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Creates a new Rule item. |
|
| 79 | + * |
|
| 80 | + * If creation is successful, the browser will be redirected to the 'view' page. |
|
| 81 | + * |
|
| 82 | + * @return mixed |
|
| 83 | + */ |
|
| 84 | + public function actionCreate() |
|
| 85 | + { |
|
| 86 | + $model = new BizRuleModel(); |
|
| 87 | + |
|
| 88 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { |
|
| 89 | + Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.')); |
|
| 90 | + |
|
| 91 | + return $this->redirect(['view', 'id' => $model->name]); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + return $this->render('create', ['model' => $model]); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Updates an existing Rule item. |
|
| 99 | + * |
|
| 100 | + * If update is successful, the browser will be redirected to the 'view' page. |
|
| 101 | + * |
|
| 102 | + * @param string $id |
|
| 103 | + * |
|
| 104 | + * @return mixed |
|
| 105 | + */ |
|
| 106 | + public function actionUpdate(string $id) |
|
| 107 | + { |
|
| 108 | + $model = $this->findModel($id); |
|
| 109 | + |
|
| 110 | + if ($model->load(Yii::$app->request->post()) && $model->save()) { |
|
| 111 | + Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.')); |
|
| 112 | + |
|
| 113 | + return $this->redirect(['view', 'id' => $model->name]); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return $this->render('update', ['model' => $model]); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Deletes an existing Rule item. |
|
| 121 | + * |
|
| 122 | + * If deletion is successful, the browser will be redirected to the 'index' page. |
|
| 123 | + * |
|
| 124 | + * @param string $id |
|
| 125 | + * |
|
| 126 | + * @return mixed |
|
| 127 | + */ |
|
| 128 | + public function actionDelete(string $id) |
|
| 129 | + { |
|
| 130 | + $model = $this->findModel($id); |
|
| 131 | + Yii::$app->authManager->remove($model->item); |
|
| 132 | + Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been deleted.')); |
|
| 133 | + |
|
| 134 | + return $this->redirect(['index']); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Finds the BizRuleModel based on its primary key value. |
|
| 139 | + * |
|
| 140 | + * If the model is not found, a 404 HTTP exception will be thrown. |
|
| 141 | + * |
|
| 142 | + * @param string $id |
|
| 143 | + * |
|
| 144 | + * @return BizRuleModel the loaded model |
|
| 145 | + * |
|
| 146 | + * @throws \yii\web\NotFoundHttpException |
|
| 147 | + */ |
|
| 148 | + protected function findModel(string $id) |
|
| 149 | + { |
|
| 150 | + $item = Yii::$app->authManager->getRule($id); |
|
| 151 | + |
|
| 152 | + if (!empty($item)) { |
|
| 153 | + return new BizRuleModel($item); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.')); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + public function beforeAction($action) |
|
| 160 | + { |
|
| 161 | + if (!parent::beforeAction($action)) { |
|
| 162 | + return false; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if (!\app\controllers\UsersController::test('admin')) { |
|
| 166 | + throw new \yii\web\ForbiddenHttpException('You are not allowed to access this page.'); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return true; |
|
| 170 | + } |
|
| 171 | 171 | } |
@@ -15,104 +15,104 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class RouteController extends Controller |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var array route model class |
|
| 20 | - */ |
|
| 21 | - public $modelClass = [ |
|
| 22 | - 'class' => RouteModel::class, |
|
| 23 | - ]; |
|
| 18 | + /** |
|
| 19 | + * @var array route model class |
|
| 20 | + */ |
|
| 21 | + public $modelClass = [ |
|
| 22 | + 'class' => RouteModel::class, |
|
| 23 | + ]; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Returns a list of behaviors that this component should behave as. |
|
| 27 | - * |
|
| 28 | - * @return array |
|
| 29 | - */ |
|
| 30 | - public function behaviors(): array |
|
| 31 | - { |
|
| 32 | - return [ |
|
| 33 | - 'verbs' => [ |
|
| 34 | - 'class' => VerbFilter::class, |
|
| 35 | - 'actions' => [ |
|
| 36 | - 'index' => ['get', 'post'], |
|
| 37 | - 'create' => ['post'], |
|
| 38 | - 'assign' => ['post'], |
|
| 39 | - 'remove' => ['post'], |
|
| 40 | - 'refresh' => ['post'], |
|
| 41 | - ], |
|
| 42 | - ], |
|
| 43 | - 'contentNegotiator' => [ |
|
| 44 | - 'class' => 'yii\filters\ContentNegotiator', |
|
| 45 | - 'only' => ['assign', 'remove', 'refresh'], |
|
| 46 | - 'formats' => [ |
|
| 47 | - 'application/json' => Response::FORMAT_JSON, |
|
| 48 | - ], |
|
| 49 | - ], |
|
| 50 | - ]; |
|
| 51 | - } |
|
| 25 | + /** |
|
| 26 | + * Returns a list of behaviors that this component should behave as. |
|
| 27 | + * |
|
| 28 | + * @return array |
|
| 29 | + */ |
|
| 30 | + public function behaviors(): array |
|
| 31 | + { |
|
| 32 | + return [ |
|
| 33 | + 'verbs' => [ |
|
| 34 | + 'class' => VerbFilter::class, |
|
| 35 | + 'actions' => [ |
|
| 36 | + 'index' => ['get', 'post'], |
|
| 37 | + 'create' => ['post'], |
|
| 38 | + 'assign' => ['post'], |
|
| 39 | + 'remove' => ['post'], |
|
| 40 | + 'refresh' => ['post'], |
|
| 41 | + ], |
|
| 42 | + ], |
|
| 43 | + 'contentNegotiator' => [ |
|
| 44 | + 'class' => 'yii\filters\ContentNegotiator', |
|
| 45 | + 'only' => ['assign', 'remove', 'refresh'], |
|
| 46 | + 'formats' => [ |
|
| 47 | + 'application/json' => Response::FORMAT_JSON, |
|
| 48 | + ], |
|
| 49 | + ], |
|
| 50 | + ]; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Lists all Route models. |
|
| 55 | - * |
|
| 56 | - * @return mixed |
|
| 57 | - */ |
|
| 58 | - public function actionIndex() |
|
| 59 | - { |
|
| 60 | - $model = Yii::createObject($this->modelClass); |
|
| 53 | + /** |
|
| 54 | + * Lists all Route models. |
|
| 55 | + * |
|
| 56 | + * @return mixed |
|
| 57 | + */ |
|
| 58 | + public function actionIndex() |
|
| 59 | + { |
|
| 60 | + $model = Yii::createObject($this->modelClass); |
|
| 61 | 61 | |
| 62 | - return $this->render('index', ['routes' => $model->getAvailableAndAssignedRoutes()]); |
|
| 63 | - } |
|
| 62 | + return $this->render('index', ['routes' => $model->getAvailableAndAssignedRoutes()]); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Assign routes |
|
| 67 | - * |
|
| 68 | - * @return array |
|
| 69 | - */ |
|
| 70 | - public function actionAssign(): array |
|
| 71 | - { |
|
| 72 | - $routes = Yii::$app->getRequest()->post('routes', []); |
|
| 73 | - $model = Yii::createObject($this->modelClass); |
|
| 74 | - $model->addNew($routes); |
|
| 65 | + /** |
|
| 66 | + * Assign routes |
|
| 67 | + * |
|
| 68 | + * @return array |
|
| 69 | + */ |
|
| 70 | + public function actionAssign(): array |
|
| 71 | + { |
|
| 72 | + $routes = Yii::$app->getRequest()->post('routes', []); |
|
| 73 | + $model = Yii::createObject($this->modelClass); |
|
| 74 | + $model->addNew($routes); |
|
| 75 | 75 | |
| 76 | - return $model->getAvailableAndAssignedRoutes(); |
|
| 77 | - } |
|
| 76 | + return $model->getAvailableAndAssignedRoutes(); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Remove routes |
|
| 81 | - * |
|
| 82 | - * @return array |
|
| 83 | - */ |
|
| 84 | - public function actionRemove(): array |
|
| 85 | - { |
|
| 86 | - $routes = Yii::$app->getRequest()->post('routes', []); |
|
| 87 | - $model = Yii::createObject($this->modelClass); |
|
| 88 | - $model->remove($routes); |
|
| 79 | + /** |
|
| 80 | + * Remove routes |
|
| 81 | + * |
|
| 82 | + * @return array |
|
| 83 | + */ |
|
| 84 | + public function actionRemove(): array |
|
| 85 | + { |
|
| 86 | + $routes = Yii::$app->getRequest()->post('routes', []); |
|
| 87 | + $model = Yii::createObject($this->modelClass); |
|
| 88 | + $model->remove($routes); |
|
| 89 | 89 | |
| 90 | - return $model->getAvailableAndAssignedRoutes(); |
|
| 91 | - } |
|
| 90 | + return $model->getAvailableAndAssignedRoutes(); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Refresh cache of routes |
|
| 95 | - * |
|
| 96 | - * @return array |
|
| 97 | - */ |
|
| 98 | - public function actionRefresh(): array |
|
| 99 | - { |
|
| 100 | - $model = Yii::createObject($this->modelClass); |
|
| 101 | - $model->invalidate(); |
|
| 93 | + /** |
|
| 94 | + * Refresh cache of routes |
|
| 95 | + * |
|
| 96 | + * @return array |
|
| 97 | + */ |
|
| 98 | + public function actionRefresh(): array |
|
| 99 | + { |
|
| 100 | + $model = Yii::createObject($this->modelClass); |
|
| 101 | + $model->invalidate(); |
|
| 102 | 102 | |
| 103 | - return $model->getAvailableAndAssignedRoutes(); |
|
| 104 | - } |
|
| 103 | + return $model->getAvailableAndAssignedRoutes(); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - public function beforeAction($action) |
|
| 107 | - { |
|
| 108 | - if (!parent::beforeAction($action)) { |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 106 | + public function beforeAction($action) |
|
| 107 | + { |
|
| 108 | + if (!parent::beforeAction($action)) { |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - if (!\app\controllers\UsersController::test('admin')) { |
|
| 113 | - throw new \yii\web\ForbiddenHttpException('You are not allowed to access this page.'); |
|
| 114 | - } |
|
| 112 | + if (!\app\controllers\UsersController::test('admin')) { |
|
| 113 | + throw new \yii\web\ForbiddenHttpException('You are not allowed to access this page.'); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - return true; |
|
| 117 | - } |
|
| 116 | + return true; |
|
| 117 | + } |
|
| 118 | 118 | } |