Commit 7e3bc0a6 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Merge branch 'zf-3.x'

parents b9219f74 790c5fdd
Loading
Loading
Loading
Loading
+10 −10
+2136 −1891

File changed.

Preview size limit exceeded, changes collapsed.

+63 −45
Original line number Diff line number Diff line
<?php
/**
 * BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
 *
 * @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
 * @license http://framework.zend.com/license/new-bsd New BSD License
 */

return array(
    'bjyauthorize' => array(

use Interop\Container\ContainerInterface;

return [
    'bjyauthorize' => [
        // default role for unauthenticated users
        'default_role'          => 'guest',

@@ -20,18 +16,18 @@ return array(

        // Role providers to be used to load all available roles into Zend\Permissions\Acl\Acl
        // Keys are the provider service names, values are the options to be passed to the provider
        'role_providers'        => array(),
        'role_providers'        => [],

        // Resource providers to be used to load all available resources into Zend\Permissions\Acl\Acl
        // Keys are the provider service names, values are the options to be passed to the provider
        'resource_providers'    => array(),
        'resource_providers'    => [],

        // Rule providers to be used to load all available rules into Zend\Permissions\Acl\Acl
        // Keys are the provider service names, values are the options to be passed to the provider
        'rule_providers'        => array(),
        'rule_providers'        => [],

        // Guard listeners to be attached to the application event manager
        'guards'                => array(),
        'guards'                => [],

        // strategy service name for the strategy listener to be used when permission-related errors are detected
        'unauthorized_strategy' => 'BjyAuthorize\View\UnauthorizedStrategy',
@@ -40,21 +36,21 @@ return array(
        'template'              => 'error/403',

        // cache options have to be compatible with Zend\Cache\StorageFactory::factory
        'cache_options'         => array(
            'adapter'   => array(
        'cache_options'         => [
            'adapter'   => [
                'name' => 'memory',
            ),
            'plugins'   => array(
            ],
            'plugins'   => [
                'serializer',
            )
        ),
            ]
        ],

        // Key used by the cache for caching the acl
        'cache_key'             => 'bjyauthorize_acl'
    ),
    ],

    'service_manager' => array(
        'factories' => array(
    'service_manager' => [
        'factories' => [
            'BjyAuthorize\Cache'                    => 'BjyAuthorize\Service\CacheFactory',
            'BjyAuthorize\CacheKeyGenerator'        => 'BjyAuthorize\Service\CacheKeyGeneratorFactory',
            'BjyAuthorize\Config'                   => 'BjyAuthorize\Service\ConfigServiceFactory',
@@ -81,37 +77,59 @@ return array(
            'BjyAuthorize\View\UnauthorizedStrategy'
                => 'BjyAuthorize\Service\UnauthorizedStrategyServiceFactory',
            'BjyAuthorize\Service\RoleDbTableGateway' => 'BjyAuthorize\Service\UserRoleServiceFactory',
        ),
        'invokables'  => array(
        ],
        'invokables'  => [
            'BjyAuthorize\View\RedirectionStrategy' => 'BjyAuthorize\View\RedirectionStrategy',
        ),
        'aliases'     => array(
        ],
        'aliases'     => [
            'bjyauthorize_zend_db_adapter' => 'Zend\Db\Adapter\Adapter',
        ),
        'initializers' => array(
        ],
        'initializers' => [
            'BjyAuthorize\Service\AuthorizeAwareServiceInitializer'
                => 'BjyAuthorize\Service\AuthorizeAwareServiceInitializer'
        ),
    ),
        ],
    ],

    'controller_plugins' => [
        'factories' => [
            'isAllowed' => function (ContainerInterface $container) {
                /* @var $authorize \BjyAuthorize\Service\Authorize */
                $authorize = $container->get('BjyAuthorize\Service\Authorize');

    'view_manager' => array(
        'template_map' => array(
                return new BjyAuthorize\Controller\Plugin\IsAllowed($authorize);
            }
        ],
    ],

    'view_manager' => [
        'template_map' => [
            'error/403' => __DIR__ . '/../view/error/403.phtml',
            'zend-developer-tools/toolbar/bjy-authorize-role'
                => __DIR__ . '/../view/zend-developer-tools/toolbar/bjy-authorize-role.phtml',
        ),
    ),
        ],
    ],

    'view_helpers'    => [
        'factories' => [
            'isAllowed' => function (ContainerInterface $container) {
                /* @var $authorize \BjyAuthorize\Service\Authorize */
                $authorize = $container->get('BjyAuthorize\Service\Authorize');

                return new \BjyAuthorize\View\Helper\IsAllowed($authorize);
            }
        ],
    ],

    'zenddevelopertools' => array(
        'profiler' => array(
            'collectors' => array(
    'zenddevelopertools' => [
        'profiler' => [
            'collectors' => [
                'bjy_authorize_role_collector' => 'BjyAuthorize\\Collector\\RoleCollector',
            ),
        ),
        'toolbar' => array(
            'entries' => array(
            ],
        ],
        'toolbar' => [
            'entries' => [
                'bjy_authorize_role_collector' => 'zend-developer-tools/toolbar/bjy-authorize-role',
            ),
        ),
    ),
);
            ],
        ],
    ],
];
+39 −12
Original line number Diff line number Diff line
<?php
/**
 * BjyAuthorize Module (https://github.com/bjyoungblood/BjyAuthorize)
 *
 * @link https://github.com/bjyoungblood/BjyAuthorize for the canonical source repository
 * @license http://framework.zend.com/license/new-bsd New BSD License
 */

namespace BjyAuthorize\Guard;

@@ -30,25 +24,58 @@ abstract class AbstractGuard extends AbstractListenerAggregate implements

    /**
     *
     * @param array                   $rules
     * @param array                   $config
     * @param ServiceLocatorInterface $serviceLocator
     */
    public function __construct(array $rules, ServiceLocatorInterface $serviceLocator)
    public function __construct(array $config, ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;

        foreach ($rules as $rule) {
        $this->setConfig($config);
    }

    /**
     * @var array
     */
    protected $config = [];

    /**
     * @param array $config
     */
    public function setConfig(array $config)
    {
        $this->config = $config;
    }

    /**
     * @return void
     */
    public function processConfig()
    {
        $this->rules = $this->makeRules();
    }

    /**
     * @return array
     */
    protected function makeRules()
    {
        $rules = [];

        foreach ($this->config as $rule) {
            $rule['roles']  = (array) $rule['roles'];
            $rule['action'] = isset($rule['action']) ? (array) $rule['action'] : array(null);

            foreach ($this->extractResourcesFromRule($rule) as $resource) {
                $this->rules[$resource] = array('roles' => (array) $rule['roles']);
                $rules[$resource] = array('roles' => (array) $rule['roles']);

                if (isset($rule['assertion'])) {
                    $this->rules[$resource]['assertion'] = $rule['assertion'];
                    $rules[$resource]['assertion'] = $rule['assertion'];
                }
            }
        }

        return $rules;
    }

    abstract protected function extractResourcesFromRule(array $rule);
@@ -75,7 +102,7 @@ abstract class AbstractGuard extends AbstractListenerAggregate implements
        $rules = array();
        foreach ($this->rules as $resource => $ruleData) {
            $rule   = array();
            $rule[] = $ruleData['roles'];
            $rule[] = (array) ($ruleData['roles'] ?? null);
            $rule[] = $resource;

            if (isset($ruleData['assertion'])) {
+4 −2
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ class Controller extends AbstractGuard
    /**
     * {@inheritDoc}
     */
    public function attach(EventManagerInterface $events)
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, array($this, 'onDispatch'), -1000);
    }
@@ -102,6 +102,8 @@ class Controller extends AbstractGuard

        /* @var $app \Zend\Mvc\ApplicationInterface */
        $app = $event->getTarget();
        $app->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);

        $event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
        $app->getEventManager()->triggerEvent($event);
    }
}
Loading