diff --git a/README.md b/README.md index 5cc3274..645908b 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,14 @@ return [ 'pageSize' => 10 ], ], + 'route' => [ + 'class' => 'yii2mod\rbac\controllers\RouteController', + // for example: exclude `api, debug and gii` modules from list of routes + 'modelClass' => [ + 'class' => 'yii2mod\rbac\models\RouteModel', + 'excludeModules' => ['api', 'debug', 'gii'], + ], + ], ] ], ] diff --git a/controllers/RouteController.php b/controllers/RouteController.php index 99d8e6f..58df611 100755 --- a/controllers/RouteController.php +++ b/controllers/RouteController.php @@ -15,6 +15,13 @@ */ class RouteController extends Controller { + /** + * @var array route model class + */ + public $modelClass = [ + 'class' => RouteModel::class, + ]; + /** * Returns a list of behaviors that this component should behave as. * @@ -50,7 +57,7 @@ public function behaviors() */ public function actionIndex() { - $model = new RouteModel(); + $model = Yii::createObject($this->modelClass); return $this->render('index', ['routes' => $model->getRoutes()]); } @@ -63,7 +70,7 @@ public function actionIndex() public function actionAssign() { $routes = Yii::$app->getRequest()->post('routes', []); - $model = new RouteModel(); + $model = Yii::createObject($this->modelClass); $model->addNew($routes); return $model->getRoutes(); @@ -77,7 +84,7 @@ public function actionAssign() public function actionRemove() { $routes = Yii::$app->getRequest()->post('routes', []); - $model = new RouteModel(); + $model = Yii::createObject($this->modelClass); $model->remove($routes); return $model->getRoutes(); @@ -88,7 +95,7 @@ public function actionRemove() */ public function actionRefresh() { - $model = new RouteModel(); + $model = Yii::createObject($this->modelClass); $model->invalidate(); return $model->getRoutes(); diff --git a/models/RouteModel.php b/models/RouteModel.php index 22bf637..75a787c 100755 --- a/models/RouteModel.php +++ b/models/RouteModel.php @@ -29,6 +29,11 @@ class RouteModel extends Object */ public $cacheDuration = 3600; + /** + * @var array list of module IDs that will be excluded + */ + public $excludeModules = []; + /** * @var \yii\rbac\ManagerInterface */ @@ -156,29 +161,31 @@ public function invalidate() */ protected function getRouteRecursive($module, &$result) { - $token = "Get Route of '" . get_class($module) . "' with id '" . $module->uniqueId . "'"; - Yii::beginProfile($token, __METHOD__); + if (!in_array($module->id, $this->excludeModules)) { + $token = "Get Route of '" . get_class($module) . "' with id '" . $module->uniqueId . "'"; + Yii::beginProfile($token, __METHOD__); + + try { + foreach ($module->getModules() as $id => $child) { + if (($child = $module->getModule($id)) !== null) { + $this->getRouteRecursive($child, $result); + } + } - try { - foreach ($module->getModules() as $id => $child) { - if (($child = $module->getModule($id)) !== null) { - $this->getRouteRecursive($child, $result); + foreach ($module->controllerMap as $id => $type) { + $this->getControllerActions($type, $id, $module, $result); } - } - foreach ($module->controllerMap as $id => $type) { - $this->getControllerActions($type, $id, $module, $result); + $namespace = trim($module->controllerNamespace, '\\') . '\\'; + $this->getControllerFiles($module, $namespace, '', $result); + $all = '/' . ltrim($module->uniqueId . '/*', '/'); + $result[$all] = $all; + } catch (\Exception $exc) { + Yii::error($exc->getMessage(), __METHOD__); } - $namespace = trim($module->controllerNamespace, '\\') . '\\'; - $this->getControllerFiles($module, $namespace, '', $result); - $all = '/' . ltrim($module->uniqueId . '/*', '/'); - $result[$all] = $all; - } catch (\Exception $exc) { - Yii::error($exc->getMessage(), __METHOD__); + Yii::endProfile($token, __METHOD__); } - - Yii::endProfile($token, __METHOD__); } /**