|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LoginWithPhone\Controller\Front; |
| 4 | + |
| 5 | +use Front\Front; |
| 6 | +use LoginWithPhone\Form\Front\CustomerLogin; |
| 7 | +use LoginWithPhone\Security\Authentication\CustomerUsernamePasswordFormAuthenticator; |
| 8 | +use Thelia\Core\Event\Customer\CustomerEvent; |
| 9 | +use Thelia\Core\Event\TheliaEvents; |
| 10 | +use Thelia\Core\Security\Exception\AuthenticationException; |
| 11 | +use Thelia\Core\Security\Exception\CustomerNotConfirmedException; |
| 12 | +use Thelia\Core\Security\Exception\UsernameNotFoundException; |
| 13 | +use Thelia\Core\Security\Exception\WrongPasswordException; |
| 14 | +use Thelia\Form\Exception\FormValidationException; |
| 15 | +use Thelia\Log\Tlog; |
| 16 | +use Thelia\Model\Customer; |
| 17 | + |
| 18 | +class CustomerController extends \Front\Controller\CustomerController |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Perform user login. On a successful login, the user is redirected to the URL |
| 22 | + * found in the success_url form parameter, or / if none was found. |
| 23 | + * |
| 24 | + * If login is not successfull, the same view is displayed again. |
| 25 | + * |
| 26 | + */ |
| 27 | + public function loginAction() |
| 28 | + { |
| 29 | + if (!$this->getSecurityContext()->hasCustomerUser()) { |
| 30 | + $request = $this->getRequest(); |
| 31 | + $customerLoginForm = new CustomerLogin($request); |
| 32 | + |
| 33 | + try { |
| 34 | + $form = $this->validateForm($customerLoginForm, "post"); |
| 35 | + |
| 36 | + // If User is a new customer |
| 37 | + if ($form->get('account')->getData() == 0 && $form->get("email")->getErrors()->count() == 0) { |
| 38 | + return $this->generateRedirectFromRoute( |
| 39 | + "customer.create.process", |
| 40 | + ["email" => $form->get("email")->getData()] |
| 41 | + ); |
| 42 | + } else { |
| 43 | + try { |
| 44 | + $authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm, [ |
| 45 | + 'username_field_name' => 'email' |
| 46 | + ]); |
| 47 | + |
| 48 | + /** @var Customer $customer */ |
| 49 | + $customer = $authenticator->getAuthentifiedUser(); |
| 50 | + |
| 51 | + $this->processLogin($customer); |
| 52 | + |
| 53 | + if (intval($form->get('remember_me')->getData()) > 0) { |
| 54 | + // If a remember me field if present and set in the form, create |
| 55 | + // the cookie thant store "remember me" information |
| 56 | + $this->createRememberMeCookie( |
| 57 | + $customer, |
| 58 | + $this->getRememberMeCookieName(), |
| 59 | + $this->getRememberMeCookieExpiration() |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + return $this->generateSuccessRedirect($customerLoginForm); |
| 64 | + |
| 65 | + } catch (UsernameNotFoundException $e) { |
| 66 | + $message = $this->getTranslator()->trans( |
| 67 | + "Wrong email or password. Please try again", |
| 68 | + [], |
| 69 | + Front::MESSAGE_DOMAIN |
| 70 | + ); |
| 71 | + } catch (WrongPasswordException $e) { |
| 72 | + $message = $this->getTranslator()->trans( |
| 73 | + "Wrong email or password. Please try again", |
| 74 | + [], |
| 75 | + Front::MESSAGE_DOMAIN |
| 76 | + ); |
| 77 | + } catch (CustomerNotConfirmedException $e) { |
| 78 | + if ($e->getUser() !== null) { |
| 79 | + // Send the confirmation email again |
| 80 | + $this->getDispatcher()->dispatch( |
| 81 | + TheliaEvents::SEND_ACCOUNT_CONFIRMATION_EMAIL, |
| 82 | + new CustomerEvent($e->getUser()) |
| 83 | + ); |
| 84 | + } |
| 85 | + $message = $this->getTranslator()->trans( |
| 86 | + "Your account is not yet confirmed. A confirmation email has been sent to your email address, please check your mailbox", |
| 87 | + [], |
| 88 | + Front::MESSAGE_DOMAIN |
| 89 | + ); |
| 90 | + } catch (AuthenticationException $e) { |
| 91 | + $message = $this->getTranslator()->trans( |
| 92 | + "Wrong email or password. Please try again", |
| 93 | + [], |
| 94 | + Front::MESSAGE_DOMAIN |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + } catch (FormValidationException $e) { |
| 100 | + $message = $this->getTranslator()->trans( |
| 101 | + "Please check your input: %s", |
| 102 | + ['%s' => $e->getMessage()], |
| 103 | + Front::MESSAGE_DOMAIN |
| 104 | + ); |
| 105 | + } catch (\Exception $e) { |
| 106 | + $message = $this->getTranslator()->trans( |
| 107 | + "Sorry, an error occured: %s", |
| 108 | + ['%s' => $e->getMessage()], |
| 109 | + Front::MESSAGE_DOMAIN |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + Tlog::getInstance()->error( |
| 114 | + sprintf( |
| 115 | + "Error during customer login process : %s. Exception was %s", |
| 116 | + $message, |
| 117 | + $e->getMessage() |
| 118 | + ) |
| 119 | + ); |
| 120 | + |
| 121 | + $customerLoginForm->setErrorMessage($message); |
| 122 | + |
| 123 | + $this->getParserContext()->addForm($customerLoginForm); |
| 124 | + |
| 125 | + if ($customerLoginForm->hasErrorUrl()) { |
| 126 | + return $this->generateErrorRedirect($customerLoginForm); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments