CompositeAdapter.php
· 2.0 KiB · PHP
Raw
<?php
use Zend\Http\Request;
use Zend\Http\Response;;
use ZF\MvcAuth\Authentication\AdapterInterface;
use ZF\MvcAuth\Identity\IdentityInterface;
use ZF\MvcAuth\MvcAuthEvent;
class CompositeAdapter implements AdapterInterface
{
private $adapters = [];
private $matched;
public function attach(AdapterInterface $adapter)
{
if (in_array($adapter, $this->adapters, true)) {
return;
}
$this->adapters[] = $adapter;
}
public function provides()
{
return array_reduce($this->adapters, function ($carry, $adapter) {
$provides = array_merge($carry, $adapter->provides());
return array_unique($provides);
}, []);
}
public function matches($type)
{
$this->matched = null;
foreach ($this->adapters as $adapter) {
$match = $adapter->matches($type);
if ($match) {
$this->matched = $adapter;
return true;
}
}
return false;
}
public function getTypeFromRequest(Request $request)
{
foreach ($this->adapters as $adapter) {
$type = $adapter->getTypeFromRequest($request);
if (false !== $type) {
return $type;
}
}
return false;
}
public function preAuth(Request $request, Response $response)
{
foreach ($this->adapters as $adapter) {
$result = $adapter->preAuth($request, $response);
if ($result instanceof Response) {
return $result;
}
}
}
public function authenticate(Request $request, Response $response, MvcAuthEvent $mvcAuthEvent)
{
if (! $this->matched) {
return false;
}
// Reset $matched so that subsequent calls fail
$adapter = $this->matched;
$this->matched = null;
return $adapter->authenticate($request, $response, $mvcAuthEvent);
}
}
1 | <?php |
2 | use Zend\Http\Request; |
3 | use Zend\Http\Response;; |
4 | use ZF\MvcAuth\Authentication\AdapterInterface; |
5 | use ZF\MvcAuth\Identity\IdentityInterface; |
6 | use ZF\MvcAuth\MvcAuthEvent; |
7 | |
8 | class CompositeAdapter implements AdapterInterface |
9 | { |
10 | private $adapters = []; |
11 | |
12 | private $matched; |
13 | |
14 | public function attach(AdapterInterface $adapter) |
15 | { |
16 | if (in_array($adapter, $this->adapters, true)) { |
17 | return; |
18 | } |
19 | |
20 | $this->adapters[] = $adapter; |
21 | } |
22 | |
23 | public function provides() |
24 | { |
25 | return array_reduce($this->adapters, function ($carry, $adapter) { |
26 | $provides = array_merge($carry, $adapter->provides()); |
27 | return array_unique($provides); |
28 | }, []); |
29 | } |
30 | |
31 | public function matches($type) |
32 | { |
33 | $this->matched = null; |
34 | |
35 | foreach ($this->adapters as $adapter) { |
36 | $match = $adapter->matches($type); |
37 | if ($match) { |
38 | $this->matched = $adapter; |
39 | return true; |
40 | } |
41 | } |
42 | |
43 | return false; |
44 | } |
45 | |
46 | public function getTypeFromRequest(Request $request) |
47 | { |
48 | foreach ($this->adapters as $adapter) { |
49 | $type = $adapter->getTypeFromRequest($request); |
50 | if (false !== $type) { |
51 | return $type; |
52 | } |
53 | } |
54 | return false; |
55 | } |
56 | |
57 | public function preAuth(Request $request, Response $response) |
58 | { |
59 | foreach ($this->adapters as $adapter) { |
60 | $result = $adapter->preAuth($request, $response); |
61 | if ($result instanceof Response) { |
62 | return $result; |
63 | } |
64 | } |
65 | } |
66 | |
67 | public function authenticate(Request $request, Response $response, MvcAuthEvent $mvcAuthEvent) |
68 | { |
69 | if (! $this->matched) { |
70 | return false; |
71 | } |
72 | |
73 | // Reset $matched so that subsequent calls fail |
74 | $adapter = $this->matched; |
75 | $this->matched = null; |
76 | |
77 | return $adapter->authenticate($request, $response, $mvcAuthEvent); |
78 | } |
79 | } |
CompositeAdapterDelegatorFactory.php
· 984 B · PHP
Raw
<?php
use Zend\ServiceManager\DelegatorFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
/**
* Delegator factory for adding the CompositeAdapter.
*
* This delegator factory assumes that you've registered the CompositeAdapter
* as the service 'CompositeAdapter'.
*
* Attach it to the DefaultAuthenticationListener using:
*
* ```php
* 'service_manager' => array(
* 'delegators' => array(
* 'ZF\MvcAuth\Authentication\DefaultAuthenticationListener' => array(
* 'CompositeAdapterDelegatorFactory',
* ),
* ),
* ),
* ```
*/
class CompositeAdapterDelegatorFactory implements DelegatorFactoryInterface
{
public function createDelegatorWithName(
ServiceLocatorInterface $services,
$name,
$requestedName,
$callback
) {
$listener = $callback();
$composite = $services->get('CompositeAdapter');
$listener->attach($composite);
return $listener;
}
}
1 | <?php |
2 | |
3 | use Zend\ServiceManager\DelegatorFactoryInterface; |
4 | use Zend\ServiceManager\ServiceLocatorInterface; |
5 | |
6 | /** |
7 | * Delegator factory for adding the CompositeAdapter. |
8 | * |
9 | * This delegator factory assumes that you've registered the CompositeAdapter |
10 | * as the service 'CompositeAdapter'. |
11 | * |
12 | * Attach it to the DefaultAuthenticationListener using: |
13 | * |
14 | * ```php |
15 | * 'service_manager' => array( |
16 | * 'delegators' => array( |
17 | * 'ZF\MvcAuth\Authentication\DefaultAuthenticationListener' => array( |
18 | * 'CompositeAdapterDelegatorFactory', |
19 | * ), |
20 | * ), |
21 | * ), |
22 | * ``` |
23 | */ |
24 | class CompositeAdapterDelegatorFactory implements DelegatorFactoryInterface |
25 | { |
26 | public function createDelegatorWithName( |
27 | ServiceLocatorInterface $services, |
28 | $name, |
29 | $requestedName, |
30 | $callback |
31 | ) { |
32 | $listener = $callback(); |
33 | |
34 | $composite = $services->get('CompositeAdapter'); |
35 | $listener->attach($composite); |
36 | return $listener; |
37 | } |
38 | } |
CompositeAdapterFactory.php
· 2.3 KiB · PHP
Raw
<?php
use ZF\MvcAuth\Factory\AuthenticationHttpAdapterFactory;
use ZF\MvcAuth\Factory\AuthenticationOAuth2AdapterFactory;
class CompositeAdapterFactory
{
public function __invoke($services)
{
$config = $services->get('Config');
$adapters = [];
/*
* You could put the key somewhere else. The idea, though, is that each
* named adapter matches a key under zf-mvc-auth.authentication.adapters. The
* primary problem with this approach is that you'll get two instances
* for adapters you attach to the composite adapter. One alternative is
* to use the approach in ZF\MvcAuth\Factory\AuthenticationAdapterDelegatorFactory
* and define the configuration for the attached adapters within this
* array, and instantiate them here only. Of course, that means you can
* only configure those adapters manually.
*/
if (isset($config['zf-mvc-auth']['authentication']['composite'])
&& is_array($config['zf-mvc-auth']['authentication']['composite'])
) {
$adapters = $config['zf-mvc-auth']['authentication']['composite'];
}
$composite = new CompositeAdapter();
foreach ($adapters as $adapterName) {
$adapter = $this->getAdapter(
$adapterName,
$config['zf-mvc-auth']['authentication']['adapters'],
$services
);
if (! $adapter) {
continue;
}
$composite->attach($adapter);
}
return $composite;
}
private function getAdapter($adapterName, array $config, $services)
{
if (! isset $config[$adapterName]
|| ! isset($config[$adapterName]['adapter'])
|| ! is_string($config[$adapterName]['adapter'])
) {
return false;
}
$config = $config[$adapterName];
switch ($config['adapter']) {
case 'ZF\MvcAuth\Authentication\HttpAdapter':
return AuthenticationHttpAdapterFactory::factory($adapterName, $config, $services);
case 'ZF\MvcAuth\Authentication\OAuth2Adapter':
return AuthenticationOAuth2AdapterFactory::factory($adapterName, $config, $services);
default:
return false;
}
}
}
1 | <?php |
2 | use ZF\MvcAuth\Factory\AuthenticationHttpAdapterFactory; |
3 | use ZF\MvcAuth\Factory\AuthenticationOAuth2AdapterFactory; |
4 | |
5 | class CompositeAdapterFactory |
6 | { |
7 | public function __invoke($services) |
8 | { |
9 | $config = $services->get('Config'); |
10 | $adapters = []; |
11 | |
12 | /* |
13 | * You could put the key somewhere else. The idea, though, is that each |
14 | * named adapter matches a key under zf-mvc-auth.authentication.adapters. The |
15 | * primary problem with this approach is that you'll get two instances |
16 | * for adapters you attach to the composite adapter. One alternative is |
17 | * to use the approach in ZF\MvcAuth\Factory\AuthenticationAdapterDelegatorFactory |
18 | * and define the configuration for the attached adapters within this |
19 | * array, and instantiate them here only. Of course, that means you can |
20 | * only configure those adapters manually. |
21 | */ |
22 | if (isset($config['zf-mvc-auth']['authentication']['composite']) |
23 | && is_array($config['zf-mvc-auth']['authentication']['composite']) |
24 | ) { |
25 | $adapters = $config['zf-mvc-auth']['authentication']['composite']; |
26 | } |
27 | |
28 | $composite = new CompositeAdapter(); |
29 | |
30 | foreach ($adapters as $adapterName) { |
31 | $adapter = $this->getAdapter( |
32 | $adapterName, |
33 | $config['zf-mvc-auth']['authentication']['adapters'], |
34 | $services |
35 | ); |
36 | if (! $adapter) { |
37 | continue; |
38 | } |
39 | $composite->attach($adapter); |
40 | } |
41 | |
42 | return $composite; |
43 | } |
44 | |
45 | private function getAdapter($adapterName, array $config, $services) |
46 | { |
47 | if (! isset $config[$adapterName] |
48 | || ! isset($config[$adapterName]['adapter']) |
49 | || ! is_string($config[$adapterName]['adapter']) |
50 | ) { |
51 | return false; |
52 | } |
53 | |
54 | $config = $config[$adapterName]; |
55 | switch ($config['adapter']) { |
56 | case 'ZF\MvcAuth\Authentication\HttpAdapter': |
57 | return AuthenticationHttpAdapterFactory::factory($adapterName, $config, $services); |
58 | case 'ZF\MvcAuth\Authentication\OAuth2Adapter': |
59 | return AuthenticationOAuth2AdapterFactory::factory($adapterName, $config, $services); |
60 | default: |
61 | return false; |
62 | } |
63 | } |
64 | } |