Complex classes like LDAPUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LDAPUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class LDAPUser extends User |
||
| 5 | { |
||
| 6 | use LDAPCachableObject; |
||
| 7 | use LDAPGettableObject; |
||
| 8 | use LDAPSettableObject; |
||
| 9 | |||
| 10 | private $ldapObj; |
||
| 11 | private $server; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Initialize a LDAPUser object |
||
| 15 | * |
||
| 16 | * @param boolean|array|object $data The data to initialize the LDAPUser with or false for an empty User |
||
| 17 | * |
||
| 18 | * @SuppressWarnings("StaticAccess") |
||
| 19 | */ |
||
| 20 | public function __construct($data = false) |
||
| 25 | |||
| 26 | private function checkChildGroup($array) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $listName The name of the list to search |
||
| 46 | * @param Group $group The group to search inside |
||
| 47 | * @param string $dn The distringuished name to search for |
||
| 48 | */ |
||
| 49 | private function isInListOrChild($listName, $group, $dn) |
||
| 50 | { |
||
| 51 | if(!isset($group[$listName])) |
||
| 52 | { |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | if(in_array($dn, $group[$listName])) |
||
| 56 | { |
||
| 57 | return true; |
||
| 58 | } |
||
| 59 | return $this->checkChildGroup($group[$listName]); |
||
| 60 | } |
||
| 61 | |||
| 62 | private function uidInMemberUid($group, $uid) |
||
| 66 | |||
| 67 | public function isInGroupNamed($name) |
||
| 68 | { |
||
| 69 | $filter = new \Data\Filter('cn eq '.$name); |
||
| 70 | $group = $this->server->read($this->server->group_base, $filter); |
||
| 71 | if(!empty($group)) |
||
| 72 | { |
||
| 73 | $group = $group[0]; |
||
| 74 | $dn = $this->ldapObj->dn; |
||
| 75 | $uid = $this->ldapObj->uid[0]; |
||
| 76 | $ret = $this->isInListOrChild('member', $group, $dn); |
||
| 77 | if($ret === false) |
||
| 78 | { |
||
| 79 | $ret = $this->isInListOrChild('uniquemember', $group, $dn); |
||
| 80 | } |
||
| 81 | if($ret === false && $this->uidInMemberUid($group, $uid)) |
||
| 82 | { |
||
| 83 | return true; |
||
| 84 | } |
||
| 85 | return $ret; |
||
| 86 | } |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | |||
| 90 | protected $valueDefaults = array( |
||
| 91 | 'o' => 'Volunteer' |
||
| 92 | ); |
||
| 93 | |||
| 94 | protected $multiValueProps = array( |
||
| 95 | 'title', |
||
| 96 | 'ou', |
||
| 97 | 'host' |
||
| 98 | ); |
||
| 99 | |||
| 100 | protected $cachedOnlyProps = array( |
||
| 101 | 'uid' |
||
| 102 | ); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Allow write for the user |
||
| 106 | * |
||
| 107 | * @SuppressWarnings("StaticAccess") |
||
| 108 | */ |
||
| 109 | protected function enableReadWrite() |
||
| 119 | |||
| 120 | public function getGroups() |
||
| 138 | |||
| 139 | public function addLoginProvider($provider) |
||
| 143 | |||
| 144 | private function generateLDAPPass($pass) |
||
| 151 | |||
| 152 | public function setPass($password) |
||
| 153 | { |
||
| 154 | if(!is_object($this->ldapObj)) |
||
| 155 | { |
||
| 156 | return $this->setFieldLocal('userPassword', $this->generateLDAPPass($password)); |
||
| 157 | } |
||
| 158 | $obj = array('dn'=>$this->ldapObj->dn); |
||
| 159 | $obj['userPassword'] = $this->generateLDAPPass($password); |
||
| 160 | if(isset($this->ldapObj->uniqueidentifier)) |
||
| 161 | { |
||
| 162 | $obj['uniqueIdentifier'] = null; |
||
| 163 | } |
||
| 164 | //Make sure we are bound in write mode |
||
| 165 | $this->enableReadWrite(); |
||
| 166 | return $this->update($obj); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function validate_password($password) |
||
| 173 | |||
| 174 | public function validate_reset_hash($hash) |
||
| 182 | |||
| 183 | public static function from_name($name, $data) |
||
| 197 | |||
| 198 | public function flushUser() |
||
| 199 | { |
||
| 200 | if(is_object($this->ldapObj)) |
||
| 201 | { |
||
| 202 | //In this mode we are always up to date |
||
| 203 | return true; |
||
| 204 | } |
||
| 205 | $obj = $this->ldapObj; |
||
| 206 | $obj['objectClass'] = array('top', 'inetOrgPerson', 'extensibleObject'); |
||
| 207 | $obj['dn'] = 'uid='.$this->ldapObj['uid'].','.$this->server->user_base; |
||
| 208 | if(!isset($obj['sn'])) |
||
| 209 | { |
||
| 210 | $obj['sn'] = $obj['uid']; |
||
| 211 | } |
||
| 212 | if(!isset($obj['cn'])) |
||
| 213 | { |
||
| 214 | $obj['cn'] = $obj['uid']; |
||
| 215 | } |
||
| 216 | $ret = $this->server->create($obj); |
||
| 217 | return $ret; |
||
| 218 | } |
||
| 219 | |||
| 220 | private function getHashFromUser($ldapObj) |
||
| 228 | |||
| 229 | public function getPasswordResetHash() |
||
| 244 | |||
| 245 | public function delete() |
||
| 251 | } |
||
| 252 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 253 |