src/Controller/UserController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Internal\ApiUser;
  4. use App\Entity\Internal\TwoFaConfig;
  5. use App\Entity\Monolith\User;
  6. use App\Helper\LoginHelper;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  13. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Serializer\SerializerInterface;
  19. class UserController extends AbstractController
  20. {
  21.     /**
  22.      * @Route("/api/me", name="users_me")
  23.      */
  24.     public function me(SerializerInterface $serializerEntityManagerInterface $internalEntityManager): JsonResponse
  25.     {
  26.         if (!$this->getUser() || $this->getUser() instanceof ApiUser) {
  27.             throw new UnauthorizedHttpException('Bearer');
  28.         }
  29.         $twoFaConfig $internalEntityManager->getRepository(TwoFaConfig::class)->findOneBy([
  30.             'userId' => $this->getUser()->getUser()->getUserId(),
  31.         ]);
  32.         $this->getUser()->setTwoFaStatus($twoFaConfig?->getStatus() ?? TwoFaConfig::STATUS_DISABLED);
  33.         return new JsonResponse($serializer->normalize($this->getUser(), 'json', ['groups' => 'user:read']));
  34.     }
  35.     /**
  36.      * @Route("/api/impersonate", name="users_impersonate", methods={"POST"})
  37.      */
  38.     public function impersonate(Request $requestJWTTokenManagerInterface $JWTManagerEntityManagerInterface $entityManager): JsonResponse
  39.     {
  40.         $data json_decode($request->getContent(), true);
  41.         if (!isset($data['secretKey']) || $data['secretKey'] != $_ENV['APP_SECRET']) {
  42.             throw new AccessDeniedHttpException();
  43.         }
  44.         $data['origin'] = $this->getUser()->getUser()->getUserId();
  45.         $userId = (int)$data['userId'] ?? 0;
  46.         unset($data['userId'], $data['secretKey']);
  47.         $user LoginHelper::fetchUser($entityManager'user_id'$userId);
  48.         if ($user) {
  49.             $impToken $JWTManager->createFromPayload($user$data);
  50.             return new JsonResponse([
  51.                 'impToken' => $impToken,
  52.             ]);
  53.         }
  54.         throw new NotFoundHttpException('User not found');
  55.     }
  56.     #[Route('/api/confirm-password'name'users_confirm_password'methods: ['POST'])]
  57.     public function confirmPassword(Request $requestEntityManagerInterface $entityManagerUserPasswordHasherInterface $passwordHasher): JsonResponse
  58.     {
  59.         $data json_decode($request->getContent(), true);
  60.         if (!isset($data['password'])) {
  61.             throw new BadRequestHttpException('Password is required.');
  62.         }
  63.         $user $entityManager->getRepository(User::class)->find($this->getUser()?->getUser()->getUserId());
  64.         if ($user && $passwordHasher->isPasswordValid($user$data['password'])) {
  65.             return new JsonResponse();
  66.         }
  67.         throw new AccessDeniedHttpException('Invalid password.');
  68.     }
  69. }