|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tqdev\PhpCrudApi\Middleware; |
| 4 | + |
| 5 | +use Psr\Http\Message\ResponseInterface; |
| 6 | +use Psr\Http\Message\ServerRequestInterface; |
| 7 | +use Psr\Http\Server\RequestHandlerInterface; |
| 8 | +use Tqdev\PhpCrudApi\Column\ReflectionService; |
| 9 | +use Tqdev\PhpCrudApi\Controller\Responder; |
| 10 | +use Tqdev\PhpCrudApi\Database\GenericDB; |
| 11 | +use Tqdev\PhpCrudApi\Middleware\Base\Middleware; |
| 12 | +use Tqdev\PhpCrudApi\Middleware\Router\Router; |
| 13 | + |
| 14 | +class ReAuthMiddleware extends Middleware |
| 15 | +{ |
| 16 | + private $reflection; |
| 17 | + private $db; |
| 18 | + |
| 19 | + public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection, GenericDB $db) |
| 20 | + { |
| 21 | + parent::__construct($router, $responder, $properties); |
| 22 | + $this->reflection = $reflection; |
| 23 | + $this->db = $db; |
| 24 | + } |
| 25 | + |
| 26 | + private function getUsername(): string |
| 27 | + { |
| 28 | + $usernameHandler = $this->getProperty('usernameHandler', ''); |
| 29 | + if ($usernameHandler) { |
| 30 | + return call_user_func($usernameHandler); |
| 31 | + } |
| 32 | + return ''; |
| 33 | + } |
| 34 | + |
| 35 | + private function getPassword(): string |
| 36 | + { |
| 37 | + $passwordHandler = $this->getProperty('passwordHandler', ''); |
| 38 | + if ($passwordHandler) { |
| 39 | + return call_user_func($passwordHandler); |
| 40 | + } |
| 41 | + return ''; |
| 42 | + } |
| 43 | + |
| 44 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface |
| 45 | + { |
| 46 | + $username = $this->getUsername(); |
| 47 | + $password = $this->getPassword(); |
| 48 | + if ($username && $password) { |
| 49 | + $this->db->pdo()->reauthenticate($username, $password); |
| 50 | + } |
| 51 | + return $next->handle($request); |
| 52 | + } |
| 53 | +} |
0 commit comments