Compare commits

..

No commits in common. "087297b0e120746d386c535d83ea9a4c04315194" and "23d8401bb229f51c4e16060cd9a612f04781d1c2" have entirely different histories.

14 changed files with 54 additions and 166 deletions

View file

@ -53,12 +53,6 @@ user_account:
requirements: requirements:
method: GET method: GET
user_account_password_change:
path: /account/password
controller: App\Controller\AccountController::changePassword
requirements:
method: POST
user_account_token_create: user_account_token_create:
path: /profile/api/token/create path: /profile/api/token/create
controller: App\Controller\AccountController::addApiToken controller: App\Controller\AccountController::addApiToken

View file

@ -44,7 +44,7 @@ class AddInvitesCommand extends Command
$this->em->flush(); $this->em->flush();
$output->writeln(sprintf('<info>%d invites added to \'%s\'.</info>', $number, $user->getUserIdentifier())); $output->writeln(sprintf('<info>%d invites added to \'%s\'.</info>', $number, $user->getUsername()));
return Command::SUCCESS; return Command::SUCCESS;
} }

View file

@ -74,7 +74,7 @@ class AddUserCommand extends Command
$this->em->flush(); $this->em->flush();
$output->writeln(sprintf('<info>User \'%s\' registered, %d invites added.</info>', $user->getUserIdentifier(), $invites)); $output->writeln(sprintf('<info>User \'%s\' registered, %d invites added.</info>', $user->getUsername(), $invites));
return Command::SUCCESS; return Command::SUCCESS;
} }

View file

@ -5,21 +5,14 @@ namespace App\Controller;
use App\Entity\{ApiToken, User}; use App\Entity\{ApiToken, User};
use App\Repository\{ApiTokenRepository, InviteRepository}; use App\Repository\{ApiTokenRepository, InviteRepository};
use App\Form\Data\PasswordChangeData;
use App\Form\PasswordChangeType;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
class AccountController extends AbstractController class AccountController extends AbstractController
{ {
public function account( public function account(InviteRepository $inviteRepo, ApiTokenRepository $apiTokenRepo): Response
InviteRepository $inviteRepo, {
ApiTokenRepository $apiTokenRepo
): Response {
/** @var User $user */ /** @var User $user */
if (null === $user = $this->getUser()) { if (null === $user = $this->getUser()) {
throw $this->createAccessDeniedException('User not found exception'); throw $this->createAccessDeniedException('User not found exception');
@ -33,32 +26,6 @@ class AccountController extends AbstractController
]); ]);
} }
public function changePassword(
Request $request,
EntityManagerInterface $em,
PasswordHasherFactoryInterface $hasherFactory,
): Response {
$data = new PasswordChangeData();
$form = $this->createChangePasswordForm($data);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var User $user */
$user = $this->getUser();
$hasher = $hasherFactory->getPasswordHasher($user);
$user->changePassword($hasher, $data->newPassword);
$em->flush();
$this->addFlash('success', 'Password changed.');
return $this->redirectToRoute('user_account');
}
return $this->renderForm('Account/password.html.twig', [
'form' => $form,
]);
}
public function addApiToken(EntityManagerInterface $em): Response public function addApiToken(EntityManagerInterface $em): Response
{ {
/** @var User|null $user */ /** @var User|null $user */
@ -90,13 +57,4 @@ class AccountController extends AbstractController
return $this->redirectToRoute('user_account'); return $this->redirectToRoute('user_account');
} }
private function createChangePasswordForm(PasswordChangeData $data): FormInterface
{
return $this
->createForm(PasswordChangeType::class, $data, [
'action' => $this->generateUrl('user_account_password_change'),
])
->add('submit', SubmitType::class);
}
} }

View file

