@@ -39,304 +39,304 @@ |
||
| 39 | 39 | |
| 40 | 40 | class SyncService { |
| 41 | 41 | |
| 42 | - /** @var CardDavBackend */ |
|
| 43 | - private $backend; |
|
| 44 | - |
|
| 45 | - /** @var IUserManager */ |
|
| 46 | - private $userManager; |
|
| 47 | - |
|
| 48 | - /** @var ILogger */ |
|
| 49 | - private $logger; |
|
| 50 | - |
|
| 51 | - /** @var array */ |
|
| 52 | - private $localSystemAddressBook; |
|
| 53 | - |
|
| 54 | - /** @var AccountManager */ |
|
| 55 | - private $accountManager; |
|
| 56 | - |
|
| 57 | - /** @var string */ |
|
| 58 | - protected $certPath; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * SyncService constructor. |
|
| 62 | - * |
|
| 63 | - * @param CardDavBackend $backend |
|
| 64 | - * @param IUserManager $userManager |
|
| 65 | - * @param ILogger $logger |
|
| 66 | - * @param AccountManager $accountManager |
|
| 67 | - */ |
|
| 68 | - public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger, AccountManager $accountManager) { |
|
| 69 | - $this->backend = $backend; |
|
| 70 | - $this->userManager = $userManager; |
|
| 71 | - $this->logger = $logger; |
|
| 72 | - $this->accountManager = $accountManager; |
|
| 73 | - $this->certPath = ''; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param string $url |
|
| 78 | - * @param string $userName |
|
| 79 | - * @param string $addressBookUrl |
|
| 80 | - * @param string $sharedSecret |
|
| 81 | - * @param string $syncToken |
|
| 82 | - * @param int $targetBookId |
|
| 83 | - * @param string $targetPrincipal |
|
| 84 | - * @param array $targetProperties |
|
| 85 | - * @return string |
|
| 86 | - * @throws \Exception |
|
| 87 | - */ |
|
| 88 | - public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
| 89 | - // 1. create addressbook |
|
| 90 | - $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
| 91 | - $addressBookId = $book['id']; |
|
| 92 | - |
|
| 93 | - // 2. query changes |
|
| 94 | - try { |
|
| 95 | - $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); |
|
| 96 | - } catch (ClientHttpException $ex) { |
|
| 97 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
| 98 | - // remote server revoked access to the address book, remove it |
|
| 99 | - $this->backend->deleteAddressBook($addressBookId); |
|
| 100 | - $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
| 101 | - throw $ex; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // 3. apply changes |
|
| 106 | - // TODO: use multi-get for download |
|
| 107 | - foreach ($response['response'] as $resource => $status) { |
|
| 108 | - $cardUri = basename($resource); |
|
| 109 | - if (isset($status[200])) { |
|
| 110 | - $vCard = $this->download($url, $userName, $sharedSecret, $resource); |
|
| 111 | - $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
| 112 | - if ($existingCard === false) { |
|
| 113 | - $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
| 114 | - } else { |
|
| 115 | - $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
| 116 | - } |
|
| 117 | - } else { |
|
| 118 | - $this->backend->deleteCard($addressBookId, $cardUri); |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $response['token']; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param string $principal |
|
| 127 | - * @param string $id |
|
| 128 | - * @param array $properties |
|
| 129 | - * @return array|null |
|
| 130 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
| 131 | - */ |
|
| 132 | - public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
| 133 | - $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
| 134 | - if (!is_null($book)) { |
|
| 135 | - return $book; |
|
| 136 | - } |
|
| 137 | - $this->backend->createAddressBook($principal, $id, $properties); |
|
| 138 | - |
|
| 139 | - return $this->backend->getAddressBooksByUri($principal, $id); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Check if there is a valid certPath we should use |
|
| 144 | - * |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - protected function getCertPath() { |
|
| 148 | - |
|
| 149 | - // we already have a valid certPath |
|
| 150 | - if ($this->certPath !== '') { |
|
| 151 | - return $this->certPath; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** @var ICertificateManager $certManager */ |
|
| 155 | - $certManager = \OC::$server->getCertificateManager(null); |
|
| 156 | - $certPath = $certManager->getAbsoluteBundlePath(); |
|
| 157 | - if (file_exists($certPath)) { |
|
| 158 | - $this->certPath = $certPath; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - return $this->certPath; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * @param string $url |
|
| 166 | - * @param string $userName |
|
| 167 | - * @param string $addressBookUrl |
|
| 168 | - * @param string $sharedSecret |
|
| 169 | - * @return Client |
|
| 170 | - */ |
|
| 171 | - protected function getClient($url, $userName, $sharedSecret) { |
|
| 172 | - $settings = [ |
|
| 173 | - 'baseUri' => $url . '/', |
|
| 174 | - 'userName' => $userName, |
|
| 175 | - 'password' => $sharedSecret, |
|
| 176 | - ]; |
|
| 177 | - $client = new Client($settings); |
|
| 178 | - $certPath = $this->getCertPath(); |
|
| 179 | - $client->setThrowExceptions(true); |
|
| 180 | - |
|
| 181 | - if ($certPath !== '' && strpos($url, 'http://') !== 0) { |
|
| 182 | - $client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - return $client; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * @param string $url |
|
| 190 | - * @param string $userName |
|
| 191 | - * @param string $addressBookUrl |
|
| 192 | - * @param string $sharedSecret |
|
| 193 | - * @param string $syncToken |
|
| 194 | - * @return array |
|
| 195 | - */ |
|
| 196 | - protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) { |
|
| 197 | - $client = $this->getClient($url, $userName, $sharedSecret); |
|
| 198 | - |
|
| 199 | - $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
| 200 | - |
|
| 201 | - $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
| 202 | - 'Content-Type' => 'application/xml' |
|
| 203 | - ]); |
|
| 204 | - |
|
| 205 | - return $this->parseMultiStatus($response['body']); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @param string $url |
|
| 210 | - * @param string $userName |
|
| 211 | - * @param string $sharedSecret |
|
| 212 | - * @param string $resourcePath |
|
| 213 | - * @return array |
|
| 214 | - */ |
|
| 215 | - protected function download($url, $userName, $sharedSecret, $resourcePath) { |
|
| 216 | - $client = $this->getClient($url, $userName, $sharedSecret); |
|
| 217 | - return $client->request('GET', $resourcePath); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @param string|null $syncToken |
|
| 222 | - * @return string |
|
| 223 | - */ |
|
| 224 | - private function buildSyncCollectionRequestBody($syncToken) { |
|
| 225 | - |
|
| 226 | - $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
| 227 | - $dom->formatOutput = true; |
|
| 228 | - $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
| 229 | - $sync = $dom->createElement('d:sync-token', $syncToken); |
|
| 230 | - $prop = $dom->createElement('d:prop'); |
|
| 231 | - $cont = $dom->createElement('d:getcontenttype'); |
|
| 232 | - $etag = $dom->createElement('d:getetag'); |
|
| 233 | - |
|
| 234 | - $prop->appendChild($cont); |
|
| 235 | - $prop->appendChild($etag); |
|
| 236 | - $root->appendChild($sync); |
|
| 237 | - $root->appendChild($prop); |
|
| 238 | - $dom->appendChild($root); |
|
| 239 | - return $dom->saveXML(); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * @param string $body |
|
| 244 | - * @return array |
|
| 245 | - * @throws \Sabre\Xml\ParseException |
|
| 246 | - */ |
|
| 247 | - private function parseMultiStatus($body) { |
|
| 248 | - $xml = new Service(); |
|
| 249 | - |
|
| 250 | - /** @var MultiStatus $multiStatus */ |
|
| 251 | - $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
| 252 | - |
|
| 253 | - $result = []; |
|
| 254 | - foreach ($multiStatus->getResponses() as $response) { |
|
| 255 | - $result[$response->getHref()] = $response->getResponseProperties(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * @param IUser $user |
|
| 263 | - */ |
|
| 264 | - public function updateUser($user) { |
|
| 265 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 266 | - $addressBookId = $systemAddressBook['id']; |
|
| 267 | - $converter = new Converter($this->accountManager); |
|
| 268 | - $name = $user->getBackendClassName(); |
|
| 269 | - $userId = $user->getUID(); |
|
| 270 | - |
|
| 271 | - $cardId = "$name:$userId.vcf"; |
|
| 272 | - $card = $this->backend->getCard($addressBookId, $cardId); |
|
| 273 | - if ($user->isEnabled()) { |
|
| 274 | - if ($card === false) { |
|
| 275 | - $vCard = $converter->createCardFromUser($user); |
|
| 276 | - if ($vCard !== null) { |
|
| 277 | - $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 278 | - } |
|
| 279 | - } else { |
|
| 280 | - $vCard = $converter->createCardFromUser($user); |
|
| 281 | - if (is_null($vCard)) { |
|
| 282 | - $this->backend->deleteCard($addressBookId, $cardId); |
|
| 283 | - } else { |
|
| 284 | - $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - } else { |
|
| 288 | - $this->backend->deleteCard($addressBookId, $cardId); |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * @param IUser|string $userOrCardId |
|
| 294 | - */ |
|
| 295 | - public function deleteUser($userOrCardId) { |
|
| 296 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 297 | - if ($userOrCardId instanceof IUser){ |
|
| 298 | - $name = $userOrCardId->getBackendClassName(); |
|
| 299 | - $userId = $userOrCardId->getUID(); |
|
| 300 | - |
|
| 301 | - $userOrCardId = "$name:$userId.vcf"; |
|
| 302 | - } |
|
| 303 | - $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * @return array|null |
|
| 308 | - */ |
|
| 309 | - public function getLocalSystemAddressBook() { |
|
| 310 | - if (is_null($this->localSystemAddressBook)) { |
|
| 311 | - $systemPrincipal = "principals/system/system"; |
|
| 312 | - $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
| 313 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
| 314 | - ]); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - return $this->localSystemAddressBook; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - public function syncInstance(\Closure $progressCallback = null) { |
|
| 321 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 322 | - $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
| 323 | - $this->updateUser($user); |
|
| 324 | - if (!is_null($progressCallback)) { |
|
| 325 | - $progressCallback(); |
|
| 326 | - } |
|
| 327 | - }); |
|
| 328 | - |
|
| 329 | - // remove no longer existing |
|
| 330 | - $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
| 331 | - foreach($allCards as $card) { |
|
| 332 | - $vCard = Reader::read($card['carddata']); |
|
| 333 | - $uid = $vCard->UID->getValue(); |
|
| 334 | - // load backend and see if user exists |
|
| 335 | - if (!$this->userManager->userExists($uid)) { |
|
| 336 | - $this->deleteUser($card['uri']); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - } |
|
| 42 | + /** @var CardDavBackend */ |
|
| 43 | + private $backend; |
|
| 44 | + |
|
| 45 | + /** @var IUserManager */ |
|
| 46 | + private $userManager; |
|
| 47 | + |
|
| 48 | + /** @var ILogger */ |
|
| 49 | + private $logger; |
|
| 50 | + |
|
| 51 | + /** @var array */ |
|
| 52 | + private $localSystemAddressBook; |
|
| 53 | + |
|
| 54 | + /** @var AccountManager */ |
|
| 55 | + private $accountManager; |
|
| 56 | + |
|
| 57 | + /** @var string */ |
|
| 58 | + protected $certPath; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * SyncService constructor. |
|
| 62 | + * |
|
| 63 | + * @param CardDavBackend $backend |
|
| 64 | + * @param IUserManager $userManager |
|
| 65 | + * @param ILogger $logger |
|
| 66 | + * @param AccountManager $accountManager |
|
| 67 | + */ |
|
| 68 | + public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger, AccountManager $accountManager) { |
|
| 69 | + $this->backend = $backend; |
|
| 70 | + $this->userManager = $userManager; |
|
| 71 | + $this->logger = $logger; |
|
| 72 | + $this->accountManager = $accountManager; |
|
| 73 | + $this->certPath = ''; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param string $url |
|
| 78 | + * @param string $userName |
|
| 79 | + * @param string $addressBookUrl |
|
| 80 | + * @param string $sharedSecret |
|
| 81 | + * @param string $syncToken |
|
| 82 | + * @param int $targetBookId |
|
| 83 | + * @param string $targetPrincipal |
|
| 84 | + * @param array $targetProperties |
|
| 85 | + * @return string |
|
| 86 | + * @throws \Exception |
|
| 87 | + */ |
|
| 88 | + public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
| 89 | + // 1. create addressbook |
|
| 90 | + $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
| 91 | + $addressBookId = $book['id']; |
|
| 92 | + |
|
| 93 | + // 2. query changes |
|
| 94 | + try { |
|
| 95 | + $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); |
|
| 96 | + } catch (ClientHttpException $ex) { |
|
| 97 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
| 98 | + // remote server revoked access to the address book, remove it |
|
| 99 | + $this->backend->deleteAddressBook($addressBookId); |
|
| 100 | + $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
| 101 | + throw $ex; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // 3. apply changes |
|
| 106 | + // TODO: use multi-get for download |
|
| 107 | + foreach ($response['response'] as $resource => $status) { |
|
| 108 | + $cardUri = basename($resource); |
|
| 109 | + if (isset($status[200])) { |
|
| 110 | + $vCard = $this->download($url, $userName, $sharedSecret, $resource); |
|
| 111 | + $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
| 112 | + if ($existingCard === false) { |
|
| 113 | + $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
| 114 | + } else { |
|
| 115 | + $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
| 116 | + } |
|
| 117 | + } else { |
|
| 118 | + $this->backend->deleteCard($addressBookId, $cardUri); |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $response['token']; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param string $principal |
|
| 127 | + * @param string $id |
|
| 128 | + * @param array $properties |
|
| 129 | + * @return array|null |
|
| 130 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
| 131 | + */ |
|
| 132 | + public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
| 133 | + $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
| 134 | + if (!is_null($book)) { |
|
| 135 | + return $book; |
|
| 136 | + } |
|
| 137 | + $this->backend->createAddressBook($principal, $id, $properties); |
|
| 138 | + |
|
| 139 | + return $this->backend->getAddressBooksByUri($principal, $id); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Check if there is a valid certPath we should use |
|
| 144 | + * |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + protected function getCertPath() { |
|
| 148 | + |
|
| 149 | + // we already have a valid certPath |
|
| 150 | + if ($this->certPath !== '') { |
|
| 151 | + return $this->certPath; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** @var ICertificateManager $certManager */ |
|
| 155 | + $certManager = \OC::$server->getCertificateManager(null); |
|
| 156 | + $certPath = $certManager->getAbsoluteBundlePath(); |
|
| 157 | + if (file_exists($certPath)) { |
|
| 158 | + $this->certPath = $certPath; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + return $this->certPath; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * @param string $url |
|
| 166 | + * @param string $userName |
|
| 167 | + * @param string $addressBookUrl |
|
| 168 | + * @param string $sharedSecret |
|
| 169 | + * @return Client |
|
| 170 | + */ |
|
| 171 | + protected function getClient($url, $userName, $sharedSecret) { |
|
| 172 | + $settings = [ |
|
| 173 | + 'baseUri' => $url . '/', |
|
| 174 | + 'userName' => $userName, |
|
| 175 | + 'password' => $sharedSecret, |
|
| 176 | + ]; |
|
| 177 | + $client = new Client($settings); |
|
| 178 | + $certPath = $this->getCertPath(); |
|
| 179 | + $client->setThrowExceptions(true); |
|
| 180 | + |
|
| 181 | + if ($certPath !== '' && strpos($url, 'http://') !== 0) { |
|
| 182 | + $client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + return $client; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * @param string $url |
|
| 190 | + * @param string $userName |
|
| 191 | + * @param string $addressBookUrl |
|
| 192 | + * @param string $sharedSecret |
|
| 193 | + * @param string $syncToken |
|
| 194 | + * @return array |
|
| 195 | + */ |
|
| 196 | + protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) { |
|
| 197 | + $client = $this->getClient($url, $userName, $sharedSecret); |
|
| 198 | + |
|
| 199 | + $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
| 200 | + |
|
| 201 | + $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
| 202 | + 'Content-Type' => 'application/xml' |
|
| 203 | + ]); |
|
| 204 | + |
|
| 205 | + return $this->parseMultiStatus($response['body']); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @param string $url |
|
| 210 | + * @param string $userName |
|
| 211 | + * @param string $sharedSecret |
|
| 212 | + * @param string $resourcePath |
|
| 213 | + * @return array |
|
| 214 | + */ |
|
| 215 | + protected function download($url, $userName, $sharedSecret, $resourcePath) { |
|
| 216 | + $client = $this->getClient($url, $userName, $sharedSecret); |
|
| 217 | + return $client->request('GET', $resourcePath); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @param string|null $syncToken |
|
| 222 | + * @return string |
|
| 223 | + */ |
|
| 224 | + private function buildSyncCollectionRequestBody($syncToken) { |
|
| 225 | + |
|
| 226 | + $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
| 227 | + $dom->formatOutput = true; |
|
| 228 | + $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
| 229 | + $sync = $dom->createElement('d:sync-token', $syncToken); |
|
| 230 | + $prop = $dom->createElement('d:prop'); |
|
| 231 | + $cont = $dom->createElement('d:getcontenttype'); |
|
| 232 | + $etag = $dom->createElement('d:getetag'); |
|
| 233 | + |
|
| 234 | + $prop->appendChild($cont); |
|
| 235 | + $prop->appendChild($etag); |
|
| 236 | + $root->appendChild($sync); |
|
| 237 | + $root->appendChild($prop); |
|
| 238 | + $dom->appendChild($root); |
|
| 239 | + return $dom->saveXML(); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * @param string $body |
|
| 244 | + * @return array |
|
| 245 | + * @throws \Sabre\Xml\ParseException |
|
| 246 | + */ |
|
| 247 | + private function parseMultiStatus($body) { |
|
| 248 | + $xml = new Service(); |
|
| 249 | + |
|
| 250 | + /** @var MultiStatus $multiStatus */ |
|
| 251 | + $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
| 252 | + |
|
| 253 | + $result = []; |
|
| 254 | + foreach ($multiStatus->getResponses() as $response) { |
|
| 255 | + $result[$response->getHref()] = $response->getResponseProperties(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * @param IUser $user |
|
| 263 | + */ |
|
| 264 | + public function updateUser($user) { |
|
| 265 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 266 | + $addressBookId = $systemAddressBook['id']; |
|
| 267 | + $converter = new Converter($this->accountManager); |
|
| 268 | + $name = $user->getBackendClassName(); |
|
| 269 | + $userId = $user->getUID(); |
|
| 270 | + |
|
| 271 | + $cardId = "$name:$userId.vcf"; |
|
| 272 | + $card = $this->backend->getCard($addressBookId, $cardId); |
|
| 273 | + if ($user->isEnabled()) { |
|
| 274 | + if ($card === false) { |
|
| 275 | + $vCard = $converter->createCardFromUser($user); |
|
| 276 | + if ($vCard !== null) { |
|
| 277 | + $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 278 | + } |
|
| 279 | + } else { |
|
| 280 | + $vCard = $converter->createCardFromUser($user); |
|
| 281 | + if (is_null($vCard)) { |
|
| 282 | + $this->backend->deleteCard($addressBookId, $cardId); |
|
| 283 | + } else { |
|
| 284 | + $this->backend->updateCard($addressBookId, $cardId, $vCard->serialize()); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + } else { |
|
| 288 | + $this->backend->deleteCard($addressBookId, $cardId); |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * @param IUser|string $userOrCardId |
|
| 294 | + */ |
|
| 295 | + public function deleteUser($userOrCardId) { |
|
| 296 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 297 | + if ($userOrCardId instanceof IUser){ |
|
| 298 | + $name = $userOrCardId->getBackendClassName(); |
|
| 299 | + $userId = $userOrCardId->getUID(); |
|
| 300 | + |
|
| 301 | + $userOrCardId = "$name:$userId.vcf"; |
|
| 302 | + } |
|
| 303 | + $this->backend->deleteCard($systemAddressBook['id'], $userOrCardId); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * @return array|null |
|
| 308 | + */ |
|
| 309 | + public function getLocalSystemAddressBook() { |
|
| 310 | + if (is_null($this->localSystemAddressBook)) { |
|
| 311 | + $systemPrincipal = "principals/system/system"; |
|
| 312 | + $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
|
| 313 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
| 314 | + ]); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + return $this->localSystemAddressBook; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + public function syncInstance(\Closure $progressCallback = null) { |
|
| 321 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
| 322 | + $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
| 323 | + $this->updateUser($user); |
|
| 324 | + if (!is_null($progressCallback)) { |
|
| 325 | + $progressCallback(); |
|
| 326 | + } |
|
| 327 | + }); |
|
| 328 | + |
|
| 329 | + // remove no longer existing |
|
| 330 | + $allCards = $this->backend->getCards($systemAddressBook['id']); |
|
| 331 | + foreach($allCards as $card) { |
|
| 332 | + $vCard = Reader::read($card['carddata']); |
|
| 333 | + $uid = $vCard->UID->getValue(); |
|
| 334 | + // load backend and see if user exists |
|
| 335 | + if (!$this->userManager->userExists($uid)) { |
|
| 336 | + $this->deleteUser($card['uri']); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | 340 | |
| 341 | 341 | |
| 342 | 342 | } |
@@ -35,128 +35,128 @@ |
||
| 35 | 35 | use OCP\Share; |
| 36 | 36 | |
| 37 | 37 | class UserPlugin implements ISearchPlugin { |
| 38 | - /* @var bool */ |
|
| 39 | - protected $shareWithGroupOnly; |
|
| 40 | - protected $shareeEnumeration; |
|
| 41 | - |
|
| 42 | - /** @var IConfig */ |
|
| 43 | - private $config; |
|
| 44 | - /** @var IGroupManager */ |
|
| 45 | - private $groupManager; |
|
| 46 | - /** @var IUserSession */ |
|
| 47 | - private $userSession; |
|
| 48 | - /** @var IUserManager */ |
|
| 49 | - private $userManager; |
|
| 50 | - |
|
| 51 | - public function __construct(IConfig $config, IUserManager $userManager, IGroupManager $groupManager, IUserSession $userSession) { |
|
| 52 | - $this->config = $config; |
|
| 53 | - |
|
| 54 | - $this->groupManager = $groupManager; |
|
| 55 | - $this->userSession = $userSession; |
|
| 56 | - $this->userManager = $userManager; |
|
| 57 | - |
|
| 58 | - $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 59 | - $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function search($search, $limit, $offset, ISearchResult $searchResult) { |
|
| 63 | - $result = ['wide' => [], 'exact' => []]; |
|
| 64 | - $users = []; |
|
| 65 | - $hasMoreResults = false; |
|
| 66 | - |
|
| 67 | - $userGroups = []; |
|
| 68 | - if ($this->shareWithGroupOnly) { |
|
| 69 | - // Search in all the groups this user is part of |
|
| 70 | - $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
|
| 71 | - foreach ($userGroups as $userGroup) { |
|
| 72 | - $usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $limit, $offset); |
|
| 73 | - foreach ($usersTmp as $uid => $userDisplayName) { |
|
| 74 | - $users[$uid] = $userDisplayName; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - } else { |
|
| 78 | - // Search in all users |
|
| 79 | - $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset); |
|
| 80 | - |
|
| 81 | - foreach ($usersTmp as $user) { |
|
| 82 | - if ($user->isEnabled()) { // Don't keep deactivated users |
|
| 83 | - $users[$user->getUID()] = $user->getDisplayName(); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $this->takeOutCurrentUser($users); |
|
| 89 | - |
|
| 90 | - if (!$this->shareeEnumeration || count($users) < $limit) { |
|
| 91 | - $hasMoreResults = true; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $foundUserById = false; |
|
| 95 | - $lowerSearch = strtolower($search); |
|
| 96 | - foreach ($users as $uid => $userDisplayName) { |
|
| 97 | - if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) { |
|
| 98 | - if (strtolower($uid) === $lowerSearch) { |
|
| 99 | - $foundUserById = true; |
|
| 100 | - } |
|
| 101 | - $result['exact'][] = [ |
|
| 102 | - 'label' => $userDisplayName, |
|
| 103 | - 'value' => [ |
|
| 104 | - 'shareType' => Share::SHARE_TYPE_USER, |
|
| 105 | - 'shareWith' => $uid, |
|
| 106 | - ], |
|
| 107 | - ]; |
|
| 108 | - } else { |
|
| 109 | - $result['wide'][] = [ |
|
| 110 | - 'label' => $userDisplayName, |
|
| 111 | - 'value' => [ |
|
| 112 | - 'shareType' => Share::SHARE_TYPE_USER, |
|
| 113 | - 'shareWith' => $uid, |
|
| 114 | - ], |
|
| 115 | - ]; |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - if ($offset === 0 && !$foundUserById) { |
|
| 120 | - // On page one we try if the search result has a direct hit on the |
|
| 121 | - // user id and if so, we add that to the exact match list |
|
| 122 | - $user = $this->userManager->get($search); |
|
| 123 | - if ($user instanceof IUser) { |
|
| 124 | - $addUser = true; |
|
| 125 | - |
|
| 126 | - if ($this->shareWithGroupOnly) { |
|
| 127 | - // Only add, if we have a common group |
|
| 128 | - $commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user)); |
|
| 129 | - $addUser = !empty($commonGroups); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ($addUser) { |
|
| 133 | - $result['exact'][] = [ |
|
| 134 | - 'label' => $user->getDisplayName(), |
|
| 135 | - 'value' => [ |
|
| 136 | - 'shareType' => Share::SHARE_TYPE_USER, |
|
| 137 | - 'shareWith' => $user->getUID(), |
|
| 138 | - ], |
|
| 139 | - ]; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - if (!$this->shareeEnumeration) { |
|
| 145 | - $result['wide'] = []; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $type = new SearchResultType('users'); |
|
| 149 | - $searchResult->addResultSet($type, $result['wide'], $result['exact']); |
|
| 150 | - |
|
| 151 | - return $hasMoreResults; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - public function takeOutCurrentUser(array &$users) { |
|
| 155 | - $currentUser = $this->userSession->getUser(); |
|
| 156 | - if(!is_null($currentUser)) { |
|
| 157 | - if (isset($users[$currentUser->getUID()])) { |
|
| 158 | - unset($users[$currentUser->getUID()]); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - } |
|
| 38 | + /* @var bool */ |
|
| 39 | + protected $shareWithGroupOnly; |
|
| 40 | + protected $shareeEnumeration; |
|
| 41 | + |
|
| 42 | + /** @var IConfig */ |
|
| 43 | + private $config; |
|
| 44 | + /** @var IGroupManager */ |
|
| 45 | + private $groupManager; |
|
| 46 | + /** @var IUserSession */ |
|
| 47 | + private $userSession; |
|
| 48 | + /** @var IUserManager */ |
|
| 49 | + private $userManager; |
|
| 50 | + |
|
| 51 | + public function __construct(IConfig $config, IUserManager $userManager, IGroupManager $groupManager, IUserSession $userSession) { |
|
| 52 | + $this->config = $config; |
|
| 53 | + |
|
| 54 | + $this->groupManager = $groupManager; |
|
| 55 | + $this->userSession = $userSession; |
|
| 56 | + $this->userManager = $userManager; |
|
| 57 | + |
|
| 58 | + $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
| 59 | + $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function search($search, $limit, $offset, ISearchResult $searchResult) { |
|
| 63 | + $result = ['wide' => [], 'exact' => []]; |
|
| 64 | + $users = []; |
|
| 65 | + $hasMoreResults = false; |
|
| 66 | + |
|
| 67 | + $userGroups = []; |
|
| 68 | + if ($this->shareWithGroupOnly) { |
|
| 69 | + // Search in all the groups this user is part of |
|
| 70 | + $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
|
| 71 | + foreach ($userGroups as $userGroup) { |
|
| 72 | + $usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $limit, $offset); |
|
| 73 | + foreach ($usersTmp as $uid => $userDisplayName) { |
|
| 74 | + $users[$uid] = $userDisplayName; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + } else { |
|
| 78 | + // Search in all users |
|
| 79 | + $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset); |
|
| 80 | + |
|
| 81 | + foreach ($usersTmp as $user) { |
|
| 82 | + if ($user->isEnabled()) { // Don't keep deactivated users |
|
| 83 | + $users[$user->getUID()] = $user->getDisplayName(); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $this->takeOutCurrentUser($users); |
|
| 89 | + |
|
| 90 | + if (!$this->shareeEnumeration || count($users) < $limit) { |
|
| 91 | + $hasMoreResults = true; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $foundUserById = false; |
|
| 95 | + $lowerSearch = strtolower($search); |
|
| 96 | + foreach ($users as $uid => $userDisplayName) { |
|
| 97 | + if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) { |
|
| 98 | + if (strtolower($uid) === $lowerSearch) { |
|
| 99 | + $foundUserById = true; |
|
| 100 | + } |
|
| 101 | + $result['exact'][] = [ |
|
| 102 | + 'label' => $userDisplayName, |
|
| 103 | + 'value' => [ |
|
| 104 | + 'shareType' => Share::SHARE_TYPE_USER, |
|
| 105 | + 'shareWith' => $uid, |
|
| 106 | + ], |
|
| 107 | + ]; |
|
| 108 | + } else { |
|
| 109 | + $result['wide'][] = [ |
|
| 110 | + 'label' => $userDisplayName, |
|
| 111 | + 'value' => [ |
|
| 112 | + 'shareType' => Share::SHARE_TYPE_USER, |
|
| 113 | + 'shareWith' => $uid, |
|
| 114 | + ], |
|
| 115 | + ]; |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + if ($offset === 0 && !$foundUserById) { |
|
| 120 | + // On page one we try if the search result has a direct hit on the |
|
| 121 | + // user id and if so, we add that to the exact match list |
|
| 122 | + $user = $this->userManager->get($search); |
|
| 123 | + if ($user instanceof IUser) { |
|
| 124 | + $addUser = true; |
|
| 125 | + |
|
| 126 | + if ($this->shareWithGroupOnly) { |
|
| 127 | + // Only add, if we have a common group |
|
| 128 | + $commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user)); |
|
| 129 | + $addUser = !empty($commonGroups); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ($addUser) { |
|
| 133 | + $result['exact'][] = [ |
|
| 134 | + 'label' => $user->getDisplayName(), |
|
| 135 | + 'value' => [ |
|
| 136 | + 'shareType' => Share::SHARE_TYPE_USER, |
|
| 137 | + 'shareWith' => $user->getUID(), |
|
| 138 | + ], |
|
| 139 | + ]; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + if (!$this->shareeEnumeration) { |
|
| 145 | + $result['wide'] = []; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $type = new SearchResultType('users'); |
|
| 149 | + $searchResult->addResultSet($type, $result['wide'], $result['exact']); |
|
| 150 | + |
|
| 151 | + return $hasMoreResults; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + public function takeOutCurrentUser(array &$users) { |
|
| 155 | + $currentUser = $this->userSession->getUser(); |
|
| 156 | + if(!is_null($currentUser)) { |
|
| 157 | + if (isset($users[$currentUser->getUID()])) { |
|
| 158 | + unset($users[$currentUser->getUID()]); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | 162 | } |