From 7695c19b402a71947ef20cebcbb82ddea3e2d3fc Mon Sep 17 00:00:00 2001 From: touffies Date: Mon, 14 Oct 2013 12:25:12 +0200 Subject: [PATCH] Login action : * Add account * If user has checked "I'm a new customer", we must verify if it's a new email address. --- .../Controller/Front/CustomerController.php | 37 +++++++------- .../Core/Template/Smarty/Plugins/Form.php | 5 ++ core/lib/Thelia/Form/CustomerLogin.php | 50 ++++++++++++++++--- templates/default/login.html | 19 ++++--- 4 files changed, 77 insertions(+), 34 deletions(-) diff --git a/core/lib/Thelia/Controller/Front/CustomerController.php b/core/lib/Thelia/Controller/Front/CustomerController.php index f322d3fe1..bf84d987f 100755 --- a/core/lib/Thelia/Controller/Front/CustomerController.php +++ b/core/lib/Thelia/Controller/Front/CustomerController.php @@ -193,31 +193,34 @@ class CustomerController extends BaseFrontController $form = $this->validateForm($customerLoginForm, "post"); - $authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm); + try { - $customer = $authenticator->getAuthentifiedUser(); + $authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm); - $this->processLogin($customer); + $customer = $authenticator->getAuthentifiedUser(); - $this->redirectSuccess($customerLoginForm); + $this->processLogin($customer); + + $this->redirectSuccess($customerLoginForm); + + } catch (UsernameNotFoundException $e) { + $message = "1.Wrong email or password. Please try again"; + } catch (WrongPasswordException $e) { + $message = "2.Wrong email or password. Please try again"; + } catch (AuthenticationException $e) { + $message = "3.Wrong email or password. Please try again"; + } } catch (FormValidationException $e) { - if ($request->request->has("account")) { - $account = $request->request->get("account"); - $form = $customerLoginForm->getForm(); - if ($account == 0 && $form->get("email")->getData() !== null) { - $this->redirectToRoute("customer.create.view", array("email" => $form->get("email")->getData())); - } + // If User is a new customer + $form = $customerLoginForm->getForm(); + if ($form->get('account')->getData() == 0 && !$form->get("email")->getErrors()) { + $this->redirectToRoute("customer.create.view", array("email" => $form->get("email")->getData())); + } else { + $message = sprintf("Please check your input: %s", $e->getMessage()); } - $message = sprintf("Please check your input: %s", $e->getMessage()); - } catch (UsernameNotFoundException $e) { - $message = "Wrong email or password. Please try again"; - } catch (WrongPasswordException $e) { - $message = "Wrong email or password. Please try again"; - } catch (AuthenticationException $e) { - $message = "Wrong email or password. Please try again"; } catch (\Exception $e) { $message = sprintf("Sorry, an error occured: %s", $e->getMessage()); } diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index e7613bbac..84ce2e7f9 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -151,6 +151,11 @@ class Form extends AbstractSmartyPlugin $template->assign("options", $formFieldView->vars); + /* access to choices */ + if (isset($formFieldView->vars['choices'])) { + $template->assign("choices", $formFieldView->vars['choices']); + } + $value = $formFieldView->vars["value"]; /* FIXME: doesnt work. We got "This form should not contain extra fields." error. // We have a collection diff --git a/core/lib/Thelia/Form/CustomerLogin.php b/core/lib/Thelia/Form/CustomerLogin.php index 63dc186aa..dee785735 100755 --- a/core/lib/Thelia/Form/CustomerLogin.php +++ b/core/lib/Thelia/Form/CustomerLogin.php @@ -22,10 +22,18 @@ /*************************************************************************************/ namespace Thelia\Form; +use Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Email; +use Symfony\Component\Validator\ExecutionContextInterface; use Thelia\Core\Translation\Translator; +use Thelia\Model\Base\CustomerQuery; +/** + * Class CustomerLogin + * @package Thelia\Form + * @author Manuel Raynaud + */ class CustomerLogin extends BaseForm { protected function buildForm() @@ -33,8 +41,14 @@ class CustomerLogin extends BaseForm $this->formBuilder ->add("email", "email", array( "constraints" => array( - new NotBlank(), - new Email() + new Constraints\NotBlank(), + new Constraints\Email(), + new Constraints\Callback(array( + "methods" => array( + array($this, + "verifyExistingEmail") + ) + )) ), "label" => Translator::getInstance()->trans("Please enter your email address"), "label_attr" => array( @@ -42,18 +56,40 @@ class CustomerLogin extends BaseForm ), "required" => true )) + ->add("account", "choice", array( + "choices" => array( + 0 => Translator::getInstance()->trans("No, I am a new customer."), + 1 => Translator::getInstance()->trans("Yes, I have a password :") + ), + "label_attr" => array( + "for" => "account" + ), + "data" => 0 + )) ->add("password", "password", array( "constraints" => array( - new NotBlank() + new Constraints\NotBlank() ), "label" => Translator::getInstance()->trans("Please enter your password"), "label_attr" => array( "for" => "password" ), - "required" => true - )) - ->add("remember_me", "checkbox") - ; + 'required' => false + )); + } + + /** + * If the user select "I'am a new customer", we make sure is email address does not exit in the database. + */ + public function verifyExistingEmail($value, ExecutionContextInterface $context) + { + $data = $context->getRoot()->getData(); + if ($data["account"] == 0) { + $customer = CustomerQuery::create()->findOneByEmail($value); + if ($customer) { + $context->addViolation("A user already exists with this email address. Please login or if you've forgotten your password, go to Reset Your Password."); + } + } } public function getName() diff --git a/templates/default/login.html b/templates/default/login.html index 8f0dc4c1e..6975a90b3 100644 --- a/templates/default/login.html +++ b/templates/default/login.html @@ -41,17 +41,16 @@ {/form_field}
+ {form_field form=$form field="account"} {intl l="Do you have an account?"} -
- -
-
- -
+ {foreach $choices as $choice} +
+ +
+ {/foreach} + {/form_field} {form_field form=$form field="password"}