From b377ebe6e8d7a492ec557400862f274938c2396a Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 12 Aug 2013 16:42:23 +0200 Subject: [PATCH] refactor action process to controller process --- core/lib/Thelia/Action/BaseAction.php | 82 ------------------- core/lib/Thelia/Action/Customer.php | 50 +---------- core/lib/Thelia/Config/Resources/action.xml | 3 - .../Thelia/Config/Resources/routing/front.xml | 8 +- .../Controller/Front/CustomerController.php | 29 +++++++ core/lib/Thelia/Core/Event/ActionEvent.php | 17 ++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 2 + templates/default/connexion.html | 3 +- 8 files changed, 60 insertions(+), 134 deletions(-) diff --git a/core/lib/Thelia/Action/BaseAction.php b/core/lib/Thelia/Action/BaseAction.php index a66ed8301..c3a0ded9f 100644 --- a/core/lib/Thelia/Action/BaseAction.php +++ b/core/lib/Thelia/Action/BaseAction.php @@ -38,14 +38,6 @@ use Thelia\Core\Security\Exception\AuthorizationException; class BaseAction { - /** - * @var The container - */ - protected $container; - - public function __construct(ContainerInterface $container) { - $this->container = $container; - } /** * Validate a BaseForm @@ -96,78 +88,4 @@ class BaseAction $event->stopPropagation(); } - /** - * Check current user authorisations. - * - * @param mixed $roles a single role or an array of roles. - * @param mixed $permissions a single permission or an array of permissions. - * - * @throws AuthenticationException if permissions are not granted to the current user. - */ - protected function checkAuth($roles, $permissions, $context = false) { - - if (! $this->getSecurityContext($context)->isGranted( - is_array($roles) ? $roles : array($roles), - is_array($permissions) ? $permissions : array($permissions)) ) { - - Tlog::getInstance()->addAlert("Authorization roles:", $roles, " permissions:", $permissions, " refused."); - - throw new AuthorizationException("Sorry, you're not allowed to perform this action"); - } - } - - /** - * Return the event dispatcher, - * - * @return ParserContext - */ - protected function getDispatcher() - { - return $this->container->get('event_dispatcher'); - } - - /** - * Return the parser context, - * - * @return ParserContext - */ - protected function getParserContext() - { - return $this->container->get('thelia.parser.context'); - } - - /** - * Return the security context, by default in admin mode. - * - * @param string the context, either SecurityContext::CONTEXT_BACK_OFFICE or SecurityContext::CONTEXT_FRONT_OFFICE - * - * @return Thelia\Core\Security\SecurityContext - */ - protected function getSecurityContext($context = false) - { - $securityContext = $this->container->get('thelia.securityContext'); - - $securityContext->setContext($context === false ? SecurityContext::CONTEXT_BACK_OFFICE : $context); - - return $securityContext; - } - - /** - * - * return the environnement context contain in \Thelia\Core\Context class - * - * @return string - */ - protected function getContext() - { - return $this->container->get("thelia.envContext")->getContext(); - } - - protected function redirect($url, $status = 302) - { - $response = new RedirectResponse($url, $status); - - $response->send(); - exit; - } } \ No newline at end of file diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 8faf23a02..bc2503ac4 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -73,8 +73,8 @@ class Customer extends BaseAction implements EventSubscriberInterface $request->getSession()->getLang() ); - // Connect the newly created user,and redirect to the success URL - $this->processSuccessfullLogin($event, $customer, $customerCreationForm, true); + $event->customer = $customer; + } catch (PropelException $e) { Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage())); @@ -167,7 +167,8 @@ class Customer extends BaseAction implements EventSubscriberInterface try { $user = $authenticator->getAuthentifiedUser(); - $this->processSuccessfullLogin($event, $user, $customerLoginForm); + $event->customer = $customer; + } catch (ValidatorException $ex) { $message = "Missing or invalid information. Please check your input."; } catch (UsernameNotFoundException $ex) { @@ -222,47 +223,4 @@ class Customer extends BaseAction implements EventSubscriberInterface "action.logoutCustomer" => array("logout", 128), ); } - - /** - * - * Stores the current user in the security context, and redirect to the - * success_url. - * @param ActionEvent $event - * @param CustomerModel $user - * @param BaseForm $form - * @param bool $sendLoginEvent - */ - protected function processSuccessfullLogin(ActionEvent $event, CustomerModel $user, BaseForm $form, $sendLoginEvent = false) - { - - $successUrl = $form->getSuccessUrl(); - if ($this->getSecurityContext(SecurityContext::CONTEXT_BACK_OFFICE)->getUser() === null) { - $this->processSuccessfullFrontEndLogin($event, $user, $form, $sendLoginEvent); - } else { - $successUrl = str_replace("__ID__", $user->getId(), $successUrl); - } - - // Redirect to the success URL - $this->redirect($successUrl); - } - - protected function processSuccessfullFrontEndLogin(ActionEvent $event, CustomerModel $user, BaseForm $form, $sendLoginEvent = false) - { - - // Success -> store user in security context - $this->getFrontSecurityContext()->setUser($user); - - if ($sendLoginEvent) $event->getDispatcher()->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event); - - } - - /** - * Return the security context, beeing sure that we're in the CONTEXT_FRONT_OFFICE context - * - * @return SecurityContext the security context - */ - - protected function getFrontSecurityContext() { - return $this->getSecurityContext(SecurityContext::CONTEXT_FRONT_OFFICE); - } } diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index 9006f2362..dbdc51a61 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -13,17 +13,14 @@ - - - diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index af56e6c85..ff050f35d 100644 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -4,8 +4,14 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - + Thelia\Controller\Front\CustomerController::createAction + connexion + + + + Thelia\Controller\Front\CustomerController::displayCreateAction + connexion \ No newline at end of file diff --git a/core/lib/Thelia/Controller/Front/CustomerController.php b/core/lib/Thelia/Controller/Front/CustomerController.php index 9ba42c3a9..e0fb2fc3d 100644 --- a/core/lib/Thelia/Controller/Front/CustomerController.php +++ b/core/lib/Thelia/Controller/Front/CustomerController.php @@ -24,13 +24,42 @@ namespace Thelia\Controller\Front; use Thelia\Controller\BaseController; use Symfony\Component\DependencyInjection\ContainerAware; +use Thelia\Core\Event\CustomerEvent; use Thelia\Core\Security\SecurityContext; +use Thelia\Model\Customer; +use Thelia\Core\Event\TheliaEvents; class CustomerController extends BaseController { public function createAction() { + $event = $this->dispatchEvent("createCustomer"); + if(null !== $customer = $event->customer) { + $this->processLogin($event->customer); + } + + } + + public function displayCreateAction() + { + + } + + public function loginAction() + { + $event = $this->dispatchEvent("loginCustomer"); + + $customerEvent = new CustomerEvent($event->getCustomer()); + + $this->processLogin($event->getCustomer(), $customerEvent, true); + } + + public function processLogin(Customer $customer,$event = null, $sendLogin = false) + { + $this->getSecurityContext(SecurityContext::CONTEXT_FRONT_OFFICE)->setUser($customer); + + if($sendLogin) $this->dispatch(TheliaEvents::CUSTOMER_LOGIN, $event); } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/ActionEvent.php b/core/lib/Thelia/Core/Event/ActionEvent.php index b56067082..2cfde3d79 100755 --- a/core/lib/Thelia/Core/Event/ActionEvent.php +++ b/core/lib/Thelia/Core/Event/ActionEvent.php @@ -51,6 +51,8 @@ abstract class ActionEvent extends Event protected $errorForm = null; + protected $parameters = array(); + /** * * @param \Symfony\Component\HttpFoundation\Request $request @@ -62,6 +64,21 @@ abstract class ActionEvent extends Event $this->action = $action; } + + public function __set($name, $value) + { + $this->parameters[$name] = $value; + } + + public function __get($name) + { + if (array_key_exists($name, $this->parameters)) { + return $this->parameters[$name]; + } + + return null; + } + /** * * @return string diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index c19ebe332..00df25a08 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -65,6 +65,8 @@ final class TheliaEvents */ const ADMIN_LOGIN = "action.admin_login"; + + /** * Sent once the customer creation form has been successfully validated, and before customer insertion in the database. */ diff --git a/templates/default/connexion.html b/templates/default/connexion.html index ac22c1bdb..e3109e8b3 100755 --- a/templates/default/connexion.html +++ b/templates/default/connexion.html @@ -2,13 +2,12 @@ {form name="thelia.customer.creation"} {* We use $INDEX_PAGE as form action to avoid mixing post and get data *} -
+ {* The two fields below are not par of the form, they are here to defines the action to process, and the view to render once the form is submited *} {* the action triggered by this form *} - {* the view to return to if the form cannot be validated *} {* This field is common to all BaseForm instances (thus, this one), and defines