src/Application/Controller/AuthController.php line 170

Open in your IDE?
  1. <?php
  2. namespace App\Application\Controller;
  3. use App\Admin\Document\Client;
  4. use App\Admin\Document\UserAccount;
  5. use App\Application\Cart\Cart;
  6. use AvenueAdminBundle\Util\FileUtil;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use \Symfony\Component\Routing\Annotation\Route;
  11. class AuthController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/register", name="register")
  15.      */
  16.     public function register(Request $request): Response
  17.     {
  18.         if ($this->hasIdentity()) {
  19.             return $this->redirectToRoute('profile');
  20.         }
  21.         $message '';
  22.         if ($request->isMethod('POST')) {
  23.             $this->detectCaptchaBot($request);
  24.             $email strtolower(trim($request->request->get('email')));
  25.             if (!$email) {
  26.                 throw new \Exception('no email');
  27.             }
  28.             $name trim($request->request->get('name'));
  29.             if (!$name) {
  30.                 throw new \Exception('no name');
  31.             }
  32.             $phone $this->trimPhone($request->request->get('phone'));
  33.             if (!$this->getDocumentRepository(UserAccount::class)->findOneBy(['email' => $email])) {
  34.                 $user = new UserAccount();
  35.                 $this->getDocumentManager()->persist($user);
  36.                 $user->setName($name);
  37.                 $user->setPhone($phone);
  38.                 $user->setEmail($email);
  39.                 $user->updateFullName();
  40.                 $this->getDocumentManager()->flush();
  41.                 $text $this->renderView(
  42.                     'application/mail/confirm.html.twig', [
  43.                         'user' => $user,
  44.                     ]
  45.                 );
  46.                 try {
  47.                     $this->getMail()->send($email'Подтверждение регистрации на cosca.ru'$text);
  48.                     $message 'На почту отправлена ссылка для подтверждения регистрации';
  49.                 } catch (\Exception $e) {
  50.                 }
  51.             } else {
  52.                 $message 'Эта почта уже занята';
  53.             }
  54.         }
  55.         return $this->render('application/auth/register.html.twig', [
  56.             'message' => $message,
  57.         ]);
  58.     }
  59.     /**
  60.      * @Route("/register/confirm", name="register-confirm")
  61.      */
  62.     public function confirm(Request $request): Response
  63.     {
  64.         if ($this->hasIdentity()) {
  65.             return $this->redirectToRoute('profile');
  66.         }
  67.         $id strtolower(trim($request->get('id')));
  68.         /**
  69.          * @var UserAccount $user
  70.          */
  71.         $user $this->getDocumentRepository(UserAccount::class)->find($id);
  72.         if (!$user) {
  73.             throw new \Exception('no user');
  74.         }
  75.         if ($user->getActive()) {
  76.             throw new \Exception('user active');
  77.         }
  78.         $user->setActive(true);
  79.         /**
  80.          * @var Client $client
  81.          */
  82.         $client = new Client();
  83.         $this->getDocumentManager()->persist($client);
  84.         $client->pending();
  85.         $client->setName($user->getFullName());
  86.         $counterparty $client->addRetailCounterparty();
  87.         $this->getDocumentManager()->persist($counterparty);
  88.         $user->setClient($client);
  89.         $newPassword $this->generatePassword();
  90.         $passwordAsHash $this->getPasswordHash($newPassword);
  91.         $user->setPassword($passwordAsHash);
  92.         $this->getDocumentManager()->flush();
  93.         $text $this->renderView(
  94.             'application/mail/register.html.twig', [
  95.                 'user' => $user,
  96.                 'newPassword' => $newPassword
  97.             ]
  98.         );
  99.         try {
  100.             $this->getMail()->send($user->getEmail(), 'Вы зарегистрированы'$text);
  101.         } catch (\Exception $e) {
  102.         }
  103.         return $this->redirectToRoute('login', ['confirm' => 1]);
  104.     }
  105.     /**
  106.      * @Route("/login", name="login")
  107.      */
  108.     public function login(Request $requestCart $cart): Response
  109.     {
  110.         if ($this->hasIdentity()) {
  111.             return $this->redirectToRoute('home');
  112.         }
  113.         $errorMessage '';
  114.         if ($request->isMethod('POST')) {
  115.             $email $request->request->get('email');
  116.             $password $request->request->get('password');
  117.             if ($this->authenticate($email$password)) {
  118.                 $redirect $request->get('redirect');
  119.                 if (!$redirect) {
  120.                     $redirect 'home';
  121.                     $user $this->getLoggedInUser();
  122.                     $client $user->getClient();
  123.                     if ($client->getSeller() && !$cart->getCounterparty()) {
  124.                         foreach ($client->getActiveCounterparties() as $counterparty) {
  125.                             $cart->setCounterparty($counterparty$user->getCartItemsByCounterparty($counterparty->getId()));
  126.                             break;
  127.                         }
  128.                     }
  129.                 }
  130.                 return $this->redirectToRoute($redirect);
  131.             } else {
  132.                 $errorMessage 'Неверные идентификационные данные';
  133.             }
  134.         } else {
  135.             if ($request->get('confirm')) {
  136.                 $errorMessage 'Почта подтверждена. Пароль отправлен на указаннную почту.';
  137.             }
  138.         }
  139.         return $this->render(
  140.             'application/auth/login.html.twig', [
  141.                 'message' => $errorMessage
  142.             ]
  143.         );
  144.     }
  145.     /**
  146.      * @Route("/reset-ajax", name="reset-ajax")
  147.      */
  148.     public function resetAjax(Request $request)
  149.     {
  150.         return new JsonResponse($this->resetPassword($request));
  151.     }
  152.     private function resetPassword(Request $request)
  153.     {
  154.         $email null;
  155.         $errorMessage null;
  156.         if ($request->isMethod('POST')) {
  157.             $email $request->request->get('email');
  158.             /**
  159.              * @var UserAccount $user
  160.              */
  161.             $user $this->findIdentity($email);
  162.             if ($user && $user->getActive()) {
  163.                 $newPassword $this->generatePassword();
  164.                 $passwordAsHash $this->getPasswordHash($newPassword);
  165.                 $user->setPassword($passwordAsHash);
  166.                 $this->getDocumentManager()->flush();
  167.                 $text $this->renderView(
  168.                     'application/mail/reset.html.twig', [
  169.                         'user' => $user,
  170.                         'newPassword' => $newPassword
  171.                     ]
  172.                 );
  173.                 try {
  174.                     $this->getMail()->send($email'Восстановление пароля'$text);
  175.                     $errorMessage 'Новый пароль отправлен на почту ' $email;
  176.                 } catch (\Exception $e) {
  177.                     $errorMessage 'Мы не смогли отправить письмо с паролем<!--' . (string)$e .  '-->';
  178.                 }
  179.             } else {
  180.                 $errorMessage 'Такой пользователь не найден';
  181.             }
  182.         }
  183.         return [
  184.             'message' => $errorMessage,
  185.             'email' => $email
  186.         ];
  187.     }
  188.     /**
  189.      * @Route("/logout", name="logout")
  190.      * @param Request $request
  191.      * @return \Symfony\Component\HttpFoundation\Response
  192.      */
  193.     public function logout(Request $request)
  194.     {
  195.         $this->clearIdentity();
  196.         return $this->redirectToRoute('home');
  197.     }
  198. }