Skip to content

Commit

Permalink
add ability to set custom container instance
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal authored and ADmad committed Mar 13, 2024
1 parent dda8730 commit c45771c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Middleware/AuthorizationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Authorization\IdentityInterface;
use Authorization\Middleware\UnauthorizedHandler\UnauthorizedHandlerTrait;
use Cake\Core\ContainerApplicationInterface;
use Cake\Core\ContainerInterface;
use Cake\Core\InstanceConfigTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -73,16 +74,25 @@ class AuthorizationMiddleware implements MiddlewareInterface
*/
protected AuthorizationServiceInterface|AuthorizationServiceProviderInterface $subject;

/**
* The container instance from the application
*
* @var \Cake\Core\ContainerInterface|null
*/
protected ?ContainerInterface $container = null;

/**
* Constructor.
*
* @param \Authorization\AuthorizationServiceInterface|\Authorization\AuthorizationServiceProviderInterface $subject Authorization service or provider instance.
* @param array $config Config array.
* @param \Cake\Core\ContainerInterface|null $container The container instance from the application
* @throws \InvalidArgumentException
*/
public function __construct(
AuthorizationServiceInterface|AuthorizationServiceProviderInterface $subject,
array $config = []
array $config = [],
?ContainerInterface $container = null
) {
if ($this->_defaultConfig['identityDecorator'] === null) {
$this->_defaultConfig['identityDecorator'] = interface_exists(AuthenIdentityInterface::class)
Expand All @@ -91,6 +101,7 @@ public function __construct(
}

$this->subject = $subject;
$this->container = $container;
$this->setConfig($config);
}

Expand All @@ -109,6 +120,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
if ($this->subject instanceof ContainerApplicationInterface) {
$container = $this->subject->getContainer();
$container->add(AuthorizationService::class, $service);
} elseif ($this->container) {
$this->container->add(AuthorizationService::class, $service);
}

$attribute = $this->getConfig('identityAttribute');
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Middleware/AuthorizationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Authorization\IdentityDecorator;
use Authorization\IdentityInterface;
use Authorization\Middleware\AuthorizationMiddleware;
use Cake\Core\Container;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
Expand Down Expand Up @@ -290,4 +291,29 @@ public function testMiddlewareInjectsServiceIntoDIC()
$container = $application->getContainer();
$this->assertInstanceOf(AuthorizationService::class, $container->get(AuthorizationService::class));
}

public function testMiddlewareInjectsServiceIntoDICViaCustomContainerInstance()
{
$request = ServerRequestFactory::fromGlobals(
['REQUEST_URI' => '/testpath'],
[],
['username' => 'mariano', 'password' => 'password']
);
$handler = new TestRequestHandler();

$service = $this->createMock(AuthorizationServiceInterface::class);
$provider = $this->createMock(AuthorizationServiceProviderInterface::class);
$provider
->expects($this->once())
->method('getAuthorizationService')
->with($this->isInstanceOf(ServerRequestInterface::class))
->willReturn($service);

$container = new Container();

$middleware = new AuthorizationMiddleware($provider, ['requireAuthorizationCheck' => false], $container);
$middleware->process($request, $handler);

$this->assertEquals($service, $container->get(AuthorizationService::class));
}
}

0 comments on commit c45771c

Please sign in to comment.