| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\ActiveDirectory\Tests\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Zend\Authentication\Result as AuthenticationResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\ActiveDirectory\Model\LDAPGateway; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Dev\TestOnly; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Zend\Ldap\Ldap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package activedirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class LDAPFakeGateway extends LDAPGateway implements TestOnly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         // do nothing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private static $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         'groups' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'CN=Users,DC=playpen,DC=local' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                 ['dn' => 'CN=Group1,CN=Users,DC=playpen,DC=local'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                 ['dn' => 'CN=Group2,CN=Users,DC=playpen,DC=local'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                 ['dn' => 'CN=Group3,CN=Users,DC=playpen,DC=local'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 ['dn' => 'CN=Group4,CN=Users,DC=playpen,DC=local'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 ['dn' => 'CN=Group5,CN=Users,DC=playpen,DC=local'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'CN=Others,DC=playpen,DC=local' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 ['dn' => 'CN=Group6,CN=Others,DC=playpen,DC=local'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 ['dn' => 'CN=Group7,CN=Others,DC=playpen,DC=local'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 ['dn' => 'CN=Group8,CN=Others,DC=playpen,DC=local'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         'users' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             '123' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 'distinguishedname' => 'CN=Joe,DC=playpen,DC=local', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 'objectguid' => '123', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 'cn' => 'jbloggs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 'useraccountcontrol' => '1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 'givenname' => 'Joe', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 'sn' => 'Bloggs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 'mail' => '[email protected]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'password' => 'mockPassword', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'canonicalName'=>'mockCanonicalName', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'userprincipalname' => '[email protected]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'samaccountname' => 'joe' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function authenticate($username, $password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $messages = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if (!$user = $this->getUserByEmail($username)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $messages[0] = 'Username not found'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $code = AuthenticationResult::FAILURE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return new AuthenticationResult($code, $username, $messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if ($user[0]['password'] == $password) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $messages[0] = 'OK'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             return new AuthenticationResult(AuthenticationResult::SUCCESS, $username, $messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $messages[0] = 'Password doesn\'t match'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return new AuthenticationResult(AuthenticationResult::FAILURE, $username, $messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function getNodes($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function getGroups($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if (isset($baseDn)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             return !empty(self::$data['groups'][$baseDn]) ? self::$data['groups'][$baseDn] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function getNestedGroups($dn, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function getGroupByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function getUsers($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function getUserByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return [self::$data['users'][$guid]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function update($dn, array $attributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function delete($dn, $recursively = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     public function move($fromDn, $toDn, $recursively = false) | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function add($dn, array $attributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function search($filter, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $records = self::$data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $results = []; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 120 |  | View Code Duplication |         foreach ($records as $record) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             foreach ($record as $attribute => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 // if the value is an array with a single value, e.g. 'samaccountname' => array(0 => 'myusername') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 // then make sure it's just set in the results as 'samaccountname' => 'myusername' so that it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 // can be used directly by ArrayData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 if (is_array($value) && count($value) == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     $value = $value[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 // ObjectGUID and ObjectSID attributes are in binary, we need to convert those to strings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 if ($attribute == 'objectguid') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     $value = LDAPUtil::bin_to_str_guid($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 if ($attribute == 'objectsid') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     $value = LDAPUtil::bin_to_str_sid($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 $record[$attribute] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $results[] = $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return $results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * Mock to search trough dummy $data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param string $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param null $baseDn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param int $scope | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @param array $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 155 |  | View Code Duplication |     public function getUserByEmail($email, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         foreach (self::$data['users'] as $guid => $info) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             if ($info['mail'] == $email) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 $result[] = $info; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * Mock to search trough dummy $data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @param string $username | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @param null $baseDn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @param int $scope | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @param array $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @internal param string $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 178 |  | View Code Duplication |     public function getUserByUsername($username, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         foreach (self::$data['users'] as $guid => $info) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             if ($info['userprincipalname'] == $username) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |                 $result[] = $info; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.