<?php
namespace App\EventSubscriber\Api;
use App\Controller\Api\AbstractApiController;
use App\Controller\Api\CoreModule\AuthenticationController;
use App\Controller\Api\CoreModule\SessionController;
use App\Entity\Api\CoreModule\User;
use App\Helper\Api\JsonFormatter\Throwable\Formatter;
use App\Helper\Api\Translator\ApiTranslator;
use App\Validator\Constraints\Api\CoreModule\User\Attributes\NewPasswordNeededCompound;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* Subscriber for kernel controller event to force the user to change his temporary password.
*
* @package API
* @internal
*/
class ChangedPasswordSubscriber extends AbstractSubscriber
{
/**
* Token storage to retrieve user.
*
* @var TokenStorageInterface
*/
protected TokenStorageInterface $tokenStorage;
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* The code must not depend on runtime state as it will only be called at compile time.
* All logic depending on runtime state must be put into the individual methods handling the events.
*
* @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
* @noinspection PhpArrayShapeAttributeCanBeAddedInspection
* @noinspection PhpUnused
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => [
['ensureCustomPassword', 16]
]
];
}
/**
* Constructor.
*
* @param Formatter $formatter
* @param ApiTranslator $translator
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(Formatter $formatter, ApiTranslator $translator, TokenStorageInterface $tokenStorage)
{
parent::__construct($formatter, $translator);
$this->tokenStorage = $tokenStorage;
}
/**
* Force users to change their temporary password.
*
* @param ControllerEvent $event The controller event.
* @return void
* @noinspection PhpUnused
*/
public function ensureCustomPassword(ControllerEvent $event): void
{
$method = null;
$controller = $event->getController();
// when a controller class defines multiple action methods, the controller
// is returned as [$controllerInstance, 'methodName']
if (is_array($controller)) {
$method = $controller[1];
$controller = $controller[0];
}
// limit to API controllers
if ($controller instanceof AbstractApiController) {
// ignore for authentication controller to allow login, logout, ping and password reset
if ($controller instanceof AuthenticationController) {
return;
}
// ignore for session controller and certain methods to allow password and language change
if ($controller instanceof SessionController &&
($method === 'userPasswordUpdate' || $method === 'userLanguageUpdate')) {
return;
}
$currentUser = $this->tokenStorage->getToken()->getUser();
/* @var User $currentUser */
/* @noinspection PhpClassConstantAccessedViaChildClassInspection */
if ($currentUser->getNewPasswordNeeded() === NewPasswordNeededCompound::YES) {
throw new HttpException(
Response::HTTP_FORBIDDEN,
'User needs to change his temporary password first.'
);
}
}
}
}