Skip to content

Commit d60da1c

Browse files
committed
Fix Tests
1 parent ebf02b0 commit d60da1c

9 files changed

+155
-46
lines changed

.github/workflows/run-tests-L8.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ jobs:
4040
extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
4141
coverage: none
4242

43+
- name: Install dependencies (remove passport)
44+
run: composer remove --dev laravel/passport --no-interaction --no-update
45+
if: matrix.laravel == '8.*'
46+
4347
- name: Install dependencies
4448
run: |
4549
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "symfony/console:>=4.3.4" "mockery/mockery:^1.3.2" "nesbot/carbon:>=2.62.1" --no-interaction --no-update

src/Guard.php

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

33
namespace Spatie\Permission;
44

5+
use Illuminate\Contracts\Auth\Access\Authorizable;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\Auth;
79

810
class Guard
911
{
@@ -72,4 +74,34 @@ public static function getDefaultName($class): string
7274

7375
return $possible_guards->first() ?: $default;
7476
}
77+
78+
/**
79+
* Lookup a passport guard
80+
*/
81+
public static function getPassportClient($guard): ?Authorizable
82+
{
83+
$guards = collect(config('auth.guards'))->where('driver', 'passport');
84+
85+
if (! $guards->count()) {
86+
return null;
87+
}
88+
89+
$authGuard = Auth::guard($guards->keys()[0]);
90+
91+
if (! \method_exists($authGuard, 'client')) {
92+
return null;
93+
}
94+
95+
$client = $authGuard->client();
96+
97+
if (! $guard || ! $client) {
98+
return $client;
99+
}
100+
101+
if (self::getNames($client)->contains($guard)) {
102+
return $client;
103+
}
104+
105+
return null;
106+
}
75107
}

src/Middlewares/PermissionMiddleware.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,22 @@
44

55
use Closure;
66
use Illuminate\Support\Facades\Auth;
7-
use Laravel\Passport\Guards\TokenGuard;
87
use Spatie\Permission\Exceptions\UnauthorizedException;
8+
use Spatie\Permission\Guard;
99

