ZF-Commons / zfc-rbac

Role-based access control module to provide additional features on top of Zend\Permissions\Rbac
BSD 3-Clause "New" or "Revised" License
181 stars 111 forks source link

RedirectStrategy + zf3 #362

Closed Nirzol closed 7 years ago

Nirzol commented 7 years ago

Hello I try the new zf version ZF3 . I apologize is my issue is stupid... But I have an error when enable redirect strategy. Follow this link : https://github.com/ZF-Commons/zfc-rbac/blob/master/docs/05.%20Strategies.md#redirectstrategy

But get this error : Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Unable to resolve service "\ZfcRbac\View\Strategy\RedirectStrategy" to a factory; are you certain you provided it during configuration?' in /Users/egrondin/workspace/ZendSkeletonDescartes/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 681

Can someone help me ? Thx

danizord commented 7 years ago

@Nirzol I guess this is caused by the leading slash in your service name \ZfcRbac\View\Strategy\RedirectStrategy :thinking:

Nirzol commented 7 years ago

$target = $event->getTarget();

    //This strategy is used to redirect the user to another route when a user is unauthorized.
    $target->getEventManager()->attach(
        $target->getServiceManager()->get('\ZfcRbac\View\Strategy\RedirectStrategy')
    );

better ? Or I miss understanding ?

$target = $event->getTarget();

    //This strategy is used to redirect the user to another route when a user is unauthorized.
    $target->getEventManager()->attach(
        $target->getServiceManager()->get(RedirectStrategy::class)
    );

But get : Catchable fatal error: Argument 2 passed to Zend\EventManager\EventManager::attach() must be callable, none given, called in .....Module.php on line 45 and defined in ...vendor/zendframework/zend-eventmanager/src/EventManager.php on line 185

danizord commented 7 years ago

Catchable fatal error: Argument 2 passed to Zend\EventManager\EventManager::attach() must be callable, none given, called in .....Module.php on line 45 and defined in ...vendor/zendframework/zend-eventmanager/src/EventManager.php on line 185

Yeah, now this error is because of the new way to attach listeners in EventManager v3, you should use $listener->attach($eventManager); instead of $eventManager->attach($listener);.

btw I'm merging https://github.com/ZF-Commons/zfc-rbac/pull/361 which updates the docs about this :)

Nirzol commented 7 years ago

Ok Thanks ! But I think you miss something . $em not declared . Better like this ?

    $app = $event->getApplication();
    $sm = $app->getServiceManager();
    $em = $app->getEventManager();

    $listener = $sm->get(\ZfcRbac\View\Strategy\RedirectStrategy::class);
    $listener->attach($em);
danizord commented 7 years ago

Good catch! Can you please send a PR to fix this?

Nirzol commented 7 years ago

Done

danizord commented 7 years ago

@Nirzol where? I don't see any PR :)

Nirzol commented 7 years ago

Sorry , I did it to fast.