Skip to content

[11.x] Named static methods for middleware #1695

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

Merged
merged 5 commits into from
Nov 2, 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
15 changes: 15 additions & 0 deletions src/Http/Middleware/CheckCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public function __construct(ResourceServer $server, TokenRepository $repository)
$this->repository = $repository;
}

/**
* Specify the scopes for the middleware.
*
* @param array|string $scopes
* @return string
*/
public static function using(...$scopes)
{
if (is_array($scopes[0])) {
return static::class.':'.implode(',', $scopes[0]);
} else {
return static::class.':'.implode(',', $scopes);
}
}

/**
* Handle an incoming request.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Http/Middleware/CheckForAnyScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

class CheckForAnyScope
{
/**
* Specify the scopes for the middleware.
*
* @param array|string $scopes
* @return string
*/
public static function using(...$scopes)
{
if (is_array($scopes[0])) {
return static::class.':'.implode(',', $scopes[0]);
} else {
return static::class.':'.implode(',', $scopes);
}
}

/**
* Handle the incoming request.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Http/Middleware/CheckScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

class CheckScopes
{
/**
* Specify the scopes for the middleware.
*
* @param array|string $scopes
* @return string
*/
public static function using(...$scopes)
{
if (is_array($scopes[0])) {
return static::class.':'.implode(',', $scopes[0]);
} else {
return static::class.':'.implode(',', $scopes);
}
}

/**
* Handle the incoming request.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Http/Middleware/CreateFreshApiToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public function __construct(ApiTokenCookieFactory $cookieFactory)
$this->cookieFactory = $cookieFactory;
}

/**
* Specify the guard for the middleware.
*
* @param string|null $guard
* @return string
*/
public static function using($guard = null)
{
$guard = is_null($guard) ? '' : ':'.$guard;

return static::class.$guard;
}

/**
* Handle an incoming request.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/ActingAsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware()
$response->assertSee('bar');
}

public function testItCanGenerateDefinitionViaStaticMethod()
{
$signature = (string) CheckScopes::using('admin');
$this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin', $signature);

$signature = (string) CheckScopes::using('admin', 'footest');
$this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin,footest', $signature);

$signature = (string) CheckForAnyScope::using('admin');
$this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin', $signature);

$signature = (string) CheckForAnyScope::using('admin', 'footest');
$this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature);
}

public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware()
{
$this->withoutExceptionHandling();
Expand Down