Hi, I am working on how to anthenticate and authorize for my member module in ZF3 project
I worked with zf2 for a while, and remember that in ZF2, we used to do with the following:
in the module.php for the module. we wrote an onBootstrap function like:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('dispatch', array($this, 'setLayout'),2);
$eventManager->attach('route', array($this, 'doAuthorization'),3);
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
public function doAuthorization(MvcEvent $e)
{
$application = $e->getApplication();
$sm = $application->getServiceManager();
$sharedManager = $application->getEventManager()->getSharedManager();
$router = $sm->get('router');
$request = $sm->get('request');
$matchedRoute = $router->match($request);
if (!preg_match('%^/'.__NAMESPACE__.'/.*%i', $request->getUri()->getPath())) return true;
if (null !== $matchedRoute) {
$sharedManager->attach('Zend\Mvc\Controller\AbstractActionController','dispatch',
function($e) use ($sm) {
$sm->get('ControllerPluginManager')->get('Appauth')->doAuthorization($e);
},4
);
}
}
then we wrote the main authorize logic in the controller plugin like:
<?php
namespace Members\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
class Appauth extends AbstractPlugin
{
public function doAuthorization($e) //just consider the module members
{
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
$action = $matches->getParam('action');
$membersTable = $this->getController()->getServiceLocator()->get('Members\Model\MembersTable');
$userInfo = $membersTable->userInfo();
$rawUri = strtolower($e->getRequest()->getUri()->getPath()); // /members/index/registration /members/index /members
$uri = substr($rawUri, 0, strpos($rawUri, $action)).$action;
$response = $e->getResponse();
$router = $e->getRouter();
if ($controller == 'Members\Controller\Index' && $action == 'login') //if it is login page
{
if ($userInfo)
{
$url = $router->assemble(array('controller'=>'index','action'=>'index'),array('name'=>'members'));
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$e->stopPropagation(true);
return $response;
}
else
{
return true;
}
}
if ($controller == 'Members\Controller\Index' && $action == 'logout')
{
return true;
}
if ($controller == 'Members\Controller\Index' && $action == 'register')
{
if ($userInfo)
{
$url = $router->assemble(array('controller'=>'index','action'=>'index'),array('name'=>'members'));
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$e->stopPropagation(true);
return $response;
}
else
{
return true;
}
}
if (!$userInfo) //others not logined will redirect to login page
{
$url = $router->assemble(array('controller'=>'index','action'=>'login'),array('name'=>'members'));
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$e->stopPropagation(true);
return $response;
}
else
{
if ($controller == 'Members\Controller\Index' && $action == 'index') return true;
if ($userInfo['rid'] == 1) return true;
$resourcesArr = $this->getController()->getServiceLocator()->get('Members\Model\ResourcesTable')->formatResources('iu');
$sid = array_search($uri,$resourcesArr);
if (!$sid) //which is not included in resourcelist
{
$url = $router->assemble(array('controller'=>'index','action'=>'index'),array('name'=>'members'));
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$e->stopPropagation(true);
return $response;
}
else
{
if ($this->getController()->getServiceLocator()->get('Members\Model\AclTable')->checkAcl($userInfo['rid'],$sid))
{
return true;
}
else
{
$url = $router->assemble(array('controller'=>'index','action'=>'index'),array('name'=>'members'));
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$e->stopPropagation(true);
return $response;
}
}
}
}
}
and config the plugin in the moudule.config.php like:
'controller_plugins' => array(
'invokables' => array(
'Appauth' => 'Members\Controller\Plugin\Appauth',
),
),
I wonder how it looks like in ZF3, when I wrote this new topic, it says my topic is similar to https://discourse.zendframework.com/t/the-right-mvc-event-to-tie-into-for-authorization/614, I am not well fit this way. I think it looks like yii framework, besides, It is a little complicate to me. I am seeking a way which is similar to the code I pasted above. Is there any ZF3-patterned one like the code above?
I prefer to write the main logic in one file with acl.
Thank you for your time.