| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Ae\FeatureBundle\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ae\FeatureBundle\Entity\Feature; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Security\Core\User\UserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Controls access to a Feature. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author Carlo Forghieri <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class FeatureSecurity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param AuthorizationCheckerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param TokenStorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $providerKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param AuthorizationCheckerInterface $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param TokenStorageInterface         $storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param string                        $providerKey | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 6 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         AuthorizationCheckerInterface $context, | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         TokenStorageInterface $storage, | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         string $providerKey | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 6 |  |         $this->context = $context; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 6 |  |         $this->storage = $storage; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 6 |  |         $this->providerKey = $providerKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param Feature $feature | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 4 |  |     public function isGranted(Feature $feature) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 4 |  |         if (!$feature->isEnabled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |         if ($feature->getRole()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |             if (!$this->context->isGranted($feature->getRole())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |         if ('' !== trim($feature->getParentRole())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |             if (!$this->context->isGranted($feature->getParentRole())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |     public function isGrantedForUser(Feature $feature, UserInterface $user): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |         $oldToken = $this->storage->getToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         $this->storage->setToken(new UsernamePasswordToken( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |             $user, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |             null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |             $this->providerKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |             $user->getRoles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |         $granted = $this->isGranted($feature); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |         $this->storage->setToken($oldToken); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |         return $granted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  |