Skip to content

Commit

Permalink
check gate policies on middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Mar 30, 2023
1 parent b2562e9 commit 0f4b3fe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Middlewares/PermissionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function handle($request, Closure $next, $permission, $guard = null)
? $permission
: explode('|', $permission);

if (! $user->hasAnyPermission($permissions)) {
if (! $user->canAny($permissions)) {
throw UnauthorizedException::forPermissions($permissions);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Middlewares/RoleOrPermissionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function handle($request, Closure $next, $roleOrPermission, $guard = null
? $roleOrPermission
: explode('|', $roleOrPermission);

if (! $user->hasAnyRole($rolesOrPermissions) && ! $user->hasAnyPermission($rolesOrPermissions)) {
if (! $user->hasAnyRole($rolesOrPermissions) && ! $user->canAny($rolesOrPermissions)) {
throw UnauthorizedException::forRolesOrPermissions($rolesOrPermissions);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/PermissionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Gate;
use InvalidArgumentException;
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Exceptions\UnauthorizedException;
Expand Down Expand Up @@ -70,6 +71,21 @@ public function a_user_cannot_access_a_route_protected_by_the_permission_middlew
);
}

/** @test */
public function a_super_admin_user_can_access_a_route_protected_by_permission_middleware()
{
Auth::login($this->testUser);

Gate::before(function ($user, $ability) {
return $user->getKey() === $this->testUser->getKey() ? true : null;
});

$this->assertEquals(
200,
$this->runMiddleware($this->permissionMiddleware, 'edit-articles')
);
}

/** @test */
public function a_user_can_access_a_route_protected_by_permission_middleware_if_have_this_permission()
{
Expand Down
16 changes: 16 additions & 0 deletions tests/RoleMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Gate;
use InvalidArgumentException;
use Spatie\Permission\Exceptions\UnauthorizedException;
use Spatie\Permission\Middlewares\RoleMiddleware;
Expand Down Expand Up @@ -75,6 +76,21 @@ public function a_user_can_access_a_route_protected_by_this_role_middleware_if_h
);
}

/** @test */
public function a_super_admin_user_can_access_a_route_protected_by_the_role_middleware()
{
Auth::login($this->testUser);

Gate::before(function ($user, $ability) {
return $user->getKey() === $this->testUser->getKey() ? true : null;
});

$this->assertEquals(
200,
$this->runMiddleware($this->roleMiddleware, 'testRole')
);
}

/** @test */
public function a_user_cannot_access_a_route_protected_by_the_role_middleware_if_have_not_has_roles_trait()
{
Expand Down
16 changes: 16 additions & 0 deletions tests/RoleOrPermissionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Gate;
use InvalidArgumentException;
use Spatie\Permission\Exceptions\UnauthorizedException;
use Spatie\Permission\Middlewares\RoleOrPermissionMiddleware;
Expand Down Expand Up @@ -65,6 +66,21 @@ public function a_user_can_access_a_route_protected_by_permission_or_role_middle
);
}

/** @test */
public function a_super_admin_user_can_access_a_route_protected_by_permission_or_role_middleware()
{
Auth::login($this->testUser);

Gate::before(function ($user, $ability) {
return $user->getKey() === $this->testUser->getKey() ? true : null;
});

$this->assertEquals(
200,
$this->runMiddleware($this->roleOrPermissionMiddleware, 'testRole|edit-articles')
);
}

/** @test */
public function a_user_can_not_access_a_route_protected_by_permission_or_role_middleware_if_have_not_has_roles_trait()
{
Expand Down

0 comments on commit 0f4b3fe

Please sign in to comment.