Skip to content

Commit

Permalink
Merge branch 'feature/8-http-interop'
Browse files Browse the repository at this point in the history
Close #8
  • Loading branch information
weierophinney committed Oct 9, 2017
2 parents b97e804 + e83e820 commit e06b440
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 23 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.2.0 - 2017-10-09

### Added

- [#8](https://github.com/zendframework/zend-expressive-authorization/pull/8) adds
support for http-interop/http-middleware 0.5.0 via a polyfill provided by the
package webimpress/http-middleware-compatibility. Essentially, this means you
can drop this package into an application targeting either the 0.4.1 or 0.5.0
versions of http-middleware, and it will "just work".

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 0.1.0 - 2017-09-28

Initial release.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"forum": "https://discourse.zendframework.com/c/questions/expressive"
},
"require": {
"http-interop/http-middleware": "^0.4.1",
"php": "^7.1",
"psr/container": "^1.0",
"psr/http-message": "^1.0.1",
"zendframework/zend-expressive-router": "^2.1.0"
"webimpress/http-middleware-compatibility": "^0.1.1",
"zendframework/zend-expressive-router": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^6.0.8",
Expand Down
64 changes: 52 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions src/AuthorizationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

namespace Zend\Expressive\Authorization;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface;
use Webimpress\HttpMiddlewareCompatibility\MiddlewareInterface;

class AuthorizationMiddleware implements ServerMiddlewareInterface
use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

class AuthorizationMiddleware implements MiddlewareInterface
{
/**
* @var AuthorizationInterface
Expand All @@ -34,7 +36,7 @@ public function __construct(AuthorizationInterface $authorization, ResponseInter
* {@inheritDoc}
* @todo Use role/identity interface from zend-expressive-authentication once published.
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, HandlerInterface $handler)
{
$role = $request->getAttribute(AuthorizationInterface::class, false);

Expand All @@ -43,7 +45,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
}

return $this->authorization->isGranted($role, $request)
? $delegate->process($request)
? $handler->{HANDLER_METHOD}($request)
: $this->responsePrototype->withStatus(403);
}
}
9 changes: 5 additions & 4 deletions test/AuthorizationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@

namespace ZendTest\Expressive\Authorization;

use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Webimpress\HttpMiddlewareCompatibility\HandlerInterface;
use Zend\Expressive\Authorization\AuthorizationInterface;
use Zend\Expressive\Authorization\AuthorizationMiddleware;
use Zend\Expressive\Router\RouteResult;

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

class AuthorizationMiddlewareTest extends TestCase
{
protected function setUp()
{
$this->authorization = $this->prophesize(AuthorizationInterface::class);
$this->request = $this->prophesize(ServerRequestInterface::class);
$this->delegate = $this->prophesize(DelegateInterface::class);
$this->delegate = $this->prophesize(HandlerInterface::class);
$this->response = $this->prophesize(ResponseInterface::class);
}

Expand Down Expand Up @@ -69,7 +70,7 @@ public function testProcessRoleGranted()
$this->authorization->isGranted('foo', $this->request->reveal())->willReturn(true);

$middleware = new AuthorizationMiddleware($this->authorization->reveal(), $this->response->reveal());
$this->delegate->process(Argument::any())->willReturn($this->response->reveal());
$this->delegate->{HANDLER_METHOD}(Argument::any())->willReturn($this->response->reveal());

$response = $middleware->process(
$this->request->reveal(),
Expand Down

0 comments on commit e06b440

Please sign in to comment.