create customer login controller

This commit is contained in:
Manuel Raynaud
2013-08-16 14:24:00 +02:00
parent 4f31627301
commit 0397f5fdf8
3 changed files with 80 additions and 48 deletions

View File

@@ -99,47 +99,6 @@ class Customer extends BaseAction implements EventSubscriberInterface
$this->getFrontSecurityContext()->clear();
}
/**
* Perform user login. On a successful login, the user is redirected to the URL
* found in the success_url form parameter, or / if none was found.
*
* If login is not successfull, the same view is dispolyed again.
*
* @param ActionEvent $event
*/
public function login(ActionEvent $event)
{
$request = $event->getRequest();
$customerLoginForm = new CustomerLogin($request);
$authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm);
try {
$user = $authenticator->getAuthentifiedUser();
$event->customer = $user;
} catch (ValidatorException $ex) {
$message = "Missing or invalid information. Please check your input.";
} catch (UsernameNotFoundException $ex) {
$message = "This email address was not found.";
} catch (AuthenticationException $ex) {
$message = "Login failed. Please check your username and password.";
} catch (\Exception $ex) {
$message = sprintf("Unable to process your request. Please try again (%s in %s).", $ex->getMessage(), $ex->getFile());
}
// The for has an error
$customerLoginForm->setError(true);
$customerLoginForm->setErrorMessage($message);
// Dispatch the errored form
$event->setErrorForm($customerLoginForm);
// A this point, the same view is displayed again.
}
public function changePassword(ActionEvent $event)
{
// TODO
@@ -170,8 +129,6 @@ class Customer extends BaseAction implements EventSubscriberInterface
return array(
"action.createCustomer" => array("create", 128),
"action.modifyCustomer" => array("modify", 128),
"action.loginCustomer" => array("login", 128),
"action.logoutCustomer" => array("logout", 128),
);
}
}

View File

@@ -23,9 +23,15 @@
namespace Thelia\Controller\Front;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\Validator\Exception\ValidatorException;
use Thelia\Core\Event\CustomerCreateOrUpdateEvent;
use Thelia\Core\Event\CustomerLoginEvent;
use Thelia\Core\Security\Authentication\CustomerUsernamePasswordFormAuthenticator;
use Thelia\Core\Security\Exception\AuthenticationException;
use Thelia\Core\Security\Exception\UsernameNotFoundException;
use Thelia\Core\Security\SecurityContext;
use Thelia\Form\CustomerCreation;
use Thelia\Form\CustomerLogin;
use Thelia\Form\CustomerModification;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Model\Customer;
@@ -79,6 +85,8 @@ class CustomerController extends BaseFrontController
$this->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_UPDATEACCOUNT, $customerChangeEvent);
$this->processLogin($customerChangeEvent->getCustomer());
$this->redirectSuccess();
} catch (FormValidationException $e) {
@@ -90,20 +98,45 @@ class CustomerController extends BaseFrontController
}
}
/**
* Perform user login. On a successful login, the user is redirected to the URL
* found in the success_url form parameter, or / if none was found.
*
* If login is not successfull, the same view is dispolyed again.
*
*/
public function loginAction()
{
$event = $this->dispatchEvent("loginCustomer");
$request = $this->getRequest();
$customerEvent = new CustomerEvent($event->getCustomer());
$customerLoginForm = new CustomerLogin($request);
$this->processLogin($event->getCustomer(), $customerEvent, true);
$authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm);
try {
$customer = $authenticator->getAuthentifiedUser();
$customerLoginEvent = new CustomerLoginEvent($customer);
$this->processLogin($customer, $customerLoginEvent);
$this->redirectSuccess();
} catch (ValidatorException $e) {
} catch(UsernameNotFoundException $e) {
} catch(AuthenticationException $e) {
} catch (\Exception $e) {
}
}
public function processLogin(Customer $customer,$event = null, $sendLogin = false)
public function processLogin(Customer $customer,$event = null)
{
$this->getSecurityContext(SecurityContext::CONTEXT_FRONT_OFFICE)->setUser($customer);
if($sendLogin) $this->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event);
if($event) $this->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event);
}
/**

View File

@@ -0,0 +1,42 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event;
use Thelia\Model\Customer;
class CustomerLoginEvent {
protected $customer;
public function __construct(Customer $customer)
{
$this->customer = $customer;
}
public function getCustomer()
{
return $this->customer;
}
}