<commit_before><?php
namespace Permissions;

use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e){
        $app = $e->getApplication();
        $sm = $app->getServiceManager();
        
        $config = $sm->get('BjyAuthorize\Config');
        $strategy = $sm->get($config['unauthorized_strategy']);

        $em = $app->getEventManager();
        
        $em->attach( 'dispatch.error', function($e) use ($sm, $strategy){
            $pluginManager = $sm->get('ControllerPluginManager');
            
            //var_dump( $e->getRouteMatch() );
            //die();
            
            $strategy->setRedirectUri('/');
            
            $flashMessenger = $pluginManager->get('FlashMessenger');
            $flashMessenger->setNamespace('Error')->addMessage('You do not have access');
        }, -4999); //execute before dispatch.error redirect
                    
        $em->attach($strategy);
        
    }
    
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}<commit_msg>added redirect to login if user goes straight to admin controller<commit_after><?php
namespace Permissions;

use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http\RouteMatch;

class Module
{
    public function onBootstrap(MvcEvent $e){
        $app = $e->getApplication();
        $sm = $app->getServiceManager();
        
        $config = $sm->get('BjyAuthorize\Config');
        $strategy = $sm->get($config['unauthorized_strategy']);

        $em = $app->getEventManager();
        
        $em->attach( 'dispatch.error', function($e) use ($sm, $strategy){
            $pluginManager = $sm->get('ControllerPluginManager');
            
            //var_dump( $e->getRouteMatch() );
            //die();
            $controller = $e->getRouteMatch()->getParam('controller');
            switch( $controller ){
                case 'Admin\Controller\Index':
                case 'users':
                    $strategy->setRedirectUri('/login');
                    break;
                default: 
                    $strategy->setRedirectUri('/');
            }
            
            $flashMessenger = $pluginManager->get('FlashMessenger');
            $flashMessenger->setNamespace('Error')->addMessage('You do not have access');
        }, -4999); //execute before dispatch.error redirect
                    
        $em->attach($strategy);
        
    }
    
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}
{'fraction_non_alphanumeric': 0.14012128949888286, 'fraction_numerical': 0.0025534631343759975, 'mean_word_length': 1.7515364354697103, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, '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': '7531580', 'n_tokens_mistral': 867, 'n_tokens_neox': 844, 'n_words': 155}