Skip to content

Commit

Permalink
added excludeModules property to RouteModel
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-chepurnoi committed Feb 8, 2017
1 parent aedffec commit 053a991
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
],
]
],
]
Expand Down
15 changes: 11 additions & 4 deletions controllers/RouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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()]);
}
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -88,7 +95,7 @@ public function actionRemove()
*/
public function actionRefresh()
{
$model = new RouteModel();
$model = Yii::createObject($this->modelClass);
$model->invalidate();

return $model->getRoutes();
Expand Down
41 changes: 24 additions & 17 deletions models/RouteModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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__);
}

/**
Expand Down

0 comments on commit 053a991

Please sign in to comment.