bjyoungblood / BjyAuthorize

MIT License
276 stars 164 forks source link

Enabling BjyAuthorize turns all my 404s into 403s #133

Closed personman closed 11 years ago

personman commented 11 years ago

I'm getting the module to function and I'm very grateful for the work that has gone into making and sharing it. Well done. But I'd like to mention one issue. Once I got the module set up, any url that would have reached a 404 not found error page is now showing a 403 forbidden page. I could implement my own UnauthorizedStrategy, but I'd like to request that existing 404 handling remain in effect by default.

Here's my config, just in case this issue is the result of me doing something wrong:

return array(
    'bjyauthorize' => array(
        // default role for unauthenticated users
        'default_role'          => 'guest',

        // default role for authenticated users (if using the
        // 'BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider' identity provider)
        'authenticated_role'    => 'user',

        // identity provider service name
        'identity_provider' => 'BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider',

        // Role providers to be used to load all available roles into Zend\Permissions\Acl\Acl
        // Keys are the provider service names, values are the options to be passed to the provider
        'role_providers'        => array(
            // using an object repository (entity repository) to load all roles into our ACL
            'BjyAuthorize\Provider\Role\ObjectRepositoryProvider' => array(
                'object_manager'    => 'doctrine.entity_manager.orm_default',
                'role_entity_class' => 'Mrss\Entity\Role',
            ),
        ),

        // Resource providers to be used to load all available resources into Zend\Permissions\Acl\Acl
        // Keys are the provider service names, values are the options to be passed to the provider
        'resource_providers'    => array(),

        // Rule providers to be used to load all available rules into Zend\Permissions\Acl\Acl
        // Keys are the provider service names, values are the options to be passed to the provider
        'rule_providers'        => array(),

        // Guard listeners to be attached to the application event manager
        'guards'                => array(
            'BjyAuthorize\Guard\Controller' => array(
                // Guests can see the index and user controller
                array(
                    'controller' => 'index',
                    'roles' => array('guest')
                ),
                array(
                    'controller' => 'zfcuser',
                    'roles' => array('guest')
                ),
                // Only authenticated users can look at these:
                array(
                    'controller' => 'colleges',
                    'roles' => array('user')
                ),
                array(
                    'controller' => 'import',
                    'roles' => array('user')
                ),
                array(
                    'controller' => 'observations',
                    'roles' => array('user')
                )
            )
        ),

        // strategy service name for the strategy listener to be used when permission-related errors are detected
        'unauthorized_strategy' => 'BjyAuthorize\View\UnauthorizedStrategy',

        // Template name for the unauthorized strategy
        'template'              => 'error/403',
    ),

    'service_manager' => array(
        'factories' => array(
            'BjyAuthorize\Config'                   => 'BjyAuthorize\Service\ConfigServiceFactory',
            'BjyAuthorize\Guards'                   => 'BjyAuthorize\Service\GuardsServiceFactory',
            'BjyAuthorize\RoleProviders'            => 'BjyAuthorize\Service\RoleProvidersServiceFactory',
            'BjyAuthorize\ResourceProviders'        => 'BjyAuthorize\Service\ResourceProvidersServiceFactory',
            'BjyAuthorize\RuleProviders'            => 'BjyAuthorize\Service\RuleProvidersServiceFactory',
            'BjyAuthorize\Guard\Controller'         => 'BjyAuthorize\Service\ControllerGuardServiceFactory',
            'BjyAuthorize\Guard\Route'              => 'BjyAuthorize\Service\RouteGuardServiceFactory',
            'BjyAuthorize\Provider\Role\Config'     => 'BjyAuthorize\Service\ConfigRoleProviderServiceFactory',
            'BjyAuthorize\Provider\Role\ZendDb'     => 'BjyAuthorize\Service\ZendDbRoleProviderServiceFactory',
            'BjyAuthorize\Provider\Resource\Config' => 'BjyAuthorize\Service\ConfigResourceProviderServiceFactory',
            'BjyAuthorize\Service\Authorize'        => 'BjyAuthorize\Service\AuthorizeFactory',
            'BjyAuthorize\Provider\Identity\ProviderInterface'
                => 'BjyAuthorize\Service\IdentityProviderServiceFactory',
            'BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider'
                => 'BjyAuthorize\Service\AuthenticationIdentityProviderServiceFactory',
            'BjyAuthorize\Provider\Role\ObjectRepositoryProvider'
                => 'BjyAuthorize\Service\ObjectRepositoryRoleProviderFactory',
            'BjyAuthorize\Collector\RoleCollector'  => 'BjyAuthorize\Service\RoleCollectorServiceFactory',
            'BjyAuthorize\Provider\Identity\ZfcUserZendDb'
                => 'BjyAuthorize\Service\ZfcUserZendDbIdentityProviderServiceFactory',
            'BjyAuthorize\View\UnauthorizedStrategy'
                => 'BjyAuthorize\Service\UnauthorizedStrategyServiceFactory',
        ),
        'invokables'  => array(
            'BjyAuthorize\View\RedirectionStrategy' => 'BjyAuthorize\View\RedirectionStrategy',
        ),
        'aliases'     => array(
            'bjyauthorize_zend_db_adapter' => 'Zend\Db\Adapter\Adapter',
        ),
        'initializers' => array(
            'BjyAuthorize\Service\AuthorizeAwareServiceInitializer'
                => 'BjyAuthorize\Service\AuthorizeAwareServiceInitializer'
        ),
    ),

    'view_manager' => array(
        'template_map' => array(
            'error/403' => __DIR__ . '/../../'
                . 'vendor/bjyoungblood/bjy-authorize/view/error/403.phtml',
            'zend-developer-tools/toolbar/bjy-authorize-role'
                => __DIR__ . '/../../vendor/bjyoungblood/bjy-authorize/view/'
                . 'zend-developer-tools/toolbar/bjy-authorize-role.phtml',
        ),
    ),

    'zenddevelopertools' => array(
        'profiler' => array(
            'collectors' => array(
                'bjy_authorize_role_collector' => 'BjyAuthorize\\Collector\\RoleCollector',
            ),
        ),
        'toolbar' => array(
            'entries' => array(
                'bjy_authorize_role_collector' => 'zend-developer-tools/toolbar/bjy-authorize-role',
            ),
        ),
    ),
);
bjyoungblood commented 11 years ago

I'll have to look further into this, but I think it is because the UnauthorizedStrategy should not run for routing errors (see here).

personman commented 11 years ago

I inspected the value of $event->getError() in the block of code you mentioned. It equals

'error-unauthorized-controller'

So, the issue is prior to that. I tinkered with the onDispatch() method in the guard controller, but I wasn't able to get this to behave like I want.

Ocramius commented 11 years ago

@personman error-unauthorized-controller happens after routing. Is your 404 related to routing or to controller mismatch?

personman commented 11 years ago

@Ocramius It was the latter. I have a segment route that can match any controller: '/[:controller[/:action[/:id]]]'. I think that's the source of my issue. So, for a non-existent url like "/notreal", routing is successful, then bjyauthorize does its thing, and only later does the app learn that there's no controller called "notreal." I'm thinking of ditching that general route anyway. Sorry if this was just a false alarm.

Ocramius commented 11 years ago

@personman I'm going to close this then: a correct match on a controller would have resulted in a 403 anyway if not configured first ;)