bjyoungblood / BjyAuthorize

MIT License
276 stars 161 forks source link

Redirect on 403 #24

Closed gmuehl closed 12 years ago

gmuehl commented 12 years ago

Is it possible to redirect to a given Route (zfcuser/login) instead of Showing a 403? Usually this is a better way for the Usability of a Page.

bjyoungblood commented 12 years ago

Personally, I override view/error/403.phtml in my application code, I display a short error message and then do this: <?php $this->headMeta('5;URL=/login', 'refresh', 'http-equiv'); ?> (provided you are already using the HeadMeta helper in your layout script).

This method allows you to do some custom logic before redirecting, since you may not want to redirect authenticated and unauthenticated users to the same route.

gmuehl commented 12 years ago

Works fine, though, i personally prefer a Header-Redirect ;)

bjyoungblood commented 12 years ago

I added some code to make the view strategy configurable, which will allow you to modify the response. You can extend BjyAuthorize\View\UnauthorizedStrategy and modify the behavior of the onDispatchError method to suit your needs (i.e. add a header redirect).

Just make sure the class is set up by the service manager and use the following configuration option:

<?php

return array(
    'bjyauthorize' => array(
        'unauthorized_strategy' => 'YourModule\View\UnauthorizedStrategy',
    ),
);
surjit commented 12 years ago

can you pleas help me , how to create setup class service manager.

i'm getting this message

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for ZfcUser\View\UnauthorizedStrategy' in C:\xampp\htdocs\caleb\zend\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:424 Stack trace: #0 C:\xampp\htdocs\caleb\zend\module\BjyAuthorize\Module.php(23): Zend\ServiceManager\ServiceManager->get('ZfcUser\View\Un...') #1 [internal function]: BjyAuthorize\Module->onBootstrap(Object(Zend\Mvc\MvcEvent)) #2 C:\xampp\htdocs\caleb\zend\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) #3 C:\xampp\htdocs\caleb\zend\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(208): Zend\EventManager\EventManager->triggerListeners('bootstrap', Object(Zend\Mvc\MvcEvent), Array) #4 C:\xampp\htdocs\caleb\zend\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(1 in C:\xampp\htdocs\caleb\zend\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 424

vinigarcia87 commented 11 years ago

@surjit, you must include this:

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Admin\View\UnauthorizedStrategy' => function ($sm) {
                $template = $sm->get('BjyAuthorize\Service\Authorize')->getTemplate();
                $strategy = new View\UnauthorizedStrategy;
                $strategy->setTemplate($template);
                return $strategy;
            },
        ),
    );
}

In Module.php.

vinigarcia87 commented 11 years ago

remind to change 'Admin\View\UnauthorizedStrategy' by your correct name...

webdevilopers commented 11 years ago

A view strategy to redirect to login page on auth failure by Rob Allen: https://gist.github.com/akrabat/3783912

Ocramius commented 11 years ago

@webdevilopers I'd love to merge such a strategy into the repository if someone wrote tests for it :)

webdevilopers commented 11 years ago

I started using ZF2 and UnitTests recently and I guess I'm no help yet! :( Maybe Mr. Rob Allen @akrabat will help us out! :)

miqueiasrafael commented 8 years ago

someone can tell me how can I solve this problem please? An alias "zfcuser Zend_Db_Adapter" was requested but the service could be found.

miqueiasrafael commented 8 years ago

@bjyoungblood It would be great if you had a video BjyAuthorize because it would help a lot! So googled several people downloaded and tried to use the package but on the way encountered several problems when configuring or installing because even the deprecated