Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3. namespace App\InterServices\ActiveUser;
  4.  
  5. use App\InterServices\ActiveUser\Exceptions\NoAuthorizationHeader;
  6. use Psr\Http\Message\ResponseInterface;
  7. use Psr\Http\Message\ServerRequestInterface;
  8. use Psr\Http\Server\MiddlewareInterface;
  9. use Psr\Http\Server\RequestHandlerInterface;
  10. use Psr\Log\LoggerAwareInterface;
  11. use Psr\Log\LoggerAwareTrait;
  12. use Zend\Diactoros\Response\JsonResponse;
  13.  
  14. /**
  15.  * Middleware передает токен авторизации текущего запроса в ActiveUser
  16.  */
  17. class ActiveUserMiddleware implements MiddlewareInterface, LoggerAwareInterface
  18. {
  19.     use LoggerAwareTrait;
  20.  
  21.     /**
  22.      * Название заголовка
  23.      *
  24.      * @var string
  25.      */
  26.     const AUTHORIZATION = 'Authorization';
  27.  
  28.     /**
  29.      * Активный пользователь как сервис
  30.      *
  31.      * @var HubUserService
  32.      */
  33.     protected $hubUserService;
  34.  
  35.     /**
  36.      * @var ActiveUser
  37.      */
  38.     protected $activeUser;
  39.  
  40.     /**
  41.      * Конструктор класса
  42.      *
  43.      * @param ActiveUser $hubUserService
  44.      */
  45.     public function __construct(ActiveUser $activeUser, HubUserService $hubUserService)
  46.     {
  47.         $this->hubUserService = $hubUserService;
  48.     }
  49.  
  50.     /**
  51.      *
  52.      * {@inheritdoc}
  53.      * @see \Psr\Http\Server\MiddlewareInterface::process()
  54.      */
  55.     public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  56.     {
  57.         $this->logger->debug('Middleware: ' . get_class($this));
  58.        
  59.         $authorization = $request->getHeaderLine(static::AUTHORIZATION);
  60.         $this->logger->debug('set authorization token', [
  61.             static::AUTHORIZATION => $authorization
  62.         ]);
  63.         $this->hubUserService->setToken($authorization);
  64.         $this->activeUser->setInfo($this->hubUserService->getCurrentUserInfo());
  65.         return $handler->handle($request);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement