refactor action process to controller process

This commit is contained in:
Manuel Raynaud
2013-08-12 16:42:23 +02:00
parent 7c6763ea59
commit b377ebe6e8
8 changed files with 60 additions and 134 deletions

View File

@@ -38,14 +38,6 @@ use Thelia\Core\Security\Exception\AuthorizationException;
class BaseAction class BaseAction
{ {
/**
* @var The container
*/
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
/** /**
* Validate a BaseForm * Validate a BaseForm
@@ -96,78 +88,4 @@ class BaseAction
$event->stopPropagation(); $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;
}
} }

View File

@@ -73,8 +73,8 @@ class Customer extends BaseAction implements EventSubscriberInterface
$request->getSession()->getLang() $request->getSession()->getLang()
); );
// Connect the newly created user,and redirect to the success URL $event->customer = $customer;
$this->processSuccessfullLogin($event, $customer, $customerCreationForm, true);
} catch (PropelException $e) { } catch (PropelException $e) {
Tlog::getInstance()->error(sprintf('error during creating customer on action/createCustomer with message "%s"', $e->getMessage())); 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 { try {
$user = $authenticator->getAuthentifiedUser(); $user = $authenticator->getAuthentifiedUser();
$this->processSuccessfullLogin($event, $user, $customerLoginForm); $event->customer = $customer;
} catch (ValidatorException $ex) { } catch (ValidatorException $ex) {
$message = "Missing or invalid information. Please check your input."; $message = "Missing or invalid information. Please check your input.";
} catch (UsernameNotFoundException $ex) { } catch (UsernameNotFoundException $ex) {
@@ -222,47 +223,4 @@ class Customer extends BaseAction implements EventSubscriberInterface
"action.logoutCustomer" => array("logout", 128), "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);
}
} }

View File

@@ -13,17 +13,14 @@
<services> <services>
<service id="thelia.action.cart" class="Thelia\Action\Cart"> <service id="thelia.action.cart" class="Thelia\Action\Cart">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/> <tag name="kernel.event_subscriber"/>
</service> </service>
<service id="thelia.action.customer" class="Thelia\Action\Customer"> <service id="thelia.action.customer" class="Thelia\Action\Customer">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/> <tag name="kernel.event_subscriber"/>
</service> </service>
<service id="thelia.action.category" class="Thelia\Action\Category"> <service id="thelia.action.category" class="Thelia\Action\Category">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/> <tag name="kernel.event_subscriber"/>
</service> </service>

View File

@@ -4,8 +4,14 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="customer.create" path="customer/create"> <route id="customer.create.process" path="/customer/create" methods="post">
<default key="_controller">Thelia\Controller\Front\CustomerController::createAction</default> <default key="_controller">Thelia\Controller\Front\CustomerController::createAction</default>
<default key="_view">connexion</default>
</route>
<route id="customer.create" path="/customer/create" methods="get">
<default key="_controller">Thelia\Controller\Front\CustomerController::displayCreateAction</default>
<default key="_view">connexion</default>
</route> </route>
</routes> </routes>

View File

@@ -24,13 +24,42 @@ namespace Thelia\Controller\Front;
use Thelia\Controller\BaseController; use Thelia\Controller\BaseController;
use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerAware;
use Thelia\Core\Event\CustomerEvent;
use Thelia\Core\Security\SecurityContext; use Thelia\Core\Security\SecurityContext;
use Thelia\Model\Customer;
use Thelia\Core\Event\TheliaEvents;
class CustomerController extends BaseController { class CustomerController extends BaseController {
public function createAction() 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);
} }
} }

View File

@@ -51,6 +51,8 @@ abstract class ActionEvent extends Event
protected $errorForm = null; protected $errorForm = null;
protected $parameters = array();
/** /**
* *
* @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\HttpFoundation\Request $request
@@ -62,6 +64,21 @@ abstract class ActionEvent extends Event
$this->action = $action; $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 * @return string

View File

@@ -65,6 +65,8 @@ final class TheliaEvents
*/ */
const ADMIN_LOGIN = "action.admin_login"; const ADMIN_LOGIN = "action.admin_login";
/** /**
* Sent once the customer creation form has been successfully validated, and before customer insertion in the database. * Sent once the customer creation form has been successfully validated, and before customer insertion in the database.
*/ */

View File

@@ -2,13 +2,12 @@
{form name="thelia.customer.creation"} {form name="thelia.customer.creation"}
{* We use $INDEX_PAGE as form action to avoid mixing post and get data *} {* We use $INDEX_PAGE as form action to avoid mixing post and get data *}
<form action="{$INDEX_PAGE}" method="post" {form_enctype form=$form}> <form action="{url path="/customer/create" }" method="post" {form_enctype form=$form}>
{* {*
The two fields below are not par of the form, they are here to defines 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 to process, and the view to render once the form is submited
*} *}
<input type="hidden" name="action" value="createCustomer" /> {* the action triggered by this form *} <input type="hidden" name="action" value="createCustomer" /> {* the action triggered by this form *}
<input type="hidden" name="view" value="connexion" /> {* the view to return to if the form cannot be validated *}
{* {*
This field is common to all BaseForm instances (thus, this one), and defines This field is common to all BaseForm instances (thus, this one), and defines