<commit_before><?php
/**
 * This file is part of Vegas package
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Amsterdam Standard Sp. Z o.o.
 * @homepage http://vegas-cmf.github.io
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Vegas\Security\Authentication\EventsManager;

use Phalcon\Events\Event,
    Phalcon\Mvc\User\Plugin as UserPlugin,
    Phalcon\Mvc\Dispatcher,
    Phalcon\Acl;
use Vegas\Security\Authentication;

/**
 * Class Plugin
 * @package Vegas\Security\Authentication\EventsManager
 */
class Plugin extends UserPlugin
{
    protected $authSessionKeys;

    /**
     * @param Event $event
     * @param Dispatcher $dispatcher
     * @throws Exception\RouteNotFoundException
     * @return bool
     */
    public function beforeDispatch(Event $event, Dispatcher $dispatcher)
    {
        $this->authSessionKeys = $this->getAuthenticationScopes();

        //checks if authentication was disabled
        if ($this->isAuthenticationDisabled()) {
            return true;
        }

        return $this->authenticate();
    }

    /**
     * @return bool
     */
    protected function authenticate()
    {
        //find the default route for current authentication scope
        $authRoute = $this->getAuthDefaultRoute();

        foreach ($this->authSessionKeys As $authSessionKey) {
            if ($this->getDI()->has($authSessionKey) && $this->getDI()->get($authSessionKey)->isAuthenticated()) {
                return true;
            }
        }

        return $this->setNotAuthenticated($authRoute);
    }

    /**
     * Return array of all used authentication scopes for current route.
     *
     * @return array
     */
    protected function getAuthenticationScopes()
    {
        $matchedRoute = $this->router->getMatchedRoute();
        if (!$matchedRoute) {
            return array(false);
        }

        $paths = $matchedRoute->getPaths();

        if (empty($paths['auth'])) {
            return array(false);
        }

        $auth = json_decode($paths['auth']);
        if (is_array($auth)) {
            return $auth;
        }

        return array($paths['auth']);
    }

    /**
     * @return \Phalcon\Mvc\Router\RouteInterface
     */
    protected function getAuthDefaultRoute()
    {
        //finds root route
        $rootRoute = $this->router->getRouteByName('root');

        $config = $this->getDI()->get('config');
        if (!isset($config->auth)) {
            return $rootRoute;
        }
<commit_msg>Security plugin fix: running authentication check beforeExecuteRoute to prevent predispatch loop.<commit_after><?php
/**
 * This file is part of Vegas package
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Amsterdam Standard Sp. Z o.o.
 * @homepage http://vegas-cmf.github.io
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Vegas\Security\Authentication\EventsManager;

use Phalcon\Events\Event,
    Phalcon\Mvc\User\Plugin as UserPlugin,
    Phalcon\Mvc\Dispatcher,
    Phalcon\Acl;
use Vegas\Security\Authentication;

/**
 * Class Plugin
 * @package Vegas\Security\Authentication\EventsManager
 */
class Plugin extends UserPlugin
{
    protected $authSessionKeys;

    /**
     * @param Event $event
     * @param Dispatcher $dispatcher
     * @throws Exception\RouteNotFoundException
     * @return bool
     */
    public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
    {
        $this->authSessionKeys = $this->getAuthenticationScopes();

        //checks if authentication was disabled
        if ($this->isAuthenticationDisabled()) {
            return true;
        }

        return $this->authenticate();
    }

    /**
     * @return bool
     */
    protected function authenticate()
    {
        //find the default route for current authentication scope
        $authRoute = $this->getAuthDefaultRoute();

        foreach ($this->authSessionKeys As $authSessionKey) {
            if ($this->getDI()->has($authSessionKey) && $this->getDI()->get($authSessionKey)->isAuthenticated()) {
                return true;
            }
        }

        return $this->setNotAuthenticated($authRoute);
    }

    /**
     * Return array of all used authentication scopes for current route.
     *
     * @return array
     */
    protected function getAuthenticationScopes()
    {
        $matchedRoute = $this->router->getMatchedRoute();
        if (!$matchedRoute) {
            // notfound route
            return array(false);
        }

        $paths = $matchedRoute->getPaths();
        if (empty($paths['auth'])) {
            // no auth required
            return array(false);
        }

        $auth = json_decode($paths['auth']);
        if (is_array($auth)) {
            return $auth;
        }

        return array($paths['auth']);
    }

    /**
     * @return \Phalcon\Mvc\Router\RouteInterface
     */
    protected function getAuthDefaultRoute()
    {
        //finds root route
        $rootRoute = $this->router->getRouteByName('root');

        $config = $this->getDI()->get('config');
        if (!isset($config->auth)) {
            return $rootRoute;
        }

{'fraction_non_alphanumeric': 0.12445456270157465, 'fraction_numerical': 0.0, 'mean_word_length': 2.3536895674300253, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6877967', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1406, 'n_words': 345}