namespace Application;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\ModuleRouteListener;
use Application\Authorization\AuthorizationListener;
use ZF\MvcAuth\MvcAuthEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach(
MvcAuthEvent::EVENT_AUTHORIZATION,
new AuthorizationListener(),
100 // Less than 1000 to allow roles to be added first && >= 100
);
}
}
namespace Application\Authorization;
use ZF\MvcAuth\MvcAuthEvent;
use Db\Fixture\RoleFixture;
class AuthorizationListener
{
public function __invoke(MvcAuthEvent $mvcAuthEvent)
{
$authorization = $mvcAuthEvent->getAuthorizationService();
// Deny from all
$authorization->deny();
// Allow from all for oauth authentication
$authorization->addResource('ZF\OAuth2\Controller\Auth::token');
$authorization->allow(null, 'ZF\OAuth2\Controller\Auth::token');
// Add application specific resources
$authorization->addResource('FooBar\V1\Rest\Foo\Controller::collection');
$authorization->allow(RoleFixture::USER, 'FooBar\V1\Rest\Foo\Controller::collection', 'GET');
}
}
use ZF\OAuth2\Doctrine\Permissions\Acl\Event;
use Zend\EventManager\Event as ZendEvent;
// Allow membership as a role
$events = $serviceManager->get('SharedEventManager');
$events->attach(
Event::class,
Event::IS_AUTHORIZED,
function(ZendEvent $event)
{
if (! $event->getParam('identity') instanceof AuthenticatedIdentity) {
return;
}
$membership = $event->getParam('identity')->getUser()->getMembership();
if ($event->getTarget()->isAllowed($membership->getName(), $event->getParam('resource'), $event->getParam('privilege'))) {
$event->stopPropagation();
return true;
}
},
100
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.