Skip to content

Commit 27c9d55

Browse files
authored
Merge pull request #296 from cakephp/2.next
Prepare for 2.5.0
2 parents 94fadea + 96deaab commit 27c9d55

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "cakephp-plugin",
1111
"require": {
1212
"php": ">=7.2",
13-
"cakephp/http": "^4.0",
13+
"cakephp/http": "^4.2",
1414
"psr/http-client": "^1.0",
1515
"psr/http-message": "^1.0",
1616
"psr/http-server-handler": "^1.0",

Diff for: src/Middleware/AuthorizationMiddleware.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Authorization\Middleware;
1818

1919
use Authentication\IdentityInterface as AuthenIdentityInterface;
20+
use Authorization\AuthorizationService;
2021
use Authorization\AuthorizationServiceInterface;
2122
use Authorization\AuthorizationServiceProviderInterface;
2223
use Authorization\Exception\AuthorizationRequiredException;
@@ -25,6 +26,8 @@
2526
use Authorization\IdentityDecorator;
2627
use Authorization\IdentityInterface;
2728
use Authorization\Middleware\UnauthorizedHandler\UnauthorizedHandlerTrait;
29+
use Cake\Core\ContainerApplicationInterface;
30+
use Cake\Core\ContainerInterface;
2831
use Cake\Core\InstanceConfigTrait;
2932
use InvalidArgumentException;
3033
use Psr\Http\Message\ResponseInterface;
@@ -71,15 +74,26 @@ class AuthorizationMiddleware implements MiddlewareInterface
7174
*/
7275
protected $subject;
7376

77+
/**
78+
* The container instance from the application
79+
*
80+
* @var \Cake\Core\ContainerInterface|null
81+
*/
82+
protected $container;
83+
7484
/**
7585
* Constructor.
7686
*
7787
* @param \Authorization\AuthorizationServiceInterface|\Authorization\AuthorizationServiceProviderInterface $subject Authorization service or provider instance.
7888
* @param array $config Config array.
89+
* @param \Cake\Core\ContainerInterface|null $container The container instance from the application
7990
* @throws \InvalidArgumentException
8091
*/
81-
public function __construct($subject, array $config = [])
82-
{
92+
public function __construct(
93+
$subject,
94+
array $config = [],
95+
?ContainerInterface $container = null
96+
) {
8397
/** @psalm-suppress DocblockTypeContradiction */
8498
if (
8599
!$subject instanceof AuthorizationServiceInterface &&
@@ -102,6 +116,7 @@ public function __construct($subject, array $config = [])
102116
}
103117

104118
$this->subject = $subject;
119+
$this->container = $container;
105120
$this->setConfig($config);
106121
}
107122

@@ -117,6 +132,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
117132
$service = $this->getAuthorizationService($request);
118133
$request = $request->withAttribute('authorization', $service);
119134

135+
if ($this->subject instanceof ContainerApplicationInterface) {
136+
$container = $this->subject->getContainer();
137+
$container->add(AuthorizationService::class, $service);
138+
} elseif ($this->container) {
139+
$this->container->add(AuthorizationService::class, $service);
140+
}
141+
120142
$attribute = $this->getConfig('identityAttribute');
121143
$identity = $request->getAttribute($attribute);
122144

Diff for: tests/TestCase/Middleware/AuthorizationMiddlewareTest.php

+46
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@
1616
*/
1717
namespace Authorization\Test\TestCase\Middleware;
1818

19+
use Authorization\AuthorizationService;
1920
use Authorization\AuthorizationServiceInterface;
2021
use Authorization\AuthorizationServiceProviderInterface;
2122
use Authorization\Exception\AuthorizationRequiredException;
2223
use Authorization\Exception\Exception;
2324
use Authorization\IdentityDecorator;
2425
use Authorization\IdentityInterface;
2526
use Authorization\Middleware\AuthorizationMiddleware;
27+
use Cake\Core\Container;
2628
use Cake\Http\Response;
2729
use Cake\Http\ServerRequest;
30+
use Cake\Http\ServerRequestFactory;
2831
use Cake\TestSuite\TestCase;
2932
use InvalidArgumentException;
3033
use Psr\Http\Message\RequestInterface;
3134
use Psr\Http\Message\ResponseInterface;
3235
use Psr\Http\Message\ServerRequestInterface;
3336
use RuntimeException;
3437
use stdClass;
38+
use TestApp\Application;
3539
use TestApp\Http\TestRequestHandler;
3640
use TestApp\Identity;
3741

@@ -282,4 +286,46 @@ public function testUnauthorizedHandlerRequireAuthz()
282286
$result = $middleware->process($request, $handler);
283287
$this->assertSame(200, $result->getStatusCode());
284288
}
289+
290+
public function testMiddlewareInjectsServiceIntoDIC()
291+
{
292+
$request = ServerRequestFactory::fromGlobals(
293+
['REQUEST_URI' => '/testpath'],
294+
[],
295+
['username' => 'mariano', 'password' => 'password']
296+
);
297+
$handler = new TestRequestHandler();
298+
$application = new Application('config');
299+
300+
$middleware = new AuthorizationMiddleware($application, ['requireAuthorizationCheck' => false]);
301+
$middleware->process($request, $handler);
302+
303+
$container = $application->getContainer();
304+
$this->assertInstanceOf(AuthorizationService::class, $container->get(AuthorizationService::class));
305+
}
306+
307+
public function testMiddlewareInjectsServiceIntoDICViaCustomContainerInstance()
308+
{
309+
$request = ServerRequestFactory::fromGlobals(
310+
['REQUEST_URI' => '/testpath'],
311+
[],
312+
['username' => 'mariano', 'password' => 'password']
313+
);
314+
$handler = new TestRequestHandler();
315+
316+
$service = $this->createMock(AuthorizationServiceInterface::class);
317+
$provider = $this->createMock(AuthorizationServiceProviderInterface::class);
318+
$provider
319+
->expects($this->once())
320+
->method('getAuthorizationService')
321+
->with($this->isInstanceOf(ServerRequestInterface::class))
322+
->willReturn($service);
323+
324+
$container = new Container();
325+
326+
$middleware = new AuthorizationMiddleware($provider, ['requireAuthorizationCheck' => false], $container);
327+
$middleware->process($request, $handler);
328+
329+
$this->assertEquals($service, $container->get(AuthorizationService::class));
330+
}
285331
}

