Skip to content

Commit f72e433

Browse files
committed
added $deleteUnusedRoutes parameter to Route::refreshRoutes()
1 parent e8e2676 commit f72e433

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

controllers/PermissionController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ public function actionSetChildRoutes($id)
117117
* Add new routes and remove unused (for example if module or controller was deleted)
118118
*
119119
* @param string $id
120+
* @param null $deleteUnused
120121
*
121122
* @return \yii\web\Response
122123
*/
123-
public function actionRefreshRoutes($id)
124+
public function actionRefreshRoutes($id, $deleteUnused = null)
124125
{
125-
Route::refreshRoutes();
126+
Route::refreshRoutes($deleteUnused !== null);
126127

127128
return $this->redirect(['view', 'id'=>$id]);
128129
}

models/rbacDB/Route.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,42 @@ public static function isSubRoute($route, $candidate)
105105
}
106106

107107
/**
108-
* Refresh list of all routes from controllers, modules, etc
108+
* Refresh list of all routes from controllers, modules, etc.
109+
*
110+
* If $deleteUnusedRoutes is true, than all routes that are not longer exists in this application
111+
* (for example if you delete some controller or module) will be deleted.
112+
*
113+
* $deleteUnusedRoutes = false is recommended for application with "advanced" structure, because frontend
114+
* and backend have they own set of routes.
115+
*
116+
* @param bool $deleteUnusedRoutes
109117
*/
110-
public static function refreshRoutes()
118+
public static function refreshRoutes($deleteUnusedRoutes = true)
111119
{
112120
$allRoutes = AuthHelper::getRoutes();
113121

114122
$currentRoutes = ArrayHelper::map(Route::find()->asArray()->all(), 'name', 'name');
115123

116124
$toAdd = array_diff(array_keys($allRoutes), array_keys($currentRoutes));
117-
$toRemove = array_diff(array_keys($currentRoutes), array_keys($allRoutes));
118-
119125

120126
foreach ($toAdd as $addItem)
121127
{
122128
Route::create($addItem);
123129
}
124130

125-
if ( $toRemove )
131+
$toRemove = false;
132+
if ( $deleteUnusedRoutes )
126133
{
127-
Route::deleteAll(['in', 'name', $toRemove]);
134+
$toRemove = array_diff(array_keys($currentRoutes), array_keys($allRoutes));
135+
136+
if ( $toRemove )
137+
{
138+
Route::deleteAll(['in', 'name', $toRemove]);
139+
}
128140
}
129141

130-
if ( $toAdd OR $toRemove )
142+
143+
if ( $toAdd || $toRemove )
131144
{
132145
Yii::$app->cache->delete('__commonRoutes');
133146
}

views/permission/view.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,26 @@
8383
<strong>
8484
<span class="glyphicon glyphicon-th"></span> Routes
8585

86+
<?= Html::a(
87+
'Refresh routes (and delete unused)',
88+
['refresh-routes', 'id'=>$item->name, 'deleteUnused'=>1],
89+
[
90+
'class' => 'btn btn-default btn-sm pull-right',
91+
'style'=>'margin-top:-5px; text-transform:none;',
92+
'data-confirm'=>UserManagementModule::t('back', 'Routes that are not exists in this application will be deleted. Do not recommended for application with "advanced" structure, because frontend and backend have they own set of routes.'),
93+
]
94+
) ?>
95+
8696
<?= Html::a(
8797
'Refresh routes',
8898
['refresh-routes', 'id'=>$item->name],
8999
[
90100
'class' => 'btn btn-default btn-sm pull-right',
91-
'style'=>'margin-top:-5px',
101+
'style'=>'margin-top:-5px; text-transform:none;',
92102
]
93103
) ?>
94104

105+
95106
</strong>
96107
</div>
97108

0 commit comments

Comments
 (0)