<?php
namespace App\Controller\Api\V1\User;
use App\Entity\User\User;
use App\Repository\Notifications\NotificationRepository;
use App\Repository\User\UserRepository;
use App\Service\AuthenticationService;
use App\Service\AWS\S3Service;
use App\Service\ProfileService;
use App\Service\UserService;
use GuzzleHttp\Exception\GuzzleException;
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailureException;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\SerializerInterface;
class Me
{
/**
* @Route(path="/v1/me", methods={"GET"})
*
* @SWG\Get(
* tags={"Authentication"}
* )
*
* @SWG\Response(
* response=200,
* description="Logged in user",
* @Model(type=User::class, groups={"user:get"})
* )
* @param TokenStorageInterface $tokenStorage
* @param AuthenticationService $authenticationService
* @param S3Service $s3Service
* @param UserRepository $userRepository
* @param ProfileService $profileService
* @param UserService $userService
* @param SerializerInterface $serializer
* @param NotificationRepository $notificationRepository
* @return JsonResponse
* @throws GuzzleException
* @throws JWTDecodeFailureException
*/
public function __invoke(
TokenStorageInterface $tokenStorage,
AuthenticationService $authenticationService,
S3Service $s3Service,
UserRepository $userRepository,
ProfileService $profileService,
UserService $userService,
SerializerInterface $serializer,
NotificationRepository $notificationRepository
): JsonResponse
{
$token = $tokenStorage->getToken();
if (! $token instanceof TokenInterface || ! $token->getUser() || ! $token->getUser() instanceof UserInterface) {
throw new AccessDeniedHttpException('Token issue');
}
/** @var User $user */
$user = $token->getUser();
$accountsUser = $authenticationService->getMeFromAccounts();
if (
! $token instanceof TokenInterface ||
! $user ||
! $user instanceof UserInterface ||
$user->getUsername() !== $accountsUser['email']
) {
throw new AccessDeniedHttpException('Token issue');
}
$canAccessMmdz = false;
foreach ($accountsUser['user_applications'] as $userApplication) {
if ($userApplication['application'] === 'mmdz') {
$canAccessMmdz = true;
}
}
if (! $user->isEnabled()) {
throw new UnauthorizedHttpException('','You are not allowed to access this application');
}
$image = $accountsUser['user_profile']['image'] ?? null;
$pictureUrl = null;
if ($image) {
$pictureUrl = $s3Service->lambdaResize(
$image,
$_ENV['S3_STORAGE_FOLDER'] . $_ENV['S3_USER_FOLDER'],
'44x44'
);
}
$defaultProfileForUser = $serializer->serialize($profileService->findDefaultProfileForUser($user),'json', ['groups' => ['profile:index']]);
$lastAccessedProfileForUser = $serializer->serialize($profileService->findLastAccessedProfileForUser($user),'json', ['groups' => ['profile:index']]);
$mainProfiles = $serializer->serialize($profileService->findMainProfilesForUser($user),'json', ['groups' => ['profile:index']]);
$mainRoles = $profileService->findMainRolesForUser($user);
$lastAccessedProfiles = $serializer->serialize($profileService->findLastAccessedProfilesForUser($user),'json', ['groups' => ['profile:index']]);
$notificationsForUser = $serializer->serialize(
$userService->notificationInMeEndpoint($user),
'json',
['groups' => 'notification:index']
);
$result = [
'id' => $user->getId(),
'email' => $user->getEmail(),
'twoFactorAuthenticationEnabled' => $user->isTwoFactorAuthenticationEnabled(),
'user_profile' => [
'first_name' => $user->getUserProfile()->getFirstName(),
'last_name' => $user->getUserProfile()->getLastName(),
'picture_url' => $pictureUrl
],
'applications' => json_decode($user->getUserProfile()->getApplications(), true),
'can_access_mmdz' => $canAccessMmdz,
'is_mmpz_service_admin' => $authenticationService->userIsMmpzServiceAdmin(),
'default_profile' => json_decode($defaultProfileForUser, true),
'last_accessed_profile' => json_decode($lastAccessedProfileForUser, true),
'main_profiles' => json_decode($mainProfiles, true),
'main_roles' => $mainRoles,
'last_accessed_profiles' => json_decode($lastAccessedProfiles, true),
'notifications' => json_decode($notificationsForUser, true)
];
return new JsonResponse($result);
}
}