Skip to content

Commit

Permalink
[10.x] Named static methods for middleware (#46362)
Browse files Browse the repository at this point in the history
* wip

* Standardise of `using` for Authorization middleware

* Update ValidateSignature.php

* Update ThrottleRequests.php

* Update ThrottleRequests.php

* Update RequirePassword.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
timacdonald and taylorotwell authored Apr 24, 2023
1 parent 107f5de commit 3fb38d0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public function __construct(Auth $auth)
$this->auth = $auth;
}

/**
* Specify the guards for the middleware.
*
* @param string $guard
* @param string $others
* @return string
*/
public static function using($guard, ...$others)
{
return static::class.':'.implode(',', [$guard, ...$others]);
}

/**
* Handle an incoming request.
*
Expand Down
14 changes: 14 additions & 0 deletions Middleware/AuthenticateWithBasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ public function __construct(AuthFactory $auth)
$this->auth = $auth;
}

/**
* Specify the guard and field for the middleware.
*
* @param string|null $guard
* @param string|null $field
* @return string
*
* @named-arguments-supported
*/
public static function using($guard = null, $field = null)
{
return static::class.':'.implode(',', func_get_args());
}

/**
* Handle an incoming request.
*
Expand Down
12 changes: 12 additions & 0 deletions Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public function __construct(Gate $gate)
$this->gate = $gate;
}

/**
* Specify the ability and models for the middleware.
*
* @param string $ability
* @param string ...$models
* @return string
*/
public static function using($ability, ...$models)
{
return static::class.':'.implode(',', [$ability, ...$models]);
}

/**
* Handle an incoming request.
*
Expand Down
11 changes: 11 additions & 0 deletions Middleware/EnsureEmailIsVerified.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

class EnsureEmailIsVerified
{
/**
* Specify the redirect route for the middleware.
*
* @param string $route
* @return string
*/
public static function redirectTo($route)
{
return static::class.':'.$route;
}

/**
* Handle an incoming request.
*
Expand Down
18 changes: 16 additions & 2 deletions Middleware/RequirePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,27 @@ public function __construct(ResponseFactory $responseFactory, UrlGenerator $urlG
$this->passwordTimeout = $passwordTimeout ?: 10800;
}

/**
* Specify the redirect route and timeout for the middleware.
*
* @param string|null $redirectToRoute
* @param string|null $passwordTimeoutSeconds
* @return string
*
* @named-arguments-supported
*/
public static function using($redirectToRoute = null, $passwordTimeoutSeconds = null)
{
return static::class.':'.implode(',', func_get_args());
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $redirectToRoute
* @param int|null $passwordTimeoutSeconds
* @param string|int|null $passwordTimeoutSeconds
* @return mixed
*/
public function handle($request, Closure $next, $redirectToRoute = null, $passwordTimeoutSeconds = null)
Expand All @@ -63,7 +77,7 @@ public function handle($request, Closure $next, $redirectToRoute = null, $passwo
}

return $this->responseFactory->redirectGuest(
$this->urlGenerator->route($redirectToRoute ?? 'password.confirm')
$this->urlGenerator->route($redirectToRoute ?: 'password.confirm')
);
}

Expand Down

0 comments on commit 3fb38d0

Please sign in to comment.