1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
5
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
12
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
15
|
|
|
*/ |
16
|
|
|
namespace Phauthentic\Authorization\Policy; |
17
|
|
|
|
18
|
|
|
use Phauthentic\Authorization\Policy\Exception\MissingPolicyException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* `ResolverCollection` is used for aggregating multiple resolvers when more than |
22
|
|
|
* one resolver is necessary. The collection will iterate over configured resolvers |
23
|
|
|
* and try to resolve a policy on each one. The first successfully resolved policy |
24
|
|
|
* will be returned. |
25
|
|
|
* |
26
|
|
|
* Configured resolvers must throw `Authorization\Policy\Exception\MissingPolicyException` |
27
|
|
|
* if a policy cannot be resolved. |
28
|
|
|
* |
29
|
|
|
* Example configuration: |
30
|
|
|
* |
31
|
|
|
* ``` |
32
|
|
|
* $collection = new ResolverCollection([ |
33
|
|
|
* new OrmResolver(), |
34
|
|
|
* new MapResolver([ |
35
|
|
|
* Service::class => ServicePolicy::class |
36
|
|
|
* ]) |
37
|
|
|
* ]); |
38
|
|
|
* |
39
|
|
|
* $service = new AuthenticationService($collection); |
40
|
|
|
* ``` |
41
|
|
|
*/ |
42
|
|
|
class CollectionResolver implements ResolverInterface |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* Policy resolver instances. |
46
|
|
|
* |
47
|
|
|
* @var \Phauthentic\Authorization\Policy\ResolverInterface[] |
48
|
|
|
*/ |
49
|
|
|
protected $collection; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructor. Takes an array of policy resolver instances. |
53
|
|
|
* |
54
|
|
|
* @param \Phauthentic\Authorization\Policy\ResolverInterface[] $resolvers An array of policy resolver instances. |
55
|
|
|
*/ |
56
|
1 |
|
public function __construct(ResolverCollection $collection) |
57
|
|
|
{ |
58
|
1 |
|
$this->collection = $collection; |
|
|
|
|
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function getPolicy($resource) |
65
|
|
|
{ |
66
|
1 |
|
foreach ($this->collection as $resolver) { |
67
|
|
|
try { |
68
|
1 |
|
return $resolver->getPolicy($resource); |
69
|
1 |
|
} catch (MissingPolicyException $e) { |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$class = get_class($resource); |
74
|
|
|
$message = sprintf('Policy for `%s` has not been defined.', $class); |
75
|
|
|
|
76
|
|
|
throw new MissingPolicyException($message); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..