Compare commits
No commits in common. "087297b0e120746d386c535d83ea9a4c04315194" and "23d8401bb229f51c4e16060cd9a612f04781d1c2" have entirely different histories.
087297b0e1
...
23d8401bb2
|
@ -53,12 +53,6 @@ user_account:
|
|||
requirements:
|
||||
method: GET
|
||||
|
||||
user_account_password_change:
|
||||
path: /account/password
|
||||
controller: App\Controller\AccountController::changePassword
|
||||
requirements:
|
||||
method: POST
|
||||
|
||||
user_account_token_create:
|
||||
path: /profile/api/token/create
|
||||
controller: App\Controller\AccountController::addApiToken
|
||||
|
|
|
@ -44,7 +44,7 @@ class AddInvitesCommand extends Command
|
|||
|
||||
$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;
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ class AddUserCommand extends Command
|
|||
|
||||
$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;
|
||||
}
|
||||
|
|
|
@ -5,21 +5,14 @@ namespace App\Controller;
|
|||
|
||||
use App\Entity\{ApiToken, User};
|
||||
use App\Repository\{ApiTokenRepository, InviteRepository};
|
||||
use App\Form\Data\PasswordChangeData;
|
||||
use App\Form\PasswordChangeType;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\{Request, Response};
|
||||
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class AccountController extends AbstractController
|
||||
{
|
||||
public function account(
|
||||
InviteRepository $inviteRepo,
|
||||
ApiTokenRepository $apiTokenRepo
|
||||
): Response {
|
||||
public function account(InviteRepository $inviteRepo, ApiTokenRepository $apiTokenRepo): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
if (null === $user = $this->getUser()) {
|
||||
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
|
||||
{
|
||||
/** @var User|null $user */
|
||||
|
@ -90,13 +57,4 @@ class AccountController extends AbstractController
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,15 +5,13 @@ namespace App\Controller;
|
|||
|
||||
use App\Entity\{Invite, PasswordResetToken};
|
||||
use App\Repository\PasswordResetTokenRepository;
|
||||
use App\Form\Data\{PasswordResetRequestData, PasswordResetData, RegisterData};
|
||||
use App\Form\{PasswordResetRequestType, PasswordResetType, RegisterType};
|
||||
use App\Form\{Data\PasswordResetRequestData, Data\PasswordResetData, PasswordResetRequestType, PasswordResetType, RegisterType, Data\RegisterData};
|
||||
use App\Repository\InviteRepository;
|
||||
use App\User\{Exception\UserNotFoundException, InviteManager, PasswordResetManager, UserManager};
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\{Extension\Core\Type\SubmitType, FormError, FormInterface};
|
||||
use Symfony\Component\HttpFoundation\{Request, Response};
|
||||
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
|
||||
|
||||
class UserController extends AbstractController
|
||||
{
|
||||
|
@ -25,8 +23,8 @@ class UserController extends AbstractController
|
|||
InviteManager $inviteManager,
|
||||
InviteRepository $inviteRepo
|
||||
): Response {
|
||||
$data = new RegisterData($code);
|
||||
$form = $this->createRegisterForm($data, $code);
|
||||
$formData = new RegisterData($code);
|
||||
$form = $this->createRegisterForm($formData, $code);
|
||||
|
||||
/** @var Invite $invite */
|
||||
$invite = $inviteRepo->findOneBy(['code' => $code, 'usedBy' => null]);
|
||||
|
@ -35,9 +33,9 @@ class UserController extends AbstractController
|
|||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$user = $userManager->createUserByInvite(
|
||||
$data->username,
|
||||
$data->password,
|
||||
$data->email,
|
||||
$formData->username,
|
||||
$formData->password,
|
||||
$formData->email,
|
||||
$invite
|
||||
);
|
||||
|
||||
|
@ -56,8 +54,8 @@ class UserController extends AbstractController
|
|||
|
||||
public function requestReset(Request $request, EntityManagerInterface $em, PasswordResetManager $manager): Response
|
||||
{
|
||||
$data = new PasswordResetRequestData();
|
||||
$form = $this->createResetRequestForm($data);
|
||||
$formData = new PasswordResetRequestData();
|
||||
$form = $this->createResetRequestForm($formData);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
|
@ -65,7 +63,7 @@ class UserController extends AbstractController
|
|||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
try {
|
||||
$manager->sendResetLink($data->email);
|
||||
$manager->sendResetLink($formData->email);
|
||||
|
||||
$message = 'Password reset link was sent';
|
||||
} catch (UserNotFoundException $e) {
|
||||
|
@ -86,12 +84,12 @@ class UserController extends AbstractController
|
|||
string $code,
|
||||
Request $request,
|
||||
EntityManagerInterface $em,
|
||||
PasswordHasherFactoryInterface $hasherFactory,
|
||||
PasswordResetTokenRepository $tokenRepository,
|
||||
UserManager $manager,
|
||||
PasswordResetTokenRepository $tokenRepository
|
||||
): Response
|
||||
{
|
||||
$data = new PasswordResetData();
|
||||
$form = $this->createPasswordResetForm($data, $code);
|
||||
$formData = new PasswordResetData();
|
||||
$form = $this->createResetForm($formData, $code);
|
||||
|
||||
/** @var PasswordResetToken $token */
|
||||
$token = $tokenRepository->find($code);
|
||||
|
@ -100,18 +98,16 @@ class UserController extends AbstractController
|
|||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
if ($token && $token->isValid()) {
|
||||
$user = $token->getUser();
|
||||
$hasher = $hasherFactory->getPasswordHasher($user);
|
||||
$user->changePassword($hasher, $data->password);
|
||||
$manager->changePassword($token->getUser(), $formData->password);
|
||||
|
||||
$em->remove($token);
|
||||
$em->flush();
|
||||
|
||||
return $this->redirectToRoute('user_auth_login');
|
||||
}
|
||||
|
||||
} else {
|
||||
$form->addError(new FormError('Invalid token.'));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('User/reset.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
|
@ -120,28 +116,32 @@ class UserController extends AbstractController
|
|||
|
||||
private function createResetRequestForm(PasswordResetRequestData $formData): FormInterface
|
||||
{
|
||||
return $this
|
||||
->createForm(PasswordResetRequestType::class, $formData, [
|
||||
$form = $this->createForm(PasswordResetRequestType::class, $formData, [
|
||||
'action' => $this->generateUrl('user_reset_request'),
|
||||
])
|
||||
->add('submit', SubmitType::class);
|
||||
]);
|
||||
$form->add('submit', SubmitType::class);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
private function createPasswordResetForm(PasswordResetData $data, string $code): FormInterface
|
||||
private function createResetForm(PasswordResetData $formData, string $code): FormInterface
|
||||
{
|
||||
return $this
|
||||
->createForm(PasswordResetType::class, $data, [
|
||||
$form = $this->createForm(PasswordResetType::class, $formData, [
|
||||
'action' => $this->generateUrl('user_reset', ['code' => $code]),
|
||||
])
|
||||
->add('submit', SubmitType::class);
|
||||
]);
|
||||
$form->add('submit', SubmitType::class);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
private function createRegisterForm(RegisterData $formData, string $code): FormInterface
|
||||
{
|
||||
return $this
|
||||
->createForm(RegisterType::class, $formData, [
|
||||
$form = $this->createForm(RegisterType::class, $formData, [
|
||||
'action' => $this->generateUrl('user_register', ['code' => $code]),
|
||||
])
|
||||
->add('submit', SubmitType::class);
|
||||
]);
|
||||
|
||||
$form->add('submit', SubmitType::class);
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -9,7 +9,7 @@ use Symfony\Component\Form\FormBuilderInterface;
|
|||
|
||||
class LoginType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('_username', TextType::class, ['mapped' => false, 'required' => true])
|
||||
|
|
|
@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
|||
|
||||
class PasswordResetRequestType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->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([
|
||||
'data_class' => PasswordResetRequestData::class,
|
||||
|
|
|
@ -11,7 +11,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
|||
|
||||
class PasswordResetType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->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([
|
||||
'data_class' => PasswordResetData::class,
|
||||
|
|
|
@ -11,7 +11,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
|
|||
|
||||
class RegisterType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->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([
|
||||
'data_class' => RegisterData::class,
|
||||
|
|
|
@ -34,6 +34,11 @@ class UserManager
|
|||
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
|
||||
{
|
||||
if (null !== $invite->getUsedBy()) {
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block content %}
|
||||
{{ form(form) }}
|
||||
{% endblock %}
|
|
@ -10,7 +10,7 @@
|
|||
<tbody>
|
||||
<tr>
|
||||
<th scope="row">Username</th>
|
||||
<td>{{ user.userIdentifier }}</td>
|
||||
<td>{{ user.username }}</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
|
@ -20,12 +20,5 @@
|
|||
<button type="button" class="btn disabled" title="Not implemented">Change</button>
|
||||
</td>
|
||||
</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>
|
||||
</table>
|
Loading…
Reference in a new issue