31 | * @license MIT
32 | */
33 | class GuardPluginManager extends AbstractPluginManager
34 | {
35 | /**
36 | * @var array
37 | */
38 | protected $factories = [
39 | 'ZfcRbac\Guard\ControllerGuard' => 'ZfcRbac\Factory\ControllerGuardFactory',
40 | 'ZfcRbac\Guard\ControllerPermissionsGuard' => 'ZfcRbac\Factory\ControllerPermissionsGuardFactory',
41 | 'ZfcRbac\Guard\RouteGuard' => 'ZfcRbac\Factory\RouteGuardFactory',
42 | 'ZfcRbac\Guard\RoutePermissionsGuard' => 'ZfcRbac\Factory\RoutePermissionsGuardFactory',
43 | ];
44 |
45 | /**
46 | * {@inheritDoc}
47 | */
48 | public function validate($plugin)
49 | {
50 | if ($plugin instanceof GuardInterface) {
51 | return; // we're okay
52 | }
53 |
54 | throw new Exception\RuntimeException(sprintf(
55 | 'Guards must implement "ZfcRbac\Guard\GuardInterface", but "%s" was given',
56 | is_object($plugin) ? get_class($plugin) : gettype($plugin)
57 | ));
58 | }
59 |
60 | /**
61 | * {@inheritDoc}
62 | */
63 | public function validatePlugin($plugin)
64 | {
65 | $this->validate($plugin);
66 | }
67 |
68 | /**
69 | * {@inheritDoc}
70 | */
71 | protected function canonicalizeName($name)
72 | {
73 | return $name;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Guard/ProtectionPolicyTrait.php:
--------------------------------------------------------------------------------
1 |
25 | * @license MIT
26 | */
27 | trait ProtectionPolicyTrait
28 | {
29 | /**
30 | * @var string
31 | */
32 | protected $protectionPolicy = GuardInterface::POLICY_DENY;
33 |
34 | /**
35 | * Set the protection policy
36 | *
37 | * @param string $protectionPolicy
38 | * @return void
39 | */
40 | public function setProtectionPolicy($protectionPolicy)
41 | {
42 | $this->protectionPolicy = (string) $protectionPolicy;
43 | }
44 |
45 | /**
46 | * Get the protection policy
47 | *
48 | * @return string
49 | */
50 | public function getProtectionPolicy()
51 | {
52 | return $this->protectionPolicy;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Identity/AuthenticationIdentityProvider.php:
--------------------------------------------------------------------------------
1 |
27 | * @license MIT
28 | */
29 | class AuthenticationIdentityProvider implements IdentityProviderInterface
30 | {
31 | /**
32 | * @var AuthenticationServiceInterface
33 | */
34 | protected $authenticationService;
35 |
36 | /**
37 | * Constructor
38 | *
39 | * @param AuthenticationServiceInterface $authenticationService
40 | */
41 | public function __construct(AuthenticationServiceInterface $authenticationService)
42 | {
43 | $this->authenticationService = $authenticationService;
44 | }
45 |
46 | /**
47 | * {@inheritDoc}
48 | */
49 | public function getIdentity()
50 | {
51 | return $this->authenticationService->getIdentity();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Identity/IdentityInterface.php:
--------------------------------------------------------------------------------
1 |
25 | * @license MIT
26 | */
27 | interface IdentityInterface
28 | {
29 | /**
30 | * Get the list of roles of this identity
31 | *
32 | * @return string[]|\Rbac\Role\RoleInterface[]
33 | */
34 | public function getRoles();
35 | }
36 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Identity/IdentityProviderInterface.php:
--------------------------------------------------------------------------------
1 |
25 | * @license MIT
26 | */
27 | interface IdentityProviderInterface
28 | {
29 | /**
30 | * Get the identity
31 | *
32 | * @return IdentityInterface|null
33 | */
34 | public function getIdentity();
35 | }
36 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Initializer/AuthorizationServiceInitializer.php:
--------------------------------------------------------------------------------
1 | get('ZfcRbac\Service\AuthorizationService');
43 | $instance->setAuthorizationService($authorizationService);
44 | }
45 | }
46 |
47 | /**
48 | * @see \Zend\ServiceManager\InitializerInterface::initialize()
49 | */
50 | public function initialize($instance, ServiceLocatorInterface $serviceLocator)
51 | {
52 | if ($serviceLocator instanceof AbstractPluginManager) {
53 | $serviceLocator = $serviceLocator->getServiceLocator();
54 | }
55 |
56 | $this($serviceLocator, $instance);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Module.php:
--------------------------------------------------------------------------------
1 |
29 | * @license MIT
30 | */
31 | class Module implements BootstrapListenerInterface, ConfigProviderInterface
32 | {
33 | /**
34 | * {@inheritDoc}
35 | */
36 | public function onBootstrap(EventInterface $event)
37 | {
38 | /* @var \Zend\Mvc\Application $application */
39 | $application = $event->getTarget();
40 | $serviceManager = $application->getServiceManager();
41 | $eventManager = $application->getEventManager();
42 |
43 | /* @var \ZfcRbac\Guard\GuardInterface[]|array $guards */
44 | $guards = $serviceManager->get('ZfcRbac\Guards');
45 |
46 | // Register listeners, if any
47 | foreach ($guards as $guard) {
48 | $guard->attach($eventManager);
49 | }
50 | }
51 |
52 | /**
53 | * {@inheritDoc}
54 | */
55 | public function getConfig()
56 | {
57 | return include __DIR__ . '/../../config/module.config.php';
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Mvc/Controller/Plugin/IsGranted.php:
--------------------------------------------------------------------------------
1 |
28 | * @license MIT
29 | */
30 | class IsGranted extends AbstractPlugin
31 | {
32 | /**
33 | * @var AuthorizationServiceInterface
34 | */
35 | private $authorizationService;
36 |
37 | /**
38 | * Constructor
39 | *
40 | * @param AuthorizationServiceInterface $authorizationService
41 | */
42 | public function __construct(AuthorizationServiceInterface $authorizationService)
43 | {
44 | $this->authorizationService = $authorizationService;
45 | }
46 |
47 | /**
48 | * Check against the given permission
49 | *
50 | * @param string $permission
51 | * @param mixed $context
52 | * @return bool
53 | */
54 | public function __invoke($permission, $context = null)
55 | {
56 | return $this->authorizationService->isGranted($permission, $context);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Options/UnauthorizedStrategyOptions.php:
--------------------------------------------------------------------------------
1 |
27 | * @license MIT
28 | */
29 | class UnauthorizedStrategyOptions extends AbstractOptions
30 | {
31 | /**
32 | * Template to use
33 | *
34 | * @var string
35 | */
36 | protected $template = 'error/403';
37 |
38 | /**
39 | * @param string $template
40 | */
41 | public function setTemplate($template)
42 | {
43 | $this->template = (string) $template;
44 | }
45 |
46 | /**
47 | * @return string
48 | */
49 | public function getTemplate()
50 | {
51 | return $this->template;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Permission/PermissionInterface.php:
--------------------------------------------------------------------------------
1 |
30 | * @license MIT
31 | */
32 | interface PermissionInterface extends BasePermissionInterface
33 | {
34 | }
35 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Role/ObjectRepositoryRoleProvider.php:
--------------------------------------------------------------------------------
1 |
28 | * @license MIT
29 | */
30 | class ObjectRepositoryRoleProvider implements RoleProviderInterface
31 | {
32 | /**
33 | * @var ObjectRepository
34 | */
35 | private $objectRepository;
36 |
37 | /**
38 | * @var string
39 | */
40 | private $roleNameProperty;
41 |
42 | /**
43 | * @var array
44 | */
45 | private $roleCache = [];
46 |
47 | /**
48 | * Constructor
49 | *
50 | * @param ObjectRepository $objectRepository
51 | * @param string $roleNameProperty
52 | */
53 | public function __construct(ObjectRepository $objectRepository, $roleNameProperty)
54 | {
55 | $this->objectRepository = $objectRepository;
56 | $this->roleNameProperty = $roleNameProperty;
57 | }
58 |
59 | /**
60 | * Clears the role cache
61 | *
62 | * @return void
63 | */
64 | public function clearRoleCache()
65 | {
66 | $this->roleCache = [];
67 | }
68 |
69 | /**
70 | * {@inheritDoc}
71 | */
72 | public function getRoles(array $roleNames)
73 | {
74 | $key = implode($roleNames);
75 |
76 | if (isset($this->roleCache[$key])) {
77 | return $this->roleCache[$key];
78 | }
79 |
80 | $roles = $this->objectRepository->findBy([$this->roleNameProperty => $roleNames]);
81 |
82 | // We allow more roles to be loaded than asked (although this should not happen because
83 | // role name should have a UNIQUE constraint in database... but just in case ;))
84 | if (count($roles) >= count($roleNames)) {
85 | $this->roleCache[$key] = $roles;
86 |
87 | return $roles;
88 | }
89 |
90 | // We have roles that were asked but couldn't be found in database... problem!
91 | foreach ($roles as &$role) {
92 | $role = $role->getName();
93 | }
94 |
95 | throw new RoleNotFoundException(sprintf(
96 | 'Some roles were asked but could not be loaded from database: %s',
97 | implode(', ', array_diff($roleNames, $roles))
98 | ));
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Role/RoleProviderInterface.php:
--------------------------------------------------------------------------------
1 |
28 | * @license MIT
29 | */
30 | interface RoleProviderInterface
31 | {
32 | /**
33 | * Get the roles from the provider
34 | *
35 | * @param string[] $roleNames
36 | * @return \Rbac\Role\RoleInterface[]
37 | */
38 | public function getRoles(array $roleNames);
39 | }
40 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Role/RoleProviderPluginManager.php:
--------------------------------------------------------------------------------
1 |
30 | * @license MIT
31 | */
32 | class RoleProviderPluginManager extends AbstractPluginManager
33 | {
34 | /**
35 | * @var array
36 | */
37 | protected $invokableClasses = [
38 | 'ZfcRbac\Role\InMemoryRoleProvider' => 'ZfcRbac\Role\InMemoryRoleProvider'
39 | ];
40 |
41 | /**
42 | * @var array
43 | */
44 | protected $factories = [
45 | 'ZfcRbac\Role\ObjectRepositoryRoleProvider' => 'ZfcRbac\Factory\ObjectRepositoryRoleProviderFactory'
46 | ];
47 |
48 | /**
49 | * {@inheritDoc}
50 | */
51 | public function validate($plugin)
52 | {
53 | if ($plugin instanceof RoleProviderInterface) {
54 | return; // we're okay
55 | }
56 |
57 | throw new Exception\RuntimeException(sprintf(
58 | 'Role provider must implement "ZfcRbac\Role\RoleProviderInterface", but "%s" was given',
59 | is_object($plugin) ? get_class($plugin) : gettype($plugin)
60 | ));
61 | }
62 |
63 | /**
64 | * {@inheritDoc}
65 | */
66 | public function validatePlugin($plugin)
67 | {
68 | $this->validate($plugin);
69 | }
70 |
71 | /**
72 | * {@inheritDoc}
73 | */
74 | protected function canonicalizeName($name)
75 | {
76 | return $name;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Service/AuthorizationServiceAwareInterface.php:
--------------------------------------------------------------------------------
1 | authorizationService = $authorizationService;
47 | }
48 |
49 | /**
50 | * Return the AuthorizationService
51 | *
52 | * @return AuthorizationService
53 | */
54 | public function getAuthorizationService()
55 | {
56 | return $this->authorizationService;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/ZfcRbac/Service/AuthorizationServiceInterface.php:
--------------------------------------------------------------------------------
1 |
27 | * @license MIT
28 | */
29 | interface AuthorizationServiceInterface
30 | {
31 | /**
32 | * Check if the permission is granted to the current identity
33 | *
34 | * @param string|PermissionInterface $permission
35 | * @param mixed $context
36 | * @return bool
37 | */
38 | public function isGranted($permission, $context = null);
39 | }
40 |
--------------------------------------------------------------------------------
/src/ZfcRbac/View/Helper/HasRole.php:
--------------------------------------------------------------------------------
1 |
28 | * @license MIT
29 | */
30 | class HasRole extends AbstractHelper
31 | {
32 | /**
33 | * @var RoleService
34 | */
35 | private $roleService;
36 |
37 | /**
38 | * Constructor
39 | *
40 | * @param RoleService $roleService
41 | */
42 | public function __construct(RoleService $roleService)
43 | {
44 | $this->roleService = $roleService;
45 | }
46 |
47 | /**
48 | * @param string|string[] $roleOrRoles
49 | * @return bool
50 | */
51 | public function __invoke($roleOrRoles)
52 | {
53 | return $this->roleService->matchIdentityRoles((array)$roleOrRoles);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/ZfcRbac/View/Helper/IsGranted.php:
--------------------------------------------------------------------------------
1 |
28 | * @license MIT
29 | */
30 | class IsGranted extends AbstractHelper
31 | {
32 | /**
33 | * @var AuthorizationServiceInterface
34 | */
35 | private $authorizationService;
36 |
37 | /**
38 | * Constructor
39 | *
40 | * @param AuthorizationServiceInterface $authorizationService
41 | */
42 | public function __construct(AuthorizationServiceInterface $authorizationService)
43 | {
44 | $this->authorizationService = $authorizationService;
45 | }
46 |
47 | /**
48 | * Check against the given permission
49 | *
50 | * @param string $permission
51 | * @param mixed $context
52 | * @return bool
53 | */
54 | public function __invoke($permission, $context = null)
55 | {
56 | return $this->authorizationService->isGranted($permission, $context);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/ZfcRbac/View/Strategy/AbstractStrategy.php:
--------------------------------------------------------------------------------
1 |
29 | * @license MIT
30 | */
31 | abstract class AbstractStrategy extends AbstractListenerAggregate
32 | {
33 | /**
34 | * {@inheritDoc}
35 | */
36 | public function attach(EventManagerInterface $events, $priority = 1)
37 | {
38 | $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'onError'], $priority);
39 | }
40 |
41 | /**
42 | * @private
43 | * @param MvcEvent $event
44 | * @return void
45 | */
46 | abstract public function onError(MvcEvent $event);
47 | }
48 |
--------------------------------------------------------------------------------
/src/ZfcRbac/View/Strategy/UnauthorizedStrategy.php:
--------------------------------------------------------------------------------
1 |
32 | * @license MIT
33 | */
34 | class UnauthorizedStrategy extends AbstractStrategy
35 | {
36 | /**
37 | * @var UnauthorizedStrategyOptions
38 | */
39 | protected $options;
40 |
41 | /**
42 | * Constructor
43 | *
44 | * @param UnauthorizedStrategyOptions $options
45 | */
46 | public function __construct(UnauthorizedStrategyOptions $options)
47 | {
48 | $this->options = $options;
49 | }
50 |
51 | /**
52 | * @private
53 | * @param MvcEvent $event
54 | * @return void
55 | */
56 | public function onError(MvcEvent $event)
57 | {
58 | // Do nothing if no error or if response is not HTTP response
59 | if (!($event->getParam('exception') instanceof UnauthorizedExceptionInterface)
60 | || ($event->getResult() instanceof HttpResponse)
61 | || !($event->getResponse() instanceof HttpResponse)
62 | ) {
63 | return;
64 | }
65 |
66 | $model = new ViewModel();
67 | $model->setTemplate($this->options->getTemplate());
68 |
69 | switch ($event->getError()) {
70 | case GuardInterface::GUARD_UNAUTHORIZED:
71 | $model->setVariable('error', GuardInterface::GUARD_UNAUTHORIZED);
72 | break;
73 |
74 | default:
75 | }
76 |
77 | $response = $event->getResponse() ?: new HttpResponse();
78 | $response->setStatusCode(403);
79 |
80 | $event->setResponse($response);
81 | $event->setResult($model);
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/tests/Bootstrap.php:
--------------------------------------------------------------------------------
1 | add('ZfcRbacTest\\', __DIR__);
42 |
43 | $r = new \ReflectionClass(Application::class);
44 | $requiredParams = $r->getConstructor()->getNumberOfRequiredParameters();
45 | $version = $requiredParams == 1 ? 3 : 2;
46 |
47 | $configFiles = [
48 | sprintf(__DIR__ . '/TestConfigurationV%s.php', $version),
49 | sprintf(__DIR__ . '/TestConfigurationV%s.php.dist', $version),
50 | ];
51 |
52 | foreach ($configFiles as $configFile) {
53 | if (file_exists($configFile)) {
54 | $config = require $configFile;
55 |
56 | break;
57 | }
58 | }
59 |
60 | ServiceManagerFactory::setApplicationConfig($config);
61 | unset($files, $file, $loader, $configFiles, $configFile, $config);
62 |
--------------------------------------------------------------------------------
/tests/TestConfigurationV2.php.dist:
--------------------------------------------------------------------------------
1 | [
21 | 'ZfcRbac',
22 | 'DoctrineModule',
23 | 'DoctrineORMModule',
24 | ],
25 | 'module_listener_options' => [
26 | 'config_glob_paths' => [
27 | __DIR__ . '/testing.config.php',
28 | ],
29 | 'module_paths' => [
30 | ],
31 | ],
32 | ];
33 |
--------------------------------------------------------------------------------
/tests/TestConfigurationV3.php.dist:
--------------------------------------------------------------------------------
1 | [
21 | 'Zend\Router',
22 | 'ZfcRbac',
23 | 'DoctrineModule',
24 | 'DoctrineORMModule',
25 | ],
26 | 'module_listener_options' => [
27 | 'config_glob_paths' => [
28 | __DIR__ . '/testing.config.php',
29 | ],
30 | 'module_paths' => [
31 | ],
32 | ],
33 | ];
34 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Asset/DummyGuard.php:
--------------------------------------------------------------------------------
1 | name = (string) $name;
61 | $this->permissions = new ArrayCollection();
62 | }
63 |
64 | /**
65 | * Get the role identifier
66 | *
67 | * @return int
68 | */
69 | public function getId()
70 | {
71 | return $this->id;
72 | }
73 |
74 | /**
75 | * Add a permission
76 | *
77 | * @param PermissionInterface|string $permission
78 | * @return void
79 | */
80 | public function addPermission($permission)
81 | {
82 | if (is_string($permission)) {
83 | $name = $permission;
84 | $permission = new Permission($name);
85 | }
86 |
87 | $this->permissions[$permission->getName()] = $permission;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Asset/HierarchicalRole.php:
--------------------------------------------------------------------------------
1 | name = (string) $name;
68 | $this->permissions = new ArrayCollection();
69 | }
70 |
71 | /**
72 | * Get the role identifier
73 | *
74 | * @return int
75 | */
76 | public function getId()
77 | {
78 | return $this->id;
79 | }
80 |
81 | /**
82 | * Add a permission
83 | *
84 | * @param PermissionInterface|string $permission
85 | * @return void
86 | */
87 | public function addPermission($permission)
88 | {
89 | if (is_string($permission)) {
90 | $name = $permission;
91 | $permission = new Permission($name);
92 | }
93 |
94 | $this->permissions[$permission->getName()] = $permission;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Asset/MockRoleWithPermissionMethod.php:
--------------------------------------------------------------------------------
1 | name = (string) $name;
53 | $this->roles = new ArrayCollection();
54 | }
55 |
56 | /**
57 | * Get the permission identifier
58 | *
59 | * @return int
60 | */
61 | public function getId()
62 | {
63 | return $this->id;
64 | }
65 |
66 | /**
67 | * Get the permission name
68 | *
69 | * @return string
70 | */
71 | public function getName()
72 | {
73 | return $this->name;
74 | }
75 |
76 | /**
77 | * {@inheritDoc}
78 | */
79 | public function __toString()
80 | {
81 | return $this->getName();
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Asset/SimpleAssertion.php:
--------------------------------------------------------------------------------
1 | called = true;
37 |
38 | return $context;
39 | }
40 |
41 | /**
42 | * @return bool
43 | */
44 | public function getCalled()
45 | {
46 | return $this->called;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/AssertionPluginManagerFactoryTest.php:
--------------------------------------------------------------------------------
1 | setService('Config', [
33 | 'zfc_rbac' => [
34 | 'assertion_manager' => []
35 | ]
36 | ]);
37 |
38 | $factory = new AssertionPluginManagerFactory();
39 | $pluginManager = $factory->createService($serviceManager);
40 |
41 | $this->assertInstanceOf('ZfcRbac\Assertion\AssertionPluginManager', $pluginManager);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/AuthenticationIdentityProviderFactoryTest.php:
--------------------------------------------------------------------------------
1 | setService(
33 | 'Zend\Authentication\AuthenticationService',
34 | $this->getMock('Zend\Authentication\AuthenticationService')
35 | );
36 |
37 | $factory = new AuthenticationIdentityProviderFactory();
38 | $authenticationProvider = $factory->createService($serviceManager);
39 |
40 | $this->assertInstanceOf('ZfcRbac\Identity\AuthenticationIdentityProvider', $authenticationProvider);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/AuthorizationServiceFactoryTest.php:
--------------------------------------------------------------------------------
1 | setService('Rbac\Rbac', $this->getMock('Rbac\Rbac', [], [], '', false));
35 |
36 | $serviceManager->setService(
37 | 'ZfcRbac\Service\RoleService',
38 | $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false)
39 | );
40 | $serviceManager->setService(
41 | 'ZfcRbac\Assertion\AssertionPluginManager',
42 | $this->getMock('ZfcRbac\Assertion\AssertionPluginManager', [], [], '', false)
43 | );
44 | $serviceManager->setService(
45 | 'ZfcRbac\Options\ModuleOptions',
46 | new ModuleOptions([])
47 | );
48 |
49 | $factory = new AuthorizationServiceFactory();
50 | $authorizationService = $factory->createService($serviceManager);
51 |
52 | $this->assertInstanceOf('ZfcRbac\Service\AuthorizationService', $authorizationService);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/GuardPluginManagerFactoryTest.php:
--------------------------------------------------------------------------------
1 | setService('Config', [
34 | 'zfc_rbac' => [
35 | 'guard_manager' => []
36 | ]
37 | ]);
38 |
39 | $factory = new GuardPluginManagerFactory();
40 | $pluginManager = $factory->createService($serviceManager);
41 |
42 | $this->assertInstanceOf(GuardPluginManager::class, $pluginManager);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/HasRoleViewHelperFactoryTest.php:
--------------------------------------------------------------------------------
1 | markTestSkipped('this test is only vor zend-servicemanager v2');
36 | }
37 |
38 | $pluginManager = new HelperPluginManager($serviceManager);
39 |
40 | $serviceManager->setService(
41 | 'ZfcRbac\Service\RoleService',
42 | $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false)
43 | );
44 |
45 | $factory = new HasRoleViewHelperFactory();
46 | $viewHelper = $factory->createService($pluginManager);
47 |
48 | $this->assertInstanceOf('ZfcRbac\View\Helper\HasRole', $viewHelper);
49 | }
50 |
51 | public function testFactoryV3()
52 | {
53 | $serviceManager = new ServiceManager();
54 |
55 | if (!method_exists($serviceManager, 'build')) {
56 | $this->markTestSkipped('this test is only vor zend-servicemanager v3');
57 | }
58 |
59 | $serviceManager->setService(
60 | 'ZfcRbac\Service\RoleService',
61 | $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false)
62 | );
63 |
64 | $factory = new HasRoleViewHelperFactory();
65 | $viewHelper = $factory($serviceManager, 'ZfcRbac\View\Helper\HasRole');
66 |
67 | $this->assertInstanceOf('ZfcRbac\View\Helper\HasRole', $viewHelper);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/IsGrantedPluginFactoryTest.php:
--------------------------------------------------------------------------------
1 | markTestSkipped('this test is only vor zend-servicemanager v2');
36 | }
37 |
38 | $pluginManager = new PluginManager($serviceManager);
39 |
40 | $serviceManager->setService(
41 | 'ZfcRbac\Service\AuthorizationService',
42 | $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface')
43 | );
44 |
45 | $factory = new IsGrantedPluginFactory();
46 | $isGranted = $factory->createService($pluginManager);
47 |
48 | $this->assertInstanceOf('ZfcRbac\Mvc\Controller\Plugin\IsGranted', $isGranted);
49 | }
50 |
51 | public function testFactoryV3()
52 | {
53 | $serviceManager = new ServiceManager();
54 |
55 | if (! method_exists($serviceManager, 'build')) {
56 | $this->markTestSkipped('this test is only vor zend-servicemanager v3');
57 | }
58 | $serviceManager->setService(
59 | 'ZfcRbac\Service\AuthorizationService',
60 | $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface')
61 | );
62 |
63 | $factory = new IsGrantedPluginFactory();
64 | $isGranted = $factory($serviceManager, 'ZfcRbac\Mvc\Controller\Plugin\IsGranted');
65 |
66 | $this->assertInstanceOf('ZfcRbac\Mvc\Controller\Plugin\IsGranted', $isGranted);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/IsGrantedViewHelperFactoryTest.php:
--------------------------------------------------------------------------------
1 | markTestSkipped('this test is only vor zend-servicemanager v2');
36 | }
37 |
38 | $pluginManager = new HelperPluginManager($serviceManager);
39 |
40 | $serviceManager->setService(
41 | 'ZfcRbac\Service\AuthorizationService',
42 | $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface')
43 | );
44 |
45 | $factory = new IsGrantedViewHelperFactory();
46 | $isGranted = $factory->createService($pluginManager);
47 |
48 | $this->assertInstanceOf('ZfcRbac\View\Helper\IsGranted', $isGranted);
49 | }
50 |
51 | public function testFactoryV3()
52 | {
53 | $serviceManager = new ServiceManager();
54 |
55 | if (! method_exists($serviceManager, 'build')) {
56 | $this->markTestSkipped('this test is only vor zend-servicemanager v3');
57 | }
58 | $serviceManager->setService(
59 | 'ZfcRbac\Service\AuthorizationService',
60 | $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface')
61 | );
62 |
63 | $factory = new IsGrantedViewHelperFactory();
64 | $isGranted = $factory($serviceManager, 'ZfcRbac\View\Helper\IsGranted');
65 |
66 | $this->assertInstanceOf('ZfcRbac\View\Helper\IsGranted', $isGranted);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/ModuleOptionsFactoryTest.php:
--------------------------------------------------------------------------------
1 | []];
32 |
33 | $serviceManager = new ServiceManager();
34 | $serviceManager->setService('Config', $config);
35 |
36 | $factory = new ModuleOptionsFactory();
37 | $options = $factory->createService($serviceManager);
38 |
39 | $this->assertInstanceOf('ZfcRbac\Options\ModuleOptions', $options);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/RbacFactoryTest.php:
--------------------------------------------------------------------------------
1 | createService($serviceManager);
35 |
36 | $this->assertInstanceOf('Rbac\Rbac', $rbac);
37 | $this->assertInstanceOf('Rbac\Traversal\Strategy\TraversalStrategyInterface', $rbac->getTraversalStrategy());
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/RedirectStrategyFactoryTest.php:
--------------------------------------------------------------------------------
1 | getMock('ZfcRbac\Options\RedirectStrategyOptions');
33 |
34 | $moduleOptionsMock = $this->getMock('ZfcRbac\Options\ModuleOptions');
35 | $moduleOptionsMock->expects($this->once())
36 | ->method('getRedirectStrategy')
37 | ->will($this->returnValue($redirectStrategyOptions));
38 |
39 | $authenticationServiceMock = $this->getMock('Zend\Authentication\AuthenticationService');
40 |
41 | $serviceLocatorMock = $this->prophesize(ServiceLocatorInterface::class);
42 | $serviceLocatorMock->willImplement(ContainerInterface::class);
43 | $serviceLocatorMock->get('ZfcRbac\Options\ModuleOptions')
44 | ->willReturn($moduleOptionsMock)
45 | ->shouldBeCalled();
46 | $serviceLocatorMock->get('Zend\Authentication\AuthenticationService')
47 | ->willReturn($authenticationServiceMock)
48 | ->shouldBeCalled();
49 |
50 | $factory = new RedirectStrategyFactory();
51 | $redirectStrategy = $factory->createService($serviceLocatorMock->reveal());
52 |
53 | $this->assertInstanceOf('ZfcRbac\View\Strategy\RedirectStrategy', $redirectStrategy);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/RoleProviderPluginManagerFactoryTest.php:
--------------------------------------------------------------------------------
1 | setService('Config', [
34 | 'zfc_rbac' => [
35 | 'role_provider_manager' => []
36 | ]
37 | ]);
38 |
39 | $factory = new RoleProviderPluginManagerFactory();
40 | $pluginManager = $factory->createService($serviceManager);
41 |
42 | $this->assertInstanceOf(RoleProviderPluginManager::class, $pluginManager);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Factory/UnauthorizedStrategyFactoryTest.php:
--------------------------------------------------------------------------------
1 | getMock('ZfcRbac\Options\UnauthorizedStrategyOptions');
32 |
33 | $moduleOptionsMock = $this->getMock('ZfcRbac\Options\ModuleOptions');
34 | $moduleOptionsMock->expects($this->once())
35 | ->method('getUnauthorizedStrategy')
36 | ->will($this->returnValue($unauthorizedStrategyOptions));
37 |
38 | $serviceLocatorMock = $this->prophesize('Zend\ServiceManager\ServiceLocatorInterface');
39 | $serviceLocatorMock->willImplement(ContainerInterface::class);
40 | $serviceLocatorMock->get('ZfcRbac\Options\ModuleOptions')->willReturn($moduleOptionsMock)->shouldBeCalled();
41 |
42 | $factory = new UnauthorizedStrategyFactory();
43 | $unauthorizedStrategy = $factory->createService($serviceLocatorMock->reveal());
44 |
45 | $this->assertInstanceOf('ZfcRbac\View\Strategy\UnauthorizedStrategy', $unauthorizedStrategy);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Guard/AbstractGuardTest.php:
--------------------------------------------------------------------------------
1 | prophesize(Application::class);
36 | $application->getEventManager()->willReturn($eventManager);
37 |
38 | $event = new MvcEvent();
39 | $event->setApplication($application->reveal());
40 |
41 | $guard = new DummyGuard();
42 | $guard->attach($eventManager);
43 |
44 | $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function (MvcEvent $event) {
45 | $event->setParam('first-listener', true);
46 | });
47 | $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function (MvcEvent $event) {
48 | $event->setParam('second-listener', true);
49 | });
50 |
51 | // attach listener with lower priority than DummyGuard
52 | $eventManager->attach(MvcEvent::EVENT_ROUTE, function (MvcEvent $event) {
53 | $this->fail('should not be called, because guard should stop propagation');
54 | }, DummyGuard::EVENT_PRIORITY - 1);
55 |
56 | $event->setName(MvcEvent::EVENT_ROUTE);
57 | $eventManager->triggerEvent($event);
58 |
59 | $this->assertTrue($event->getParam('first-listener'));
60 | $this->assertTrue($event->getParam('second-listener'));
61 | $this->assertTrue($event->propagationIsStopped());
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Guard/GuardPluginManagerTest.php:
--------------------------------------------------------------------------------
1 | 'foo'
37 | ]
38 | ],
39 | [
40 | 'ZfcRbac\Guard\RoutePermissionsGuard',
41 | [
42 | 'post/delete' => 'post.delete'
43 | ]
44 | ],
45 | [
46 | 'ZfcRbac\Guard\ControllerGuard',
47 | [
48 | [
49 | 'controller' => 'Foo',
50 | 'actions' => 'bar',
51 | 'roles' => 'baz'
52 | ]
53 | ]
54 | ],
55 | [
56 | 'ZfcRbac\Guard\ControllerPermissionsGuard',
57 | [
58 | [
59 | 'controller' => 'Foo',
60 | 'actions' => 'bar',
61 | 'permissions' => 'baz'
62 | ]
63 | ]
64 | ],
65 | ];
66 | }
67 |
68 | /**
69 | * @dataProvider guardProvider
70 | */
71 | public function testCanCreateDefaultGuards($type, $options)
72 | {
73 | $serviceManager = new ServiceManager();
74 | $serviceManager->setService('ZfcRbac\Options\ModuleOptions', new ModuleOptions());
75 | $serviceManager->setService(
76 | 'ZfcRbac\Service\RoleService',
77 | $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false)
78 | );
79 | $serviceManager->setService(
80 | 'ZfcRbac\Service\AuthorizationService',
81 | $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false)
82 | );
83 |
84 | $pluginManager = new GuardPluginManager($serviceManager);
85 |
86 | $guard = $pluginManager->get($type, $options);
87 |
88 | $this->assertInstanceOf($type, $guard);
89 | }
90 |
91 | public function testThrowExceptionForInvalidPlugin()
92 | {
93 | $this->setExpectedException('ZfcRbac\Exception\RuntimeException');
94 |
95 | $pluginManager = new GuardPluginManager(new ServiceManager());
96 | $pluginManager->get('stdClass');
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Guard/ProtectionPolicyTraitTest.php:
--------------------------------------------------------------------------------
1 | getObjectForTrait('ZfcRbac\Guard\ProtectionPolicyTrait');
31 | $trait->setProtectionPolicy(GuardInterface::POLICY_DENY);
32 |
33 | $this->assertEquals(GuardInterface::POLICY_DENY, $trait->getProtectionPolicy());
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Identity/AuthenticationIdentityProviderTest.php:
--------------------------------------------------------------------------------
1 | authenticationService = $this->getMock('Zend\Authentication\AuthenticationService');
41 | $this->identityProvider = new AuthenticationIdentityProvider($this->authenticationService);
42 | }
43 |
44 | public function testCanReturnIdentity()
45 | {
46 | $identity = $this->getMock('ZfcRbac\Identity\IdentityInterface');
47 |
48 | $this->authenticationService->expects($this->once())
49 | ->method('getIdentity')
50 | ->will($this->returnValue($identity));
51 |
52 | $this->assertSame($identity, $this->identityProvider->getIdentity());
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Initializer/AuthorizationAwareFake.php:
--------------------------------------------------------------------------------
1 | prophesize(ServiceLocatorInterface::class)->willImplement(ContainerInterface::class);
38 | $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false);
39 |
40 | $serviceLocator
41 | ->get($authServiceClassName)
42 | ->willReturn($authorizationService)
43 | ->shouldBeCalled();
44 |
45 | $initializer->initialize($instance, $serviceLocator->reveal());
46 |
47 | $this->assertEquals($authorizationService, $instance->getAuthorizationService());
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/ModuleTest.php:
--------------------------------------------------------------------------------
1 | assertInternalType('array', $module->getConfig());
32 | }
33 |
34 | public function testCanRegisterGuards()
35 | {
36 | $module = new Module();
37 | $mvcEvent = $this->getMock('Zend\Mvc\MvcEvent');
38 | $application = $this->getMock('Zend\Mvc\Application', [], [], '', false);
39 | $eventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
40 | $serviceManager = $this->getMock('Zend\ServiceManager\ServiceManager');
41 |
42 | $mvcEvent->expects($this->once())->method('getTarget')->will($this->returnValue($application));
43 | $application->expects($this->once())->method('getEventManager')->will($this->returnValue($eventManager));
44 | $application->expects($this->once())->method('getServiceManager')->will($this->returnValue($serviceManager));
45 |
46 | $guards = [
47 | $this->getMock('ZfcRbac\Guard\GuardInterface')
48 | ];
49 |
50 | $serviceManager->expects($this->once())
51 | ->method('get')
52 | ->with('ZfcRbac\Guards')
53 | ->will($this->returnValue($guards));
54 |
55 | $module->onBootstrap($mvcEvent);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Mvc/Controller/Plugin/IsGrantedTest.php:
--------------------------------------------------------------------------------
1 | getMock('ZfcRbac\Service\AuthorizationServiceInterface');
31 |
32 | $authorizationService->expects($this->once())
33 | ->method('isGranted')
34 | ->with('edit')
35 | ->will($this->returnValue(true));
36 |
37 | $helper = new IsGranted($authorizationService);
38 |
39 | $this->assertTrue($helper('edit'));
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Options/RedirectStrategyOptionsTest.php:
--------------------------------------------------------------------------------
1 | assertTrue($redirectStrategyOptions->getRedirectWhenConnected());
33 | $this->assertEquals('login', $redirectStrategyOptions->getRedirectToRouteDisconnected());
34 | $this->assertEquals('home', $redirectStrategyOptions->getRedirectToRouteConnected());
35 | $this->assertTrue($redirectStrategyOptions->getAppendPreviousUri());
36 | $this->assertEquals('redirectTo', $redirectStrategyOptions->getPreviousUriQueryKey());
37 | }
38 |
39 | public function testSettersAndGetters()
40 | {
41 | $redirectStrategyOptions = new RedirectStrategyOptions([
42 | 'redirect_when_connected' => false,
43 | 'redirect_to_route_connected' => 'foo',
44 | 'redirect_to_route_disconnected' => 'bar',
45 | 'append_previous_uri' => false,
46 | 'previous_uri_query_key' => 'redirect-to'
47 | ]);
48 |
49 | $this->assertFalse($redirectStrategyOptions->getRedirectWhenConnected());
50 | $this->assertEquals('foo', $redirectStrategyOptions->getRedirectToRouteConnected());
51 | $this->assertEquals('bar', $redirectStrategyOptions->getRedirectToRouteDisconnected());
52 | $this->assertFalse($redirectStrategyOptions->getAppendPreviousUri());
53 | $this->assertEquals('redirect-to', $redirectStrategyOptions->getPreviousUriQueryKey());
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Options/UnauthorizedStrategyOptionsTest.php:
--------------------------------------------------------------------------------
1 | assertEquals('error/403', $unauthorizedStrategyOptions->getTemplate());
33 | }
34 |
35 | public function testSettersAndGetters()
36 | {
37 | $unauthorizedStrategyOptions = new UnauthorizedStrategyOptions([
38 | 'template' => 'error/unauthorized'
39 | ]);
40 |
41 | $this->assertEquals('error/unauthorized', $unauthorizedStrategyOptions->getTemplate());
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Role/InMemoryRoleProviderTest.php:
--------------------------------------------------------------------------------
1 | [
32 | 'children' => ['member'],
33 | 'permissions' => ['delete']
34 | ],
35 | 'member' => [
36 | 'children' => ['guest'],
37 | 'permissions' => ['write']
38 | ],
39 | 'guest'
40 | ]);
41 |
42 | $roles = $inMemoryProvider->getRoles(['admin', 'member', 'guest']);
43 |
44 | $this->assertCount(3, $roles);
45 |
46 | // Test admin role
47 | $adminRole = $roles[0];
48 | $this->assertInstanceOf('Rbac\Role\HierarchicalRoleInterface', $adminRole);
49 | $this->assertEquals('admin', $adminRole->getName());
50 | $this->assertTrue($adminRole->hasPermission('delete'));
51 |
52 | // Test member role
53 | $memberRole = $roles[1];
54 | $this->assertInstanceOf('Rbac\Role\HierarchicalRoleInterface', $memberRole);
55 | $this->assertEquals('member', $memberRole->getName());
56 | $this->assertTrue($memberRole->hasPermission('write'));
57 | $this->assertFalse($memberRole->hasPermission('delete'));
58 |
59 | // Test guest role
60 | $guestRole = $roles[2];
61 | $this->assertInstanceOf('Rbac\Role\RoleInterface', $guestRole);
62 | $this->assertNotInstanceOf('Rbac\Role\HierarchicalRoleInterface', $guestRole);
63 | $this->assertEquals('guest', $guestRole->getName());
64 | $this->assertFalse($guestRole->hasPermission('write'));
65 | $this->assertFalse($guestRole->hasPermission('delete'));
66 |
67 | $this->assertSame($adminRole->getChildren()['member'], $memberRole);
68 | $this->assertSame($memberRole->getChildren()['guest'], $guestRole);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Role/RoleProviderPluginManagerTest.php:
--------------------------------------------------------------------------------
1 | getMock('ZfcRbac\Role\RoleProviderInterface');
32 | $pluginManager = new RoleProviderPluginManager(new ServiceManager());
33 |
34 | $this->assertNull($pluginManager->validatePlugin($pluginMock));
35 | }
36 |
37 | public function testValidationOfPluginFailsIfRoleProviderInterfaceIsNotImplemented()
38 | {
39 | $this->setExpectedException('ZfcRbac\Exception\RuntimeException');
40 |
41 | $pluginManager = new RoleProviderPluginManager(new ServiceManager());
42 | $pluginManager->get('stdClass', []);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Service/AuthorizationServiceAwareTraitTest.php:
--------------------------------------------------------------------------------
1 | getObjectForTrait('ZfcRbac\Service\AuthorizationServiceAwareTrait');
30 | $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationService', [], [], '', false);
31 |
32 | $trait->setAuthorizationService($authorizationService);
33 |
34 | $this->assertEquals($authorizationService, $trait->getAuthorizationService());
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/Util/ServiceManagerFactory.php:
--------------------------------------------------------------------------------
1 |
29 | */
30 | abstract class ServiceManagerFactory
31 | {
32 | /**
33 | * @var array
34 | */
35 | private static $config = [];
36 |
37 | /**
38 | * @static
39 | * @param array $config
40 | */
41 | public static function setApplicationConfig(array $config)
42 | {
43 | static::$config = $config;
44 | }
45 |
46 | /**
47 | * @static
48 | * @return array
49 | */
50 | public static function getApplicationConfig()
51 | {
52 | return static::$config;
53 | }
54 |
55 | /**
56 | * @param array|null $config
57 | * @return ServiceManager
58 | */
59 | public static function getServiceManager(array $config = null)
60 | {
61 | $config = $config ?: static::getApplicationConfig();
62 | $serviceManagerConfig = new ServiceManagerConfig(
63 | isset($config['service_manager']) ? $config['service_manager'] : []
64 | );
65 | $serviceManager = new ServiceManager();
66 | $serviceManagerConfig->configureServiceManager($serviceManager);
67 | $serviceManager->setService('ApplicationConfig', $config);
68 | $serviceManager->setAllowOverride(true);
69 |
70 | /* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */
71 | $moduleManager = $serviceManager->get('ModuleManager');
72 |
73 | $moduleManager->loadModules();
74 |
75 | return $serviceManager;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/View/Helper/HasRoleTest.php:
--------------------------------------------------------------------------------
1 | get('Config');
33 | $this->assertArrayHasKey('view_helpers', $config);
34 | $viewHelpersConfig = $config['view_helpers'];
35 | $this->assertEquals('ZfcRbac\View\Helper\HasRole', $viewHelpersConfig['aliases']['hasRole']);
36 | $this->assertEquals(
37 | 'ZfcRbac\Factory\HasRoleViewHelperFactory',
38 | $viewHelpersConfig['factories']['ZfcRbac\View\Helper\HasRole']
39 | );
40 | }
41 |
42 | public function testCallAuthorizationService()
43 | {
44 | $rolesConfig = [
45 | ['member', true],
46 | [['member'], true],
47 | ];
48 |
49 | $authorizationService = $this->getMock('ZfcRbac\Service\RoleService', [], [], '', false);
50 | $authorizationService->expects($this->any())
51 | ->method('matchIdentityRoles')
52 | ->will($this->returnValueMap($rolesConfig));
53 |
54 | $helper = new HasRole($authorizationService);
55 |
56 | $this->assertTrue($helper('member'));
57 | $this->assertTrue($helper(['member']));
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/View/Helper/IsGrantedTest.php:
--------------------------------------------------------------------------------
1 | get('Config');
33 | $this->assertArrayHasKey('view_helpers', $config);
34 | $viewHelpersConfig = $config['view_helpers'];
35 | $this->assertEquals('ZfcRbac\View\Helper\IsGranted', $viewHelpersConfig['aliases']['isGranted']);
36 | $this->assertEquals(
37 | 'ZfcRbac\Factory\IsGrantedViewHelperFactory',
38 | $viewHelpersConfig['factories']['ZfcRbac\View\Helper\IsGranted']
39 | );
40 | }
41 |
42 | public function testCallAuthorizationService()
43 | {
44 | $authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface');
45 |
46 | $authorizationService->expects($this->once())
47 | ->method('isGranted')
48 | ->with('edit')
49 | ->will($this->returnValue(true));
50 |
51 | $helper = new IsGranted($authorizationService);
52 |
53 | $this->assertTrue($helper('edit'));
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/tests/ZfcRbacTest/View/Strategy/UnauthorizedStrategyTest.php:
--------------------------------------------------------------------------------
1 | getMock('Zend\EventManager\EventManagerInterface');
38 | $eventManager->expects($this->once())
39 | ->method('attach')
40 | ->with(MvcEvent::EVENT_DISPATCH_ERROR);
41 |
42 | $strategyListener->attach($eventManager);
43 | }
44 |
45 | public function testFillEvent()
46 | {
47 | $response = new HttpResponse();
48 |
49 | $mvcEvent = new MvcEvent();
50 | $mvcEvent->setParam('exception', new UnauthorizedException());
51 | $mvcEvent->setResponse($response);
52 |
53 | $options = new UnauthorizedStrategyOptions([
54 | 'template' => 'error/403'
55 | ]);
56 |
57 | $unauthorizedStrategy = new UnauthorizedStrategy($options);
58 |
59 | $unauthorizedStrategy->onError($mvcEvent);
60 |
61 | $this->assertEquals(403, $mvcEvent->getResponse()->getStatusCode());
62 | $this->assertInstanceOf('Zend\View\Model\ModelInterface', $mvcEvent->getResult());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/tests/testing.config.php:
--------------------------------------------------------------------------------
1 | [],
21 |
22 | 'doctrine' => [
23 | 'driver' => [
24 | 'application_driver' => [
25 | 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
26 | 'cache' => 'array',
27 | 'paths' => [__DIR__ . '/ZfcRbacTest/Asset']
28 | ],
29 | 'orm_default' => [
30 | 'drivers' => [
31 | 'ZfcRbacTest\Asset' => 'application_driver'
32 | ]
33 | ]
34 | ],
35 |
36 | 'connection' => [
37 | 'orm_default' => [
38 | 'driverClass' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
39 | 'params' => [
40 | 'host' => null,
41 | 'port' => null,
42 | 'user' => null,
43 | 'password' => null,
44 | 'dbname' => 'test',
45 | 'driver' => 'pdo_sqlite',
46 | 'path' => null,
47 | 'memory' => true,
48 | ],
49 | ],
50 | ],
51 | ],
52 | ];
53 |
--------------------------------------------------------------------------------
/view/error/403.phtml:
--------------------------------------------------------------------------------
1 | A 403 error occurred
2 |
3 | You are not allowed to access this resource
4 |
5 | Details
6 |
7 | error) {
11 | case GuardInterface::GUARD_UNAUTHORIZED:
12 | echo 'Request was blocked by a ZfcRbac guard
';
13 | break;
14 | }
15 | ?>
16 |
--------------------------------------------------------------------------------