Skip to content

Commit 93bb9c3

Browse files
authored
Fix breaking change introduced by #1682 (#1686)
1 parent e225960 commit 93bb9c3

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/Bridge/ScopeRepository.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ class ScopeRepository implements ScopeRepositoryInterface
1212
/**
1313
* The client repository.
1414
*
15-
* @var \Laravel\Passport\ClientRepository
15+
* @var \Laravel\Passport\ClientRepository|null
1616
*/
17-
protected ClientRepository $clients;
17+
protected ?ClientRepository $clients;
1818

1919
/**
2020
* Create a new scope repository.
2121
*
22-
* @param \Laravel\Passport\ClientRepository $clients
22+
* @param \Laravel\Passport\ClientRepository|null $clients
2323
* @return void
2424
*/
25-
public function __construct(ClientRepository $clients)
25+
public function __construct(?ClientRepository $clients = null)
2626
{
2727
$this->clients = $clients;
2828
}
@@ -50,11 +50,12 @@ public function finalizeScopes(
5050
})->values()->all();
5151
}
5252

53-
$client = $this->clients->findActive($clientEntity->getIdentifier());
53+
$client = $this->clients?->findActive($clientEntity->getIdentifier());
5454

55-
return collect($scopes)->filter(function ($scope) use ($client) {
56-
return Passport::hasScope($scope->getIdentifier())
57-
&& $client->hasScope($scope->getIdentifier());
55+
return collect($scopes)->filter(function ($scope) {
56+
return Passport::hasScope($scope->getIdentifier());
57+
})->when($client, function ($scopes, $client) {
58+
return $scopes->filter(fn ($scope) => $client->hasScope($scope->getIdentifier()));
5859
})->values()->all();
5960
}
6061
}

tests/Unit/BridgeScopeRepositoryTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ public function test_invalid_scopes_are_removed()
3838
$this->assertEquals([$scope1], $scopes);
3939
}
4040

41+
public function test_invalid_scopes_are_removed_without_a_client_repository()
42+
{
43+
Passport::tokensCan([
44+
'scope-1' => 'description',
45+
]);
46+
47+
$repository = new ScopeRepository();
48+
49+
$scopes = $repository->finalizeScopes(
50+
[$scope1 = new Scope('scope-1'), new Scope('scope-2')], 'client_credentials', new Client('id', 'name', 'http://localhost'), 1
51+
);
52+
53+
$this->assertEquals([$scope1], $scopes);
54+
}
55+
4156
public function test_clients_do_not_restrict_scopes_by_default()
4257
{
4358
Passport::tokensCan([
@@ -126,4 +141,19 @@ public function test_superuser_scope_cant_be_applied_if_wrong_grant()
126141

127142
$this->assertEquals([], $scopes);
128143
}
144+
145+
public function test_superuser_scope_cant_be_applied_if_wrong_grant_without_a_client_repository()
146+
{
147+
Passport::tokensCan([
148+
'scope-1' => 'description',
149+
]);
150+
151+
$repository = new ScopeRepository();
152+
153+
$scopes = $repository->finalizeScopes(
154+
[$scope1 = new Scope('*')], 'refresh_token', new Client('id', 'name', 'http://localhost'), 1
155+
);
156+
157+
$this->assertEquals([], $scopes);
158+
}
129159
}

0 commit comments

Comments
 (0)