src/Controller/AccountController.php line 35

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. if (!$this->getUser()) {
  32. $this->addFlash('warning', 'You need login first!');
  33. return $this->redirectToRoute('app_login');
  34. } else {
  35. // $this->getUser()->getRoles();
  36. // $this->em->persist($this->getUser());
  37. // $this->em->flush();
  38. if (!$this->getUser()->isVerified()) {
  39. $this->addFlash('warning', 'You need to have a verified account');
  40. return $this->redirectToRoute('app_home');
  41. } else {
  42. $mainTeacher = $this->mainTeacherRepo->findOneBy(array("teacher"=> $this->getUser(), "schoolYear"=> $this->schoolYearService->sessionYearById()));
  43. $attributions = $this->attRepo->findBy(array("teacher"=> $this->getUser(), "schoolYear"=> $this->schoolYearService->sessionYearById()));
  44. $hasAccess = $this->isGranted('ROLE_USER');
  45. if (!$hasAccess) {
  46. return $this->redirectToRoute('app_home');
  47. } else {
  48. return $this->render('account/profile.html.twig', compact("mainTeacher", "attributions"));
  49. }
  50. }
  51. }
  52. }
  53. /**
  54. * @Route("/edit", name="admin_account_edit", methods={"GET","POST"})
  55. */
  56. public function edit(Request $request): Response
  57. {
  58. if (!$this->getUser()) {
  59. $this->addFlash('warning', 'You need login first!');
  60. return $this->redirectToRoute('app_login');
  61. }
  62. $user = $this->getUser();
  63. $form = $this->createForm(UserFormType::class, $user);
  64. $form->handleRequest($request);
  65. if ($form->isSubmitted() && $form->isValid()) {
  66. $this->em->flush();
  67. $this->addFlash('success', 'Account successfully modified');
  68. return $this->redirectToRoute('app_account');
  69. }
  70. return $this->render('account/edit.html.twig', [
  71. 'form' => $form->createView()
  72. ]);
  73. }
  74. /*
  75. * @Route("/changepwd", name="admin_account_changepwd", methods={"GET","POST"})
  76. */
  77. public function changePwd(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
  78. {
  79. if (!$this->getUser()) {
  80. $this->addFlash('warning', 'You need login first!');
  81. return $this->redirectToRoute('app_login');
  82. }
  83. $form = $this->createForm(ChangePasswordFormType::class, null, [
  84. 'current_password_required' => true,
  85. ]);
  86. $form->handleRequest($request);
  87. $user = $this->getUser();
  88. //$form = $this->createForm(UserFormType::class, $user);
  89. if ($form->isSubmitted() && $form->isValid()) {
  90. $user->setPassword(
  91. $passwordEncoder->encodePassword(
  92. $user,
  93. $form->get('plainPassword')->getData()
  94. )
  95. );
  96. $this->em->flush();
  97. $this->addFlash('success', 'Account successfully modified');
  98. return $this->redirectToRoute('app_account');
  99. }
  100. return $this->render('account/changepwd.html.twig', [
  101. 'form' => $form->createView()
  102. ]);
  103. }
  104. }