src/Controller/Frontend/SecurityController.php line 21
<?php
namespace App\Controller\Frontend;
use App\Entity\User\User;
use App\Exception\User\PasswordRecovery\InvalidLink;
use App\Forms\Types\User\RegistrationType;
use App\Service\User\Event\UserCreatedEvent;
use App\Service\User\UserManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'login', priority: 50)]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
#[Route(path: '/password-recover/{token}', name: 'password_restore')]
public function passwordRestore(
\App\Entity\User\PasswordRecovery $passwordRecovery,
\App\Service\User\PasswordRecovery $prService,
EntityManagerInterface $em
) {
if ($this->getUser()) {
return $this->redirectToRoute('main_olymp');
}
try {
$result = $prService->createNewPassword($passwordRecovery);
$em->flush();
} catch (InvalidLink $e) {
return $this->render(
'security/password-restore-invalid-link.html.twig',
[]
);
}
return $this->render(
'security/password-restore-send.html.twig',
[]
);
}
/**
* @return Response
*/
#[Route(path: '/password-recover', name: 'password_recovery')]
public function passwordRecoveryForm(
\App\Service\User\PasswordRecovery $prService,
EntityManagerInterface $em,
Request $request
) {
$data = new \App\Model\User\PasswordRecovery();
$form = $this->createForm(\App\Forms\Types\User\PasswordRecoveryType::class, $data);
$form->handleRequest($request);
// $form->addError(new FormError('test'));
$send = false;
$haveError = false;
if ($form->isSubmitted() && $form->isValid()) {
$prService->initPasswordRecovery($data);
$em->flush();
$send = true;
// if ($form->isValid()) {
// $em->flush();
// return $this->json(['ok' => true]);
// }
}
return $this->render(
'security/password-recovery-form.html.twig',
[
'send' => $send,
'form' => $form->createView(),
]
);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout()
{
throw new \LogicException(
'This method can be blank - it will be intercepted by the logout key on your firewall.'
);
}
/**
* @return Response
*/
#[Route(path: '/registration', name: 'registration')]
public function registration(
Request $request,
EntityManagerInterface $em,
UserManager $userManager,
EventDispatcherInterface $ed
): Response {
$user = new User();
// $user->setEmail("kulebyakin@gmail.com");
// $user->setPhone("+79266970986");
// $user->setFirstName("Миша");
$send = false;
$form = $this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userManager->create($user);
$event = new UserCreatedEvent(
$user/*, [
UserCreatedEvent::SUBSCRIBE => $form->get('subscribe')->getData(),
]
*/
);
$ed->dispatch($event, $event::NAME);
$em->persist($user);
// if ($form->get('subscribe')->getData()) {
// try {
// $subscribe->subscribe($user->getEmail());
// } catch (DuplicateException $e) {
// }
// }
$em->flush();
$send = true;
}
return $this->render(
'security/registration-form.html.twig',
[
'send' => $send,
'form' => $form->createView(),
]
);
}
/**
* @param Request $request
* @param EntityManagerInterface $em
* @param UserManager $userManager
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
#[Route(path: '/reg/ajax', name: 'registration_ajax', methods: ['post'])]
public function ajaxRegistration(
Request $request,
EntityManagerInterface $em,
UserManager $userManager,
EventDispatcherInterface $ed
) {
$form = $this->createForm(RegistrationType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$userManager->create($user);
$event = new UserCreatedEvent(
$user,
[
UserCreatedEvent::SUBSCRIBE => $form->get('subscribe')->getData(),
]
);
$ed->dispatch($event, $event::NAME);
$em->persist($user);
if ($form->get('subscribe')->getData()) {
}
$em->flush();
return $this->json(['ok' => true]);
}
$errors = [];
// $form->get('phone')->addError(new FormError('sdf'));
foreach ($form->getErrors(true) as $error) {
$name = $error->getOrigin()->getName();
if (empty($errors[$name])) {
$errors[$name] = [];
}
$errors[$name][] = $error->getMessage();
}
return $this->json(
[
'ok' => false,
"errors" => $errors,
]
);
}
}