2017 © Pedro Peláez
 

library zf-oauth2-doctrine-permissions-acl

ACL permissions for zf-oauth2-doctrine

image

api-skeletons/zf-oauth2-doctrine-permissions-acl

ACL permissions for zf-oauth2-doctrine

  • Saturday, May 12, 2018
  • by tom_anderson
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3,515 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 52 % Grown

The README.md

OAuth2 Doctrine Permissions ACL

Build Status Gitter Total Downloads, (*1)

Versions

1.x for PHP 5.5 to 7.0. 2.x for PHP 7.1 onward., (*2)

About

This provides ACL for api-skeletons/zf-oauth2-doctrine. This replaces some components of zfcampus/zf-mvc-auth to enable multiple roles per user and auto injecting roles into the ACL., (*3)

This library is specifically for a many to many relationship between Role and User. If you have a one to many relationship where each user may have only one role this library is not for you., (*4)

This library depends on api-skeletons/zf-oauth2-doctrine-identity. Please see that library for implementation details., (*5)

Entity Relationship Diagram, (*6)

Entity Relationship Diagram created with Skipper, (*7)

Installation

Installation of this module uses composer. For composer documentation, please refer to getcomposer.org., (*8)

composer require api-skeletons/zf-oauth2-doctrine-permissions-acl

This will be added to your application's list of modules:, (*9)

'modules' => array(
   ...
   'ZF\OAuth2\Doctrine\Permissions\Acl',
),

The ERD above shows the Doctrine relationship to a Role entity. To fetch Roles for a user the User enitity must implement ZF\OAuth2\Doctrine\Permissions\Acl\Role\ProviderInterface. The Role entity must implement Zend\Permissions\Acl\Role\RoleInterface., (*10)

Roles may have parents. This is optional but the parent relationship is often important in ACL. To create a role hierarchy your Role entity must implement ZF\OAuth2\Doctrine\Permissions\Acl\Role\HierarchicalInterface. This interface also implements Zend\Permissions\Acl\Role\RoleInterface., (*11)

Adding Roles to the ACL

To copy roles into the ACL from your Role entity copy config/oauth2.doctrine.permisisons.acl.global.php.dist to your application config/autoload/oauth2.doctrine.permisisons.acl.global.php, (*12)

'zf-oauth2-doctrine-permissions-acl' => [
    'role' => [
        'entity' => 'Db\Entity\Role',
        'object_manager' => 'doctrine.entitymanager.orm_default',
    ],
],

This will run at priority 1000 in the MvcAuthEvent::EVENT_AUTHORIZATION event. If you do not want to autoload roles remove the 'role' configuration entirely., (*13)

Adding Resource Guards

With all of the above this library has set the stage to create permissions on your resources. All your roles may be loaded and you can follow the official Apigility guide: https://apigility.org/documentation/recipes/how-do-i-customize-authorization-for-a-particular-identity Be sure your listener(s) run at priority < 1000., (*14)

This is a short summary of the linked article., (*15)

Add this bootstrap to your Module:, (*16)

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
        );
    }
}

Create your AuthorizationListener:, (*17)

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');
    }
}

Overriding the IS_AUTHORIZED event

An event manager on the AclAuthorization allows you to override any ACL call. For instance if you have another entity which requires permissions based in its value you can add new Roles to your ACL manually then create an override when the authorization is checked to allow for those other entity values now proxied as roles:, (*18)

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
);

The Versions