forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pkp#10343 from touhidurabir/i10342_main
pkp#10342 Required depending middleware attachment check
- Loading branch information
Showing
3 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/** | ||
* @file classes/middleware/traits/HasRequiredMiddleware.php | ||
* | ||
* Copyright (c) 2024 Simon Fraser University | ||
* Copyright (c) 2024 John Willinsky | ||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. | ||
* | ||
* @class HasRequiredMiddleware | ||
* | ||
* @brief Trait to find the required middleware attachments in a middleware | ||
*/ | ||
|
||
namespace PKP\middleware\traits; | ||
|
||
use Exception; | ||
use Illuminate\Http\Request; | ||
use PKP\core\PKPBaseController; | ||
|
||
trait HasRequiredMiddleware | ||
{ | ||
/** | ||
* Following constant define how the required middleware validation will be handled | ||
* | ||
* MIDDLEWARE_MATCH_STRICT All required middleware must be attached to target route | ||
* MIDDLEWARE_MATCH_LOOSE At least required middleware must be attached to target route | ||
*/ | ||
public const MIDDLEWARE_MATCH_STRICT = 1; | ||
public const MIDDLEWARE_MATCH_LOOSE = 2; | ||
|
||
/** | ||
* List of required middleware for this middleware to run passing logic | ||
*/ | ||
abstract public function requiredMiddleware(): array; | ||
|
||
/** | ||
* Determine if required middleware attached to target routes | ||
*/ | ||
public function hasRequiredMiddleware(Request $request, int $matchingCriteria = self::MIDDLEWARE_MATCH_STRICT): bool | ||
{ | ||
$requiredMiddleware = collect($this->requiredMiddleware()); | ||
|
||
if ($requiredMiddleware->count() === 0) { | ||
throw new Exception( | ||
sprintf( | ||
'Empty required middleware list provided for middleware class %s', | ||
static::class | ||
) | ||
); | ||
} | ||
|
||
$currentRoute = PKPBaseController::getRequestedRoute($request); | ||
$routeMiddleware = collect($currentRoute?->middleware() ?? []); | ||
|
||
$router = app('router'); /** @var \Illuminate\Routing\Router $router */ | ||
$routerMiddleware = $router->getMiddleware(); | ||
|
||
// need to replace the alias name with full class path | ||
$routeMiddleware = $routeMiddleware->map(function (string $middleware) use($routerMiddleware): string { | ||
// extract the middleware class or alias name if in format | ||
// such as `has.roles:1|16|17` or `PKP\middleware\HasRoles:1|16|17` | ||
$middleware = array_shift(array_pad(explode(":", $middleware, 2), 2, [])); | ||
|
||
if (class_exists($middleware)) { | ||
return $middleware; | ||
} | ||
|
||
return $routerMiddleware[$middleware] ?? $middleware; | ||
}); | ||
|
||
return match($matchingCriteria) { | ||
static::MIDDLEWARE_MATCH_STRICT | ||
=> $routeMiddleware->intersect($requiredMiddleware)->count() === $requiredMiddleware->count(), | ||
static::MIDDLEWARE_MATCH_LOOSE | ||
=> $routeMiddleware->intersect($requiredMiddleware)->count() > 0, | ||
}; | ||
} | ||
} |