refactor action process to controller process
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user