| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Class AuthenticatorService | AuthenticatorService.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @package Faulancer\Service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author  Florian Knapp <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Faulancer\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Faulancer\Controller\AbstractController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Faulancer\ORM\User\Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Faulancer\Security\Crypt; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Faulancer\ServiceLocator\ServiceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class AuthenticatorService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class AuthenticatorService implements ServiceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var AbstractController */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var DbService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $orm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var Config */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $redirectAfterAuth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Authenticator constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param AbstractController $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param Config             $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function __construct(AbstractController $controller, Config $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->controller = $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->config     = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param Entity $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param bool   $shouldBeActive | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function loginUser(Entity $user, $shouldBeActive) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         /** @var Entity $userData */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $userData = $this->controller | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ->getDb() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ->fetch(get_class($user)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ->where('login', '=', $user->login) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ->orWhere('email', '=', $user->login) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ->one(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (empty($userData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->controller->setFlashMessage('error.login', 'invalid_username_or_password'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return $this->redirectToAuthentication(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if ($shouldBeActive && $userData->active !== 1) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $this->controller->setFlashMessage('error.active', 'user_is_not_activated'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return $this->redirectToAuthentication(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $passOk = Crypt::verifyPassword($user->password, $userData->password); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($passOk && $userData instanceof Entity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->saveUserInSession($userData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if ($userData->roles[0]->roleName === 'registered') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 return $this->controller->redirect($this->controller->route('user')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 return $this->controller->redirect($this->controller->route('admin')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->controller->setFlashMessage('error.login', 'invalid_username_or_password'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $this->redirectToAuthentication(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 91 |  | View Code Duplication |     public function redirectToAccessDeniedPage() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         /** @var Config $config */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $config  = $this->controller->getServiceLocator()->get(Config::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $authUrl = $config->get('auth:authUrl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $this->controller->redirect($authUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 103 |  | View Code Duplication |     public function redirectToAuthentication() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         /** @var Config $config */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $config  = $this->controller->getServiceLocator()->get(Config::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $authUrl = $config->get('auth:authUrl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return $this->controller->redirect($authUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param array $roles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     public function isPermitted(array $roles) | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         /** @var Entity $user */ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         $user = $this->getUserFromSession(); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         if (!$user instanceof Entity) { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         foreach ($user->roles as $userRole) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             if (in_array($userRole->roleName, $roles, true)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |                 return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param Entity $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function saveUserInSession(Entity $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $this->controller->getSessionManager()->set('user', $user->id); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param string $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @return Entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @codeCoverageIgnore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     public function getUserFromSession(string $entity = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $id = $this->controller->getSessionManager()->get('user'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         if (empty($id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         /** @var Entity $user */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         if (!empty($entity)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             $user = $this->controller->getDb()->fetch($entity, $id); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             $user = $this->controller->getDb()->fetch(Entity::class, $id); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         return $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 168 |  |  | } | 
            
                        
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: