Skip to content

Commit

Permalink
New feat Middleware (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
eramitgupta authored Dec 25, 2024
2 parents eac6bef + 024c588 commit 09e487b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,19 @@ if (hasRole('admin')) {

## Middleware Usage


```php
Route::group(['middleware' => ['role:admin,post-create']], function () {
// Routes protected by role and permission
// Routes protected by role and permissions
});

Route::group(['middleware' => ['permissions:post-create']], function () {
// Routes protected by permissions
});

Route::post('/create-post', [PostController::class, 'create'])->name('post.create')->middleware('role:admin,post-create');
Route::post('/create-post', [PostController::class, 'create'])->name('post.create')->middleware('permissions:post-create');

```

## How to Use Permissions Expiration
Expand Down
28 changes: 28 additions & 0 deletions src/Middleware/PermissionsMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace EragPermission\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class PermissionsMiddleware
{
/**
* Handle an incoming request.
*
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next, ...$permissions): Response
{
if (! $request->user()) {
abort(403, 'Unauthorized action.');
}

if (! $request->user()->hasPermissions($permissions)) {
abort(403, 'You do not have the required permission.');
}

return $next($request);
}
}
6 changes: 2 additions & 4 deletions src/Middleware/RolePermissionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class RolePermissionMiddleware
{
/**
* Handle an incoming request.
*
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next, $role = null, $permission = null): Response
{
Expand All @@ -20,10 +18,10 @@ public function handle(Request $request, Closure $next, $role = null, $permissio
}

if (! $request->user()->hasRole($role)) {
abort(404, 'Unauthorized action.');
abort(403, 'You do not have the required role.');
}
if ($permission !== null && ! $request->user()->hasPermissions($permission)) {
abort(404, 'Unauthorized action.');
abort(403, 'You do not have the required permission.');
}

return $next($request);
Expand Down
4 changes: 4 additions & 0 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EragPermission\Commands\UpgradeVersions;
use EragPermission\Contracts\PermissionContract;
use EragPermission\Contracts\RoleContract;
use EragPermission\Middleware\PermissionsMiddleware;
use EragPermission\Middleware\RolePermissionMiddleware;
use EragPermission\Models\Permission;
use EragPermission\Models\Role;
Expand Down Expand Up @@ -54,6 +55,9 @@ public function boot(Router $router): void
$router->aliasMiddleware('role', RolePermissionMiddleware::class);
$router->middlewareGroup('role', [RolePermissionMiddleware::class]);

$router->aliasMiddleware('permissions', PermissionsMiddleware::class);
$router->middlewareGroup('permissions', [PermissionsMiddleware::class]);

if (Schema::hasTable('users') && Schema::hasTable('roles') && Schema::hasTable('permissions')) {
Permission::with('roles.users')->get()->each(function ($permission) {
Gate::define($permission->name, function ($user) use ($permission) {
Expand Down
2 changes: 0 additions & 2 deletions src/Traits/HasPermissionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ public function hasPermissions(string|array $permissions): bool

public function hasPermissionThroughRole($permission): bool
{
$this->load('roles');

return $this->roles->pluck('id')->intersect($permission->roles->pluck('id'))->isNotEmpty();
}

Expand Down

0 comments on commit 09e487b

Please sign in to comment.