From 5749d6365588faa4179d3eb008b35a20836cf29b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 19 Dec 2012 15:34:47 +0100 Subject: [PATCH] CS fixes and cleanup of the service class --- Module.php | 128 +----------------- config/services.config.php | 12 +- src/BjyAuthorize/Acl/Role.php | 3 +- src/BjyAuthorize/Guard/Route.php | 2 +- .../Provider/Identity/ZfcUserZendDb.php | 3 +- src/BjyAuthorize/Provider/Role/ZendDb.php | 1 - src/BjyAuthorize/Service/Authorize.php | 121 +++++++++++++++-- src/BjyAuthorize/Service/AuthorizeFactory.php | 1 - .../View/UnauthorizedStrategy.php | 42 +++++- 9 files changed, 165 insertions(+), 148 deletions(-) diff --git a/Module.php b/Module.php index 6987158..3e0de53 100644 --- a/Module.php +++ b/Module.php @@ -1,123 +1,7 @@ getTarget(); - $config = $app->getConfig(); - $sm = $app->getServiceManager(); - $service = $sm->get('BjyAuthorize\Service\Authorize'); - $strategy = $sm->get($config['bjyauthorize']['unauthorized_strategy']); - - foreach ($service->getGuards() as $guard) { - $app->getEventManager()->attach($guard); - } - - $app->getEventManager()->attach($strategy); - } - - public function getServiceConfig() - { - return array( - 'initializers' => array( - function ($instance, $sm) { - if ($instance instanceof Service\AuthorizeAwareInterface) { - $instance->setAuthorizeService($sm->get('BjyAuthorize\Service\Authorize')); - } - } - ), - 'factories' => array( - 'BjyAuthorize\Service\Authorize' => 'BjyAuthorize\Service\AuthorizeFactory', - - 'BjyAuthorize\Provider\Identity\ZfcUserZendDb' => function ($sm) { - $adapter = $sm->get('zfcuser_zend_db_adapter'); - $provider = new Provider\Identity\ZfcUserZendDb($adapter); - $provider->setUserService($sm->get('zfcuser_user_service')); - return $provider; - }, - - 'BjyAuthorize\Provider\Identity\ZfcUserDoctrine' => function ($sm) { - $em = $sm->get('doctrine.entitymanager.orm_default'); - $provider = new Provider\Identity\ZfcUserDoctrine($em); - $provider->setUserService($sm->get('zfcuser_user_service')); - return $provider; - }, - - 'BjyAuthorize\View\UnauthorizedStrategy' => function ($sm) { - $template = $sm->get('BjyAuthorize\Service\Authorize')->getTemplate(); - $strategy = new View\UnauthorizedStrategy; - $strategy->setTemplate($template); - return $strategy; - }, - - 'BjyAuthorize\Provider\Role\ZendDb' => function ($sm) { - $provider = new Provider\Role\ZendDb; - $provider->setAdapter($sm->get('Zend\Db\Adapter\Adapter')); - return $provider; - }, - - 'BjyAuthorize\Provider\Role\Doctrine' => function ($sm) { - $provider = new Provider\Role\Doctrine; - return $provider; - }, - ), - ); - } - - public function getViewHelperConfig() - { - return array( - 'factories' => array( - 'isAllowed' => function($sm) { - $sm = $sm->getServiceLocator(); // get the main SM instance - $helper = new View\Helper\IsAllowed(); - $helper->setAuthorizeService($sm->get('BjyAuthorize\Service\Authorize')); - return $helper; - } - ), - ); - } - - public function getControllerPluginConfig() - { - return array( - 'factories' => array( - 'isAllowed' => function($sm) { - $sm = $sm->getServiceLocator(); // get the main SM instance - $helper = new Controller\Plugin\IsAllowed(); - $helper->setAuthorizeService($sm->get('BjyAuthorize\Service\Authorize')); - return $helper; - } - ), - ); - } - - public function getAutoloaderConfig() - { - return array( - 'Zend\Loader\StandardAutoloader' => array( - 'namespaces' => array( - __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, - ), - ), - ); - } - - public function getConfig() - { - return include __DIR__ . '/config/module.config.php'; - } -} +/** + * This file is placed here for compatibility with ZendFramework 2's ModuleManager. + * It allows usage of this module even without composer. + * The original Module.php is in 'src/BjyAuthorize' in order to respect PSR-0 + */ +require_once __DIR__ . '/src/BjyAuthorize/Module.php'; diff --git a/config/services.config.php b/config/services.config.php index 4464175..a6eab67 100644 --- a/config/services.config.php +++ b/config/services.config.php @@ -14,7 +14,10 @@ 'initializers' => array( function ($instance, ServiceLocatorInterface $serviceLocator) { if ($instance instanceof Service\AuthorizeAwareInterface) { - $instance->setAuthorizeService($serviceLocator->get('BjyAuthorize\Service\Authorize')); + /* @var $authorize \BjyAuthorize\Service\Authorize */ + $authorize = $serviceLocator->get('BjyAuthorize\Service\Authorize'); + + $instance->setAuthorizeService($authorize); } } ), @@ -32,7 +35,7 @@ function ($instance, ServiceLocatorInterface $serviceLocator) { }, 'BjyAuthorize\Provider\Identity\ZfcUserDoctrine' => function (ServiceLocatorInterface $serviceLocator) { - /* @var $adapter \Doctrine\Common\Persistence\ObjectManager */ + /* @var $objectManager \Doctrine\ORM\EntityManager */ $objectManager = $serviceLocator->get('doctrine.entitymanager.orm_default'); /* @var $userService \ZfcUser\Service\User */ $userService = $serviceLocator->get('zfcuser_user_service'); @@ -57,7 +60,10 @@ function ($instance, ServiceLocatorInterface $serviceLocator) { }, 'BjyAuthorize\Provider\Role\Doctrine' => function (ServiceLocatorInterface $serviceLocator) { - return new Provider\Role\Doctrine(array(), $serviceLocator); + /* @var $objectManager \Doctrine\ORM\EntityManager */ + $objectManager = $serviceLocator->get('doctrine.entitymanager.orm_default'); + + return new Provider\Role\Doctrine(array(), $objectManager); }, ), ); diff --git a/src/BjyAuthorize/Acl/Role.php b/src/BjyAuthorize/Acl/Role.php index f104461..e0e9619 100644 --- a/src/BjyAuthorize/Acl/Role.php +++ b/src/BjyAuthorize/Acl/Role.php @@ -28,7 +28,7 @@ class Role implements RoleInterface protected $parent; /** - * @param string|null $roleId + * @param string|null $roleId * @param RoleInterface|string|null $parent */ public function __construct($roleId = null, $parent = null) @@ -57,6 +57,7 @@ public function getRoleId() public function setRoleId($roleId) { $this->roleId = $roleId; + return $this; } diff --git a/src/BjyAuthorize/Guard/Route.php b/src/BjyAuthorize/Guard/Route.php index b74cfdb..3dd9938 100644 --- a/src/BjyAuthorize/Guard/Route.php +++ b/src/BjyAuthorize/Guard/Route.php @@ -39,7 +39,7 @@ class Route implements GuardInterface, RuleProviderInterface, ResourceProviderIn protected $listeners = array(); /** - * @param array $rules + * @param array $rules * @param ServiceLocatorInterface $serviceLocator */ public function __construct(array $rules, ServiceLocatorInterface $serviceLocator) diff --git a/src/BjyAuthorize/Provider/Identity/ZfcUserZendDb.php b/src/BjyAuthorize/Provider/Identity/ZfcUserZendDb.php index 3ed3b24..923a05b 100644 --- a/src/BjyAuthorize/Provider/Identity/ZfcUserZendDb.php +++ b/src/BjyAuthorize/Provider/Identity/ZfcUserZendDb.php @@ -9,7 +9,6 @@ namespace BjyAuthorize\Provider\Identity; use Zend\Db\Adapter\Adapter; -use Zend\Db\ResultSet\ResultSet; use Zend\Db\Sql\Where; use Zend\Db\Sql\Sql; use ZfcUser\Service\User; @@ -89,6 +88,7 @@ public function getUserService() public function setUserService($userService) { $this->userService = $userService; + return $this; } @@ -106,6 +106,7 @@ public function getDefaultRole() public function setDefaultRole($defaultRole) { $this->defaultRole = $defaultRole; + return $this; } } diff --git a/src/BjyAuthorize/Provider/Role/ZendDb.php b/src/BjyAuthorize/Provider/Role/ZendDb.php index a1dcfe8..edf4eb1 100644 --- a/src/BjyAuthorize/Provider/Role/ZendDb.php +++ b/src/BjyAuthorize/Provider/Role/ZendDb.php @@ -9,7 +9,6 @@ namespace BjyAuthorize\Provider\Role; use BjyAuthorize\Acl\Role; -use Zend\Db\ResultSet\ResultSet; use Zend\Db\TableGateway\TableGateway; use Zend\Db\Sql\Select; use Zend\ServiceManager\ServiceLocatorInterface; diff --git a/src/BjyAuthorize/Service/Authorize.php b/src/BjyAuthorize/Service/Authorize.php index c11ced2..4069e51 100644 --- a/src/BjyAuthorize/Service/Authorize.php +++ b/src/BjyAuthorize/Service/Authorize.php @@ -1,4 +1,10 @@ + */ class Authorize { + /** + * @var Acl + */ protected $acl; + /** + * @var RoleProvider[] + */ protected $roleProviders = array(); + /** + * @var ResourceProvider[] + */ protected $resourceProviders = array(); + /** + * @var RuleProvider[] + */ protected $ruleProviders = array(); + /** + * @var IdentityProvider + */ protected $identityProvider; + /** + * @var GuardInterface[] + */ protected $guards = array(); - protected $identity; - protected $template = 'error/403'; protected $loaded = false; @@ -38,6 +67,10 @@ class Authorize const TYPE_DENY = 'deny'; + /** + * @param array $config + * @param ServiceLocatorInterface $serviceLocator + */ public function __construct(array $config, ServiceLocatorInterface $serviceLocator) { $this->acl = new Acl; @@ -77,6 +110,11 @@ public function __construct(array $config, ServiceLocatorInterface $serviceLocat } } + /** + * @param RoleProvider $provider + * + * @return self + */ public function addRoleProvider(RoleProvider $provider) { $this->roleProviders[] = $provider; @@ -84,6 +122,11 @@ public function addRoleProvider(RoleProvider $provider) return $this; } + /** + * @param ResourceProvider $provider + * + * @return self + */ public function addResourceProvider(ResourceProvider $provider) { $this->resourceProviders[] = $provider; @@ -91,6 +134,11 @@ public function addResourceProvider(ResourceProvider $provider) return $this; } + /** + * @param RuleProvider $provider + * + * @return self + */ public function addRuleProvider(RuleProvider $provider) { $this->ruleProviders[] = $provider; @@ -98,6 +146,11 @@ public function addRuleProvider(RuleProvider $provider) return $this; } + /** + * @param IdentityProvider $provider + * + * @return self + */ public function setIdentityProvider(IdentityProvider $provider) { $this->identityProvider = $provider; @@ -105,12 +158,20 @@ public function setIdentityProvider(IdentityProvider $provider) return $this; } + /** + * @return IdentityProvider + */ public function getIdentityProvider() { return $this->identityProvider; } - public function addGuard($guard) + /** + * @param GuardInterface $guard + * + * @return self + */ + public function addGuard(GuardInterface $guard) { $this->guards[] = $guard; @@ -125,26 +186,44 @@ public function addGuard($guard) return $this; } + /** + * @return GuardInterface[] + */ public function getGuards() { return $this->guards; } + /** + * @return string + */ public function getTemplate() { return $this->template; } + /** + * @return string + */ public function getIdentity() { return 'bjyauthorize-identity'; } + /** + * @return Acl + */ public function getAcl() { return $this->acl; } + /** + * @param string|ResourceInterface $resource + * @param string $privilege + * + * @return bool + */ public function isAllowed($resource, $privilege = null) { if (!$this->loaded) { @@ -158,10 +237,13 @@ public function isAllowed($resource, $privilege = null) } } + /** + * Initializes the service + */ protected function load() { - foreach ($this->roleProviders as $i) { - $this->addRoles($i->getRoles()); + foreach ($this->roleProviders as $provider) { + $this->addRoles($provider->getRoles()); } foreach ($this->resourceProviders as $provider) { @@ -189,27 +271,34 @@ protected function load() $this->loaded = true; } + /** + * @param \Zend\Permissions\Acl\Role\RoleInterface[] $roles + */ protected function addRoles($roles) { if (!is_array($roles)) { $roles = array($roles); } - /* @var $i Role */ - foreach ($roles as $i) { - if ($this->acl->hasRole($i)) { + /* @var $role Role */ + foreach ($roles as $role) { + if ($this->acl->hasRole($role)) { continue; } - if ($i->getParent() !== null) { - $this->addRoles($i->getParent()); - $this->acl->addRole($i, $i->getParent()); + if ($role->getParent() !== null) { + $this->addRoles($role->getParent()); + $this->acl->addRole($role, $role->getParent()); } else { - $this->acl->addRole($i); + $this->acl->addRole($role); } } } + /** + * @param string[]|\Zend\Permissions\Acl\Resource\ResourceInterface[] $resources + * @param mixed|null $parent + */ protected function loadResource(array $resources, $parent = null) { foreach ($resources as $key => $value) { @@ -228,6 +317,12 @@ protected function loadResource(array $resources, $parent = null) } } + /** + * @param mixed $rule + * @param mixed $type + * + * @throws \InvalidArgumentException + */ protected function loadRule(array $rule, $type) { $privileges = $assertion = null; diff --git a/src/BjyAuthorize/Service/AuthorizeFactory.php b/src/BjyAuthorize/Service/AuthorizeFactory.php index ae330ef..ef53708 100644 --- a/src/BjyAuthorize/Service/AuthorizeFactory.php +++ b/src/BjyAuthorize/Service/AuthorizeFactory.php @@ -10,7 +10,6 @@ use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; -use Zend\Permissions\Acl\Acl; /** * Factory responsible of building the {@see \BjyAuthorize\Service\Authorize} service diff --git a/src/BjyAuthorize/View/UnauthorizedStrategy.php b/src/BjyAuthorize/View/UnauthorizedStrategy.php index 089fd2b..621580b 100644 --- a/src/BjyAuthorize/View/UnauthorizedStrategy.php +++ b/src/BjyAuthorize/View/UnauthorizedStrategy.php @@ -1,4 +1,10 @@ + */ class UnauthorizedStrategy implements ListenerAggregateInterface { /** @@ -23,11 +35,17 @@ class UnauthorizedStrategy implements ListenerAggregateInterface */ protected $listeners = array(); + /** + * {@inheritDoc} + */ public function attach(EventManagerInterface $events) { $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), -5000); } + /** + * {@inheritDoc} + */ public function detach(EventManagerInterface $events) { foreach ($this->listeners as $index => $listener) { @@ -37,20 +55,34 @@ public function detach(EventManagerInterface $events) } } + /** + * @param string $template + */ public function setTemplate($template) { - $this->template = $template; + $this->template = (string) $template; } + /** + * @return string + */ public function getTemplate() { return $this->template; } + /** + * Callback used when a dispatch error occurs. Modifies the + * response object with an according error if the application + * event contains an exception related with authorization. + * + * @param MvcEvent $event + */ public function onDispatchError(MvcEvent $event) { // Do nothing if the result is a response object $result = $event->getResult(); + if ($result instanceof Response) { return; } @@ -62,11 +94,10 @@ public function onDispatchError(MvcEvent $event) ); $error = $event->getError(); - switch($error) - { + switch ($error) { case 'error-unauthorized-controller': $viewVariables['controller'] = $event->getParam('controller'); - $viewVariables['action'] = $event->getParam('action'); + $viewVariables['action'] = $event->getParam('action'); break; case 'error-unauthorized-route': $viewVariables['route'] = $event->getParam('route'); @@ -77,7 +108,7 @@ public function onDispatchError(MvcEvent $event) } $viewVariables['reason'] = $event->getParam('exception')->getMessage(); - $viewVariables['error'] = 'error-unauthorized'; + $viewVariables['error'] = 'error-unauthorized'; break; default: /* @@ -85,6 +116,7 @@ public function onDispatchError(MvcEvent $event) * does not match one of our predefined errors (we don't want * our 403.phtml to handle other types of errors) */ + return; }