Complex classes like LdapUserProvider 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 LdapUserProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class LdapUserProvider implements UserProviderInterface |
||
31 | { |
||
32 | /** |
||
33 | * The base LdapUser class instantiated by this user provider. |
||
34 | */ |
||
35 | const BASE_USER_CLASS = '\LdapTools\Bundle\LdapToolsBundle\Security\User\LdapUser'; |
||
36 | |||
37 | /** |
||
38 | * @var LdapManager |
||
39 | */ |
||
40 | protected $ldap; |
||
41 | |||
42 | /** |
||
43 | * @var array The map for the LDAP attribute names. |
||
44 | */ |
||
45 | protected $attrMap = []; |
||
46 | |||
47 | /** |
||
48 | * @var array The role to LDAP group name map. |
||
49 | */ |
||
50 | protected $roleMap = []; |
||
51 | |||
52 | /** |
||
53 | * @var array Map names to their LDAP attribute names when querying for LDAP groups used for roles. |
||
54 | */ |
||
55 | protected $roleAttrMap = [ |
||
56 | 'name' => 'name', |
||
57 | 'sid' => 'sid', |
||
58 | 'guid' => 'guid', |
||
59 | 'members' => 'members', |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * @var array Any additional LDAP attributes to select. |
||
64 | */ |
||
65 | protected $attributes = []; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $userClass = self::BASE_USER_CLASS; |
||
71 | |||
72 | /** |
||
73 | * @var bool Whether or not to check group membership recursively when checking role membership. |
||
74 | */ |
||
75 | protected $checkGroupsRecursively; |
||
76 | |||
77 | /** |
||
78 | * @var string|null The default role to be assigned to a user. |
||
79 | */ |
||
80 | protected $defaultRole; |
||
81 | |||
82 | /** |
||
83 | * @var string The object type to search LDAP for. |
||
84 | */ |
||
85 | protected $ldapObjectType = LdapObjectType::USER; |
||
86 | |||
87 | /** |
||
88 | * @var string The group object type when searching group membership. |
||
89 | */ |
||
90 | protected $groupObjectType = LdapObjectType::GROUP; |
||
91 | |||
92 | /** |
||
93 | * @var string The container/OU to search for the user under. |
||
94 | */ |
||
95 | protected $searchBase; |
||
96 | |||
97 | /** |
||
98 | * @var bool Whether or not user attributes should be re-queried on a refresh. |
||
99 | */ |
||
100 | protected $refreshAttributes = true; |
||
101 | |||
102 | /** |
||
103 | * @var bool Whether or not user roles should be re-queried on a refresh. |
||
104 | */ |
||
105 | protected $refreshRoles = true; |
||
106 | |||
107 | /** |
||
108 | * @param LdapManager $ldap |
||
109 | * @param array $attrMap |
||
110 | * @param array $roleMap |
||
111 | * @param bool $checkGroupsRecursively |
||
112 | */ |
||
113 | public function __construct(LdapManager $ldap, array $attrMap, array $roleMap, $checkGroupsRecursively = true) |
||
120 | |||
121 | /** |
||
122 | * Set the default role to add to a LDAP user. |
||
123 | * |
||
124 | * @param string|null $role |
||
125 | */ |
||
126 | public function setDefaultRole($role) |
||
133 | |||
134 | /** |
||
135 | * Set the user class to be instantiated and returned from the LDAP provider. |
||
136 | * |
||
137 | * @param string $class |
||
138 | */ |
||
139 | public function setUserClass($class) |
||
151 | |||
152 | /** |
||
153 | * Set any additional attributes to be selected for the LDAP user. |
||
154 | * |
||
155 | * @param array $attributes |
||
156 | */ |
||
157 | public function setAttributes(array $attributes) |
||
161 | |||
162 | /** |
||
163 | * Set the LDAP object type that will be searched for. |
||
164 | * |
||
165 | * @param string $type |
||
166 | */ |
||
167 | public function setLdapObjectType($type) |
||
171 | |||
172 | /** |
||
173 | * Set the LdapTools object type to search for group membership. |
||
174 | * |
||
175 | * @param string $type |
||
176 | */ |
||
177 | public function setRoleLdapType($type) |
||
181 | |||
182 | /** |
||
183 | * Set the attribute name to LDAP name attributes used in querying LDAP groups for roles. |
||
184 | * |
||
185 | * @param array $map |
||
186 | */ |
||
187 | public function setRoleAttributeMap(array $map) |
||
191 | |||
192 | /** |
||
193 | * @param string $searchBase |
||
194 | */ |
||
195 | public function setSearchBase($searchBase) |
||
199 | |||
200 | /** |
||
201 | * @param bool $refreshRoles |
||
202 | */ |
||
203 | public function setRefreshRoles($refreshRoles) |
||
207 | |||
208 | /** |
||
209 | * @param bool $refreshAttributes |
||
210 | */ |
||
211 | public function setRefreshAttributes($refreshAttributes) |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function loadUserByUsername($username) |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function refreshUser(UserInterface $user) |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function supportsClass($class) |
||
253 | |||
254 | /** |
||
255 | * Search for, and return, the LDAP user by a specific attribute. |
||
256 | * |
||
257 | * @param string $attribute |
||
258 | * @param string $value |
||
259 | * @return LdapUser |
||
260 | */ |
||
261 | protected function searchForUser($attribute, $value) |
||
280 | |||
281 | /** |
||
282 | * Get all the attributes that should be selected for when querying LDAP. |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | protected function getAttributesToSelect() |
||
295 | |||
296 | /** |
||
297 | * Set the roles for the user based on group membership. |
||
298 | * |
||
299 | * @param LdapUser $user |
||
300 | * @return LdapUser |
||
301 | */ |
||
302 | protected function setRolesForUser(LdapUser $user) |
||
317 | |||
318 | /** |
||
319 | * Check all of the groups that are valid for a specific role against all of the LDAP groups that the user belongs |
||
320 | * to. |
||
321 | * |
||
322 | * @param array $roleGroups |
||
323 | * @param LdapObjectCollection $ldapGroups |
||
324 | * @return bool |
||
325 | */ |
||
326 | protected function hasGroupForRoles(array $roleGroups, LdapObjectCollection $ldapGroups) |
||
346 | |||
347 | /** |
||
348 | * Check each LDAP group to see if any of them have an attribute with a specific value. |
||
349 | * |
||
350 | * @param LdapObjectCollection $groups |
||
351 | * @param string $attribute |
||
352 | * @param string $value |
||
353 | * @return bool |
||
354 | */ |
||
355 | protected function hasGroupWithAttributeValue(LdapObjectCollection $groups, $attribute, $value) |
||
368 | |||
369 | /** |
||
370 | * @param LdapUser $user |
||
371 | * @return LdapObjectCollection |
||
372 | */ |
||
373 | protected function getGroupsForUser(LdapUser $user) |
||
390 | |||
391 | /** |
||
392 | * @param LdapObject $ldapObject |
||
393 | * @return LdapUser |
||
394 | */ |
||
395 | protected function constructUserClass(LdapObject $ldapObject) |
||
410 | } |
||
411 |