1010
class PermissionMiddleware
1111
{
1212
public function handle($request, Closure $next, $permission, $guard = null)
1313
{
1414
$authGuard = Auth::guard($guard);
1515

16+
$user = $authGuard->user();
17+
1618
// For machine-to-machine Passport clients
17-
$bearerToken = $request->bearerToken();
18-
if ($bearerToken) {
19-
if (! $authGuard instanceof TokenGuard && ! $guard) {
20-
$authGuard = Auth::guard('api');
21-
}
22-
23-
if (method_exists($authGuard, 'client')) {
24-
$user = $authGuard->client();
25-
}
19+
if (! $user && $request->bearerToken()) {
20+
$user = Guard::getPassportClient($guard);
2621
}
2722

28-
$user = $user ?? $authGuard->user();
29-
3023
if (! $user) {
3124
throw UnauthorizedException::notLoggedIn();
3225
}

src/Middlewares/RoleMiddleware.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,22 @@
44

55
use Closure;
66
use Illuminate\Support\Facades\Auth;
7-
use Laravel\Passport\Guards\TokenGuard;
87
use Spatie\Permission\Exceptions\UnauthorizedException;
8+
use Spatie\Permission\Guard;
99

1010
class RoleMiddleware
1111
{
1212
public function handle($request, Closure $next, $role, $guard = null)
1313
{
1414
$authGuard = Auth::guard($guard);
1515

16+
$user = $authGuard->user();
17+
1618
// For machine-to-machine Passport clients
17-
$bearerToken = $request->bearerToken();
18-
if ($bearerToken) {
19-
if (! $authGuard instanceof TokenGuard && ! $guard) {
20-
$authGuard = Auth::guard('api');
21-
}
22-
23-
if (method_exists($authGuard, 'client')) {
24-
$user = $authGuard->client();
25-
}
19+
if (! $user && $request->bearerToken()) {
20+
$user = Guard::getPassportClient($guard);
2621
}
2722

28-
$user = $user ?? $authGuard->user();
29-
3023
if (! $user) {
3124
throw UnauthorizedException::notLoggedIn();
3225
}

src/Middlewares/RoleOrPermissionMiddleware.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,22 @@
44

55
use Closure;
66
use Illuminate\Support\Facades\Auth;
7-
use Laravel\Passport\Guards\TokenGuard;
87
use Spatie\Permission\Exceptions\UnauthorizedException;
8+
use Spatie\Permission\Guard;
99

1010
class RoleOrPermissionMiddleware
1111
{
1212
public function handle($request, Closure $next, $roleOrPermission, $guard = null)
1313
{
1414
$authGuard = Auth::guard($guard);
1515

16+
$user = $authGuard->user();
17+
1618
// For machine-to-machine Passport clients
17-
$bearerToken = $request->bearerToken();
18-
if ($bearerToken) {
19-
if (! $authGuard instanceof TokenGuard && ! $guard) {
20-
$authGuard = Auth::guard('api');
21-
}
22-
23-
if (method_exists($authGuard, 'client')) {
24-
$user = $authGuard->client();
25-
}
19+
if (! $user && $request->bearerToken()) {
20+
$user = Guard::getPassportClient($guard);
2621
}
2722

28-
$user = $user ?? $authGuard->user();
29-
3023
if (! $user) {
3124
throw UnauthorizedException::notLoggedIn();
3225
}

tests/PermissionMiddlewareTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class PermissionMiddlewareTest extends TestCase
1818
{
1919
protected $permissionMiddleware;
2020

21+
protected $usePassport = true;
22+
2123
protected function setUp(): void
2224
{
2325
parent::setUp();
@@ -75,6 +77,10 @@ public function a_user_cannot_access_a_route_protected_by_the_permission_middlew
7577
/** @test */
7678
public function a_client_cannot_access_a_route_protected_by_the_permission_middleware_of_a_different_guard(): void
7779
{
80+
if ($this->getLaravelVersion() < 9) {
81+
$this->markTestSkipped('requires laravel >= 9');
82+
}
83+
7884
// These permissions are created fresh here in reverse order of guard being applied, so they are not "found first" in the db lookup when matching
7985
app(Permission::class)->create(['name' => 'admin-permission2', 'guard_name' => 'web']);
8086
$p1 = app(Permission::class)->create(['name' => 'admin-permission2', 'guard_name' => 'api']);
@@ -125,6 +131,10 @@ public function a_user_can_access_a_route_protected_by_permission_middleware_if_
125131
/** @test */
126132
public function a_client_can_access_a_route_protected_by_permission_middleware_if_have_this_permission(): void
127133
{
134+
if ($this->getLaravelVersion() < 9) {
135+
$this->markTestSkipped('requires laravel >= 9');
136+
}
137+
128138
Passport::actingAsClient($this->testClient, ['*'], 'api');
129139

130140
$this->testClient->givePermissionTo('edit-posts');
@@ -156,6 +166,10 @@ public function a_user_can_access_a_route_protected_by_this_permission_middlewar
156166
/** @test */
157167
public function a_client_can_access_a_route_protected_by_this_permission_middleware_if_have_one_of_the_permissions(): void
158168
{
169+
if ($this->getLaravelVersion() < 9) {
170+
$this->markTestSkipped('requires laravel >= 9');
171+
}
172+
159173
Passport::actingAsClient($this->testClient, ['*']);
160174

161175
$this->testClient->givePermissionTo('edit-posts');
@@ -200,6 +214,10 @@ public function a_user_cannot_access_a_route_protected_by_the_permission_middlew
200214
/** @test */
201215
public function a_client_cannot_access_a_route_protected_by_the_permission_middleware_if_have_a_different_permission(): void
202216
{
217+
if ($this->getLaravelVersion() < 9) {
218+
$this->markTestSkipped('requires laravel >= 9');
219+
}
220+
203221
Passport::actingAsClient($this->testClient, ['*']);
204222

205223
$this->testClient->givePermissionTo('edit-posts');
@@ -224,6 +242,10 @@ public function a_user_cannot_access_a_route_protected_by_permission_middleware_
224242
/** @test */
225243
public function a_client_cannot_access_a_route_protected_by_permission_middleware_if_have_not_permissions(): void
226244
{
245+
if ($this->getLaravelVersion() < 9) {
246+
$this->markTestSkipped('requires laravel >= 9');
247+
}
248+
227249
Passport::actingAsClient($this->testClient, ['*']);
228250

229251
$this->assertEquals(
@@ -254,6 +276,10 @@ public function a_user_can_access_a_route_protected_by_permission_middleware_if_
254276
/** @test */
255277
public function a_client_can_access_a_route_protected_by_permission_middleware_if_has_permission_via_role(): void
256278
{
279+
if ($this->getLaravelVersion() < 9) {
280+
$this->markTestSkipped('requires laravel >= 9');
281+
}
282+
257283
Passport::actingAsClient($this->testClient, ['*']);
258284

259285
$this->assertEquals(
@@ -342,6 +368,10 @@ public function user_can_not_access_permission_with_guard_admin_while_login_usin
342368
/** @test */
343369
public function client_can_not_access_permission_with_guard_admin_while_login_using_default_guard(): void
344370
{
371+
if ($this->getLaravelVersion() < 9) {
372+
$this->markTestSkipped('requires laravel >= 9');
373+
}
374+
345375
Passport::actingAsClient($this->testClient, ['*']);
346376

347377
$this->testClient->givePermissionTo('edit-posts');

tests/RoleMiddlewareTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class RoleMiddlewareTest extends TestCase
1616
{
1717
protected $roleMiddleware;
1818

19+
protected $usePassport = true;
20+
1921
protected function setUp(): void
2022
{
2123
parent::setUp();
@@ -48,6 +50,10 @@ public function a_user_cannot_access_a_route_protected_by_role_middleware_of_ano
4850
/** @test */
4951
public function a_client_cannot_access_a_route_protected_by_role_middleware_of_another_guard(): void
5052
{
53+
if ($this->getLaravelVersion() < 9) {
54+
$this->markTestSkipped('requires laravel >= 9');
55+
}
56+
5157
Passport::actingAsClient($this->testClient, ['*']);
5258

5359
$this->testClient->assignRole('clientRole');
@@ -74,6 +80,10 @@ public function a_user_can_access_a_route_protected_by_role_middleware_if_have_t
7480
/** @test */
7581
public function a_client_can_access_a_route_protected_by_role_middleware_if_have_this_role(): void
7682
{
83+
if ($this->getLaravelVersion() < 9) {
84+
$this->markTestSkipped('requires laravel >= 9');
85+
}
86+
7787
Passport::actingAsClient($this->testClient, ['*']);
7888

7989
$this->testClient->assignRole('clientRole');
@@ -105,6 +115,10 @@ public function a_user_can_access_a_route_protected_by_this_role_middleware_if_h
105115
/** @test */
106116
public function a_client_can_access_a_route_protected_by_this_role_middleware_if_have_one_of_the_roles(): void
107117
{
118+
if ($this->getLaravelVersion() < 9) {
119+
$this->markTestSkipped('requires laravel >= 9');
120+
}
121+
108122
Passport::actingAsClient($this->testClient, ['*']);
109123

110124
$this->testClient->assignRole('clientRole');
@@ -149,6 +163,10 @@ public function a_user_cannot_access_a_route_protected_by_the_role_middleware_if
149163
/** @test */
150164
public function a_client_cannot_access_a_route_protected_by_the_role_middleware_if_have_a_different_role(): void
151165
{
166+
if ($this->getLaravelVersion() < 9) {
167+
$this->markTestSkipped('requires laravel >= 9');
168+
}
169+
152170
Passport::actingAsClient($this->testClient, ['*']);
153171

154172
$this->testClient->assignRole(['clientRole']);
@@ -173,6 +191,10 @@ public function a_user_cannot_access_a_route_protected_by_role_middleware_if_hav
173191
/** @test */
174192
public function a_client_cannot_access_a_route_protected_by_role_middleware_if_have_not_roles(): void
175193
{
194+
if ($this->getLaravelVersion() < 9) {
195+
$this->markTestSkipped('requires laravel >= 9');
196+
}
197+
176198
Passport::actingAsClient($this->testClient, ['*']);
177199

178200
$this->assertEquals(
@@ -195,6 +217,10 @@ public function a_user_cannot_access_a_route_protected_by_role_middleware_if_rol
195217
/** @test */
196218
public function a_client_cannot_access_a_route_protected_by_role_middleware_if_role_is_undefined(): void
197219
{
220+
if ($this->getLaravelVersion() < 9) {
221+
$this->markTestSkipped('requires laravel >= 9');
222+
}
223+
198224
Passport::actingAsClient($this->testClient, ['*']);
199225

200226
$this->assertEquals(
@@ -275,6 +301,10 @@ public function user_can_not_access_role_with_guard_admin_while_login_using_defa
275301
/** @test */
276302
public function client_can_not_access_role_with_guard_admin_while_login_using_default_guard(): void
277303
{
304+
if ($this->getLaravelVersion() < 9) {
305+
$this->markTestSkipped('requires laravel >= 9');
306+
}
307+
278308
Passport::actingAsClient($this->testClient, ['*']);
279309

280310
$this->testClient->assignRole('clientRole');

tests/RoleOrPermissionMiddlewareTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class RoleOrPermissionMiddlewareTest extends TestCase
1717
{
1818
protected $roleOrPermissionMiddleware;
1919

20+
protected $usePassport = true;
21+
2022
protected function setUp(): void
2123
{
2224
parent::setUp();
@@ -70,6 +72,10 @@ public function a_user_can_access_a_route_protected_by_permission_or_role_middle
7072
/** @test */
7173
public function a_client_can_access_a_route_protected_by_permission_or_role_middleware_if_has_this_permission_or_role(): void
7274
{
75+
if ($this->getLaravelVersion() < 9) {
76+
$this->markTestSkipped('requires laravel >= 9');
77+
}
78+
7379
Passport::actingAsClient($this->testClient, ['*']);
7480

7581
$this->testClient->assignRole('clientRole');
@@ -148,6 +154,10 @@ public function a_user_can_not_access_a_route_protected_by_permission_or_role_mi
148154
/** @test */
149155
public function a_client_can_not_access_a_route_protected_by_permission_or_role_middleware_if_have_not_this_permission_and_role(): void
150156
{
157+
if ($this->getLaravelVersion() < 9) {
158+
$this->markTestSkipped('requires laravel >= 9');
159+
}
160+
151161
Passport::actingAsClient($this->testClient, ['*']);
152162

153163
$this->assertEquals(
@@ -194,6 +204,10 @@ public function user_can_not_access_permission_or_role_with_guard_admin_while_lo
194204
/** @test */
195205
public function client_can_not_access_permission_or_role_with_guard_admin_while_login_using_default_guard(): void
196206
{
207+
if ($this->getLaravelVersion() < 9) {
208+
$this->markTestSkipped('requires laravel >= 9');
209+
}
210+
197211
Passport::actingAsClient($this->testClient, ['*']);
198212

199213
$this->testClient->assignRole('clientRole');

0 commit comments

Comments
 (0)