| Total Complexity | 42 |
| Total Lines | 305 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GroupPrincipalBackend 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 GroupPrincipalBackend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class GroupPrincipalBackend implements BackendInterface { |
||
| 43 | public const PRINCIPAL_PREFIX = 'principals/groups'; |
||
| 44 | |||
| 45 | /** @var IGroupManager */ |
||
| 46 | private $groupManager; |
||
| 47 | |||
| 48 | /** @var IUserSession */ |
||
| 49 | private $userSession; |
||
| 50 | |||
| 51 | /** @var IShareManager */ |
||
| 52 | private $shareManager; |
||
| 53 | /** @var IConfig */ |
||
| 54 | private $config; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param IGroupManager $IGroupManager |
||
| 58 | * @param IUserSession $userSession |
||
| 59 | * @param IShareManager $shareManager |
||
| 60 | */ |
||
| 61 | public function __construct( |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Returns a list of principals based on a prefix. |
||
| 75 | * |
||
| 76 | * This prefix will often contain something like 'principals'. You are only |
||
| 77 | * expected to return principals that are in this base path. |
||
| 78 | * |
||
| 79 | * You are expected to return at least a 'uri' for every user, you can |
||
| 80 | * return any additional properties if you wish so. Common properties are: |
||
| 81 | * {DAV:}displayname |
||
| 82 | * |
||
| 83 | * @param string $prefixPath |
||
| 84 | * @return string[] |
||
| 85 | */ |
||
| 86 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 87 | $principals = []; |
||
| 88 | |||
| 89 | if ($prefixPath === self::PRINCIPAL_PREFIX) { |
||
| 90 | foreach ($this->groupManager->search('') as $user) { |
||
| 91 | $principals[] = $this->groupToPrincipal($user); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return $principals; |
||
|
|
|||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns a specific principal, specified by it's path. |
||
| 100 | * The returned structure should be the exact same as from |
||
| 101 | * getPrincipalsByPrefix. |
||
| 102 | * |
||
| 103 | * @param string $path |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function getPrincipalByPath($path) { |
||
| 107 | $elements = explode('/', $path, 3); |
||
| 108 | if ($elements[0] !== 'principals') { |
||
| 109 | return null; |
||
| 110 | } |
||
| 111 | if ($elements[1] !== 'groups') { |
||
| 112 | return null; |
||
| 113 | } |
||
| 114 | $name = urldecode($elements[2]); |
||
| 115 | $group = $this->groupManager->get($name); |
||
| 116 | |||
| 117 | if (!is_null($group)) { |
||
| 118 | return $this->groupToPrincipal($group); |
||
| 119 | } |
||
| 120 | |||
| 121 | return null; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns the list of members for a group-principal |
||
| 126 | * |
||
| 127 | * @param string $principal |
||
| 128 | * @return string[] |
||
| 129 | * @throws Exception |
||
| 130 | */ |
||
| 131 | public function getGroupMemberSet($principal) { |
||
| 132 | $elements = explode('/', $principal); |
||
| 133 | if ($elements[0] !== 'principals') { |
||
| 134 | return []; |
||
| 135 | } |
||
| 136 | if ($elements[1] !== 'groups') { |
||
| 137 | return []; |
||
| 138 | } |
||
| 139 | $name = $elements[2]; |
||
| 140 | $group = $this->groupManager->get($name); |
||
| 141 | |||
| 142 | if (is_null($group)) { |
||
| 143 | return []; |
||
| 144 | } |
||
| 145 | |||
| 146 | return array_map(function ($user) { |
||
| 147 | return $this->userToPrincipal($user); |
||
| 148 | }, $group->getUsers()); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the list of groups a principal is a member of |
||
| 153 | * |
||
| 154 | * @param string $principal |
||
| 155 | * @return array |
||
| 156 | * @throws Exception |
||
| 157 | */ |
||
| 158 | public function getGroupMembership($principal) { |
||
| 159 | return []; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Updates the list of group members for a group principal. |
||
| 164 | * |
||
| 165 | * The principals should be passed as a list of uri's. |
||
| 166 | * |
||
| 167 | * @param string $principal |
||
| 168 | * @param string[] $members |
||
| 169 | * @throws Exception |
||
| 170 | */ |
||
| 171 | public function setGroupMemberSet($principal, array $members) { |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $path |
||
| 177 | * @param PropPatch $propPatch |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | public function updatePrincipal($path, PropPatch $propPatch) { |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param string $prefixPath |
||
| 186 | * @param array $searchProperties |
||
| 187 | * @param string $test |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $uri |
||
| 270 | * @param string $principalPrefix |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function findByUri($uri, $principalPrefix) { |
||
| 274 | // If sharing is disabled, return the empty array |
||
| 275 | if (!$this->groupSharingEnabled()) { |
||
| 276 | return null; |
||
| 277 | } |
||
| 278 | |||
| 279 | // If sharing is restricted to group members only, |
||
| 280 | // return only members that have groups in common |
||
| 281 | $restrictGroups = false; |
||
| 282 | if ($this->shareManager->shareWithGroupMembersOnly()) { |
||
| 283 | $user = $this->userSession->getUser(); |
||
| 284 | if (!$user) { |
||
| 285 | return null; |
||
| 286 | } |
||
| 287 | |||
| 288 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
| 289 | } |
||
| 290 | |||
| 291 | if (strpos($uri, 'principal:principals/groups/') === 0) { |
||
| 292 | $name = urlencode(substr($uri, 28)); |
||
| 293 | if ($restrictGroups !== false && !\in_array($name, $restrictGroups, true)) { |
||
| 294 | return null; |
||
| 295 | } |
||
| 296 | |||
| 297 | return substr($uri, 10); |
||
| 298 | } |
||
| 299 | |||
| 300 | return null; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param IGroup $group |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | protected function groupToPrincipal($group) { |
||
| 308 | $groupId = $group->getGID(); |
||
| 309 | // getDisplayName returns UID if none |
||
| 310 | $displayName = $group->getDisplayName(); |
||
| 311 | |||
| 312 | return [ |
||
| 313 | 'uri' => 'principals/groups/' . urlencode($groupId), |
||
| 314 | '{DAV:}displayname' => $displayName, |
||
| 315 | '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'GROUP', |
||
| 316 | ]; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param IUser $user |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | protected function userToPrincipal($user) { |
||
| 324 | $userId = $user->getUID(); |
||
| 325 | // getDisplayName returns UID if none |
||
| 326 | $displayName = $user->getDisplayName(); |
||
| 327 | |||
| 328 | $principal = [ |
||
| 329 | 'uri' => 'principals/users/' . $userId, |
||
| 330 | '{DAV:}displayname' => $displayName, |
||
| 331 | '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL', |
||
| 332 | ]; |
||
| 333 | |||
| 334 | $email = $user->getEMailAddress(); |
||
| 335 | if (!empty($email)) { |
||
| 336 | $principal['{http://sabredav.org/ns}email-address'] = $email; |
||
| 337 | } |
||
| 338 | |||
| 339 | return $principal; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | private function groupSharingEnabled(): bool { |
||
| 347 | } |
||
| 348 | } |
||
| 349 |