src/Controller/AccountController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\UserFormType;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use App\Service\SchoolYearService;
  11. use App\Repository\AttributionRepository;
  12. use App\Repository\MainTeacherRepository;
  13. class AccountController extends AbstractController
  14. {
  15.     private $em;
  16.     private SchoolYearService $schoolYearService;
  17.     private AttributionRepository $attRepo;
  18.     private MainTeacherRepository $mainTeacherRepo;
  19.     public function __construct(MainTeacherRepository $mainTeacherRepo,AttributionRepository $attRepo,SchoolYearService $schoolYearService,EntityManagerInterface $em)
  20.     {
  21.         $this->em $em;
  22.         $this->schoolYearService $schoolYearService;
  23.         $this->attRepo $attRepo;
  24.         $this->mainTeacherRepo $mainTeacherRepo;
  25.     }
  26.     /**
  27.      * @Route("/account", name="app_account")
  28.      */
  29.     public function index(): Response
  30.     {
  31.         
  32.         if (!$this->getUser()) {
  33.             $this->addFlash('warning''You need login first!');
  34.             return $this->redirectToRoute('app_login');
  35.         } else {
  36.            
  37.             // $this->getUser()->getRoles();
  38.             // $this->em->persist($this->getUser());
  39.             // $this->em->flush();
  40.             if (!$this->getUser()->isVerified()) {
  41.                 $this->addFlash('warning''You need to have a verified account');
  42.                 return $this->redirectToRoute('app_home');
  43.             } else {
  44.                 $mainTeacher $this->mainTeacherRepo->findOneBy(array("teacher"=> $this->getUser(), "schoolYear"=> $this->schoolYearService->sessionYearById()));
  45.                 $attributions $this->attRepo->findBy(array("teacher"=> $this->getUser(), "schoolYear"=> $this->schoolYearService->sessionYearById()));
  46.                 $hasAccess $this->isGranted('ROLE_USER');
  47.                 if (!$hasAccess) {
  48.                     return $this->redirectToRoute('app_home');
  49.                 } else {
  50.                     return $this->render('account/profile.html.twig'compact("mainTeacher""attributions"));
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     /**
  56.      * @Route("/edit", name="admin_account_edit", methods={"GET","POST"})
  57.      */
  58.     public function edit(Request $request): Response
  59.     {
  60.         if (!$this->getUser()) {
  61.             $this->addFlash('warning''You need login first!');
  62.             return $this->redirectToRoute('app_login');
  63.         }
  64.         $user $this->getUser();
  65.         $form $this->createForm(UserFormType::class, $user);
  66.         $form->handleRequest($request);
  67.         if ($form->isSubmitted() && $form->isValid()) {
  68.             $this->em->flush();
  69.             $this->addFlash('success''Account successfully modified');
  70.             return $this->redirectToRoute('app_account');
  71.         }
  72.         return $this->render('account/edit.html.twig', [
  73.             'form' => $form->createView()
  74.         ]);
  75.     }
  76.     /*
  77.      * @Route("/changepwd", name="admin_account_changepwd", methods={"GET","POST"})
  78.     */
  79.     public function changePwd(Request $requestUserPasswordEncoderInterface $passwordEncoder): Response
  80.     {
  81.         if (!$this->getUser()) {
  82.             $this->addFlash('warning''You need login first!');
  83.             return $this->redirectToRoute('app_login');
  84.         }
  85.         $form $this->createForm(ChangePasswordFormType::class, null, [
  86.             'current_password_required' => true,
  87.         ]);
  88.         $form->handleRequest($request);
  89.         $user $this->getUser();
  90.         //$form = $this->createForm(UserFormType::class, $user);
  91.         if ($form->isSubmitted() && $form->isValid()) {
  92.             $user->setPassword(
  93.                 $passwordEncoder->encodePassword(
  94.                     $user,
  95.                     $form->get('plainPassword')->getData()
  96.                 )
  97.             );
  98.             $this->em->flush();
  99.             $this->addFlash('success''Account successfully modified');
  100.             return $this->redirectToRoute('app_account');
  101.         }
  102.         return $this->render('account/changepwd.html.twig', [
  103.             'form' => $form->createView()
  104.         ]);
  105.     }
  106. }