<?php
namespace MedBrief\MSR\Controller;
use Doctrine\ORM\EntityManagerInterface;
use MedBrief\MSR\Entity\Expert;
use MedBrief\MSR\Entity\Project;
use MedBrief\MSR\Entity\User;
use MedBrief\MSR\Repository\ExpertRepository;
use MedBrief\MSR\Repository\ExpertUserRepository;
use MedBrief\MSR\Repository\MatchLetterRepository;
use MedBrief\MSR\Service\File\FileHandler\VichUploaderFileHandler;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/user/profile")
*/
class ExpertProfileController extends BaseController
{
/**
* @Route(
* "/my-profile/{reactRouting}",
* name="app_expert_profile",
* requirements={"reactRouting"=".*"},
* defaults={"reactRouting"=""},
* methods={"GET"}
* )
*
* @Security("is_granted('VIEW_MY_PROFILE')")
*
* @param Request $request
* @param ExpertRepository $expertRepository
* @param ExpertUserRepository $expertUserRepository
* @param EntityManagerInterface $entityManager
*/
public function indexAction(Request $request, EntityManagerInterface $entityManager, ExpertUserRepository $expertUserRepository): Response
{
/** @var User $user */
$user = $this->getUser();
// This allows any user that is linked to an expert entity to access the profile, not just the primary user.
$expertUser = $expertUserRepository->getExpertByUser($user);
// Create an expert profile if the user does not have one, and they are allowed to have one.
if (!($expertUser instanceof Expert)) {
$expertUser = new Expert();
$expertUser->setUser($user);
$expertUser->setCreatedBy($user);
// Since the user is creating their own profile, it will automatically be in the expert reviewed status.
$expertUser->setReviewedBy(Expert::REVIEWED_BY_EXPERT_REVIEWED);
$entityManager->persist($expertUser);
$entityManager->flush();
}
return $this->render('UserProfile/profile.html.twig', [
'currentExpertUserId' => $expertUser->getId(),
'user' => $expertUser,
'activeTab' => 'my-profile',
]);
}
/**
* @Route(
* "/verify-my-profile/{reactRouting}",
* name="app_expert_verify_profile",
* requirements={"reactRouting"=".*"},
* defaults={"reactRouting"=""},
* methods={"GET"}
* )
*
* @Security("is_granted('VIEW_MY_PROFILE')")
*
* @param Request $request
* @param ExpertUserRepository $expertUserRepository
* @param MatchLetterRepository $matchLetterRepository
*/
public function verifyProfileAction(Request $request, ExpertUserRepository $expertUserRepository): Response
{
/** @var User $user */
$user = $this->getUser();
// This allows any user that is linked to an expert entity to access the profile, not just the primary user.
$expertUser = $expertUserRepository->getExpertByUser($user);
// At verify stage, we do not create an Expert profile as they should not feature on Match if they don't have an Expert profile.
if (!($expertUser instanceof Expert)) {
throw $this->createAccessDeniedException('User has no expert profile associated with their account.');
}
// Add a flash message if one is passed through.
$responseMessage = $request->query->get('message', null);
if ($responseMessage) {
$this->addFlash('success', $responseMessage);
}
if ($expertUser->isExpertReviewed() === true) {
return $this->redirectToRoute('infology_medbrief_dashboard');
}
$reviewedByExpert = $expertUser->isExpertReviewed();
return $this->render('UserProfile/verifyProfile.html.twig', [
'currentExpertUserId' => $expertUser->getId(),
'user' => $expertUser,
'reviewedByExpert' => $reviewedByExpert,
]);
}
/**
* This end-point streams an Expert's profile photo.
*
* @Route(
* "/photo/expert/{id}/{projectId}",
* name="app_expert_profile_photo",
* methods={"GET"},
* defaults={"projectId"=null}
* )
*
* @ParamConverter("paramUser", class="MedBrief\MSR\Entity\User", options={"id" = "id"})
* @ParamConverter("paramProject", class="MedBrief\MSR\Entity\Project", options={"id" = "projectId"}, isOptional=true)
*
* @param Request $request
* @param User $paramUser
* @param Project|null $paramProject
* @param VichUploaderFileHandler $fileHandler
* @param ExpertUserRepository $expertUserRepository
*
* @return Response
*/
public function streamProfilePhotoAction(
Request $request,
User $paramUser,
?Project $paramProject = null,
ExpertUserRepository $expertUserRepository,
VichUploaderFileHandler $fileHandler
) {
$viewMyProfile = $this->isGranted('VIEW_MY_PROFILE');
$viewFirm = $paramProject ? $this->isGranted('MATCH_VIEW_FIRM', $paramProject) : false;
if (!$viewMyProfile && !$viewFirm) {
throw $this->createAccessDeniedException();
}
// This allows any user that is linked to an expert entity to access the profile, not just the primary user.
$expertUser = $expertUserRepository->getExpertByUser($paramUser);
if (!($expertUser instanceof Expert)) {
throw $this->createNotFoundException('No expert profile associated with this user.');
}
// Grab the expert's profile photo filename to see if it exists.
if (!$expertUser->getProfilePhotoFilename()) {
throw $this->createNotFoundException();
}
$response = $fileHandler->serveAzureBlob($request, $expertUser, 'profilePhotoFile');
return $response;
}
}