@ -5,15 +5,13 @@ namespace App\Controller;
use App\Entity\{Invite, PasswordResetToken}; use App\Entity\{Invite, PasswordResetToken};
use App\Repository\PasswordResetTokenRepository; use App\Repository\PasswordResetTokenRepository;
use App\Form\Data\{PasswordResetRequestData, PasswordResetData, RegisterData}; use App\Form\{Data\PasswordResetRequestData, Data\PasswordResetData, PasswordResetRequestType, PasswordResetType, RegisterType, Data\RegisterData};
use App\Form\{PasswordResetRequestType, PasswordResetType, RegisterType};
use App\Repository\InviteRepository; use App\Repository\InviteRepository;
use App\User\{Exception\UserNotFoundException, InviteManager, PasswordResetManager, UserManager}; use App\User\{Exception\UserNotFoundException, InviteManager, PasswordResetManager, UserManager};
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\{Extension\Core\Type\SubmitType, FormError, FormInterface}; use Symfony\Component\Form\{Extension\Core\Type\SubmitType, FormError, FormInterface};
use Symfony\Component\HttpFoundation\{Request, Response}; use Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
class UserController extends AbstractController class UserController extends AbstractController
{ {
@ -25,8 +23,8 @@ class UserController extends AbstractController
InviteManager $inviteManager, InviteManager $inviteManager,
InviteRepository $inviteRepo InviteRepository $inviteRepo
): Response { ): Response {
$data = new RegisterData($code); $formData = new RegisterData($code);
$form = $this->createRegisterForm($data, $code); $form = $this->createRegisterForm($formData, $code);
/** @var Invite $invite */ /** @var Invite $invite */
$invite = $inviteRepo->findOneBy(['code' => $code, 'usedBy' => null]); $invite = $inviteRepo->findOneBy(['code' => $code, 'usedBy' => null]);
@ -35,9 +33,9 @@ class UserController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$user = $userManager->createUserByInvite( $user = $userManager->createUserByInvite(
$data->username, $formData->username,
$data->password, $formData->password,
$data->email, $formData->email,
$invite $invite
); );
@ -56,8 +54,8 @@ class UserController extends AbstractController
public function requestReset(Request $request, EntityManagerInterface $em, PasswordResetManager $manager): Response public function requestReset(Request $request, EntityManagerInterface $em, PasswordResetManager $manager): Response
{ {
$data = new PasswordResetRequestData(); $formData = new PasswordResetRequestData();
$form = $this->createResetRequestForm($data); $form = $this->createResetRequestForm($formData);
$form->handleRequest($request); $form->handleRequest($request);
@ -65,7 +63,7 @@ class UserController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
try { try {
$manager->sendResetLink($data->email); $manager->sendResetLink($formData->email);
$message = 'Password reset link was sent'; $message = 'Password reset link was sent';
} catch (UserNotFoundException $e) { } catch (UserNotFoundException $e) {
@ -86,12 +84,12 @@ class UserController extends AbstractController
string $code, string $code,
Request $request, Request $request,
EntityManagerInterface $em, EntityManagerInterface $em,
PasswordHasherFactoryInterface $hasherFactory, UserManager $manager,
PasswordResetTokenRepository $tokenRepository, PasswordResetTokenRepository $tokenRepository
): Response ): Response
{ {
$data = new PasswordResetData(); $formData = new PasswordResetData();
$form = $this->createPasswordResetForm($data, $code); $form = $this->createResetForm($formData, $code);
/** @var PasswordResetToken $token */ /** @var PasswordResetToken $token */
$token = $tokenRepository->find($code); $token = $tokenRepository->find($code);
@ -100,17 +98,15 @@ class UserController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
if ($token && $token->isValid()) { if ($token && $token->isValid()) {
$user = $token->getUser(); $manager->changePassword($token->getUser(), $formData->password);
$hasher = $hasherFactory->getPasswordHasher($user);
$user->changePassword($hasher, $data->password);
$em->remove($token); $em->remove($token);
$em->flush(); $em->flush();
return $this->redirectToRoute('user_auth_login'); return $this->redirectToRoute('user_auth_login');
} else {
$form->addError(new FormError('Invalid token.'));
} }
$form->addError(new FormError('Invalid token.'));
} }
return $this->render('User/reset.html.twig', [ return $this->render('User/reset.html.twig', [
@ -120,28 +116,32 @@ class UserController extends AbstractController
private function createResetRequestForm(PasswordResetRequestData $formData): FormInterface private function createResetRequestForm(PasswordResetRequestData $formData): FormInterface
{ {
return $this $form = $this->createForm(PasswordResetRequestType::class, $formData, [
->createForm(PasswordResetRequestType::class, $formData, [ 'action' => $this->generateUrl('user_reset_request'),
'action' => $this->generateUrl('user_reset_request'), ]);
]) $form->add('submit', SubmitType::class);
->add('submit', SubmitType::class);
return $form;
} }
private function createPasswordResetForm(PasswordResetData $data, string $code): FormInterface private function createResetForm(PasswordResetData $formData, string $code): FormInterface
{ {
return $this $form = $this->createForm(PasswordResetType::class, $formData, [
->createForm(PasswordResetType::class, $data, [ 'action' => $this->generateUrl('user_reset', ['code' => $code]),
'action' => $this->generateUrl('user_reset', ['code' => $code]), ]);
]) $form->add('submit', SubmitType::class);
->add('submit', SubmitType::class);
return $form;
} }
private function createRegisterForm(RegisterData $formData, string $code): FormInterface private function createRegisterForm(RegisterData $formData, string $code): FormInterface
{ {
return $this $form = $this->createForm(RegisterType::class, $formData, [
->createForm(RegisterType::class, $formData, [ 'action' => $this->generateUrl('user_register', ['code' => $code]),
'action' => $this->generateUrl('user_register', ['code' => $code]), ]);
])
->add('submit', SubmitType::class); $form->add('submit', SubmitType::class);
return $form;
} }
} }

View file

@ -1,19 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Form\Data;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
class PasswordChangeData
{
#[Assert\NotBlank]
#[SecurityAssert\UserPassword(message: 'Wrong password')]
public string $currentPassword;
#[Assert\NotBlank]
#[Assert\Length(min: 8, max: 4096)]
#[Assert\NotCompromisedPassword(skipOnError: true)]
public string $newPassword;
}

View file

@ -9,7 +9,7 @@ use Symfony\Component\Form\FormBuilderInterface;
class LoginType extends AbstractType class LoginType extends AbstractType
{ {
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder $builder
->add('_username', TextType::class, ['mapped' => false, 'required' => true]) ->add('_username', TextType::class, ['mapped' => false, 'required' => true])

View file

@ -1,38 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Form;
use App\Form\Data\PasswordChangeData;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\{PasswordType, RepeatedType};
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class PasswordChangeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('currentPassword', PasswordType::class, [
'label' => 'Current password',
'invalid_message' => 'Wrong password.',
'required' => true,
])
->add('newPassword', RepeatedType::class, [
'type' => PasswordType::class,
'invalid_message' => 'The password fields must match.',
'required' => true,
'first_options' => ['label' => 'New password'],
'second_options' => ['label' => 'Repeat'],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => PasswordChangeData::class,
]);
}
}

View file

@ -10,7 +10,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class PasswordResetRequestType extends AbstractType class PasswordResetRequestType extends AbstractType
{ {
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder $builder
->add('email', EmailType::class, ['required' => true]) ->add('email', EmailType::class, ['required' => true])
@ -18,7 +18,7 @@ class PasswordResetRequestType extends AbstractType
; ;
} }
public function configureOptions(OptionsResolver $resolver): void public function configureOptions(OptionsResolver $resolver)
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'data_class' => PasswordResetRequestData::class, 'data_class' => PasswordResetRequestData::class,

View file

@ -11,7 +11,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class PasswordResetType extends AbstractType class PasswordResetType extends AbstractType
{ {
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder $builder
->add('password', RepeatedType::class, [ ->add('password', RepeatedType::class, [
@ -24,7 +24,7 @@ class PasswordResetType extends AbstractType
; ;
} }
public function configureOptions(OptionsResolver $resolver): void public function configureOptions(OptionsResolver $resolver)
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'data_class' => PasswordResetData::class, 'data_class' => PasswordResetData::class,

View file

@ -11,7 +11,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
class RegisterType extends AbstractType class RegisterType extends AbstractType
{ {
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder $builder
->add('username', TextType::class, ['required' => true]) ->add('username', TextType::class, ['required' => true])
@ -21,7 +21,7 @@ class RegisterType extends AbstractType
; ;
} }
public function configureOptions(OptionsResolver $resolver): void public function configureOptions(OptionsResolver $resolver)
{ {
$resolver->setDefaults([ $resolver->setDefaults([
'data_class' => RegisterData::class, 'data_class' => RegisterData::class,

View file

@ -34,6 +34,11 @@ class UserManager
return $user; return $user;
} }
public function changePassword(User $user, string $rawPassword): void
{
$user->changePassword($this->hasherFactory->getPasswordHasher(User::class), $rawPassword);
}
public function createUserByInvite(string $username, string $password, string $email, Invite $invite, array $roles = self::DEFAULT_ROLES): User public function createUserByInvite(string $username, string $password, string $email, Invite $invite, array $roles = self::DEFAULT_ROLES): User
{ {
if (null !== $invite->getUsedBy()) { if (null !== $invite->getUsedBy()) {

View file

@ -1,5 +0,0 @@
{% extends 'base.html.twig' %}
{% block content %}
{{ form(form) }}
{% endblock %}

View file

@ -10,7 +10,7 @@
<tbody> <tbody>
<tr> <tr>
<th scope="row">Username</th> <th scope="row">Username</th>
<td>{{ user.userIdentifier }}</td> <td>{{ user.username }}</td>
<td></td> <td></td>
</tr> </tr>
<tr> <tr>
@ -20,12 +20,5 @@
<button type="button" class="btn disabled" title="Not implemented">Change</button> <button type="button" class="btn disabled" title="Not implemented">Change</button>
</td> </td>
</tr> </tr>
<tr>
<th scope="row">Password</th>
<td>********</td>
<td>
<a role="button" class="btn btn-primary" href="{{ path('user_account_password_change') }}">Change</a>
</td>
</tr>
</tbody> </tbody>
</table> </table>