Diff for: tests/test_app/TestApp/Application.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33

44
namespace TestApp;
55

6+
use Authorization\AuthorizationService;
7+
use Authorization\AuthorizationServiceInterface;
8+
use Authorization\AuthorizationServiceProviderInterface;
9+
use Authorization\Policy\MapResolver;
610
use Cake\Http\BaseApplication;
711
use Cake\Http\MiddlewareQueue;
812
use Cake\Routing\RouteBuilder;
13+
use Psr\Http\Message\ServerRequestInterface;
14+
use TestApp\Model\Entity\Article;
15+
use TestApp\Policy\ArticlePolicy;
916

10-
class Application extends BaseApplication
17+
class Application extends BaseApplication implements AuthorizationServiceProviderInterface
1118
{
1219
public function middleware(MiddlewareQueue $middleware): MiddlewareQueue
1320
{
@@ -23,4 +30,13 @@ public function bootstrap(): void
2330
$this->addPlugin('Authorization');
2431
$this->addPlugin('Bake');
2532
}
33+
34+
public function getAuthorizationService(ServerRequestInterface $request): AuthorizationServiceInterface
35+
{
36+
$resolver = new MapResolver([
37+
Article::class => ArticlePolicy::class,
38+
]);
39+
40+
return new AuthorizationService($resolver);
41+
}
2642
}

0 commit comments

Comments
 (0)