diff --git a/core/lib/Thelia/Admin/Controller/BaseAdminController.php b/core/lib/Thelia/Admin/Controller/BaseAdminController.php index 4c0120ad4..5a4020561 100755 --- a/core/lib/Thelia/Admin/Controller/BaseAdminController.php +++ b/core/lib/Thelia/Admin/Controller/BaseAdminController.php @@ -28,6 +28,10 @@ use Symfony\Component\HttpFoundation\Response; use Thelia\Form\BaseForm; use Thelia\Model\ConfigQuery; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Thelia\Core\Security\Exception\AuthenticationTokenNotFoundException; /** * @@ -41,25 +45,48 @@ use Thelia\Model\ConfigQuery; class BaseAdminController extends ContainerAware { + const TEMPLATE_404 = "404.html"; + + public function notFoundAction() + { + return new Response($this->renderRaw(self::TEMPLATE_404), 404); + } + /** - * @param $templateName - * @param array $args + * Render the givent template, and returns the result as an Http Response. + * + * @param $templateName the complete template name, with extension + * @param array $args the template arguments * @return \Symfony\Component\HttpFoundation\Response */ public function render($templateName, $args = array()) { - $args = array_merge($args, array('lang' => 'fr')); - $response = new Response(); return $response->setContent($this->renderRaw($templateName, $args)); } + /** + * Render the givent template, and returns the result as a string. + * + * @param $templateName the complete template name, with extension + * @param array $args the template arguments + * @return \Symfony\Component\HttpFoundation\Response + */ public function renderRaw($templateName, $args = array()) { - $args = array_merge($args, array('lang' => 'fr')); + $args = array_merge($args, array('lang' => 'fr')); // FIXME - return $this->getParser()->render($templateName, $args); + try { + $data = $this->getParser()->render($templateName, $args); + } + catch (AuthenticationTokenNotFoundException $ex) { + + // No auth token -> perform login + return new RedirectResponse($this->generateUrl('/admin/login')); + } + + return $data; } /** @@ -70,6 +97,10 @@ class BaseAdminController extends ContainerAware return $this->container->get('request'); } + /** + * + * @return a ParserInterface instance parser, as configured. + */ public function getParser() { $parser = $this->container->get("thelia.parser"); @@ -80,13 +111,61 @@ class BaseAdminController extends ContainerAware return $parser; } - public function getFormFactory() + protected function getFormFactory() { return BaseForm::getFormFactory($this->getRequest(), ConfigQuery::read("form.secret.admin", md5(__DIR__))); } - public function getFormBuilder() + protected function getFormBuilder() { return $this->getFormFactory()->createBuilder("form"); } -} + + /** + * Generates a URL from the given parameters. + * + * @param string $route The name of the route + * @param mixed $parameters An array of parameters + * @param Boolean|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface) + * + * @return string The generated URL + * + * @see UrlGeneratorInterface + */ + public function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) + { + return "$route"; + + //return $this->container->get('router')->generate($route, $parameters, $referenceType); + } + + /** + * Forwards the request to another controller. + * + * @param string $controller The controller name (a string like BlogBundle:Post:index) + * @param array $path An array of path parameters + * @param array $query An array of query parameters + * + * @return Response A Response instance + */ + public function forward($controller, array $path = array(), array $query = array()) + { + $path['_controller'] = $controller; + $subRequest = $this->container->get('request')->duplicate($query, null, $path); + + return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST); + } + + /** + * Returns a RedirectResponse to the given URL. + * + * @param string $url The URL to redirect to + * @param integer $status The status code to use for the Response + * + * @return RedirectResponse + */ + public function redirect($url, $status = 302) + { + return new RedirectResponse($url, $status); + } +} \ No newline at end of file