Skip to content

Commit

Permalink
Merge pull request #1 from prolic/master
Browse files Browse the repository at this point in the history
Implementation, tests, and docs
  • Loading branch information
codeliner committed Sep 13, 2015
2 parents fc91823 + 72b0ad6 commit 2eac96c
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 5 deletions.
38 changes: 38 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
$finder = Symfony\CS\Finder\DefaultFinder::create()
->in('src')
->in('tests');
$config = Symfony\CS\Config\Config::create();
$config->level(null);
$config->fixers(
array(
'braces',
'duplicate_semicolon',
'elseif',
'empty_return',
'encoding',
'eof_ending',
'function_call_space',
'function_declaration',
'indentation',
'join_function',
'line_after_namespace',
'linefeed',
'lowercase_keywords',
'parenthesis',
'multiple_use',
'method_argument_space',
'object_operator',
'php_closing_tag',
'remove_lines_between_uses',
'short_array_syntax',
'short_tag',
'standardize_not_equal',
'trailing_spaces',
'unused_use',
'visibility',
'whitespacy_lines',
)
);
$config->finder($finder);
return $config;
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
# service-bus-zfc-rbac-bridge

Marry Service Bus with ZfcRbac

[![Build Status](https://travis-ci.org/prooph/service-bus-zfc-rbac-bridge.svg)](https://travis-ci.org/prooph/service-bus-zfc-rbac-bridge)
[![Coverage Status](https://coveralls.io/repos/prooph/service-bus-zfc-rbac-bridge/badge.svg?branch=master&service=github)](https://coveralls.io/github/prooph/service-bus-zfc-rbac-bridge?branch=master)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/prooph/improoph)


# Installation

1. Add `"prooph/service-bus-zfc-rbac-bridge": "~1.0"` as requirement to your composer.json.
2. In the `config` folder you will find a [configuration skeleton](config/services.php). The configuration is a simple PHP array flavored with some comments to help you understand the structure.


# Requirements

1. Your Inversion of Control container must implement the [interop-container interface](https://github.com/container-interop/container-interop).
2. ZfcRbac's authorization service should be registered in the container under the `ZfcRbac\Service\AuthorizationService` key.

*Note: Don't worry, if your environment doesn't provide the requirements. You can
always bootstrap the authorization service by hand. Just look at the factories for inspiration in this case.*


# Sample

Assuming a TestQuery with message name `test` and you want to use the route guard and finalize guard together with an assertion (TestAssertion), your config should look like this:

return [
'prooph' => [
'service_bus' => [
'query_bus' => [
'plugins' => [
\Prooph\ServiceBus\RouteGuard::class,
\Prooph\ServiceBus\FinalizeGuard::class,
]
]
]
],
'zfc_rbac' => [
'assertion_manager' => [
'TestAssertion' => 'TestAssertion',
],
'assertion_map' => [
'test' => 'TestAssertion'
],
'role_provider' => [
'ZfcRbac\Role\InMemoryRoleProvider' => [
'user' => [
'permissions' => [
'test'
]
]
]
]
]
];

And your TestAssertion should look like this:

class TestAssertion implements \ZfcRbac\Assertion\AssertionInterface
{
public function assert(AuthorizationService $authorizationService, $context = null)
{
// return true, if no context present, otherwise your route guard will always fail, because the result is not yet known.
if (null === $context) {
return true;
}

return ($context['owner'] == $authorizationService->getIdentity());
}
}

# Support

- Ask questions on [prooph-users](https://groups.google.com/forum/?hl=de#!forum/prooph) google group.
Expand Down
23 changes: 18 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prooph/service-bus-zfc-rbac-bridge",
"description": "",
"description": "Marry Service Bus with ZfcRbac",
"homepage": "https://github.com/prooph/service-bus-zfc-rbac-bridge",
"license": "BSD-3-Clause",
"authors": [
Expand All @@ -17,15 +17,28 @@
"keywords": [
"prooph",
"CQRS",
"messaging"
"messaging",
"rbac"
],
"require": {
"php": ">=5.5"
"php": ">=5.5",
"prooph/service-bus": "dev-develop",
"zf-commons/zfc-rbac": "^2.5.2",
"zfr/rbac": "^1.2"
},
"require-dev": {
"prooph/service-bus": "dev-master",
"phpunit/phpunit": "~4.7",
"phpunit/phpunit": "~4.8",
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {
"Prooph\\ServiceBusZfcRbacBridge\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Prooph\\ServiceBusZfcRbacBridgeTest\\": "tests/"
}
}
}
18 changes: 18 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 09/13/15 - 20:18
*/

namespace Prooph\ServiceBusZfcRbacBridge;

return [
'factories' => [
'\Prooph\ServiceBus\Plugin\Guard\AuthorizationService' => Container\AuthorizationServiceFactory::class
]
];
23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="false"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
>
<testsuite name="Prooph Service Bus Zfc Rbac Bridge Test Suite">
<directory>./tests</directory>
</testsuite>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
47 changes: 47 additions & 0 deletions src/AuthorizationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 09/13/15 - 19:57
*/

namespace Prooph\ServiceBusZfcRbacBridge;

use Prooph\ServiceBus\Plugin\Guard\AuthorizationService as AuthorizationServiceInterface;
use ZfcRbac\Service\AuthorizationServiceInterface as ZfcRbacAuthorizationServiceInterface;

/**
* Class AuthorizationService
* @package Prooph\ServiceBusZfcRbacBridge
*/
final class AuthorizationService implements AuthorizationServiceInterface
{
/**
* @var ZfcRbacAuthorizationServiceInterface
*/
private $zfcRbacAuthorizationService;

/**
* @param ZfcRbacAuthorizationServiceInterface $zfcRbacAuthorizationService
*/
public function __construct(ZfcRbacAuthorizationServiceInterface $zfcRbacAuthorizationService)
{
$this->zfcRbacAuthorizationService = $zfcRbacAuthorizationService;
}

/**
* Check if the permission is granted to the current identity
*
* @param string $messageName
* @param mixed $context
* @return bool
*/
public function isGranted($messageName, $context = null)
{
return $this->zfcRbacAuthorizationService->isGranted($messageName, $context);
}
}
33 changes: 33 additions & 0 deletions src/Container/AuthorizationServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 09/13/15 - 20:00
*/

namespace Prooph\ServiceBusZfcRbacBridge\Container;

use Interop\Container\ContainerInterface;
use Prooph\ServiceBusZfcRbacBridge\AuthorizationService;

/**
* Class AuthorizationServiceFactory
* @package Prooph\ServiceBusZfcRbacBridge\Container
*/
final class AuthorizationServiceFactory
{
/**
* @param ContainerInterface $container
* @return AuthorizationService
*/
public function __invoke(ContainerInterface $container)
{
$zfcRbacAuthorizationService = $container->get('ZfcRbac\Service\AuthorizationService');

return new AuthorizationService($zfcRbacAuthorizationService);
}
}
36 changes: 36 additions & 0 deletions tests/AuthorizationServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 09/13/15 - 20:16
*/

namespace Prooph\ServiceBusZfcRbacBridgeTest;

use PHPUnit_Framework_TestCase as TestCase;
use Prooph\ServiceBusZfcRbacBridge\AuthorizationService;
use ZfcRbac\Service\AuthorizationServiceInterface;

/**
* Class AuthorizationServiceTest
* @package Prooph\ServiceBusZfcRbacBridgeTest
*/
final class AuthorizationServiceTest extends TestCase
{
/**
* @test
*/
public function it_delegates_to_zfc_rbac_authorization_service()
{
$zfcRbacAuthorizationService = $this->prophesize(AuthorizationServiceInterface::class);
$zfcRbacAuthorizationService->isGranted('foo', 'bar')->willReturn(true);

$authorizationService = new AuthorizationService($zfcRbacAuthorizationService->reveal());

$this->assertTrue($authorizationService->isGranted('foo', 'bar'));
}
}
41 changes: 41 additions & 0 deletions tests/Container/AuthorizationServiceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 09/13/15 - 20:14
*/

namespace Prooph\ServiceBusZfcRbacBridge\Container;

use Interop\Container\ContainerInterface;
use PHPUnit_Framework_TestCase as TestCase;
use Prooph\ServiceBusZfcRbacBridge\AuthorizationService;
use ZfcRbac\Service\AuthorizationServiceInterface as ZfcRbacAuthorizationService;

/**
* Class AuthorizationServiceFactoryTest
* @package Prooph\ServiceBusZfcRbacBridge\Container
*/
final class AuthorizationServiceFactoryTest extends TestCase
{
/**
* @test
*/
public function it_creates_authorization_service()
{
$zfcRbacAuthorizationService = $this->prophesize(ZfcRbacAuthorizationService::class);

$container = $this->prophesize(ContainerInterface::class);
$container->get('ZfcRbac\Service\AuthorizationService')->willReturn($zfcRbacAuthorizationService->reveal());

$factory = new AuthorizationServiceFactory();

$authorizationService = $factory($container->reveal());

$this->assertInstanceOf(AuthorizationService::class, $authorizationService);
}
}

0 comments on commit 2eac96c

Please sign in to comment.