Total Complexity | 73 |
Total Lines | 479 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Principal 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.
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 Principal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
52 | class Principal implements BackendInterface { |
||
53 | |||
54 | /** @var IUserManager */ |
||
55 | private $userManager; |
||
56 | |||
57 | /** @var IGroupManager */ |
||
58 | private $groupManager; |
||
59 | |||
60 | /** @var IShareManager */ |
||
61 | private $shareManager; |
||
62 | |||
63 | /** @var IUserSession */ |
||
64 | private $userSession; |
||
65 | |||
66 | /** @var IAppManager */ |
||
67 | private $appManager; |
||
68 | |||
69 | /** @var string */ |
||
70 | private $principalPrefix; |
||
71 | |||
72 | /** @var bool */ |
||
73 | private $hasGroups; |
||
74 | |||
75 | /** @var bool */ |
||
76 | private $hasCircles; |
||
77 | |||
78 | /** @var ProxyMapper */ |
||
79 | private $proxyMapper; |
||
80 | |||
81 | /** @var IConfig */ |
||
82 | private $config; |
||
83 | |||
84 | /** |
||
85 | * Principal constructor. |
||
86 | * |
||
87 | * @param IUserManager $userManager |
||
88 | * @param IGroupManager $groupManager |
||
89 | * @param IShareManager $shareManager |
||
90 | * @param IUserSession $userSession |
||
91 | * @param IAppManager $appManager |
||
92 | * @param ProxyMapper $proxyMapper |
||
93 | * @param IConfig $config |
||
94 | * @param string $principalPrefix |
||
95 | */ |
||
96 | public function __construct(IUserManager $userManager, |
||
97 | IGroupManager $groupManager, |
||
98 | IShareManager $shareManager, |
||
99 | IUserSession $userSession, |
||
100 | IAppManager $appManager, |
||
101 | ProxyMapper $proxyMapper, |
||
102 | IConfig $config, |
||
103 | string $principalPrefix = 'principals/users/') { |
||
104 | $this->userManager = $userManager; |
||
105 | $this->groupManager = $groupManager; |
||
106 | $this->shareManager = $shareManager; |
||
107 | $this->userSession = $userSession; |
||
108 | $this->appManager = $appManager; |
||
109 | $this->principalPrefix = trim($principalPrefix, '/'); |
||
110 | $this->hasGroups = $this->hasCircles = ($principalPrefix === 'principals/users/'); |
||
111 | $this->proxyMapper = $proxyMapper; |
||
112 | $this->config = $config; |
||
113 | } |
||
114 | |||
115 | use PrincipalProxyTrait { |
||
116 | getGroupMembership as protected traitGetGroupMembership; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns a list of principals based on a prefix. |
||
121 | * |
||
122 | * This prefix will often contain something like 'principals'. You are only |
||
123 | * expected to return principals that are in this base path. |
||
124 | * |
||
125 | * You are expected to return at least a 'uri' for every user, you can |
||
126 | * return any additional properties if you wish so. Common properties are: |
||
127 | * {DAV:}displayname |
||
128 | * |
||
129 | * @param string $prefixPath |
||
130 | * @return string[] |
||
131 | */ |
||
132 | public function getPrincipalsByPrefix($prefixPath) { |
||
133 | $principals = []; |
||
134 | |||
135 | if ($prefixPath === $this->principalPrefix) { |
||
136 | foreach($this->userManager->search('') as $user) { |
||
137 | $principals[] = $this->userToPrincipal($user); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | return $principals; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns a specific principal, specified by it's path. |
||
146 | * The returned structure should be the exact same as from |
||
147 | * getPrincipalsByPrefix. |
||
148 | * |
||
149 | * @param string $path |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getPrincipalByPath($path) { |
||
153 | list($prefix, $name) = \Sabre\Uri\split($path); |
||
154 | |||
155 | if ($name === 'calendar-proxy-write' || $name === 'calendar-proxy-read') { |
||
156 | list($prefix2, $name2) = \Sabre\Uri\split($prefix); |
||
157 | |||
158 | if ($prefix2 === $this->principalPrefix) { |
||
159 | $user = $this->userManager->get($name2); |
||
160 | |||
161 | if ($user !== null) { |
||
162 | return [ |
||
163 | 'uri' => 'principals/users/' . $user->getUID() . '/' . $name, |
||
164 | ]; |
||
165 | } |
||
166 | return null; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | if ($prefix === $this->principalPrefix) { |
||
171 | $user = $this->userManager->get($name); |
||
172 | |||
173 | if ($user !== null) { |
||
174 | return $this->userToPrincipal($user); |
||
175 | } |
||
176 | } else if ($prefix === 'principals/circles') { |
||
177 | try { |
||
178 | return $this->circleToPrincipal($name); |
||
179 | } catch (QueryException $e) { |
||
180 | return null; |
||
181 | } |
||
182 | } |
||
183 | return null; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Returns the list of groups a principal is a member of |
||
188 | * |
||
189 | * @param string $principal |
||
190 | * @param bool $needGroups |
||
191 | * @return array |
||
192 | * @throws Exception |
||
193 | */ |
||
194 | public function getGroupMembership($principal, $needGroups = false) { |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param string $path |
||
225 | * @param PropPatch $propPatch |
||
226 | * @return int |
||
227 | */ |
||
228 | function updatePrincipal($path, PropPatch $propPatch) { |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Search user principals |
||
234 | * |
||
235 | * @param array $searchProperties |
||
236 | * @param string $test |
||
237 | * @return array |
||
238 | */ |
||
239 | protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
||
240 | $results = []; |
||
241 | |||
242 | // If sharing is disabled, return the empty array |
||
243 | $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
||
244 | if (!$shareAPIEnabled) { |
||
245 | return []; |
||
246 | } |
||
247 | |||
248 | $allowEnumeration = $this->shareManager->allowEnumeration(); |
||
249 | $limitEnumeration = $this->shareManager->limitEnumerationToGroups(); |
||
250 | |||
251 | // If sharing is restricted to group members only, |
||
252 | // return only members that have groups in common |
||
253 | $restrictGroups = false; |
||
254 | if ($this->shareManager->shareWithGroupMembersOnly()) { |
||
255 | $user = $this->userSession->getUser(); |
||
256 | if (!$user) { |
||
257 | return []; |
||
258 | } |
||
259 | |||
260 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
261 | } |
||
262 | |||
263 | $currentUserGroups = []; |
||
264 | if ($limitEnumeration) { |
||
265 | $currentUser = $this->userSession->getUser(); |
||
266 | if ($currentUser) { |
||
267 | $currentUserGroups = $this->groupManager->getUserGroupIds($currentUser); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | foreach ($searchProperties as $prop => $value) { |
||
272 | switch ($prop) { |
||
273 | case '{http://sabredav.org/ns}email-address': |
||
274 | $users = $this->userManager->getByEmail($value); |
||
275 | |||
276 | if (!$allowEnumeration) { |
||
277 | $users = \array_filter($users, static function(IUser $user) use ($value) { |
||
278 | return $user->getEMailAddress() === $value; |
||
279 | }); |
||
280 | } |
||
281 | |||
282 | if ($limitEnumeration) { |
||
283 | $users = \array_filter($users, function (IUser $user) use ($currentUserGroups, $value) { |
||
284 | return !empty(array_intersect( |
||
285 | $this->groupManager->getUserGroupIds($user), |
||
286 | $currentUserGroups |
||
287 | )) || $user->getEMailAddress() === $value; |
||
288 | }); |
||
289 | } |
||
290 | |||
291 | $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
||
292 | // is sharing restricted to groups only? |
||
293 | if ($restrictGroups !== false) { |
||
294 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
295 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
296 | return $carry; |
||
297 | } |
||
298 | } |
||
299 | |||
300 | $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
||
301 | return $carry; |
||
302 | }, []); |
||
303 | break; |
||
304 | |||
305 | case '{DAV:}displayname': |
||
306 | $users = $this->userManager->searchDisplayName($value); |
||
307 | |||
308 | if (!$allowEnumeration) { |
||
309 | $users = \array_filter($users, static function(IUser $user) use ($value) { |
||
310 | return $user->getDisplayName() === $value; |
||
311 | }); |
||
312 | } |
||
313 | |||
314 | if ($limitEnumeration) { |
||
315 | $users = \array_filter($users, function (IUser $user) use ($currentUserGroups, $value) { |
||
316 | return !empty(array_intersect( |
||
317 | $this->groupManager->getUserGroupIds($user), |
||
318 | $currentUserGroups |
||
319 | )) || $user->getDisplayName() === $value; |
||
320 | }); |
||
321 | } |
||
322 | |||
323 | $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
||
324 | // is sharing restricted to groups only? |
||
325 | if ($restrictGroups !== false) { |
||
326 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
327 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
328 | return $carry; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
||
333 | return $carry; |
||
334 | }, []); |
||
335 | break; |
||
336 | |||
337 | case '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set': |
||
338 | // If you add support for more search properties that qualify as a user-address, |
||
339 | // please also add them to the array below |
||
340 | $results[] = $this->searchUserPrincipals([ |
||
341 | // In theory this should also search for principal:principals/users/... |
||
342 | // but that's used internally only anyway and i don't know of any client querying that |
||
343 | '{http://sabredav.org/ns}email-address' => $value, |
||
344 | ], 'anyof'); |
||
345 | break; |
||
346 | |||
347 | default: |
||
348 | $results[] = []; |
||
349 | break; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | // results is an array of arrays, so this is not the first search result |
||
354 | // but the results of the first searchProperty |
||
355 | if (count($results) === 1) { |
||
356 | return $results[0]; |
||
357 | } |
||
358 | |||
359 | switch ($test) { |
||
360 | case 'anyof': |
||
361 | return array_values(array_unique(array_merge(...$results))); |
||
362 | |||
363 | case 'allof': |
||
364 | default: |
||
365 | return array_values(array_intersect(...$results)); |
||
366 | } |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @param string $prefixPath |
||
371 | * @param array $searchProperties |
||
372 | * @param string $test |
||
373 | * @return array |
||
374 | */ |
||
375 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
386 | } |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param string $uri |
||
391 | * @param string $principalPrefix |
||
392 | * @return string |
||
393 | */ |
||
394 | function findByUri($uri, $principalPrefix) { |
||
395 | // If sharing is disabled, return the empty array |
||
396 | $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
||
397 | if (!$shareAPIEnabled) { |
||
398 | return null; |
||
399 | } |
||
400 | |||
401 | // If sharing is restricted to group members only, |
||
402 | // return only members that have groups in common |
||
403 | $restrictGroups = false; |
||
404 | if ($this->shareManager->shareWithGroupMembersOnly()) { |
||
405 | $user = $this->userSession->getUser(); |
||
406 | if (!$user) { |
||
407 | return null; |
||
408 | } |
||
409 | |||
410 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
411 | } |
||
412 | |||
413 | if (strpos($uri, 'mailto:') === 0) { |
||
414 | if ($principalPrefix === 'principals/users') { |
||
415 | $users = $this->userManager->getByEmail(substr($uri, 7)); |
||
416 | if (count($users) !== 1) { |
||
417 | return null; |
||
418 | } |
||
419 | $user = $users[0]; |
||
420 | |||
421 | if ($restrictGroups !== false) { |
||
422 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
423 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
424 | return null; |
||
425 | } |
||
426 | } |
||
427 | |||
428 | return $this->principalPrefix . '/' . $user->getUID(); |
||
429 | } |
||
430 | } |
||
431 | if (substr($uri, 0, 10) === 'principal:') { |
||
432 | $principal = substr($uri, 10); |
||
433 | $principal = $this->getPrincipalByPath($principal); |
||
434 | if ($principal !== null) { |
||
435 | return $principal['uri']; |
||
436 | } |
||
437 | } |
||
438 | |||
439 | return null; |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @param IUser $user |
||
444 | * @return array |
||
445 | */ |
||
446 | protected function userToPrincipal($user) { |
||
447 | $userId = $user->getUID(); |
||
448 | $displayName = $user->getDisplayName(); |
||
449 | $principal = [ |
||
450 | 'uri' => $this->principalPrefix . '/' . $userId, |
||
451 | '{DAV:}displayname' => is_null($displayName) ? $userId : $displayName, |
||
452 | '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
||
453 | ]; |
||
454 | |||
455 | $email = $user->getEMailAddress(); |
||
456 | if (!empty($email)) { |
||
457 | $principal['{http://sabredav.org/ns}email-address'] = $email; |
||
458 | } |
||
459 | |||
460 | return $principal; |
||
461 | } |
||
462 | |||
463 | public function getPrincipalPrefix() { |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * @param string $circleUniqueId |
||
469 | * @return array|null |
||
470 | * @throws \OCP\AppFramework\QueryException |
||
471 | * @suppress PhanUndeclaredClassMethod |
||
472 | * @suppress PhanUndeclaredClassCatch |
||
473 | */ |
||
474 | protected function circleToPrincipal($circleUniqueId) { |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Returns the list of circles a principal is a member of |
||
501 | * |
||
502 | * @param string $principal |
||
503 | * @return array |
||
504 | * @throws Exception |
||
505 | * @throws \OCP\AppFramework\QueryException |
||
506 | * @suppress PhanUndeclaredClassMethod |
||
507 | */ |
||
508 | public function getCircleMembership($principal):array { |
||
531 | } |
||
532 | } |
||
533 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths