src/Controller/SecurityController.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\Exception\ValidationErrorException;
  5. use App\Response\MessageResponse;
  6. use App\Service\CustomerServiceInterface;
  7. use App\Service\Security\AuthServiceInterface;
  8. use App\Service\User\UserServiceInterface;
  9. use App\Validator\ViolationsTrait;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Validator\Validator\ValidatorInterface;
  17. final class SecurityController extends AbstractController
  18. {
  19.     use ViolationsTrait;
  20.     public function __construct(
  21.         private AuthServiceInterface $authService,
  22.         private UserServiceInterface $userService,
  23.         private CustomerServiceInterface $customerService,
  24.         private ValidatorInterface $validator,
  25.     ) {
  26.     }
  27.     #[Route('/'name'default_route')]
  28.     public function index(): Response
  29.     {
  30.         return new Response('No direct access.'400);
  31.     }
  32.     #[Route(path'/auth/forgotten_password/{email}'name'auth_forgotten_password'methods: ['GET'])]
  33.     public function forgottenPassword(string $email): Response
  34.     {
  35.         if (!empty(trim($email))) {
  36.             $user $this->userService->getUserByEmail($email);
  37.             if (null === $user) {
  38.                 $user $this->customerService->findCustomerByEmail($email);
  39.             }
  40.             if (null !== $user) {
  41.                 $this->authService->resetPassword($user);
  42.             }
  43.         }
  44.         return new MessageResponse('OK');
  45.     }
  46.     /**
  47.      * @throws ValidationErrorException
  48.      */
  49.     #[Route(path'/auth/reset_password/{resetToken}'name'auth_reset_password'methods: ['POST'])]
  50.     public function changePassword(string $resetTokenRequest $request): MessageResponse
  51.     {
  52.         $user $this->userService->findByResetToken($resetToken);
  53.         $request json_decode($request->getContent(), true);
  54.         if (
  55.             null === $user->getResetTokenCreatedAt() ||
  56.             (new \DateTimeImmutable())->getTimestamp() - $user->getResetTokenCreatedAt()->getTimestamp() >= 172800
  57.         ) {
  58.             throw new \DomainException('ERROR.USER.PASSWORD.TOKEN.EXPIRED'Response::HTTP_BAD_REQUEST);
  59.         }
  60.         if (!isset($request['newPassword'])) {
  61.             throw new \DomainException('ERROR.USER.PASSWORD.MISSING'Response::HTTP_BAD_REQUEST);
  62.         }
  63.         $user->setPassword($request['newPassword']);
  64.         $this->checkViolations($this->validator->validate($user));
  65.         $this->authService->changePassword($user);
  66.         return new MessageResponse('INFO.USER.PASSWORD.UPDATED');
  67.     }
  68.     #[Route(path'/auth/invalidate_refresh_token'name'auth_invalidate_refresh_token'methods: ['GET'])]
  69.     #[IsGranted('ROLE_ADMIN')]
  70.     public function invalidateRefreshToken(): Response
  71.     {
  72.         if (!$this->getUser() instanceof UserInterface) {
  73.             throw new \DomainException('ERROR.USER.INVALID.ACCESS.RIGHTS'403);
  74.         }
  75.         $this->authService->invalidate($this->getUser());
  76.         return new MessageResponse('INFO.USER.REFRESH.TOKEN.INVALIDATED');
  77.     }
  78. }