| 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 ArrayIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Phauthentic\Authorization\Policy\Exception\MissingPolicyException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * `ResolverCollection` is used for aggregating multiple resolvers when more than | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * one resolver is necessary. The collection will iterate over configured resolvers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * and try to resolve a policy on each one. The first successfully resolved policy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * will be returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Configured resolvers must throw `Authorization\Policy\Exception\MissingPolicyException` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * if a policy cannot be resolved. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * Example configuration: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * $collection = new ResolverCollection([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  *     new OrmResolver(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  *     new MapResolver([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  *         Service::class => ServicePolicy::class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  *     ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * $service = new AuthenticationService($collection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * ``` | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | class ResolverCollection implements ResolverInterface, ResolverCollectionInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * Policy resolver instances. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @var \Phauthentic\Authorization\Policy\ResolverInterface[] | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     protected $resolvers = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * Constructor. Takes an array of policy resolver instances. | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @param \Phauthentic\Authorization\Policy\ResolverInterface[] $resolvers An array of policy resolver instances. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 2 |  |     public function __construct(array $resolvers = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 2 |  |         foreach ($resolvers as $resolver) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 2 |  |             $this->add($resolver); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * Adds a resolver to the collection. | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @param \Phauthentic\Authorization\Policy\ResolverInterface $resolver Resolver instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @return $this | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 2 |  |     public function add(ResolverInterface $resolver): ResolverCollectionInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 2 |  |         $this->resolvers[] = $resolver; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 2 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     public function count(): int | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         return count($this->resolvers); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * Gets the iterator | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @return \Iterator | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |     public function getIterator() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 | 2 |  |         return new ArrayIterator($this->resolvers); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function getPolicy($resource) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         foreach ($this->resolvers as $resolver) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 return $resolver->getPolicy($resource); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } catch (MissingPolicyException $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $exception = new MissingPolicyException(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |         throw $exception->setMessageVars([get_class($resource)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |