Skip to content

Commit 7136338

Browse files
[10.x] Named static methods for middleware (#46362)
* wip * Standardise of `using` for Authorization middleware * Update ValidateSignature.php * Update ThrottleRequests.php * Update ThrottleRequests.php * Update RequirePassword.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 2922575 commit 7136338

15 files changed

+247
-2
lines changed

src/Illuminate/Auth/Middleware/Authenticate.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ public function __construct(Auth $auth)
2828
$this->auth = $auth;
2929
}
3030

31+
/**
32+
* Specify the guards for the middleware.
33+
*
34+
* @param string $guard
35+
* @param string $others
36+
* @return string
37+
*/
38+
public static function using($guard, ...$others)
39+
{
40+
return static::class.':'.implode(',', [$guard, ...$others]);
41+
}
42+
3143
/**
3244
* Handle an incoming request.
3345
*

src/Illuminate/Auth/Middleware/AuthenticateWithBasicAuth.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public function __construct(AuthFactory $auth)
2525
$this->auth = $auth;
2626
}
2727

28+
/**
29+
* Specify the guard and field for the middleware.
30+
*
31+
* @param string|null $guard
32+
* @param string|null $field
33+
* @return string
34+
*
35+
* @named-arguments-supported
36+
*/
37+
public static function using($guard = null, $field = null)
38+
{
39+
return static::class.':'.implode(',', func_get_args());
40+
}
41+
2842
/**
2943
* Handle an incoming request.
3044
*

src/Illuminate/Auth/Middleware/Authorize.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ public function __construct(Gate $gate)
2626
$this->gate = $gate;
2727
}
2828

29+
/**
30+
* Specify the ability and models for the middleware.
31+
*
32+
* @param string $ability
33+
* @param string ...$models
34+
* @return string
35+
*/
36+
public static function using($ability, ...$models)
37+
{
38+
return static::class.':'.implode(',', [$ability, ...$models]);
39+
}
40+
2941
/**
3042
* Handle an incoming request.
3143
*

src/Illuminate/Auth/Middleware/EnsureEmailIsVerified.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
class EnsureEmailIsVerified
1111
{
12+
/**
13+
* Specify the redirect route for the middleware.
14+
*
15+
* @param string $route
16+
* @return string
17+
*/
18+
public static function redirectTo($route)
19+
{
20+
return static::class.':'.$route;
21+
}
22+
1223
/**
1324
* Handle an incoming request.
1425
*

src/Illuminate/Auth/Middleware/RequirePassword.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,27 @@ public function __construct(ResponseFactory $responseFactory, UrlGenerator $urlG
4444
$this->passwordTimeout = $passwordTimeout ?: 10800;
4545
}
4646

47+
/**
48+
* Specify the redirect route and timeout for the middleware.
49+
*
50+
* @param string|null $redirectToRoute
51+
* @param string|null $passwordTimeoutSeconds
52+
* @return string
53+
*
54+
* @named-arguments-supported
55+
*/
56+
public static function using($redirectToRoute = null, $passwordTimeoutSeconds = null)
57+
{
58+
return static::class.':'.implode(',', func_get_args());
59+
}
60+
4761
/**
4862
* Handle an incoming request.
4963
*
5064
* @param \Illuminate\Http\Request $request
5165
* @param \Closure $next
5266
* @param string|null $redirectToRoute
53-
* @param int|null $passwordTimeoutSeconds
67+
* @param string|int|null $passwordTimeoutSeconds
5468
* @return mixed
5569
*/
5670
public function handle($request, Closure $next, $redirectToRoute = null, $passwordTimeoutSeconds = null)
@@ -63,7 +77,7 @@ public function handle($request, Closure $next, $redirectToRoute = null, $passwo
6377
}
6478

6579
return $this->responseFactory->redirectGuest(
66-
$this->urlGenerator->route($redirectToRoute ?? 'password.confirm')
80+
$this->urlGenerator->route($redirectToRoute ?: 'password.confirm')
6781
);
6882
}
6983

src/Illuminate/Http/Middleware/SetCacheHeaders.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,29 @@
44

55
use Closure;
66
use Illuminate\Support\Carbon;
7+
use Illuminate\Support\Str;
78
use Symfony\Component\HttpFoundation\BinaryFileResponse;
89

910
class SetCacheHeaders
1011
{
12+
/**
13+
* Specify the options for the middleware.
14+
*
15+
* @param array|string $options
16+
* @return string
17+
*/
18+
public static function using($options)
19+
{
20+
if (is_string($options)) {
21+
return static::class.':'.$options;
22+
}
23+
24+
return collect($options)
25+
->map(fn ($value, $key) => is_int($key) ? $value : "{$key}={$value}")
26+
->map(fn ($value) => Str::finish($value, ';'))
27+
->pipe(fn ($options) => rtrim(static::class.':'.$options->implode(''), ';'));
28+
}
29+
1130
/**
1231
* Add cache related HTTP headers.
1332
*

src/Illuminate/Routing/Middleware/ThrottleRequests.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ public function __construct(RateLimiter $limiter)
3434
$this->limiter = $limiter;
3535
}
3636

37+
/**
38+
* Specify the named rate limiter to use for the middleware.
39+
*
40+
* @param string $name
41+
* @return string
42+
*/
43+
public static function using($name)
44+
{
45+
return static::class.':'.$name;
46+
}
47+
48+
/**
49+
* Specify the rate limiter configuration for the middleware.
50+
*
51+
* @param int $maxAttempts
52+
* @param int $decayMinutes
53+
* @param string $prefix
54+
* @return string
55+
*
56+
* @named-arguments-supported
57+
*/
58+
public static function with($maxAttempts = 60, $decayMinutes = 1, $prefix = '')
59+
{
60+
return static::class.':'.implode(',', func_get_args());
61+
}
62+
3763
/**
3864
* Handle an incoming request.
3965
*

src/Illuminate/Routing/Middleware/ValidateSignature.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ class ValidateSignature
1616
//
1717
];
1818

19+
/**
20+
* Specify that the URL signature is for a relative URL.
21+
*
22+
* @return string
23+
*/
24+
public static function relative()
25+
{
26+
return static::class.':relative';
27+
}
28+
29+
/**
30+
* Specify that the URL signature is for an absolute URL.
31+
*
32+
* @return class-string
33+
*/
34+
public static function absolute()
35+
{
36+
return static::class;
37+
}
38+
1939
/**
2040
* Handle an incoming request.
2141
*

tests/Auth/AuthenticateMiddlewareTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Auth\AuthManager;
77
use Illuminate\Auth\EloquentUserProvider;
88
use Illuminate\Auth\Middleware\Authenticate;
9+
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
910
use Illuminate\Auth\RequestGuard;
1011
use Illuminate\Config\Repository as Config;
1112
use Illuminate\Container\Container;
@@ -36,6 +37,30 @@ protected function tearDown(): void
3637
Container::setInstance(null);
3738
}
3839

40+
public function testItCanGenerateDefinitionViaStaticMethod()
41+
{
42+
$signature = (string) Authenticate::using('foo');
43+
$this->assertSame('Illuminate\Auth\Middleware\Authenticate:foo', $signature);
44+
45+
$signature = (string) Authenticate::using('foo', 'bar');
46+
$this->assertSame('Illuminate\Auth\Middleware\Authenticate:foo,bar', $signature);
47+
48+
$signature = (string) Authenticate::using('foo', 'bar', 'baz');
49+
$this->assertSame('Illuminate\Auth\Middleware\Authenticate:foo,bar,baz', $signature);
50+
}
51+
52+
public function testItCanGenerateDefinitionViaStaticMethodForBasic()
53+
{
54+
$signature = (string) AuthenticateWithBasicAuth::using('guard');
55+
$this->assertSame('Illuminate\Auth\Middleware\AuthenticateWithBasicAuth:guard', $signature);
56+
57+
$signature = (string) AuthenticateWithBasicAuth::using('guard', 'field');
58+
$this->assertSame('Illuminate\Auth\Middleware\AuthenticateWithBasicAuth:guard,field', $signature);
59+
60+
$signature = (string) AuthenticateWithBasicAuth::using(field: 'field');
61+
$this->assertSame('Illuminate\Auth\Middleware\AuthenticateWithBasicAuth:,field', $signature);
62+
}
63+
3964
public function testDefaultUnauthenticatedThrows()
4065
{
4166
$this->expectException(AuthenticationException::class);

tests/Auth/AuthorizeMiddlewareTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ protected function tearDown(): void
5555
Container::setInstance(null);
5656
}
5757

58+
public function testItCanGenerateDefinitionViaStaticMethod()
59+
{
60+
$signature = (string) Authorize::using('ability');
61+
$this->assertSame('Illuminate\Auth\Middleware\Authorize:ability', $signature);
62+
63+
$signature = (string) Authorize::using('ability', 'model');
64+
$this->assertSame('Illuminate\Auth\Middleware\Authorize:ability,model', $signature);
65+
66+
$signature = (string) Authorize::using('ability', 'model', \App\Models\Comment::class);
67+
$this->assertSame('Illuminate\Auth\Middleware\Authorize:ability,model,App\Models\Comment', $signature);
68+
}
69+
5870
public function testSimpleAbilityUnauthorized()
5971
{
6072
$this->expectException(AuthorizationException::class);

0 commit comments

Comments
 (0)