src/Postroyka/AccountBundle/Controller/AuthController.php line 48

Open in your IDE?
  1. <?php
  2. namespace Postroyka\AccountBundle\Controller;
  3. use Postroyka\AccountBundle\Events\AccountEvents;
  4. use Postroyka\AccountBundle\Form\LoginForm;
  5. use Postroyka\AccountBundle\Form\RestorePasswordForm;
  6. use Postroyka\AccountBundle\Mailer\AccountMailer;
  7. use Postroyka\AppBundle\Controller\AbstractController;
  8. use Submarine\ConfirmationBundle\Service\ConfirmationService;
  9. use Submarine\CoreBundle\AccessControl\Role;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class AuthController extends AbstractController
  14. {
  15.     /**
  16.      * @return AccountMailer
  17.      */
  18.     private function accountMailer()
  19.     {
  20.         return $this->get('postroyka_account.account_mailer');
  21.     }
  22.     /**
  23.      * @return ConfirmationService
  24.      */
  25.     private function confirmation()
  26.     {
  27.         return $this->get('submarine.confirmation');
  28.     }
  29.     /**
  30.      * Логин
  31.      */
  32.     public function loginAction(Request $requestAuthenticationUtils $authenticationUtils)
  33.     {
  34.         if ($this->isGranted(Role::ROLE_USER)) {
  35.             return $this->redirect($this->generateUrl('account_profile'));
  36.         }
  37.         $this->getMetaTags()->setDescription('Личный кабинет. Войти в аккаунту postroyka.by. Вход в личный кабинет аккаунта строительного интернет-магазина postroyka.by.');
  38.         $data = [];
  39.         $data['showBlockConfirm'] = false;
  40.         $lastEmail $request->getSession()->get('email');
  41.         $codeError Security::AUTHENTICATION_ERROR;
  42.         if ($request->attributes->has($codeError) || $request->getSession()->has($codeError)) {
  43.             $request->getSession()->remove($codeError);
  44.             $lastEmail $request->getSession()->get(Security::LAST_USERNAME);
  45.             $user $this->get('submarine.users')->getUserByEmail($lastEmail);
  46.             if ($user->getId() && !$user->isConfirmed()) {
  47.                 $data['showBlockConfirm'] = true;
  48.             } elseif ($user->getId() && !$user->isEnabled()) {
  49.                 $this->notifier()->warning('message.error.user_disabled');
  50.             } else {
  51.                 $this->notifier()->error('message.error.login_failed');
  52.             }
  53.         }
  54.         $data['_email'] = $lastEmail;
  55.         $data['_remember_me'] = true;
  56.         $form $this->createForm(LoginForm::class, $data);
  57.         $data['form'] = $form->createView();
  58.         $data['lastEmail'] = $lastEmail;
  59.         $viewPage getenv("APP_LOGIN_ENV") === "prod" "login.html.twig" "login_dev.html.twig";
  60.         return $this->renderPage("Postroyka/AccountBundle/Auth/$viewPage"$data);
  61.     }
  62.     /**
  63.      * Восстановление пароля
  64.      */
  65.     public function restorePasswordAction(Request $request)
  66.     {
  67.         $this->getMetaTags()->setDescription('Не получается войти? Забыли пароль к аккаунту postroyka.by? С помощью восстановления пароля можете восстановить пароль.');
  68.         $data = [];
  69.         $form $this->createForm(RestorePasswordForm::class);
  70.         $form->handleRequest($request);
  71.         if ($form->isSubmitted() && $form->isValid()) {
  72.             $formData $form->getData();
  73.             $user $this->get('submarine.users')->getUserByEmail($formData['email']);
  74.             if (!$user->getId()) {
  75.                 $this->notifier()->error('Пользователь с таким e-mail не найден.');
  76.                 return $this->redirect($this->generateUrl('restore_password'));
  77.             }
  78.             $code $this->confirmation()->createCodeConfirm(AccountEvents::RESTORE_PASSWORD$formData$user->getId());
  79.             if (!$this->accountMailer()->sendRestorePasswordConfirm($user$code$formData['password'])) {
  80.                 $this->notifier()->error('message.error.email_not_send');
  81.             } else {
  82.                 $this->notifier()->success('message.success.password_restore_must_confirmed');
  83.                 return $this->redirectToRoute('login');
  84.             }
  85.         }
  86.         $data['form'] = $form->createView();
  87.         return $this->renderPage('Postroyka/AccountBundle/Auth/restore_password.html.twig'$data);
  88.     }
  89. }