src/Controller/Api/V1/User/Me.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\V1\User;
  3. use App\Entity\User\User;
  4. use App\Repository\Notifications\NotificationRepository;
  5. use App\Repository\User\UserRepository;
  6. use App\Service\AuthenticationService;
  7. use App\Service\AWS\S3Service;
  8. use App\Service\ProfileService;
  9. use App\Service\UserService;
  10. use GuzzleHttp\Exception\GuzzleException;
  11. use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailureException;
  12. use Nelmio\ApiDocBundle\Annotation\Model;
  13. use Swagger\Annotations as SWG;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  16. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. use Symfony\Component\Serializer\SerializerInterface;
  22. class Me
  23. {
  24.     /**
  25.      * @Route(path="/v1/me", methods={"GET"})
  26.      *
  27.      * @SWG\Get(
  28.      *   tags={"Authentication"}
  29.      * )
  30.      *
  31.      * @SWG\Response(
  32.      *     response=200,
  33.      *     description="Logged in user",
  34.      *     @Model(type=User::class, groups={"user:get"})
  35.      * )
  36.      * @param TokenStorageInterface $tokenStorage
  37.      * @param AuthenticationService $authenticationService
  38.      * @param S3Service $s3Service
  39.      * @param UserRepository $userRepository
  40.      * @param ProfileService $profileService
  41.      * @param UserService $userService
  42.      * @param SerializerInterface $serializer
  43.      * @param NotificationRepository $notificationRepository
  44.      * @return JsonResponse
  45.      * @throws GuzzleException
  46.      * @throws JWTDecodeFailureException
  47.      */
  48.     public function __invoke(
  49.         TokenStorageInterface $tokenStorage,
  50.         AuthenticationService $authenticationService,
  51.         S3Service $s3Service,
  52.         UserRepository $userRepository,
  53.         ProfileService $profileService,
  54.         UserService $userService,
  55.         SerializerInterface $serializer,
  56.         NotificationRepository $notificationRepository
  57.     ): JsonResponse
  58.     {
  59.         $token $tokenStorage->getToken();
  60.         if (! $token instanceof TokenInterface || ! $token->getUser() || ! $token->getUser() instanceof UserInterface) {
  61.             throw new AccessDeniedHttpException('Token issue');
  62.         }
  63.         /** @var User $user */
  64.         $user $token->getUser();
  65.         $accountsUser $authenticationService->getMeFromAccounts();
  66.         if (
  67.             ! $token instanceof TokenInterface ||
  68.             ! $user ||
  69.             ! $user instanceof UserInterface ||
  70.             $user->getUsername() !== $accountsUser['email']
  71.         ) {
  72.             throw new AccessDeniedHttpException('Token issue');
  73.         }
  74.         $canAccessMmdz false;
  75.         foreach ($accountsUser['user_applications'] as $userApplication) {
  76.             if ($userApplication['application'] === 'mmdz') {
  77.                 $canAccessMmdz true;
  78.             }
  79.         }
  80.         if (! $user->isEnabled()) {
  81.             throw new UnauthorizedHttpException('','You are not allowed to access this application');
  82.         }
  83.         $image $accountsUser['user_profile']['image'] ?? null;
  84.         $pictureUrl null;
  85.         if ($image) {
  86.             $pictureUrl $s3Service->lambdaResize(
  87.                 $image,
  88.                 $_ENV['S3_STORAGE_FOLDER'] . $_ENV['S3_USER_FOLDER'],
  89.                 '44x44'
  90.             );
  91.         }
  92.         $defaultProfileForUser $serializer->serialize($profileService->findDefaultProfileForUser($user),'json', ['groups' => ['profile:index']]);
  93.         $lastAccessedProfileForUser $serializer->serialize($profileService->findLastAccessedProfileForUser($user),'json', ['groups' => ['profile:index']]);
  94.         $mainProfiles $serializer->serialize($profileService->findMainProfilesForUser($user),'json', ['groups' => ['profile:index']]);
  95.         $mainRoles $profileService->findMainRolesForUser($user);
  96.         $lastAccessedProfiles $serializer->serialize($profileService->findLastAccessedProfilesForUser($user),'json', ['groups' => ['profile:index']]);
  97.         $notificationsForUser $serializer->serialize(
  98.             $userService->notificationInMeEndpoint($user),
  99.             'json',
  100.             ['groups' => 'notification:index']
  101.         );
  102.         $result = [
  103.             'id'                                => $user->getId(),
  104.             'email'                             => $user->getEmail(),
  105.             'twoFactorAuthenticationEnabled'    => $user->isTwoFactorAuthenticationEnabled(),
  106.             'user_profile'                      => [
  107.                 'first_name'    => $user->getUserProfile()->getFirstName(),
  108.                 'last_name'     => $user->getUserProfile()->getLastName(),
  109.                 'picture_url'   => $pictureUrl
  110.             ],
  111.             'applications'                      => json_decode($user->getUserProfile()->getApplications(), true),
  112.             'can_access_mmdz'                   => $canAccessMmdz,
  113.             'is_mmpz_service_admin'             => $authenticationService->userIsMmpzServiceAdmin(),
  114.             'default_profile'                   => json_decode($defaultProfileForUsertrue),
  115.             'last_accessed_profile'             => json_decode($lastAccessedProfileForUsertrue),
  116.             'main_profiles'                     => json_decode($mainProfilestrue),
  117.             'main_roles'                        => $mainRoles,
  118.             'last_accessed_profiles'            => json_decode($lastAccessedProfilestrue),
  119.             'notifications'                     => json_decode($notificationsForUsertrue)
  120.         ];
  121.         return new JsonResponse($result);
  122.     }
  123. }