Example #1
0
 /**
  * @param Route $route
  * @param UserVO $user
  * @throws MethodNotAllowedException
  */
 protected function checkForRole(Route $route, UserVO $user)
 {
     if ($route->hasDefault('_role')) {
         $role = $route->getDefault('_role');
         if (!in_array($role, $user->roles)) {
             throw new MethodNotAllowedException([], sprintf('Need role %s', $role));
         }
     }
 }
 private function convertController(Route $route)
 {
     $nameParser = $this->getContainer()->get('controller_name_converter');
     if ($route->hasDefault('_controller')) {
         try {
             $route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));
         } catch (\InvalidArgumentException $e) {
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function generateI18nPatterns($routeName, Route $route)
 {
     $patterns = array();
     foreach ($route->getOption('i18n_locales') ?: $this->locales as $locale) {
         // Check if translation exists in the translation catalogue to avoid errors being logged by
         // the new LoggingTranslator of Symfony 2.6. However, the LoggingTranslator did not implement
         // the interface until Symfony 2.6.5, so an extra check is needed.
         if ($this->translator instanceof TranslatorBagInterface || $this->translator instanceof LoggingTranslator) {
             // Check if route is translated.
             if (!$this->translator->getCatalogue($locale)->has($routeName, $this->translationDomain)) {
                 // No translation found.
                 $i18nPattern = $route->getPath();
             } else {
                 // Get translation.
                 $i18nPattern = $this->translator->trans($routeName, array(), $this->translationDomain, $locale);
             }
         } else {
             // if no translation exists, we use the current pattern
             if ($routeName === ($i18nPattern = $this->translator->trans($routeName, array(), $this->translationDomain, $locale))) {
                 $i18nPattern = $route->getPath();
             }
         }
         ///////////////////////////////////////
         // Begin customizations
         // prefix with zikula module url if requested
         if ($route->hasDefault('_zkModule')) {
             $module = $route->getDefault('_zkModule');
             $zkNoBundlePrefix = $route->getOption('zkNoBundlePrefix');
             if (!isset($zkNoBundlePrefix) || !$zkNoBundlePrefix) {
                 $untranslatedPrefix = $this->getModUrlString($module);
                 if ($this->translator->getCatalogue($locale)->has($untranslatedPrefix, strtolower($module))) {
                     $prefix = $this->translator->trans($untranslatedPrefix, [], strtolower($module), $locale);
                 } else {
                     $prefix = $untranslatedPrefix;
                 }
                 $i18nPattern = "/" . $prefix . $i18nPattern;
             }
         }
         // End customizations
         ///////////////////////////////////////
         // prefix with locale if requested
         if (self::STRATEGY_PREFIX === $this->strategy || self::STRATEGY_PREFIX_EXCEPT_DEFAULT === $this->strategy && $this->defaultLocale !== $locale) {
             $i18nPattern = '/' . $locale . $i18nPattern;
             if (null !== $route->getOption('i18n_prefix')) {
                 $i18nPattern = $route->getOption('i18n_prefix') . $i18nPattern;
             }
         }
         $patterns[$i18nPattern][] = $locale;
     }
     return $patterns;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function processRequest(Request $request, Route $route)
 {
     $givenToken = $request->headers->get(self::HEADER);
     $session = $request->getSession();
     $expectedToken = $session->get(self::CSRF);
     if ($request->isMethod('GET') && !$route->hasOption(self::CSRF) || $route->hasDefault('_guest')) {
         if (empty($expectedToken)) {
             $this->renewCsrfToken();
         }
         return;
     }
     if (empty($givenToken) || $givenToken !== $expectedToken) {
         throw new MethodNotAllowedException(['POST'], 'invalid CSRF token');
     }
     $this->generateNewTokenWhenNeeded($session);
 }
    /**
     * Compiles the current route instance.
     *
     * @param Route $route A Route instance
     *
     * @return CompiledRoute A CompiledRoute instance
     */
    public function compile(Route $route)
    {
        $pattern = $route->getPattern();
        $len = strlen($pattern);
        $tokens = array();
        $variables = array();
        $pos = 0;
        preg_match_all('#.\{([\w\d_]+)\}#', $pattern, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
        foreach ($matches as $match) {
            if ($text = substr($pattern, $pos, $match[0][1] - $pos)) {
                $tokens[] = array('text', $text);
            }
            $seps = array($pattern[$pos]);
            $pos = $match[0][1] + strlen($match[0][0]);
            $var = $match[1][0];

            if ($req = $route->getRequirement($var)) {
                $regexp = $req;
            } else {
                if ($pos !== $len) {
                    $seps[] = $pattern[$pos];
                }
                $regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), '#'));
            }

            $tokens[] = array('variable', $match[0][0][0], $regexp, $var);
            $variables[] = $var;
        }

        if ($pos < $len) {
            $tokens[] = array('text', substr($pattern, $pos));
        }

        // find the first optional token
        $firstOptional = INF;
        for ($i = count($tokens) - 1; $i >= 0; $i--) {
            if ('variable' === $tokens[$i][0] && $route->hasDefault($tokens[$i][3])) {
                $firstOptional = $i;
            } else {
                break;
            }
        }

        // compute the matching regexp
        $regex = '';
        $indent = 1;
        if (1 === count($tokens) && 0 === $firstOptional) {
            $token = $tokens[0];
            ++$indent;
            $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?:\n", preg_quote($token[1], '#'));
            $regex .= str_repeat(' ', $indent * 4).sprintf("(?P<%s>%s)\n", $token[3], $token[2]);
        } else {
            foreach ($tokens as $i => $token) {
                if ('text' === $token[0]) {
                    $regex .= str_repeat(' ', $indent * 4).preg_quote($token[1], '#')."\n";
                } else {
                    if ($i >= $firstOptional) {
                        $regex .= str_repeat(' ', $indent * 4)."(?:\n";
                        ++$indent;
                    }
                    $regex .= str_repeat(' ', $indent * 4).sprintf("%s(?P<%s>%s)\n", preg_quote($token[1], '#'), $token[3], $token[2]);
                }
            }
        }
        while (--$indent) {
            $regex .= str_repeat(' ', $indent * 4).")?\n";
        }

        return new CompiledRoute(
            'text' === $tokens[0][0] ? $tokens[0][1] : '',
            sprintf("#^\n%s#x", $regex),
            array_reverse($tokens),
            $variables
        );
    }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function applies(Route $route)
 {
     return !$route->hasDefault('_controller') && ($route->hasDefault('_entity_form') || $route->hasDefault('_entity_list') || $route->hasDefault('_entity_view'));
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function applies(Route $route)
 {
     return $route->hasDefault('_form') && !$route->hasDefault('_controller');
 }
 /**
  * {@inheritdoc}
  */
 public function applies(Route $route)
 {
     return $route->hasDefault('view_id');
 }
 /**
  * {@inheritdoc}
  */
 public function applies(Route $route)
 {
     return !$route->hasDefault('_controller') && ($route->hasDefault('_wizard') || $route->hasDefault('_entity_wizard'));
 }
 /**
  * {@inheritdoc}
  */
 public function applies(Route $route)
 {
     return $route->hasDefault('_entity_wizard') && strpos($route->getDefault('_entity_wizard'), 'entity_browser.') === 0;
 }
 /**
  * Returns a more readable controller/action description for a Route.
  *
  * @param Route $route The selected Route
  *
  * @return string The couple Controller::Action if available
  */
 private function convertController(Route $route)
 {
     if ($route->hasDefault('_controller') && $route->hasDefault('_action')) {
         return $route->getDefault('_controller') . '::' . $route->getDefault('_action');
     }
     throw new \InvalidArgumentException(sprintf('The route with path "%s" does not have defaults _controller or _action.', $route->getPath()));
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public function applies($definition, $name, Route $route)
 {
     if (!empty($definition['type']) && ($definition['type'] == 'tempstore' || strpos($definition['type'], 'tempstore:') === 0)) {
         if (!empty($definition['tempstore_id']) || $route->hasDefault('tempstore_id')) {
             return TRUE;
         }
     }
     return FALSE;
 }