|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace App\Controller; |
| 4 | + |
| 5 | +use App\Form\Type\ChangePasswordType; |
| 6 | +use App\Service\ConfigurationService; |
| 7 | +use App\Service\DOMJudgeService; |
| 8 | +use App\Service\EventLogService; |
| 9 | +use Doctrine\ORM\EntityManagerInterface; |
| 10 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
| 14 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 15 | +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
| 16 | +use Symfony\Component\Routing\Attribute\Route; |
| 17 | +use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 18 | + |
| 19 | +#[Route(path: '/profile')] |
| 20 | +#[IsGranted('IS_AUTHENTICATED_FULLY')] |
| 21 | +class ProfileController extends BaseController |
| 22 | +{ |
| 23 | + public function __construct( |
| 24 | + EntityManagerInterface $em, |
| 25 | + DOMJudgeService $dj, |
| 26 | + EventLogService $eventLogService, |
| 27 | + KernelInterface $kernel, |
| 28 | + ) { |
| 29 | + parent::__construct($em, $eventLogService, $dj, $kernel); |
| 30 | + } |
| 31 | + |
| 32 | + #[Route(path: '', name: 'profile_index')] |
| 33 | + public function changePasswordAction(Request $request, UserPasswordHasherInterface $passwordHasher): Response |
| 34 | + { |
| 35 | + $isJury = $this->isGranted('ROLE_JURY') || $this->isGranted('ROLE_ADMIN') || $this->isGranted('ROLE_BALLOON'); |
| 36 | + $redirectRoute = $isJury ? 'jury_index' : 'team_index'; |
| 37 | + |
| 38 | + if (!$this->dj->canChangePassword()) { |
| 39 | + throw new AccessDeniedHttpException('You are not allowed to change your password.'); |
| 40 | + } |
| 41 | + |
| 42 | + $user = $this->dj->getUser(); |
| 43 | + $form = $this->createForm(ChangePasswordType::class); |
| 44 | + $form->handleRequest($request); |
| 45 | + |
| 46 | + if ($form->isSubmitted() && $form->isValid()) { |
| 47 | + $newPassword = $form->get('newPassword')->getData(); |
| 48 | + |
| 49 | + $user->setPassword($passwordHasher->hashPassword($user, $newPassword)); |
| 50 | + $this->saveEntity($user, $user->getUserid(), false); |
| 51 | + $this->addFlash('success', 'Password changed successfully.'); |
| 52 | + return $this->redirectToRoute($redirectRoute); |
| 53 | + } |
| 54 | + |
| 55 | + return $this->render('profile/change_password.html.twig', [ |
| 56 | + 'form' => $form, |
| 57 | + 'redirect_route' => $redirectRoute, |
| 58 | + ]); |
| 59 | + } |
| 60 | +} |
0 commit comments