Password change #13
|
@ -5,13 +5,15 @@ 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, Data\PasswordResetData, PasswordResetRequestType, PasswordResetType, RegisterType, Data\RegisterData};
|
use App\Form\Data\{PasswordResetRequestData, PasswordResetData, 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
|
||||||
{
|
{
|
||||||
|
@ -23,8 +25,8 @@ class UserController extends AbstractController
|
||||||
InviteManager $inviteManager,
|
InviteManager $inviteManager,
|
||||||
InviteRepository $inviteRepo
|
InviteRepository $inviteRepo
|
||||||
): Response {
|
): Response {
|
||||||
$formData = new RegisterData($code);
|
$data = new RegisterData($code);
|
||||||
$form = $this->createRegisterForm($formData, $code);
|
$form = $this->createRegisterForm($data, $code);
|
||||||
|
|
||||||
/** @var Invite $invite */
|
/** @var Invite $invite */
|
||||||
$invite = $inviteRepo->findOneBy(['code' => $code, 'usedBy' => null]);
|
$invite = $inviteRepo->findOneBy(['code' => $code, 'usedBy' => null]);
|
||||||
|
@ -33,9 +35,9 @@ class UserController extends AbstractController
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
$user = $userManager->createUserByInvite(
|
$user = $userManager->createUserByInvite(
|
||||||
$formData->username,
|
$data->username,
|
||||||
$formData->password,
|
$data->password,
|
||||||
$formData->email,
|
$data->email,
|
||||||
$invite
|
$invite
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -54,8 +56,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
|
||||||
{
|
{
|
||||||
$formData = new PasswordResetRequestData();
|
$data = new PasswordResetRequestData();
|
||||||
$form = $this->createResetRequestForm($formData);
|
$form = $this->createResetRequestForm($data);
|
||||||
|
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
@ -63,7 +65,7 @@ class UserController extends AbstractController
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
try {
|
try {
|
||||||
$manager->sendResetLink($formData->email);
|
$manager->sendResetLink($data->email);
|
||||||
|
|
||||||
$message = 'Password reset link was sent';
|
$message = 'Password reset link was sent';
|
||||||
} catch (UserNotFoundException $e) {
|
} catch (UserNotFoundException $e) {
|
||||||
|
@ -84,12 +86,12 @@ class UserController extends AbstractController
|
||||||
string $code,
|
string $code,
|
||||||
Request $request,
|
Request $request,
|
||||||
EntityManagerInterface $em,
|
EntityManagerInterface $em,
|
||||||
UserManager $manager,
|
PasswordHasherFactoryInterface $hasherFactory,
|
||||||
PasswordResetTokenRepository $tokenRepository
|
PasswordResetTokenRepository $tokenRepository,
|
||||||
): Response
|
): Response
|
||||||
{
|
{
|
||||||
$formData = new PasswordResetData();
|
$data = new PasswordResetData();
|
||||||
$form = $this->createResetForm($formData, $code);
|
$form = $this->createPasswordResetForm($data, $code);
|
||||||
|
|
||||||
/** @var PasswordResetToken $token */
|
/** @var PasswordResetToken $token */
|
||||||
$token = $tokenRepository->find($code);
|
$token = $tokenRepository->find($code);
|
||||||
|
@ -98,15 +100,17 @@ class UserController extends AbstractController
|
||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
if ($token && $token->isValid()) {
|
if ($token && $token->isValid()) {
|
||||||
$manager->changePassword($token->getUser(), $formData->password);
|
$user = $token->getUser();
|
||||||
|
$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', [
|
||||||
|
@ -116,32 +120,28 @@ class UserController extends AbstractController
|
||||||
|
|
||||||
private function createResetRequestForm(PasswordResetRequestData $formData): FormInterface
|
private function createResetRequestForm(PasswordResetRequestData $formData): FormInterface
|
||||||
{
|
{
|
||||||
$form = $this->createForm(PasswordResetRequestType::class, $formData, [
|
return $this
|
||||||
|
->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 createResetForm(PasswordResetData $formData, string $code): FormInterface
|
private function createPasswordResetForm(PasswordResetData $data, string $code): FormInterface
|
||||||
{
|
{
|
||||||
$form = $this->createForm(PasswordResetType::class, $formData, [
|
return $this
|
||||||
|
->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
|
||||||
{
|
{
|
||||||
$form = $this->createForm(RegisterType::class, $formData, [
|
return $this
|
||||||
|
->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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -34,11 +34,6 @@ 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()) {
|
||||||
|
|
Loading…
Reference in a new issue