| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Kaliop\IdentityManagementBundle\Adapter\LDAP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Kaliop\IdentityManagementBundle\Security\User\RemoteUser as BaseRemoteUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * A 'generic' LDAP Remote user class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Since this is not a service, we allow all config to be set in the code creating instances of this (i.e. the Client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class RemoteUser extends BaseRemoteUser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $emailField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @param array $authUserResult (nested array) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param string $emailField the name of the ldap attribute which holds the user email address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param string $login | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param string $password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @todo decide what to store of $AuthUserResult, so that it can be serialized without taking up too much space | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      *       (otoh maybe this never gets serialized, and only the eZ-mvc-user does? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      *       Note that the list of attributes gotten from ladp is decided by settings for the client class... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @todo store the password salted and encrypted in memory instead of plaintext | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct($authUserResult, $emailField, $login, $password='') | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $this->username = $login; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->password = $password; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $this->emailField = $emailField; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $this->profile = $this->ldap2array($authUserResult); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * SF roles. Important: not to have this empty, otherwise SF will think this user is not an authenticated one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function getRoles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return array('ROLE_USER'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function getEmail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (!isset($this->profile[$this->emailField])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             throw new \RuntimeException("User account misses or has empty email (from ldap profile field '{$this->emailField}')"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this->profile[$this->emailField]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Add typehint :-) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function getProfile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return parent::getProfile(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Transforms the data received from an LDAP query into a more 'normal' php array by removing redundant stuff. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * NB: assumes a well-formed array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @todo return a stdclass object instead ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     protected function ldap2array($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         //return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         foreach($data as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if ($key === 'dn') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             if (is_int($key) || $key === 'count') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 unset($data[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             if ($value['count'] === 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $data[$key] = $value[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             if ($value['count'] > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 unset($data[$key]['count']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |