-
Notifications
You must be signed in to change notification settings - Fork 292
Add self-service password change option. #3340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
meisterT
wants to merge
1
commit into
DOMjudge:main
Choose a base branch
from
meisterT:pw_change
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace DoctrineMigrations; | ||
|
|
||
| use Doctrine\DBAL\Schema\Schema; | ||
| use Doctrine\Migrations\AbstractMigration; | ||
|
|
||
| /** | ||
| * Auto-generated Migration: Please modify to your needs! | ||
| */ | ||
| final class Version20260118153404 extends AbstractMigration | ||
| { | ||
| public function getDescription(): string | ||
| { | ||
| return 'add allow_password_change to team_category'; | ||
| } | ||
|
|
||
| public function up(Schema $schema): void | ||
| { | ||
| // this up() migration is auto-generated, please modify it to your needs | ||
| $this->addSql('ALTER TABLE team_category ADD allow_password_change TINYINT(1) DEFAULT 0 NOT NULL COMMENT \'Are teams in this category allowed to change their own password?\''); | ||
| } | ||
|
|
||
| public function down(Schema $schema): void | ||
| { | ||
| // this down() migration is auto-generated, please modify it to your needs | ||
| $this->addSql('ALTER TABLE team_category DROP allow_password_change'); | ||
| } | ||
|
|
||
| public function isTransactional(): bool | ||
| { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace App\Controller; | ||
|
|
||
| use App\Form\Type\ChangePasswordType; | ||
| use App\Service\ConfigurationService; | ||
| use App\Service\DOMJudgeService; | ||
| use App\Service\EventLogService; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | ||
| use Symfony\Component\HttpKernel\KernelInterface; | ||
| use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | ||
| use Symfony\Component\Routing\Attribute\Route; | ||
| use Symfony\Component\Security\Http\Attribute\IsGranted; | ||
|
|
||
| #[Route(path: '/profile')] | ||
| #[IsGranted('IS_AUTHENTICATED_FULLY')] | ||
| class ProfileController extends BaseController | ||
| { | ||
| public function __construct( | ||
| EntityManagerInterface $em, | ||
| DOMJudgeService $dj, | ||
| EventLogService $eventLogService, | ||
| KernelInterface $kernel, | ||
| ) { | ||
| parent::__construct($em, $eventLogService, $dj, $kernel); | ||
| } | ||
|
|
||
| #[Route(path: '', name: 'profile_index')] | ||
| public function changePasswordAction(Request $request, UserPasswordHasherInterface $passwordHasher): Response | ||
| { | ||
| $isJury = $this->isGranted('ROLE_JURY') || $this->isGranted('ROLE_ADMIN') || $this->isGranted('ROLE_BALLOON'); | ||
| $redirectRoute = $isJury ? 'jury_index' : 'team_index'; | ||
|
|
||
| if (!$this->dj->canChangePassword()) { | ||
| throw new AccessDeniedHttpException('You are not allowed to change your password.'); | ||
| } | ||
|
|
||
| $user = $this->dj->getUser(); | ||
| $form = $this->createForm(ChangePasswordType::class); | ||
| $form->handleRequest($request); | ||
|
|
||
| if ($form->isSubmitted() && $form->isValid()) { | ||
| $newPassword = $form->get('newPassword')->getData(); | ||
|
|
||
| $user->setPassword($passwordHasher->hashPassword($user, $newPassword)); | ||
| $this->saveEntity($user, $user->getUserid(), false); | ||
| $this->addFlash('success', 'Password changed successfully.'); | ||
| return $this->redirectToRoute($redirectRoute); | ||
| } | ||
|
|
||
| return $this->render('profile/change_password.html.twig', [ | ||
| 'form' => $form, | ||
| 'redirect_route' => $redirectRoute, | ||
| ]); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace App\Form\Type; | ||
|
|
||
| use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Extension\Core\Type\PasswordType; | ||
| use Symfony\Component\Form\Extension\Core\Type\RepeatedType; | ||
| use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
| use Symfony\Component\Form\FormBuilderInterface; | ||
| use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; | ||
| use Symfony\Component\Validator\Constraints\Length; | ||
| use Symfony\Component\Validator\Constraints\NotBlank; | ||
|
|
||
| class ChangePasswordType extends AbstractType | ||
| { | ||
| public function __construct( | ||
| #[Autowire(param: 'min_password_length')] | ||
| private readonly int $minimumPasswordLength | ||
| ) { | ||
| } | ||
|
|
||
| public function buildForm(FormBuilderInterface $builder, array $options): void | ||
| { | ||
| $builder->add('currentPassword', PasswordType::class, [ | ||
| 'label' => 'Current password', | ||
| 'mapped' => false, | ||
| 'constraints' => [ | ||
| new NotBlank(), | ||
| new UserPassword(), | ||
| ], | ||
| 'attr' => [ | ||
| 'autocomplete' => 'current-password', | ||
| ], | ||
| ]); | ||
| $builder->add('newPassword', RepeatedType::class, [ | ||
| 'type' => PasswordType::class, | ||
| 'invalid_message' => 'The password fields must match.', | ||
| 'mapped' => false, | ||
| 'first_options' => [ | ||
| 'label' => 'New password', | ||
| 'help' => sprintf('Minimum length: %d characters', $this->minimumPasswordLength), | ||
| 'attr' => [ | ||
| 'autocomplete' => 'new-password', | ||
| 'minlength' => $this->minimumPasswordLength, | ||
| ], | ||
| ], | ||
| 'second_options' => [ | ||
| 'label' => 'Repeat new password', | ||
| 'attr' => [ | ||
| 'autocomplete' => 'new-password', | ||
| 'minlength' => $this->minimumPasswordLength, | ||
| ], | ||
| ], | ||
| 'constraints' => [ | ||
| new NotBlank(), | ||
| new Length([ | ||
| 'min' => $this->minimumPasswordLength, | ||
| ]), | ||
| ], | ||
| ]); | ||
| $builder->add('save', SubmitType::class, [ | ||
| 'label' => 'Change password', | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| {% extends is_granted('ROLE_JURY') or is_granted('ROLE_BALLOON') or is_granted('ROLE_ADMIN') ? 'jury/base.html.twig' : 'team/base.html.twig' %} | ||
|
|
||
| {% block title %}Change password - {{ parent() }}{% endblock %} | ||
|
|
||
| {% block content %} | ||
| <div class="container"> | ||
| <h1 class="mt-3">Change password</h1> | ||
|
|
||
| <div class="row"> | ||
| <div class="col-md-6"> | ||
| {{ form_start(form) }} | ||
| {{ form_row(form.currentPassword) }} | ||
| {{ form_row(form.newPassword) }} | ||
| {{ form_widget(form.save, {'attr': {'class': 'btn btn-primary'}}) }} | ||
| <a href="{{ path(redirect_route) }}" class="btn btn-secondary">Cancel</a> | ||
| {{ form_end(form) }} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| {% endblock %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we allow these changes, they might deserve an audit log message.