Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow scope repository to be constructed without parameters #1686

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Bridge/ScopeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class ScopeRepository implements ScopeRepositoryInterface
/**
* The client repository.
*
* @var \Laravel\Passport\ClientRepository
* @var \Laravel\Passport\ClientRepository|null
*/
protected ClientRepository $clients;
protected ?ClientRepository $clients;

/**
* Create a new scope repository.
*
* @param \Laravel\Passport\ClientRepository $clients
* @param \Laravel\Passport\ClientRepository|null $clients
* @return void
*/
public function __construct(ClientRepository $clients)
public function __construct(?ClientRepository $clients = null)
{
$this->clients = $clients;
}
Expand Down Expand Up @@ -50,11 +50,12 @@ public function finalizeScopes(
})->values()->all();
}

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

return collect($scopes)->filter(function ($scope) use ($client) {
return Passport::hasScope($scope->getIdentifier())
&& $client->hasScope($scope->getIdentifier());
return collect($scopes)->filter(function ($scope) {
return Passport::hasScope($scope->getIdentifier());
})->when($client, function ($scopes, $client) {
return $scopes->filter(fn ($scope) => $client->hasScope($scope->getIdentifier()));
})->values()->all();
}
}
30 changes: 30 additions & 0 deletions tests/Unit/BridgeScopeRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public function test_invalid_scopes_are_removed()
$this->assertEquals([$scope1], $scopes);
}

public function test_invalid_scopes_are_removed_without_a_client_repository()
{
Passport::tokensCan([
'scope-1' => 'description',
]);

$repository = new ScopeRepository();

$scopes = $repository->finalizeScopes(
[$scope1 = new Scope('scope-1'), new Scope('scope-2')], 'client_credentials', new Client('id', 'name', 'http://localhost'), 1
);

$this->assertEquals([$scope1], $scopes);
}

public function test_clients_do_not_restrict_scopes_by_default()
{
Passport::tokensCan([
Expand Down Expand Up @@ -126,4 +141,19 @@ public function test_superuser_scope_cant_be_applied_if_wrong_grant()

$this->assertEquals([], $scopes);
}

public function test_superuser_scope_cant_be_applied_if_wrong_grant_without_a_client_repository()
{
Passport::tokensCan([
'scope-1' => 'description',
]);

$repository = new ScopeRepository();

$scopes = $repository->finalizeScopes(
[$scope1 = new Scope('*')], 'refresh_token', new Client('id', 'name', 'http://localhost'), 1
);

$this->assertEquals([], $scopes);
}
}