Skip to content

Commit 56f9f49

Browse files
committed
Normal users should not be able to access our Users and CRUD menu. This requires new config file to be published with new key.
1 parent fad6b4f commit 56f9f49

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/Config/quickadmin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@
1717
'route' => 'admin',
1818
// Default home route
1919
'homeRoute' => 'admin',
20+
// Default role to access users and CRUD
21+
'defaultRole' => 1
2022

2123
];

src/Traits/AdminPermissionsTrait.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ public function permissionCan($request)
1010
if (is_null($request->route()->getName())) {
1111
return true;
1212
}
13-
1413
list($role, $crud) = $this->parseData($request);
15-
1614
if (in_array($role, explode(',', $crud->roles))) {
1715
return true;
1816
}
@@ -28,8 +26,17 @@ public function permissionCan($request)
2826
private function parseData($request)
2927
{
3028
$role = $request->user()->role_id;
31-
$crudName = explode('.', $request->route()->getName())[1];
32-
$crud = Crud::where('name', ucfirst($crudName))->firstOrFail();
29+
$route = explode('.', $request->route()->getName());
30+
$official = [
31+
'crud',
32+
'users'
33+
];
34+
if (in_array($route[0], $official)) {
35+
return [$role, (object) ['roles' => config('quickadmin.defaultRole') . ',']];
36+
} else {
37+
$crudName = $route[1];
38+
}
39+
$crud = Crud::where('name', ucfirst($crudName))->firstOrFail();
3340

3441
return [$role, $crud];
3542
}

src/routes.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,28 @@
2929
], function () {
3030
// Dashboard home page route
3131
Route::get(config('quickadmin.homeRoute'), 'QuickadminController@index');
32-
Route::get(config('quickadmin.route') . '/crud', 'QuickadminCrudController@create');
33-
Route::post(config('quickadmin.route') . '/crud', 'QuickadminCrudController@insert');
32+
Route::group([
33+
'middleware' => 'role'
34+
], function () {
35+
Route::get(config('quickadmin.route') . '/crud', [
36+
'as' => 'crud',
37+
'uses' => 'QuickadminCrudController@create'
38+
]);
39+
Route::post(config('quickadmin.route') . '/crud', [
40+
'as' => 'crud',
41+
'uses' => 'QuickadminCrudController@insert'
42+
]);
43+
});
3444
});
3545

3646
// @todo move to default routes.php
3747
Route::group(['namespace' => 'App\Http\Controllers'], function () {
3848
// Point to App\Http\Controllers\UsersController as a resource
39-
resource('users', 'UsersController');
49+
Route::group([
50+
'middleware' => 'role'
51+
], function () {
52+
resource('users', 'UsersController');
53+
});
4054
// Authentication routes...
4155
Route::get('auth/login', 'Auth\AuthController@getLogin');
4256
Route::post('auth/login', 'Auth\AuthController@postLogin');

0 commit comments

Comments
 (0)