Skip to content

Commit 3ef0331

Browse files
committed
Refactor middleware & migration
2 parents e2dc68e + af5228c commit 3ef0331

File tree

8 files changed

+33
-41
lines changed

8 files changed

+33
-41
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ An admin panel for managing users, roles, permissions & crud.
4848
4949
5. For checking authenticated user's role see below:
5050
```php
51-
// Add roles middleware in app/Http/Kernel.php
51+
// Add role middleware in app/Http/Kernel.php
5252
protected $routeMiddleware = [
5353
...
54-
'roles' => \App\Http\Middleware\CheckRole::class,
54+
'role' => \App\Http\Middleware\CheckRole::class,
5555
];
5656
```
5757
@@ -64,7 +64,12 @@ An admin panel for managing users, roles, permissions & crud.
6464
}
6565
6666
// Check role in route middleware
67-
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => 'admin'], function () {
67+
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => ['auth', 'role:admin']], function () {
68+
Route::get('/', ['uses' => 'AdminController@index']);
69+
});
70+
71+
// Check permission in route middleware
72+
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => ['auth', 'can:write_user']], function () {
6873
Route::get('/', ['uses' => 'AdminController@index']);
6974
});
7075
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
],
1818
"require": {
1919
"php": "^7.1",
20-
"illuminate/support": "^5.5|^6.0",
20+
"illuminate/support": "^5.5|^6.0|^7.0",
2121
"appzcoder/crud-generator": "^3.0",
22-
"laravelcollective/html": "^5.5|^6.0",
22+
"laravelcollective/html": "^5.5|^6.0|^7.0",
2323
"spatie/laravel-activitylog": "^3.2"
2424
},
2525
"autoload": {

publish/Middleware/CheckRole.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,18 @@ class CheckRole
1111
*
1212
* @param \Illuminate\Http\Request $request
1313
* @param \Closure $next
14+
* @param $role
1415
*
1516
* @return mixed
1617
*/
17-
public function handle($request, Closure $next)
18+
public function handle($request, Closure $next, $role)
1819
{
19-
// Get the required roles from the route
20-
$roles = $this->getRequiredRoleForRoute($request->route());
2120
// Check if a role is required for the route, and
2221
// if so, ensure that the user has that role.
23-
if ($request->user()->hasRole($roles) || !$roles) {
22+
if ($request->user()->hasRole($role) || !$role) {
2423
return $next($request);
2524
}
2625

27-
return response([
28-
'error' => [
29-
'code' => 'INSUFFICIENT_ROLE',
30-
'description' => 'You are not authorized to access this resource.',
31-
],
32-
], 401);
33-
}
34-
35-
private function getRequiredRoleForRoute($route)
36-
{
37-
$actions = $route->getAction();
38-
39-
return isset($actions['roles']) ? $actions['roles'] : null;
26+
abort(403, 'This action is unauthorized.');
4027
}
4128
}

publish/Providers/AuthServiceProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use App\Permission;
66
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
77
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
8+
use Illuminate\Support\Facades\Schema;
9+
use Illuminate\Database\QueryException;
810

911
class AuthServiceProvider extends ServiceProvider
1012
{
@@ -29,15 +31,15 @@ public function boot(GateContract $gate)
2931
parent::registerPolicies($gate);
3032

3133
try {
32-
if (\Schema::hasTable('permissions')) {
34+
if (Schema::hasTable('permissions')) {
3335
// Dynamically register permissions with Laravel's Gate.
3436
foreach ($this->getPermissions() as $permission) {
3537
$gate->define($permission->name, function ($user) use ($permission) {
3638
return $user->hasPermission($permission);
3739
});
3840
}
3941
}
40-
} catch (\Illuminate\Database\QueryException $ex) {
42+
} catch (QueryException $ex) {
4143
return;
4244
}
4345
}

publish/migrations/2016_01_01_193651_create_roles_permissions_tables.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
56

67
class CreateRolesPermissionsTables extends Migration
78
{
@@ -13,22 +14,22 @@ class CreateRolesPermissionsTables extends Migration
1314
public function up()
1415
{
1516
Schema::create('roles', function (Blueprint $table) {
16-
$table->increments('id');
17+
$table->bigIncrements('id');
1718
$table->string('name');
1819
$table->string('label')->nullable();
1920
$table->timestamps();
2021
});
2122

2223
Schema::create('permissions', function (Blueprint $table) {
23-
$table->increments('id');
24+
$table->bigIncrements('id');
2425
$table->string('name');
2526
$table->string('label')->nullable();
2627
$table->timestamps();
2728
});
2829

2930
Schema::create('permission_role', function (Blueprint $table) {
30-
$table->integer('permission_id')->unsigned();
31-
$table->integer('role_id')->unsigned();
31+
$table->unsignedBigInteger('permission_id');
32+
$table->unsignedBigInteger('role_id');
3233

3334
$table->foreign('permission_id')
3435
->references('id')
@@ -44,12 +45,9 @@ public function up()
4445
});
4546

4647
Schema::create('role_user', function (Blueprint $table) {
47-
$table->integer('role_id')->unsigned();
48-
if (\App::VERSION() >= '5.8') {
49-
$table->bigInteger('user_id')->unsigned();
50-
} else {
51-
$table->integer('user_id')->unsigned();
52-
}
48+
$table->unsignedBigInteger('role_id');
49+
$table->unsignedBigInteger('user_id');
50+
5351

5452
$table->foreign('role_id')
5553
->references('id')

publish/migrations/2018_08_01_183154_create_pages_table.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class CreatePagesTable extends Migration
1313
public function up()
1414
{
1515
Schema::create('pages', function (Blueprint $table) {
16-
$table->increments('id');
17-
$table->timestamps();
18-
$table->softDeletes();
16+
$table->bigIncrements('id');
1917
$table->string('title')->nullable();
2018
$table->text('content')->nullable();
21-
});
19+
$table->softDeletes();
20+
$table->timestamps();
21+
});
2222
}
2323

2424
/**

publish/resources/views/layouts/backend.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<title>{{ config('app.name', 'Laravel') }}</title>
1212

1313
<!-- Styles -->
14-
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
14+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
1515
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
1616
</head>
1717
<body>

src/LaravelAdminCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function handle()
4646
exit();
4747
}
4848

49-
if (\App::VERSION() >= '5.2') {
49+
if (\App::VERSION() >= '5.2' && \App::VERSION() < '6.0') {
5050
$this->info("Generating the authentication scaffolding");
5151
$this->call('make:auth');
5252
}
@@ -57,7 +57,7 @@ public function handle()
5757
$this->call('vendor:publish', ['--provider' => 'Spatie\Activitylog\ActivitylogServiceProvider', '--tag' => 'migrations']);
5858

5959
$this->info("Dumping the composer autoload");
60-
(new Process('composer dump-autoload'))->run();
60+
(new Process(['composer dump-autoload']))->run();
6161

6262
$this->info("Migrating the database tables into your application");
6363
$this->call('migrate');

0 commit comments

Comments
 (0)