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
{
/**
* @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;
}
}

View File

@@ -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);
}
}

View File

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

View File

@@ -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">
<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="_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>
</routes>

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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.
*/