Skip to content

Commit

Permalink
[11.x] Fix scope inheritance when using Passport::actingAs() (#1551)
Browse files Browse the repository at this point in the history
* Fix scope inheritance when using Passport::actingAs()

* Update Passport.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
axlon and taylorotwell authored Jul 13, 2022
1 parent 42677aa commit ed6f6c1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/Passport.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,9 @@ public static function ignoreCsrfToken($ignoreCsrfToken = true)
*/
public static function actingAs($user, $scopes = [], $guard = 'api')
{
$token = Mockery::mock(self::tokenModel())->shouldIgnoreMissing(false);
$token = app(self::tokenModel());

foreach ($scopes as $scope) {
$token->shouldReceive('can')->with($scope)->andReturn(true);
}
$token->scopes = $scopes;

$user->withAccessToken($token);

Expand Down
35 changes: 35 additions & 0 deletions tests/Feature/ActingAsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Route;
use Laravel\Passport\HasApiTokens;
use Laravel\Passport\Http\Middleware\CheckForAnyScope;
use Laravel\Passport\Http\Middleware\CheckScopes;
Expand Down Expand Up @@ -64,6 +65,40 @@ public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware(
$response->assertSuccessful();
$response->assertSee('bar');
}

public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddlewareWithInheritance()
{
Passport::$withInheritedScopes = true;

$this->withoutExceptionHandling();

Route::middleware(CheckScopes::class.':foo:bar,baz:qux')->get('/foo', function () {
return 'bar';
});

Passport::actingAs(new PassportUser(), ['foo', 'baz']);

$response = $this->get('/foo');
$response->assertSuccessful();
$response->assertSee('bar');
}

public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddlewareWithInheritance()
{
Passport::$withInheritedScopes = true;

$this->withoutExceptionHandling();

Route::middleware(CheckForAnyScope::class.':foo:baz,baz:qux')->get('/foo', function () {
return 'bar';
});

Passport::actingAs(new PassportUser(), ['foo']);

$response = $this->get('/foo');
$response->assertSuccessful();
$response->assertSee('bar');
}
}

class PassportUser extends User
Expand Down

0 comments on commit ed6f6c1

Please sign in to comment.