Skip to content

Commit

Permalink
Refactor middleware & migration
Browse files Browse the repository at this point in the history
  • Loading branch information
sohelamin committed Aug 17, 2020
2 parents e2dc68e + af5228c commit 3ef0331
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 41 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ An admin panel for managing users, roles, permissions & crud.
5. For checking authenticated user's role see below:
```php
// Add roles middleware in app/Http/Kernel.php
// Add role middleware in app/Http/Kernel.php
protected $routeMiddleware = [
...
'roles' => \App\Http\Middleware\CheckRole::class,
'role' => \App\Http\Middleware\CheckRole::class,
];
```
Expand All @@ -64,7 +64,12 @@ An admin panel for managing users, roles, permissions & crud.
}
// Check role in route middleware
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => 'admin'], function () {
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => ['auth', 'role:admin']], function () {
Route::get('/', ['uses' => 'AdminController@index']);
});
// Check permission in route middleware
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => ['auth', 'can:write_user']], function () {
Route::get('/', ['uses' => 'AdminController@index']);
});
```
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
],
"require": {
"php": "^7.1",
"illuminate/support": "^5.5|^6.0",
"illuminate/support": "^5.5|^6.0|^7.0",
"appzcoder/crud-generator": "^3.0",
"laravelcollective/html": "^5.5|^6.0",
"laravelcollective/html": "^5.5|^6.0|^7.0",
"spatie/laravel-activitylog": "^3.2"
},
"autoload": {
Expand Down
21 changes: 4 additions & 17 deletions publish/Middleware/CheckRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,18 @@ class CheckRole
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param $role
*
* @return mixed
*/
public function handle($request, Closure $next)
public function handle($request, Closure $next, $role)
{
// Get the required roles from the route
$roles = $this->getRequiredRoleForRoute($request->route());
// Check if a role is required for the route, and
// if so, ensure that the user has that role.
if ($request->user()->hasRole($roles) || !$roles) {
if ($request->user()->hasRole($role) || !$role) {
return $next($request);
}

return response([
'error' => [
'code' => 'INSUFFICIENT_ROLE',
'description' => 'You are not authorized to access this resource.',
],
], 401);
}

private function getRequiredRoleForRoute($route)
{
$actions = $route->getAction();

return isset($actions['roles']) ? $actions['roles'] : null;
abort(403, 'This action is unauthorized.');
}
}
6 changes: 4 additions & 2 deletions publish/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Permission;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\QueryException;

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

try {
if (\Schema::hasTable('permissions')) {
if (Schema::hasTable('permissions')) {
// Dynamically register permissions with Laravel's Gate.
foreach ($this->getPermissions() as $permission) {
$gate->define($permission->name, function ($user) use ($permission) {
return $user->hasPermission($permission);
});
}
}
} catch (\Illuminate\Database\QueryException $ex) {
} catch (QueryException $ex) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRolesPermissionsTables extends Migration
{
Expand All @@ -13,22 +14,22 @@ class CreateRolesPermissionsTables extends Migration
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->bigIncrements('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});

Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->bigIncrements('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});

Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->unsignedBigInteger('permission_id');
$table->unsignedBigInteger('role_id');

$table->foreign('permission_id')
->references('id')
Expand All @@ -44,12 +45,9 @@ public function up()
});

Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
if (\App::VERSION() >= '5.8') {
$table->bigInteger('user_id')->unsigned();
} else {
$table->integer('user_id')->unsigned();
}
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');


$table->foreign('role_id')
->references('id')
Expand Down
8 changes: 4 additions & 4 deletions publish/migrations/2018_08_01_183154_create_pages_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class CreatePagesTable extends Migration
public function up()
{
Schema::create('pages', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->bigIncrements('id');
$table->string('title')->nullable();
$table->text('content')->nullable();
});
$table->softDeletes();
$table->timestamps();
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion publish/resources/views/layouts/backend.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<title>{{ config('app.name', 'Laravel') }}</title>

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
Expand Down
4 changes: 2 additions & 2 deletions src/LaravelAdminCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function handle()
exit();
}

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

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

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

0 comments on commit 3ef0331

Please sign in to comment.