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\Helper\LoginHelper;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Serializer\SerializerInterface;
  16. class UserController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/api/me", name="users_me")
  20.      */
  21.     public function me(SerializerInterface $serializerEntityManagerInterface $internalEntityManager): JsonResponse
  22.     {
  23.         if (!$this->getUser() || $this->getUser() instanceof ApiUser) {
  24.             throw new UnauthorizedHttpException('Bearer');
  25.         }
  26.         $twoFaConfig $internalEntityManager->getRepository(TwoFaConfig::class)->findOneBy([
  27.             'userId' => $this->getUser()->getUser()->getUserId(),
  28.         ]);
  29.         $this->getUser()->setTwoFaStatus($twoFaConfig?->getStatus() ?? TwoFaConfig::STATUS_DISABLED);
  30.         return new JsonResponse($serializer->normalize($this->getUser(), 'json', ['groups' => 'user:read']));
  31.     }
  32.     /**
  33.      * @Route("/api/impersonate", name="users_impersonate", methods={"POST"})
  34.      */
  35.     public function impersonate(Request $requestJWTTokenManagerInterface $JWTManagerEntityManagerInterface $entityManager): JsonResponse
  36.     {
  37.         $data json_decode($request->getContent(), true);
  38.         if (!isset($data['secretKey']) || $data['secretKey'] != $_ENV['APP_SECRET']) {
  39.             throw new AccessDeniedHttpException();
  40.         }
  41.         $data['origin'] = $this->getUser()->getUser()->getUserId();
  42.         $userId = (int)$data['userId'] ?? 0;
  43.         unset($data['userId'], $data['secretKey']);
  44.         $user LoginHelper::fetchUser($entityManager'user_id'$userId);
  45.         if ($user) {
  46.             $impToken $JWTManager->createFromPayload($user$data);
  47.             return new JsonResponse([
  48.                 'impToken' => $impToken,
  49.             ]);
  50.         }
  51.         throw new NotFoundHttpException('User not found');
  52.     }
  53. }