Download src/Presentation/Middlewares/AuthorizationMiddleware.php from pnacorp/AIKEEDO: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/pnacorp/AIKEEDO/resolve/main/src/Presentation/Middlewares/AuthorizationMiddleware.php
- Command line
-
hf download hf://pnacorp/AIKEEDO/src/Presentation/Middlewares/AuthorizationMiddleware.php
-
curl -L -o AuthorizationMiddleware.php https://huggingface.co/pnacorp/AIKEEDO/resolve/main/src/Presentation/Middlewares/AuthorizationMiddleware.php
1.42 kB
| declare(strict_types=1); | |
| namespace Presentation\Middlewares; | |
| use Presentation\Exceptions\HttpException; | |
| use Presentation\Exceptions\UnauthorizedException; | |
| use Psr\Http\Message\ServerRequestInterface; | |
| use Psr\Http\Message\ResponseInterface; | |
| use Psr\Http\Server\MiddlewareInterface; | |
| use Psr\Http\Server\RequestHandlerInterface; | |
| use User\Domain\Entities\UserEntity; | |
| use User\Domain\ValueObjects\Role; | |
| use User\Domain\ValueObjects\Status; | |
| class AuthorizationMiddleware implements MiddlewareInterface | |
| { | |
| /** | |
| * @param ServerRequestInterface $request | |
| * @param RequestHandlerInterface $handler | |
| * @return ResponseInterface | |
| * @throws HttpException | |
| */ | |
| public function process( | |
| ServerRequestInterface $request, | |
| RequestHandlerInterface $handler | |
| ): ResponseInterface { | |
| $user = $request->getAttribute(UserEntity::class); | |
| $path = $request->getUri()->getPath(); | |
| if (strpos($path, '/auth/') !== false) { | |
| return $handler->handle($request); | |
| } | |
| if (!$user) { | |
| throw new UnauthorizedException(); | |
| } | |
| if ($user->getStatus() !== Status::ACTIVE) { | |
| throw new UnauthorizedException(); | |
| } | |
| if (strpos($path, '/admin') !== false && $user->getRole() !== Role::ADMIN) { | |
| throw new UnauthorizedException(); | |
| } | |
| return $handler->handle($request); | |
| } | |
| } | |