prooph / service-bus-zfc-rbac-bridge

Marry Service Bus with ZfcRbac
http://getprooph.org
BSD 3-Clause "New" or "Revised" License
7 stars 6 forks source link

Provide compatibility with zfc-rbac next version #7

Open basz opened 8 years ago

basz commented 8 years ago

Note: Next version is still a PR

I've been integrating that within my prooph application and needed to slightly change this module.

Most important change is that zfc-common doesn't contain references to the identity anymore. The concept of an identity provider has been removed. It is therefore up to this plugin to get the identity and provide it as an argument to the isGranted method.

For now I have chosen to use AuthenticationService as a melanism for that.

What would we need to do to provide compatibility for both versions?

This becomes.

<?php

declare(strict_types = 1);

namespace Prooph\ServiceBusZfcRbacBridge;

use Prooph\ServiceBus\Plugin\Guard\AuthorizationService;
use Zend\Authentication\AuthenticationServiceInterface;
use ZfcRbac\Identity\IdentityInterface;
use ZfcRbac\Service\AuthorizationServiceInterface;

/**
 * Class ZfcRbacAuthorizationServiceBridge
 *
 * @package Prooph\ServiceBusZfcRbacBridge
 */
final class ZfcRbacAuthorizationServiceBridge implements AuthorizationService
{
    /**
     * @var AuthenticationServiceInterface
     */
    private authenticationService;

    /**
     * @var AuthorizationServiceInterface
     */
    private $authorizationService;

    /**
     * ZfcRbacAuthorizationServiceBridge constructor.
     *
     * @param AuthenticationServiceInterface $authenticationService
     * @param AuthorizationServiceInterface  $authorizationService
     */
    public function __construct(
        AuthenticationServiceInterface $authenticationService,
        AuthorizationServiceInterface $authorizationService
    ) {
        $this->authenticationService = $authenticationService;
        $this->authorizationService  = $authorizationService;
    }

    /**
     * Check if the permission is granted to the current identity
     *
     * @param string $messageName
     * @param mixed  $context
     * @return bool
     */
    public function isGranted($messageName, $context = null)
    {
        $identity = null;

        if ($this->authenticationService->hasIdentity()) {
            /** @var IdentityInterface $identity */
            $identity = $this->authenticationService->getIdentity();
        }

        return $this->authorizationService->isGranted($identity, $messageName, $context);
    }
}

Should the factory return based on some difference? I know ZfcRbac\Service\AuthorizationServiceInterface will be new for example.

prolic commented 8 years ago

factory return based on some difference sounds good for me.