@@ -26,71 +26,71 @@ |
||
26 | 26 | use Sabre\DAV\File; |
27 | 27 | |
28 | 28 | class AvatarNode extends File { |
29 | - private $ext; |
|
30 | - private $size; |
|
31 | - private $avatar; |
|
29 | + private $ext; |
|
30 | + private $size; |
|
31 | + private $avatar; |
|
32 | 32 | |
33 | - /** |
|
34 | - * AvatarNode constructor. |
|
35 | - * |
|
36 | - * @param integer $size |
|
37 | - * @param string $ext |
|
38 | - * @param IAvatar $avatar |
|
39 | - */ |
|
40 | - public function __construct($size, $ext, $avatar) { |
|
41 | - $this->size = $size; |
|
42 | - $this->ext = $ext; |
|
43 | - $this->avatar = $avatar; |
|
44 | - } |
|
33 | + /** |
|
34 | + * AvatarNode constructor. |
|
35 | + * |
|
36 | + * @param integer $size |
|
37 | + * @param string $ext |
|
38 | + * @param IAvatar $avatar |
|
39 | + */ |
|
40 | + public function __construct($size, $ext, $avatar) { |
|
41 | + $this->size = $size; |
|
42 | + $this->ext = $ext; |
|
43 | + $this->avatar = $avatar; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Returns the name of the node. |
|
48 | - * |
|
49 | - * This is used to generate the url. |
|
50 | - * |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function getName() { |
|
54 | - return "$this->size.$this->ext"; |
|
55 | - } |
|
46 | + /** |
|
47 | + * Returns the name of the node. |
|
48 | + * |
|
49 | + * This is used to generate the url. |
|
50 | + * |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function getName() { |
|
54 | + return "$this->size.$this->ext"; |
|
55 | + } |
|
56 | 56 | |
57 | - public function get() { |
|
58 | - $image = $this->avatar->get($this->size); |
|
59 | - $res = $image->resource(); |
|
57 | + public function get() { |
|
58 | + $image = $this->avatar->get($this->size); |
|
59 | + $res = $image->resource(); |
|
60 | 60 | |
61 | - ob_start(); |
|
62 | - if ($this->ext === 'png') { |
|
63 | - imagepng($res); |
|
64 | - } else { |
|
65 | - imagejpeg($res); |
|
66 | - } |
|
61 | + ob_start(); |
|
62 | + if ($this->ext === 'png') { |
|
63 | + imagepng($res); |
|
64 | + } else { |
|
65 | + imagejpeg($res); |
|
66 | + } |
|
67 | 67 | |
68 | - return ob_get_clean(); |
|
69 | - } |
|
68 | + return ob_get_clean(); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Returns the mime-type for a file |
|
73 | - * |
|
74 | - * If null is returned, we'll assume application/octet-stream |
|
75 | - * |
|
76 | - * @return string|null |
|
77 | - */ |
|
78 | - public function getContentType() { |
|
79 | - if ($this->ext === 'png') { |
|
80 | - return 'image/png'; |
|
81 | - } |
|
82 | - return 'image/jpeg'; |
|
83 | - } |
|
71 | + /** |
|
72 | + * Returns the mime-type for a file |
|
73 | + * |
|
74 | + * If null is returned, we'll assume application/octet-stream |
|
75 | + * |
|
76 | + * @return string|null |
|
77 | + */ |
|
78 | + public function getContentType() { |
|
79 | + if ($this->ext === 'png') { |
|
80 | + return 'image/png'; |
|
81 | + } |
|
82 | + return 'image/jpeg'; |
|
83 | + } |
|
84 | 84 | |
85 | - public function getETag() { |
|
86 | - return $this->avatar->getFile($this->size)->getEtag(); |
|
87 | - } |
|
85 | + public function getETag() { |
|
86 | + return $this->avatar->getFile($this->size)->getEtag(); |
|
87 | + } |
|
88 | 88 | |
89 | - public function getLastModified() { |
|
90 | - $timestamp = $this->avatar->getFile($this->size)->getMTime(); |
|
91 | - if (!empty($timestamp)) { |
|
92 | - return (int)$timestamp; |
|
93 | - } |
|
94 | - return $timestamp; |
|
95 | - } |
|
89 | + public function getLastModified() { |
|
90 | + $timestamp = $this->avatar->getFile($this->size)->getMTime(); |
|
91 | + if (!empty($timestamp)) { |
|
92 | + return (int)$timestamp; |
|
93 | + } |
|
94 | + return $timestamp; |
|
95 | + } |
|
96 | 96 | } |
@@ -89,7 +89,7 @@ |
||
89 | 89 | public function getLastModified() { |
90 | 90 | $timestamp = $this->avatar->getFile($this->size)->getMTime(); |
91 | 91 | if (!empty($timestamp)) { |
92 | - return (int)$timestamp; |
|
92 | + return (int) $timestamp; |
|
93 | 93 | } |
94 | 94 | return $timestamp; |
95 | 95 | } |
@@ -39,7 +39,7 @@ |
||
39 | 39 | |
40 | 40 | public function xmlSerialize(Writer $writer) { |
41 | 41 | foreach ($this->groups as $group) { |
42 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
42 | + $writer->writeElement('{'.self::NS_OWNCLOUD.'}group', $group); |
|
43 | 43 | } |
44 | 44 | } |
45 | 45 | } |
@@ -27,21 +27,21 @@ |
||
27 | 27 | use Sabre\Xml\XmlSerializable; |
28 | 28 | |
29 | 29 | class Groups implements XmlSerializable { |
30 | - public const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
30 | + public const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
31 | 31 | |
32 | - /** @var string[] of TYPE:CHECKSUM */ |
|
33 | - private $groups; |
|
32 | + /** @var string[] of TYPE:CHECKSUM */ |
|
33 | + private $groups; |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param string $groups |
|
37 | - */ |
|
38 | - public function __construct($groups) { |
|
39 | - $this->groups = $groups; |
|
40 | - } |
|
35 | + /** |
|
36 | + * @param string $groups |
|
37 | + */ |
|
38 | + public function __construct($groups) { |
|
39 | + $this->groups = $groups; |
|
40 | + } |
|
41 | 41 | |
42 | - public function xmlSerialize(Writer $writer) { |
|
43 | - foreach ($this->groups as $group) { |
|
44 | - $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
45 | - } |
|
46 | - } |
|
42 | + public function xmlSerialize(Writer $writer) { |
|
43 | + foreach ($this->groups as $group) { |
|
44 | + $writer->writeElement('{' . self::NS_OWNCLOUD . '}group', $group); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
97 | 97 | // remote server revoked access to the address book, remove it |
98 | 98 | $this->backend->deleteAddressBook($addressBookId); |
99 | - $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
99 | + $this->logger->info('Authorization failed, remove address book: '.$url, ['app' => 'dav']); |
|
100 | 100 | throw $ex; |
101 | 101 | } |
102 | 102 | } |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | */ |
170 | 170 | protected function getClient($url, $userName, $sharedSecret) { |
171 | 171 | $settings = [ |
172 | - 'baseUri' => $url . '/', |
|
172 | + 'baseUri' => $url.'/', |
|
173 | 173 | 'userName' => $userName, |
174 | 174 | 'password' => $sharedSecret, |
175 | 175 | ]; |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | if (is_null($this->localSystemAddressBook)) { |
307 | 307 | $systemPrincipal = "principals/system/system"; |
308 | 308 | $this->localSystemAddressBook = $this->ensureSystemAddressBookExists($systemPrincipal, 'system', [ |
309 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
309 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => 'System addressbook which holds all users of this instance' |
|
310 | 310 | ]); |
311 | 311 | } |
312 | 312 | |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | |
316 | 316 | public function syncInstance(\Closure $progressCallback = null) { |
317 | 317 | $systemAddressBook = $this->getLocalSystemAddressBook(); |
318 | - $this->userManager->callForAllUsers(function ($user) use ($systemAddressBook, $progressCallback) { |
|
318 | + $this->userManager->callForAllUsers(function($user) use ($systemAddressBook, $progressCallback) { |
|
319 | 319 | $this->updateUser($user); |
320 | 320 | if (!is_null($progressCallback)) { |
321 | 321 | $progressCallback(); |
@@ -42,299 +42,299 @@ |
||
42 | 42 | |
43 | 43 | class SyncService { |
44 | 44 | |
45 | - /** @var CardDavBackend */ |
|
46 | - private $backend; |
|
47 | - |
|
48 | - /** @var IUserManager */ |
|
49 | - private $userManager; |
|
50 | - |
|
51 | - /** @var ILogger */ |
|
52 | - private $logger; |
|
53 | - |
|
54 | - /** @var array */ |
|
55 | - private $localSystemAddressBook; |
|
56 | - |
|
57 | - /** @var Converter */ |
|
58 | - private $converter; |
|
59 | - |
|
60 | - /** @var string */ |
|
61 | - protected $certPath; |
|
62 | - |
|
63 | - /** |
|
64 | - * SyncService constructor. |
|
65 | - * |
|
66 | - * @param CardDavBackend $backend |
|
67 | - * @param IUserManager $userManager |
|
68 | - * @param ILogger $logger |
|
69 | - * @param AccountManager $accountManager |
|
70 | - */ |
|
71 | - public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger, Converter $converter) { |
|
72 | - $this->backend = $backend; |
|
73 | - $this->userManager = $userManager; |
|
74 | - $this->logger = $logger; |
|
75 | - $this->converter = $converter; |
|
76 | - $this->certPath = ''; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param string $url |
|
81 | - * @param string $userName |
|
82 | - * @param string $addressBookUrl |
|
83 | - * @param string $sharedSecret |
|
84 | - * @param string $syncToken |
|
85 | - * @param int $targetBookId |
|
86 | - * @param string $targetPrincipal |
|
87 | - * @param array $targetProperties |
|
88 | - * @return string |
|
89 | - * @throws \Exception |
|
90 | - */ |
|
91 | - public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
92 | - // 1. create addressbook |
|
93 | - $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
94 | - $addressBookId = $book['id']; |
|
95 | - |
|
96 | - // 2. query changes |
|
97 | - try { |
|
98 | - $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); |
|
99 | - } catch (ClientHttpException $ex) { |
|
100 | - if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
101 | - // remote server revoked access to the address book, remove it |
|
102 | - $this->backend->deleteAddressBook($addressBookId); |
|
103 | - $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
104 | - throw $ex; |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - // 3. apply changes |
|
109 | - // TODO: use multi-get for download |
|
110 | - foreach ($response['response'] as $resource => $status) { |
|
111 | - $cardUri = basename($resource); |
|
112 | - if (isset($status[200])) { |
|
113 | - $vCard = $this->download($url, $userName, $sharedSecret, $resource); |
|
114 | - $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
115 | - if ($existingCard === false) { |
|
116 | - $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
117 | - } else { |
|
118 | - $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
119 | - } |
|
120 | - } else { |
|
121 | - $this->backend->deleteCard($addressBookId, $cardUri); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - return $response['token']; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @param string $principal |
|
130 | - * @param string $id |
|
131 | - * @param array $properties |
|
132 | - * @return array|null |
|
133 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
134 | - */ |
|
135 | - public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
136 | - $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
137 | - if (!is_null($book)) { |
|
138 | - return $book; |
|
139 | - } |
|
140 | - $this->backend->createAddressBook($principal, $id, $properties); |
|
141 | - |
|
142 | - return $this->backend->getAddressBooksByUri($principal, $id); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Check if there is a valid certPath we should use |
|
147 | - * |
|
148 | - * @return string |
|
149 | - */ |
|
150 | - protected function getCertPath() { |
|
151 | - |
|
152 | - // we already have a valid certPath |
|
153 | - if ($this->certPath !== '') { |
|
154 | - return $this->certPath; |
|
155 | - } |
|
156 | - |
|
157 | - $certManager = \OC::$server->getCertificateManager(); |
|
158 | - $certPath = $certManager->getAbsoluteBundlePath(); |
|
159 | - if (file_exists($certPath)) { |
|
160 | - $this->certPath = $certPath; |
|
161 | - } |
|
162 | - |
|
163 | - return $this->certPath; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @param string $url |
|
168 | - * @param string $userName |
|
169 | - * @param string $addressBookUrl |
|
170 | - * @param string $sharedSecret |
|
171 | - * @return Client |
|
172 | - */ |
|
173 | - protected function getClient($url, $userName, $sharedSecret) { |
|
174 | - $settings = [ |
|
175 | - 'baseUri' => $url . '/', |
|
176 | - 'userName' => $userName, |
|
177 | - 'password' => $sharedSecret, |
|
178 | - ]; |
|
179 | - $client = new Client($settings); |
|
180 | - $certPath = $this->getCertPath(); |
|
181 | - $client->setThrowExceptions(true); |
|
182 | - |
|
183 | - if ($certPath !== '' && strpos($url, 'http://') !== 0) { |
|
184 | - $client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
185 | - } |
|
186 | - |
|
187 | - return $client; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @param string $url |
|
192 | - * @param string $userName |
|
193 | - * @param string $addressBookUrl |
|
194 | - * @param string $sharedSecret |
|
195 | - * @param string $syncToken |
|
196 | - * @return array |
|
197 | - */ |
|
198 | - protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) { |
|
199 | - $client = $this->getClient($url, $userName, $sharedSecret); |
|
200 | - |
|
201 | - $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
202 | - |
|
203 | - $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
204 | - 'Content-Type' => 'application/xml' |
|
205 | - ]); |
|
206 | - |
|
207 | - return $this->parseMultiStatus($response['body']); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * @param string $url |
|
212 | - * @param string $userName |
|
213 | - * @param string $sharedSecret |
|
214 | - * @param string $resourcePath |
|
215 | - * @return array |
|
216 | - */ |
|
217 | - protected function download($url, $userName, $sharedSecret, $resourcePath) { |
|
218 | - $client = $this->getClient($url, $userName, $sharedSecret); |
|
219 | - return $client->request('GET', $resourcePath); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @param string|null $syncToken |
|
224 | - * @return string |
|
225 | - */ |
|
226 | - private function buildSyncCollectionRequestBody($syncToken) { |
|
227 | - $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
228 | - $dom->formatOutput = true; |
|
229 | - $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
230 | - $sync = $dom->createElement('d:sync-token', $syncToken); |
|
231 | - $prop = $dom->createElement('d:prop'); |
|
232 | - $cont = $dom->createElement('d:getcontenttype'); |
|
233 | - $etag = $dom->createElement('d:getetag'); |
|
234 | - |
|
235 | - $prop->appendChild($cont); |
|
236 | - $prop->appendChild($etag); |
|
237 | - $root->appendChild($sync); |
|
238 | - $root->appendChild($prop); |
|
239 | - $dom->appendChild($root); |
|
240 | - return $dom->saveXML(); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @param string $body |
|
245 | - * @return array |
|
246 | - * @throws \Sabre\Xml\ParseException |
|
247 | - */ |
|
248 | - private function parseMultiStatus($body) { |
|
249 | - $xml = new Service(); |
|
250 | - |
|
251 | - /** @var MultiStatus $multiStatus */ |
|
252 | - $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
253 | - |
|
254 | - $result = []; |
|
255 | - foreach ($multiStatus->getResponses() as $response) { |
|
256 | - $result[$response->getHref()] = $response->getResponseProperties(); |
|
257 | - } |
|
258 | - |
|
259 | - return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * @param IUser $user |
|
264 | - */ |
|
265 | - public function updateUser(IUser $user) { |
|
266 | - $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
267 | - $addressBookId = $systemAddressBook['id']; |
|
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 = $this->converter->createCardFromUser($user); |
|
276 | - if ($vCard !== null) { |
|
277 | - $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
278 | - } |
|
279 | - } else { |
|
280 | - $vCard = $this->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 | - } |
|
45 | + /** @var CardDavBackend */ |
|
46 | + private $backend; |
|
47 | + |
|
48 | + /** @var IUserManager */ |
|
49 | + private $userManager; |
|
50 | + |
|
51 | + /** @var ILogger */ |
|
52 | + private $logger; |
|
53 | + |
|
54 | + /** @var array */ |
|
55 | + private $localSystemAddressBook; |
|
56 | + |
|
57 | + /** @var Converter */ |
|
58 | + private $converter; |
|
59 | + |
|
60 | + /** @var string */ |
|
61 | + protected $certPath; |
|
62 | + |
|
63 | + /** |
|
64 | + * SyncService constructor. |
|
65 | + * |
|
66 | + * @param CardDavBackend $backend |
|
67 | + * @param IUserManager $userManager |
|
68 | + * @param ILogger $logger |
|
69 | + * @param AccountManager $accountManager |
|
70 | + */ |
|
71 | + public function __construct(CardDavBackend $backend, IUserManager $userManager, ILogger $logger, Converter $converter) { |
|
72 | + $this->backend = $backend; |
|
73 | + $this->userManager = $userManager; |
|
74 | + $this->logger = $logger; |
|
75 | + $this->converter = $converter; |
|
76 | + $this->certPath = ''; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param string $url |
|
81 | + * @param string $userName |
|
82 | + * @param string $addressBookUrl |
|
83 | + * @param string $sharedSecret |
|
84 | + * @param string $syncToken |
|
85 | + * @param int $targetBookId |
|
86 | + * @param string $targetPrincipal |
|
87 | + * @param array $targetProperties |
|
88 | + * @return string |
|
89 | + * @throws \Exception |
|
90 | + */ |
|
91 | + public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) { |
|
92 | + // 1. create addressbook |
|
93 | + $book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties); |
|
94 | + $addressBookId = $book['id']; |
|
95 | + |
|
96 | + // 2. query changes |
|
97 | + try { |
|
98 | + $response = $this->requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken); |
|
99 | + } catch (ClientHttpException $ex) { |
|
100 | + if ($ex->getCode() === Http::STATUS_UNAUTHORIZED) { |
|
101 | + // remote server revoked access to the address book, remove it |
|
102 | + $this->backend->deleteAddressBook($addressBookId); |
|
103 | + $this->logger->info('Authorization failed, remove address book: ' . $url, ['app' => 'dav']); |
|
104 | + throw $ex; |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + // 3. apply changes |
|
109 | + // TODO: use multi-get for download |
|
110 | + foreach ($response['response'] as $resource => $status) { |
|
111 | + $cardUri = basename($resource); |
|
112 | + if (isset($status[200])) { |
|
113 | + $vCard = $this->download($url, $userName, $sharedSecret, $resource); |
|
114 | + $existingCard = $this->backend->getCard($addressBookId, $cardUri); |
|
115 | + if ($existingCard === false) { |
|
116 | + $this->backend->createCard($addressBookId, $cardUri, $vCard['body']); |
|
117 | + } else { |
|
118 | + $this->backend->updateCard($addressBookId, $cardUri, $vCard['body']); |
|
119 | + } |
|
120 | + } else { |
|
121 | + $this->backend->deleteCard($addressBookId, $cardUri); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + return $response['token']; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @param string $principal |
|
130 | + * @param string $id |
|
131 | + * @param array $properties |
|
132 | + * @return array|null |
|
133 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
134 | + */ |
|
135 | + public function ensureSystemAddressBookExists($principal, $id, $properties) { |
|
136 | + $book = $this->backend->getAddressBooksByUri($principal, $id); |
|
137 | + if (!is_null($book)) { |
|
138 | + return $book; |
|
139 | + } |
|
140 | + $this->backend->createAddressBook($principal, $id, $properties); |
|
141 | + |
|
142 | + return $this->backend->getAddressBooksByUri($principal, $id); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Check if there is a valid certPath we should use |
|
147 | + * |
|
148 | + * @return string |
|
149 | + */ |
|
150 | + protected function getCertPath() { |
|
151 | + |
|
152 | + // we already have a valid certPath |
|
153 | + if ($this->certPath !== '') { |
|
154 | + return $this->certPath; |
|
155 | + } |
|
156 | + |
|
157 | + $certManager = \OC::$server->getCertificateManager(); |
|
158 | + $certPath = $certManager->getAbsoluteBundlePath(); |
|
159 | + if (file_exists($certPath)) { |
|
160 | + $this->certPath = $certPath; |
|
161 | + } |
|
162 | + |
|
163 | + return $this->certPath; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @param string $url |
|
168 | + * @param string $userName |
|
169 | + * @param string $addressBookUrl |
|
170 | + * @param string $sharedSecret |
|
171 | + * @return Client |
|
172 | + */ |
|
173 | + protected function getClient($url, $userName, $sharedSecret) { |
|
174 | + $settings = [ |
|
175 | + 'baseUri' => $url . '/', |
|
176 | + 'userName' => $userName, |
|
177 | + 'password' => $sharedSecret, |
|
178 | + ]; |
|
179 | + $client = new Client($settings); |
|
180 | + $certPath = $this->getCertPath(); |
|
181 | + $client->setThrowExceptions(true); |
|
182 | + |
|
183 | + if ($certPath !== '' && strpos($url, 'http://') !== 0) { |
|
184 | + $client->addCurlSetting(CURLOPT_CAINFO, $this->certPath); |
|
185 | + } |
|
186 | + |
|
187 | + return $client; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @param string $url |
|
192 | + * @param string $userName |
|
193 | + * @param string $addressBookUrl |
|
194 | + * @param string $sharedSecret |
|
195 | + * @param string $syncToken |
|
196 | + * @return array |
|
197 | + */ |
|
198 | + protected function requestSyncReport($url, $userName, $addressBookUrl, $sharedSecret, $syncToken) { |
|
199 | + $client = $this->getClient($url, $userName, $sharedSecret); |
|
200 | + |
|
201 | + $body = $this->buildSyncCollectionRequestBody($syncToken); |
|
202 | + |
|
203 | + $response = $client->request('REPORT', $addressBookUrl, $body, [ |
|
204 | + 'Content-Type' => 'application/xml' |
|
205 | + ]); |
|
206 | + |
|
207 | + return $this->parseMultiStatus($response['body']); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * @param string $url |
|
212 | + * @param string $userName |
|
213 | + * @param string $sharedSecret |
|
214 | + * @param string $resourcePath |
|
215 | + * @return array |
|
216 | + */ |
|
217 | + protected function download($url, $userName, $sharedSecret, $resourcePath) { |
|
218 | + $client = $this->getClient($url, $userName, $sharedSecret); |
|
219 | + return $client->request('GET', $resourcePath); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @param string|null $syncToken |
|
224 | + * @return string |
|
225 | + */ |
|
226 | + private function buildSyncCollectionRequestBody($syncToken) { |
|
227 | + $dom = new \DOMDocument('1.0', 'UTF-8'); |
|
228 | + $dom->formatOutput = true; |
|
229 | + $root = $dom->createElementNS('DAV:', 'd:sync-collection'); |
|
230 | + $sync = $dom->createElement('d:sync-token', $syncToken); |
|
231 | + $prop = $dom->createElement('d:prop'); |
|
232 | + $cont = $dom->createElement('d:getcontenttype'); |
|
233 | + $etag = $dom->createElement('d:getetag'); |
|
234 | + |
|
235 | + $prop->appendChild($cont); |
|
236 | + $prop->appendChild($etag); |
|
237 | + $root->appendChild($sync); |
|
238 | + $root->appendChild($prop); |
|
239 | + $dom->appendChild($root); |
|
240 | + return $dom->saveXML(); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @param string $body |
|
245 | + * @return array |
|
246 | + * @throws \Sabre\Xml\ParseException |
|
247 | + */ |
|
248 | + private function parseMultiStatus($body) { |
|
249 | + $xml = new Service(); |
|
250 | + |
|
251 | + /** @var MultiStatus $multiStatus */ |
|
252 | + $multiStatus = $xml->expect('{DAV:}multistatus', $body); |
|
253 | + |
|
254 | + $result = []; |
|
255 | + foreach ($multiStatus->getResponses() as $response) { |
|
256 | + $result[$response->getHref()] = $response->getResponseProperties(); |
|
257 | + } |
|
258 | + |
|
259 | + return ['response' => $result, 'token' => $multiStatus->getSyncToken()]; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * @param IUser $user |
|
264 | + */ |
|
265 | + public function updateUser(IUser $user) { |
|
266 | + $systemAddressBook = $this->getLocalSystemAddressBook(); |
|
267 | + $addressBookId = $systemAddressBook['id']; |
|
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 = $this->converter->createCardFromUser($user); |
|
276 | + if ($vCard !== null) { |
|
277 | + $this->backend->createCard($addressBookId, $cardId, $vCard->serialize()); |
|
278 | + } |
|
279 | + } else { |
|
280 | + $vCard = $this->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 | } |
@@ -29,47 +29,47 @@ |
||
29 | 29 | */ |
30 | 30 | interface IShareable extends INode { |
31 | 31 | |
32 | - /** |
|
33 | - * Updates the list of shares. |
|
34 | - * |
|
35 | - * The first array is a list of people that are to be added to the |
|
36 | - * resource. |
|
37 | - * |
|
38 | - * Every element in the add array has the following properties: |
|
39 | - * * href - A url. Usually a mailto: address |
|
40 | - * * commonName - Usually a first and last name, or false |
|
41 | - * * summary - A description of the share, can also be false |
|
42 | - * * readOnly - A boolean value |
|
43 | - * |
|
44 | - * Every element in the remove array is just the address string. |
|
45 | - * |
|
46 | - * @param array $add |
|
47 | - * @param array $remove |
|
48 | - * @return void |
|
49 | - */ |
|
50 | - public function updateShares(array $add, array $remove); |
|
32 | + /** |
|
33 | + * Updates the list of shares. |
|
34 | + * |
|
35 | + * The first array is a list of people that are to be added to the |
|
36 | + * resource. |
|
37 | + * |
|
38 | + * Every element in the add array has the following properties: |
|
39 | + * * href - A url. Usually a mailto: address |
|
40 | + * * commonName - Usually a first and last name, or false |
|
41 | + * * summary - A description of the share, can also be false |
|
42 | + * * readOnly - A boolean value |
|
43 | + * |
|
44 | + * Every element in the remove array is just the address string. |
|
45 | + * |
|
46 | + * @param array $add |
|
47 | + * @param array $remove |
|
48 | + * @return void |
|
49 | + */ |
|
50 | + public function updateShares(array $add, array $remove); |
|
51 | 51 | |
52 | - /** |
|
53 | - * Returns the list of people whom this resource is shared with. |
|
54 | - * |
|
55 | - * Every element in this array should have the following properties: |
|
56 | - * * href - Often a mailto: address |
|
57 | - * * commonName - Optional, for example a first + last name |
|
58 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
59 | - * * readOnly - boolean |
|
60 | - * * summary - Optional, a description for the share |
|
61 | - * |
|
62 | - * @return array |
|
63 | - */ |
|
64 | - public function getShares(); |
|
52 | + /** |
|
53 | + * Returns the list of people whom this resource is shared with. |
|
54 | + * |
|
55 | + * Every element in this array should have the following properties: |
|
56 | + * * href - Often a mailto: address |
|
57 | + * * commonName - Optional, for example a first + last name |
|
58 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
59 | + * * readOnly - boolean |
|
60 | + * * summary - Optional, a description for the share |
|
61 | + * |
|
62 | + * @return array |
|
63 | + */ |
|
64 | + public function getShares(); |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return int |
|
68 | - */ |
|
69 | - public function getResourceId(); |
|
66 | + /** |
|
67 | + * @return int |
|
68 | + */ |
|
69 | + public function getResourceId(); |
|
70 | 70 | |
71 | - /** |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - public function getOwner(); |
|
71 | + /** |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + public function getOwner(); |
|
75 | 75 | } |
@@ -41,120 +41,120 @@ |
||
41 | 41 | */ |
42 | 42 | class Invite implements XmlSerializable { |
43 | 43 | |
44 | - /** |
|
45 | - * The list of users a calendar has been shared to. |
|
46 | - * |
|
47 | - * @var array |
|
48 | - */ |
|
49 | - protected $users; |
|
44 | + /** |
|
45 | + * The list of users a calendar has been shared to. |
|
46 | + * |
|
47 | + * @var array |
|
48 | + */ |
|
49 | + protected $users; |
|
50 | 50 | |
51 | - /** |
|
52 | - * The organizer contains information about the person who shared the |
|
53 | - * object. |
|
54 | - * |
|
55 | - * @var array|null |
|
56 | - */ |
|
57 | - protected $organizer; |
|
51 | + /** |
|
52 | + * The organizer contains information about the person who shared the |
|
53 | + * object. |
|
54 | + * |
|
55 | + * @var array|null |
|
56 | + */ |
|
57 | + protected $organizer; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Creates the property. |
|
61 | - * |
|
62 | - * Users is an array. Each element of the array has the following |
|
63 | - * properties: |
|
64 | - * |
|
65 | - * * href - Often a mailto: address |
|
66 | - * * commonName - Optional, for example a first and lastname for a user. |
|
67 | - * * status - One of the SharingPlugin::STATUS_* constants. |
|
68 | - * * readOnly - true or false |
|
69 | - * * summary - Optional, description of the share |
|
70 | - * |
|
71 | - * The organizer key is optional to specify. It's only useful when a |
|
72 | - * 'sharee' requests the sharing information. |
|
73 | - * |
|
74 | - * The organizer may have the following properties: |
|
75 | - * * href - Often a mailto: address. |
|
76 | - * * commonName - Optional human-readable name. |
|
77 | - * * firstName - Optional first name. |
|
78 | - * * lastName - Optional last name. |
|
79 | - * |
|
80 | - * If you wonder why these two structures are so different, I guess a |
|
81 | - * valid answer is that the current spec is still a draft. |
|
82 | - * |
|
83 | - * @param array $users |
|
84 | - */ |
|
85 | - public function __construct(array $users, array $organizer = null) { |
|
86 | - $this->users = $users; |
|
87 | - $this->organizer = $organizer; |
|
88 | - } |
|
59 | + /** |
|
60 | + * Creates the property. |
|
61 | + * |
|
62 | + * Users is an array. Each element of the array has the following |
|
63 | + * properties: |
|
64 | + * |
|
65 | + * * href - Often a mailto: address |
|
66 | + * * commonName - Optional, for example a first and lastname for a user. |
|
67 | + * * status - One of the SharingPlugin::STATUS_* constants. |
|
68 | + * * readOnly - true or false |
|
69 | + * * summary - Optional, description of the share |
|
70 | + * |
|
71 | + * The organizer key is optional to specify. It's only useful when a |
|
72 | + * 'sharee' requests the sharing information. |
|
73 | + * |
|
74 | + * The organizer may have the following properties: |
|
75 | + * * href - Often a mailto: address. |
|
76 | + * * commonName - Optional human-readable name. |
|
77 | + * * firstName - Optional first name. |
|
78 | + * * lastName - Optional last name. |
|
79 | + * |
|
80 | + * If you wonder why these two structures are so different, I guess a |
|
81 | + * valid answer is that the current spec is still a draft. |
|
82 | + * |
|
83 | + * @param array $users |
|
84 | + */ |
|
85 | + public function __construct(array $users, array $organizer = null) { |
|
86 | + $this->users = $users; |
|
87 | + $this->organizer = $organizer; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Returns the list of users, as it was passed to the constructor. |
|
92 | - * |
|
93 | - * @return array |
|
94 | - */ |
|
95 | - public function getValue() { |
|
96 | - return $this->users; |
|
97 | - } |
|
90 | + /** |
|
91 | + * Returns the list of users, as it was passed to the constructor. |
|
92 | + * |
|
93 | + * @return array |
|
94 | + */ |
|
95 | + public function getValue() { |
|
96 | + return $this->users; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * The xmlSerialize metod is called during xml writing. |
|
101 | - * |
|
102 | - * Use the $writer argument to write its own xml serialization. |
|
103 | - * |
|
104 | - * An important note: do _not_ create a parent element. Any element |
|
105 | - * implementing XmlSerializble should only ever write what's considered |
|
106 | - * its 'inner xml'. |
|
107 | - * |
|
108 | - * The parent of the current element is responsible for writing a |
|
109 | - * containing element. |
|
110 | - * |
|
111 | - * This allows serializers to be re-used for different element names. |
|
112 | - * |
|
113 | - * If you are opening new elements, you must also close them again. |
|
114 | - * |
|
115 | - * @param Writer $writer |
|
116 | - * @return void |
|
117 | - */ |
|
118 | - public function xmlSerialize(Writer $writer) { |
|
119 | - $cs = '{' . Plugin::NS_OWNCLOUD . '}'; |
|
99 | + /** |
|
100 | + * The xmlSerialize metod is called during xml writing. |
|
101 | + * |
|
102 | + * Use the $writer argument to write its own xml serialization. |
|
103 | + * |
|
104 | + * An important note: do _not_ create a parent element. Any element |
|
105 | + * implementing XmlSerializble should only ever write what's considered |
|
106 | + * its 'inner xml'. |
|
107 | + * |
|
108 | + * The parent of the current element is responsible for writing a |
|
109 | + * containing element. |
|
110 | + * |
|
111 | + * This allows serializers to be re-used for different element names. |
|
112 | + * |
|
113 | + * If you are opening new elements, you must also close them again. |
|
114 | + * |
|
115 | + * @param Writer $writer |
|
116 | + * @return void |
|
117 | + */ |
|
118 | + public function xmlSerialize(Writer $writer) { |
|
119 | + $cs = '{' . Plugin::NS_OWNCLOUD . '}'; |
|
120 | 120 | |
121 | - if (!is_null($this->organizer)) { |
|
122 | - $writer->startElement($cs . 'organizer'); |
|
123 | - $writer->writeElement('{DAV:}href', $this->organizer['href']); |
|
121 | + if (!is_null($this->organizer)) { |
|
122 | + $writer->startElement($cs . 'organizer'); |
|
123 | + $writer->writeElement('{DAV:}href', $this->organizer['href']); |
|
124 | 124 | |
125 | - if (isset($this->organizer['commonName']) && $this->organizer['commonName']) { |
|
126 | - $writer->writeElement($cs . 'common-name', $this->organizer['commonName']); |
|
127 | - } |
|
128 | - if (isset($this->organizer['firstName']) && $this->organizer['firstName']) { |
|
129 | - $writer->writeElement($cs . 'first-name', $this->organizer['firstName']); |
|
130 | - } |
|
131 | - if (isset($this->organizer['lastName']) && $this->organizer['lastName']) { |
|
132 | - $writer->writeElement($cs . 'last-name', $this->organizer['lastName']); |
|
133 | - } |
|
134 | - $writer->endElement(); // organizer |
|
135 | - } |
|
125 | + if (isset($this->organizer['commonName']) && $this->organizer['commonName']) { |
|
126 | + $writer->writeElement($cs . 'common-name', $this->organizer['commonName']); |
|
127 | + } |
|
128 | + if (isset($this->organizer['firstName']) && $this->organizer['firstName']) { |
|
129 | + $writer->writeElement($cs . 'first-name', $this->organizer['firstName']); |
|
130 | + } |
|
131 | + if (isset($this->organizer['lastName']) && $this->organizer['lastName']) { |
|
132 | + $writer->writeElement($cs . 'last-name', $this->organizer['lastName']); |
|
133 | + } |
|
134 | + $writer->endElement(); // organizer |
|
135 | + } |
|
136 | 136 | |
137 | - foreach ($this->users as $user) { |
|
138 | - $writer->startElement($cs . 'user'); |
|
139 | - $writer->writeElement('{DAV:}href', $user['href']); |
|
140 | - if (isset($user['commonName']) && $user['commonName']) { |
|
141 | - $writer->writeElement($cs . 'common-name', $user['commonName']); |
|
142 | - } |
|
143 | - $writer->writeElement($cs . 'invite-accepted'); |
|
137 | + foreach ($this->users as $user) { |
|
138 | + $writer->startElement($cs . 'user'); |
|
139 | + $writer->writeElement('{DAV:}href', $user['href']); |
|
140 | + if (isset($user['commonName']) && $user['commonName']) { |
|
141 | + $writer->writeElement($cs . 'common-name', $user['commonName']); |
|
142 | + } |
|
143 | + $writer->writeElement($cs . 'invite-accepted'); |
|
144 | 144 | |
145 | - $writer->startElement($cs . 'access'); |
|
146 | - if ($user['readOnly']) { |
|
147 | - $writer->writeElement($cs . 'read'); |
|
148 | - } else { |
|
149 | - $writer->writeElement($cs . 'read-write'); |
|
150 | - } |
|
151 | - $writer->endElement(); // access |
|
145 | + $writer->startElement($cs . 'access'); |
|
146 | + if ($user['readOnly']) { |
|
147 | + $writer->writeElement($cs . 'read'); |
|
148 | + } else { |
|
149 | + $writer->writeElement($cs . 'read-write'); |
|
150 | + } |
|
151 | + $writer->endElement(); // access |
|
152 | 152 | |
153 | - if (isset($user['summary']) && $user['summary']) { |
|
154 | - $writer->writeElement($cs . 'summary', $user['summary']); |
|
155 | - } |
|
153 | + if (isset($user['summary']) && $user['summary']) { |
|
154 | + $writer->writeElement($cs . 'summary', $user['summary']); |
|
155 | + } |
|
156 | 156 | |
157 | - $writer->endElement(); //user |
|
158 | - } |
|
159 | - } |
|
157 | + $writer->endElement(); //user |
|
158 | + } |
|
159 | + } |
|
160 | 160 | } |
@@ -116,42 +116,42 @@ |
||
116 | 116 | * @return void |
117 | 117 | */ |
118 | 118 | public function xmlSerialize(Writer $writer) { |
119 | - $cs = '{' . Plugin::NS_OWNCLOUD . '}'; |
|
119 | + $cs = '{'.Plugin::NS_OWNCLOUD.'}'; |
|
120 | 120 | |
121 | 121 | if (!is_null($this->organizer)) { |
122 | - $writer->startElement($cs . 'organizer'); |
|
122 | + $writer->startElement($cs.'organizer'); |
|
123 | 123 | $writer->writeElement('{DAV:}href', $this->organizer['href']); |
124 | 124 | |
125 | 125 | if (isset($this->organizer['commonName']) && $this->organizer['commonName']) { |
126 | - $writer->writeElement($cs . 'common-name', $this->organizer['commonName']); |
|
126 | + $writer->writeElement($cs.'common-name', $this->organizer['commonName']); |
|
127 | 127 | } |
128 | 128 | if (isset($this->organizer['firstName']) && $this->organizer['firstName']) { |
129 | - $writer->writeElement($cs . 'first-name', $this->organizer['firstName']); |
|
129 | + $writer->writeElement($cs.'first-name', $this->organizer['firstName']); |
|
130 | 130 | } |
131 | 131 | if (isset($this->organizer['lastName']) && $this->organizer['lastName']) { |
132 | - $writer->writeElement($cs . 'last-name', $this->organizer['lastName']); |
|
132 | + $writer->writeElement($cs.'last-name', $this->organizer['lastName']); |
|
133 | 133 | } |
134 | 134 | $writer->endElement(); // organizer |
135 | 135 | } |
136 | 136 | |
137 | 137 | foreach ($this->users as $user) { |
138 | - $writer->startElement($cs . 'user'); |
|
138 | + $writer->startElement($cs.'user'); |
|
139 | 139 | $writer->writeElement('{DAV:}href', $user['href']); |
140 | 140 | if (isset($user['commonName']) && $user['commonName']) { |
141 | - $writer->writeElement($cs . 'common-name', $user['commonName']); |
|
141 | + $writer->writeElement($cs.'common-name', $user['commonName']); |
|
142 | 142 | } |
143 | - $writer->writeElement($cs . 'invite-accepted'); |
|
143 | + $writer->writeElement($cs.'invite-accepted'); |
|
144 | 144 | |
145 | - $writer->startElement($cs . 'access'); |
|
145 | + $writer->startElement($cs.'access'); |
|
146 | 146 | if ($user['readOnly']) { |
147 | - $writer->writeElement($cs . 'read'); |
|
147 | + $writer->writeElement($cs.'read'); |
|
148 | 148 | } else { |
149 | - $writer->writeElement($cs . 'read-write'); |
|
149 | + $writer->writeElement($cs.'read-write'); |
|
150 | 150 | } |
151 | 151 | $writer->endElement(); // access |
152 | 152 | |
153 | 153 | if (isset($user['summary']) && $user['summary']) { |
154 | - $writer->writeElement($cs . 'summary', $user['summary']); |
|
154 | + $writer->writeElement($cs.'summary', $user['summary']); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | $writer->endElement(); //user |
@@ -26,54 +26,54 @@ |
||
26 | 26 | use Sabre\Xml\XmlDeserializable; |
27 | 27 | |
28 | 28 | class ShareRequest implements XmlDeserializable { |
29 | - public $set = []; |
|
29 | + public $set = []; |
|
30 | 30 | |
31 | - public $remove = []; |
|
31 | + public $remove = []; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Constructor |
|
35 | - * |
|
36 | - * @param array $set |
|
37 | - * @param array $remove |
|
38 | - */ |
|
39 | - public function __construct(array $set, array $remove) { |
|
40 | - $this->set = $set; |
|
41 | - $this->remove = $remove; |
|
42 | - } |
|
33 | + /** |
|
34 | + * Constructor |
|
35 | + * |
|
36 | + * @param array $set |
|
37 | + * @param array $remove |
|
38 | + */ |
|
39 | + public function __construct(array $set, array $remove) { |
|
40 | + $this->set = $set; |
|
41 | + $this->remove = $remove; |
|
42 | + } |
|
43 | 43 | |
44 | - public static function xmlDeserialize(Reader $reader) { |
|
45 | - $elements = $reader->parseInnerTree([ |
|
46 | - '{' . Plugin::NS_OWNCLOUD. '}set' => 'Sabre\\Xml\\Element\\KeyValue', |
|
47 | - '{' . Plugin::NS_OWNCLOUD . '}remove' => 'Sabre\\Xml\\Element\\KeyValue', |
|
48 | - ]); |
|
44 | + public static function xmlDeserialize(Reader $reader) { |
|
45 | + $elements = $reader->parseInnerTree([ |
|
46 | + '{' . Plugin::NS_OWNCLOUD. '}set' => 'Sabre\\Xml\\Element\\KeyValue', |
|
47 | + '{' . Plugin::NS_OWNCLOUD . '}remove' => 'Sabre\\Xml\\Element\\KeyValue', |
|
48 | + ]); |
|
49 | 49 | |
50 | - $set = []; |
|
51 | - $remove = []; |
|
50 | + $set = []; |
|
51 | + $remove = []; |
|
52 | 52 | |
53 | - foreach ($elements as $elem) { |
|
54 | - switch ($elem['name']) { |
|
53 | + foreach ($elements as $elem) { |
|
54 | + switch ($elem['name']) { |
|
55 | 55 | |
56 | - case '{' . Plugin::NS_OWNCLOUD . '}set': |
|
57 | - $sharee = $elem['value']; |
|
56 | + case '{' . Plugin::NS_OWNCLOUD . '}set': |
|
57 | + $sharee = $elem['value']; |
|
58 | 58 | |
59 | - $sumElem = '{' . Plugin::NS_OWNCLOUD . '}summary'; |
|
60 | - $commonName = '{' . Plugin::NS_OWNCLOUD . '}common-name'; |
|
59 | + $sumElem = '{' . Plugin::NS_OWNCLOUD . '}summary'; |
|
60 | + $commonName = '{' . Plugin::NS_OWNCLOUD . '}common-name'; |
|
61 | 61 | |
62 | - $set[] = [ |
|
63 | - 'href' => $sharee['{DAV:}href'], |
|
64 | - 'commonName' => isset($sharee[$commonName]) ? $sharee[$commonName] : null, |
|
65 | - 'summary' => isset($sharee[$sumElem]) ? $sharee[$sumElem] : null, |
|
66 | - 'readOnly' => !array_key_exists('{' . Plugin::NS_OWNCLOUD . '}read-write', $sharee), |
|
67 | - ]; |
|
68 | - break; |
|
62 | + $set[] = [ |
|
63 | + 'href' => $sharee['{DAV:}href'], |
|
64 | + 'commonName' => isset($sharee[$commonName]) ? $sharee[$commonName] : null, |
|
65 | + 'summary' => isset($sharee[$sumElem]) ? $sharee[$sumElem] : null, |
|
66 | + 'readOnly' => !array_key_exists('{' . Plugin::NS_OWNCLOUD . '}read-write', $sharee), |
|
67 | + ]; |
|
68 | + break; |
|
69 | 69 | |
70 | - case '{' . Plugin::NS_OWNCLOUD . '}remove': |
|
71 | - $remove[] = $elem['value']['{DAV:}href']; |
|
72 | - break; |
|
70 | + case '{' . Plugin::NS_OWNCLOUD . '}remove': |
|
71 | + $remove[] = $elem['value']['{DAV:}href']; |
|
72 | + break; |
|
73 | 73 | |
74 | - } |
|
75 | - } |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - return new self($set, $remove); |
|
78 | - } |
|
77 | + return new self($set, $remove); |
|
78 | + } |
|
79 | 79 | } |
@@ -43,8 +43,8 @@ discard block |
||
43 | 43 | |
44 | 44 | public static function xmlDeserialize(Reader $reader) { |
45 | 45 | $elements = $reader->parseInnerTree([ |
46 | - '{' . Plugin::NS_OWNCLOUD. '}set' => 'Sabre\\Xml\\Element\\KeyValue', |
|
47 | - '{' . Plugin::NS_OWNCLOUD . '}remove' => 'Sabre\\Xml\\Element\\KeyValue', |
|
46 | + '{'.Plugin::NS_OWNCLOUD.'}set' => 'Sabre\\Xml\\Element\\KeyValue', |
|
47 | + '{'.Plugin::NS_OWNCLOUD.'}remove' => 'Sabre\\Xml\\Element\\KeyValue', |
|
48 | 48 | ]); |
49 | 49 | |
50 | 50 | $set = []; |
@@ -53,21 +53,21 @@ discard block |
||
53 | 53 | foreach ($elements as $elem) { |
54 | 54 | switch ($elem['name']) { |
55 | 55 | |
56 | - case '{' . Plugin::NS_OWNCLOUD . '}set': |
|
56 | + case '{'.Plugin::NS_OWNCLOUD.'}set': |
|
57 | 57 | $sharee = $elem['value']; |
58 | 58 | |
59 | - $sumElem = '{' . Plugin::NS_OWNCLOUD . '}summary'; |
|
60 | - $commonName = '{' . Plugin::NS_OWNCLOUD . '}common-name'; |
|
59 | + $sumElem = '{'.Plugin::NS_OWNCLOUD.'}summary'; |
|
60 | + $commonName = '{'.Plugin::NS_OWNCLOUD.'}common-name'; |
|
61 | 61 | |
62 | 62 | $set[] = [ |
63 | 63 | 'href' => $sharee['{DAV:}href'], |
64 | 64 | 'commonName' => isset($sharee[$commonName]) ? $sharee[$commonName] : null, |
65 | 65 | 'summary' => isset($sharee[$sumElem]) ? $sharee[$sumElem] : null, |
66 | - 'readOnly' => !array_key_exists('{' . Plugin::NS_OWNCLOUD . '}read-write', $sharee), |
|
66 | + 'readOnly' => !array_key_exists('{'.Plugin::NS_OWNCLOUD.'}read-write', $sharee), |
|
67 | 67 | ]; |
68 | 68 | break; |
69 | 69 | |
70 | - case '{' . Plugin::NS_OWNCLOUD . '}remove': |
|
70 | + case '{'.Plugin::NS_OWNCLOUD.'}remove': |
|
71 | 71 | $remove[] = $elem['value']['{DAV:}href']; |
72 | 72 | break; |
73 | 73 |
@@ -191,13 +191,13 @@ discard block |
||
191 | 191 | foreach ($shares as $share) { |
192 | 192 | $acl[] = [ |
193 | 193 | 'privilege' => '{DAV:}read', |
194 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
194 | + 'principal' => $share['{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}principal'], |
|
195 | 195 | 'protected' => true, |
196 | 196 | ]; |
197 | 197 | if (!$share['readOnly']) { |
198 | 198 | $acl[] = [ |
199 | 199 | 'privilege' => '{DAV:}write', |
200 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
200 | + 'principal' => $share['{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}principal'], |
|
201 | 201 | 'protected' => true, |
202 | 202 | ]; |
203 | 203 | } elseif ($this->resourceType === 'calendar') { |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | // so users can change the visibility. |
206 | 206 | $acl[] = [ |
207 | 207 | 'privilege' => '{DAV:}write-properties', |
208 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
208 | + 'principal' => $share['{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}principal'], |
|
209 | 209 | 'protected' => true, |
210 | 210 | ]; |
211 | 211 | } |
@@ -36,215 +36,215 @@ |
||
36 | 36 | |
37 | 37 | class Backend { |
38 | 38 | |
39 | - /** @var IDBConnection */ |
|
40 | - private $db; |
|
41 | - /** @var IUserManager */ |
|
42 | - private $userManager; |
|
43 | - /** @var IGroupManager */ |
|
44 | - private $groupManager; |
|
45 | - /** @var Principal */ |
|
46 | - private $principalBackend; |
|
47 | - /** @var string */ |
|
48 | - private $resourceType; |
|
49 | - |
|
50 | - public const ACCESS_OWNER = 1; |
|
51 | - public const ACCESS_READ_WRITE = 2; |
|
52 | - public const ACCESS_READ = 3; |
|
53 | - |
|
54 | - /** |
|
55 | - * @param IDBConnection $db |
|
56 | - * @param IUserManager $userManager |
|
57 | - * @param IGroupManager $groupManager |
|
58 | - * @param Principal $principalBackend |
|
59 | - * @param string $resourceType |
|
60 | - */ |
|
61 | - public function __construct(IDBConnection $db, IUserManager $userManager, IGroupManager $groupManager, Principal $principalBackend, $resourceType) { |
|
62 | - $this->db = $db; |
|
63 | - $this->userManager = $userManager; |
|
64 | - $this->groupManager = $groupManager; |
|
65 | - $this->principalBackend = $principalBackend; |
|
66 | - $this->resourceType = $resourceType; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @param IShareable $shareable |
|
71 | - * @param string[] $add |
|
72 | - * @param string[] $remove |
|
73 | - */ |
|
74 | - public function updateShares(IShareable $shareable, array $add, array $remove) { |
|
75 | - foreach ($add as $element) { |
|
76 | - $principal = $this->principalBackend->findByUri($element['href'], ''); |
|
77 | - if ($principal !== '') { |
|
78 | - $this->shareWith($shareable, $element); |
|
79 | - } |
|
80 | - } |
|
81 | - foreach ($remove as $element) { |
|
82 | - $principal = $this->principalBackend->findByUri($element, ''); |
|
83 | - if ($principal !== '') { |
|
84 | - $this->unshare($shareable, $element); |
|
85 | - } |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param IShareable $shareable |
|
91 | - * @param string $element |
|
92 | - */ |
|
93 | - private function shareWith($shareable, $element) { |
|
94 | - $user = $element['href']; |
|
95 | - $parts = explode(':', $user, 2); |
|
96 | - if ($parts[0] !== 'principal') { |
|
97 | - return; |
|
98 | - } |
|
99 | - |
|
100 | - // don't share with owner |
|
101 | - if ($shareable->getOwner() === $parts[1]) { |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - $principal = explode('/', $parts[1], 3); |
|
106 | - if (count($principal) !== 3 || $principal[0] !== 'principals' || !in_array($principal[1], ['users', 'groups', 'circles'], true)) { |
|
107 | - // Invalid principal |
|
108 | - return; |
|
109 | - } |
|
110 | - |
|
111 | - $principal[2] = urldecode($principal[2]); |
|
112 | - if (($principal[1] === 'users' && !$this->userManager->userExists($principal[2])) || |
|
113 | - ($principal[1] === 'groups' && !$this->groupManager->groupExists($principal[2]))) { |
|
114 | - // User or group does not exist |
|
115 | - return; |
|
116 | - } |
|
117 | - |
|
118 | - // remove the share if it already exists |
|
119 | - $this->unshare($shareable, $element['href']); |
|
120 | - $access = self::ACCESS_READ; |
|
121 | - if (isset($element['readOnly'])) { |
|
122 | - $access = $element['readOnly'] ? self::ACCESS_READ : self::ACCESS_READ_WRITE; |
|
123 | - } |
|
124 | - |
|
125 | - $query = $this->db->getQueryBuilder(); |
|
126 | - $query->insert('dav_shares') |
|
127 | - ->values([ |
|
128 | - 'principaluri' => $query->createNamedParameter($parts[1]), |
|
129 | - 'type' => $query->createNamedParameter($this->resourceType), |
|
130 | - 'access' => $query->createNamedParameter($access), |
|
131 | - 'resourceid' => $query->createNamedParameter($shareable->getResourceId()) |
|
132 | - ]); |
|
133 | - $query->execute(); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @param $resourceId |
|
138 | - */ |
|
139 | - public function deleteAllShares($resourceId) { |
|
140 | - $query = $this->db->getQueryBuilder(); |
|
141 | - $query->delete('dav_shares') |
|
142 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
143 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
144 | - ->execute(); |
|
145 | - } |
|
146 | - |
|
147 | - public function deleteAllSharesByUser($principaluri) { |
|
148 | - $query = $this->db->getQueryBuilder(); |
|
149 | - $query->delete('dav_shares') |
|
150 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri))) |
|
151 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
152 | - ->execute(); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * @param IShareable $shareable |
|
157 | - * @param string $element |
|
158 | - */ |
|
159 | - private function unshare($shareable, $element) { |
|
160 | - $parts = explode(':', $element, 2); |
|
161 | - if ($parts[0] !== 'principal') { |
|
162 | - return; |
|
163 | - } |
|
164 | - |
|
165 | - // don't share with owner |
|
166 | - if ($shareable->getOwner() === $parts[1]) { |
|
167 | - return; |
|
168 | - } |
|
169 | - |
|
170 | - $query = $this->db->getQueryBuilder(); |
|
171 | - $query->delete('dav_shares') |
|
172 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($shareable->getResourceId()))) |
|
173 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
174 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($parts[1]))) |
|
175 | - ; |
|
176 | - $query->execute(); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Returns the list of people whom this resource is shared with. |
|
181 | - * |
|
182 | - * Every element in this array should have the following properties: |
|
183 | - * * href - Often a mailto: address |
|
184 | - * * commonName - Optional, for example a first + last name |
|
185 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
186 | - * * readOnly - boolean |
|
187 | - * * summary - Optional, a description for the share |
|
188 | - * |
|
189 | - * @param int $resourceId |
|
190 | - * @return array |
|
191 | - */ |
|
192 | - public function getShares($resourceId) { |
|
193 | - $query = $this->db->getQueryBuilder(); |
|
194 | - $result = $query->select(['principaluri', 'access']) |
|
195 | - ->from('dav_shares') |
|
196 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
197 | - ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
198 | - ->groupBy(['principaluri', 'access']) |
|
199 | - ->execute(); |
|
200 | - |
|
201 | - $shares = []; |
|
202 | - while ($row = $result->fetch()) { |
|
203 | - $p = $this->principalBackend->getPrincipalByPath($row['principaluri']); |
|
204 | - $shares[] = [ |
|
205 | - 'href' => "principal:${row['principaluri']}", |
|
206 | - 'commonName' => isset($p['{DAV:}displayname']) ? $p['{DAV:}displayname'] : '', |
|
207 | - 'status' => 1, |
|
208 | - 'readOnly' => (int) $row['access'] === self::ACCESS_READ, |
|
209 | - '{http://owncloud.org/ns}principal' => $row['principaluri'], |
|
210 | - '{http://owncloud.org/ns}group-share' => is_null($p) |
|
211 | - ]; |
|
212 | - } |
|
213 | - |
|
214 | - return $shares; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * For shared resources the sharee is set in the ACL of the resource |
|
219 | - * |
|
220 | - * @param int $resourceId |
|
221 | - * @param array $acl |
|
222 | - * @return array |
|
223 | - */ |
|
224 | - public function applyShareAcl($resourceId, $acl) { |
|
225 | - $shares = $this->getShares($resourceId); |
|
226 | - foreach ($shares as $share) { |
|
227 | - $acl[] = [ |
|
228 | - 'privilege' => '{DAV:}read', |
|
229 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
230 | - 'protected' => true, |
|
231 | - ]; |
|
232 | - if (!$share['readOnly']) { |
|
233 | - $acl[] = [ |
|
234 | - 'privilege' => '{DAV:}write', |
|
235 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
236 | - 'protected' => true, |
|
237 | - ]; |
|
238 | - } elseif ($this->resourceType === 'calendar') { |
|
239 | - // Allow changing the properties of read only calendars, |
|
240 | - // so users can change the visibility. |
|
241 | - $acl[] = [ |
|
242 | - 'privilege' => '{DAV:}write-properties', |
|
243 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
244 | - 'protected' => true, |
|
245 | - ]; |
|
246 | - } |
|
247 | - } |
|
248 | - return $acl; |
|
249 | - } |
|
39 | + /** @var IDBConnection */ |
|
40 | + private $db; |
|
41 | + /** @var IUserManager */ |
|
42 | + private $userManager; |
|
43 | + /** @var IGroupManager */ |
|
44 | + private $groupManager; |
|
45 | + /** @var Principal */ |
|
46 | + private $principalBackend; |
|
47 | + /** @var string */ |
|
48 | + private $resourceType; |
|
49 | + |
|
50 | + public const ACCESS_OWNER = 1; |
|
51 | + public const ACCESS_READ_WRITE = 2; |
|
52 | + public const ACCESS_READ = 3; |
|
53 | + |
|
54 | + /** |
|
55 | + * @param IDBConnection $db |
|
56 | + * @param IUserManager $userManager |
|
57 | + * @param IGroupManager $groupManager |
|
58 | + * @param Principal $principalBackend |
|
59 | + * @param string $resourceType |
|
60 | + */ |
|
61 | + public function __construct(IDBConnection $db, IUserManager $userManager, IGroupManager $groupManager, Principal $principalBackend, $resourceType) { |
|
62 | + $this->db = $db; |
|
63 | + $this->userManager = $userManager; |
|
64 | + $this->groupManager = $groupManager; |
|
65 | + $this->principalBackend = $principalBackend; |
|
66 | + $this->resourceType = $resourceType; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @param IShareable $shareable |
|
71 | + * @param string[] $add |
|
72 | + * @param string[] $remove |
|
73 | + */ |
|
74 | + public function updateShares(IShareable $shareable, array $add, array $remove) { |
|
75 | + foreach ($add as $element) { |
|
76 | + $principal = $this->principalBackend->findByUri($element['href'], ''); |
|
77 | + if ($principal !== '') { |
|
78 | + $this->shareWith($shareable, $element); |
|
79 | + } |
|
80 | + } |
|
81 | + foreach ($remove as $element) { |
|
82 | + $principal = $this->principalBackend->findByUri($element, ''); |
|
83 | + if ($principal !== '') { |
|
84 | + $this->unshare($shareable, $element); |
|
85 | + } |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param IShareable $shareable |
|
91 | + * @param string $element |
|
92 | + */ |
|
93 | + private function shareWith($shareable, $element) { |
|
94 | + $user = $element['href']; |
|
95 | + $parts = explode(':', $user, 2); |
|
96 | + if ($parts[0] !== 'principal') { |
|
97 | + return; |
|
98 | + } |
|
99 | + |
|
100 | + // don't share with owner |
|
101 | + if ($shareable->getOwner() === $parts[1]) { |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + $principal = explode('/', $parts[1], 3); |
|
106 | + if (count($principal) !== 3 || $principal[0] !== 'principals' || !in_array($principal[1], ['users', 'groups', 'circles'], true)) { |
|
107 | + // Invalid principal |
|
108 | + return; |
|
109 | + } |
|
110 | + |
|
111 | + $principal[2] = urldecode($principal[2]); |
|
112 | + if (($principal[1] === 'users' && !$this->userManager->userExists($principal[2])) || |
|
113 | + ($principal[1] === 'groups' && !$this->groupManager->groupExists($principal[2]))) { |
|
114 | + // User or group does not exist |
|
115 | + return; |
|
116 | + } |
|
117 | + |
|
118 | + // remove the share if it already exists |
|
119 | + $this->unshare($shareable, $element['href']); |
|
120 | + $access = self::ACCESS_READ; |
|
121 | + if (isset($element['readOnly'])) { |
|
122 | + $access = $element['readOnly'] ? self::ACCESS_READ : self::ACCESS_READ_WRITE; |
|
123 | + } |
|
124 | + |
|
125 | + $query = $this->db->getQueryBuilder(); |
|
126 | + $query->insert('dav_shares') |
|
127 | + ->values([ |
|
128 | + 'principaluri' => $query->createNamedParameter($parts[1]), |
|
129 | + 'type' => $query->createNamedParameter($this->resourceType), |
|
130 | + 'access' => $query->createNamedParameter($access), |
|
131 | + 'resourceid' => $query->createNamedParameter($shareable->getResourceId()) |
|
132 | + ]); |
|
133 | + $query->execute(); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @param $resourceId |
|
138 | + */ |
|
139 | + public function deleteAllShares($resourceId) { |
|
140 | + $query = $this->db->getQueryBuilder(); |
|
141 | + $query->delete('dav_shares') |
|
142 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
143 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
144 | + ->execute(); |
|
145 | + } |
|
146 | + |
|
147 | + public function deleteAllSharesByUser($principaluri) { |
|
148 | + $query = $this->db->getQueryBuilder(); |
|
149 | + $query->delete('dav_shares') |
|
150 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principaluri))) |
|
151 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
152 | + ->execute(); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * @param IShareable $shareable |
|
157 | + * @param string $element |
|
158 | + */ |
|
159 | + private function unshare($shareable, $element) { |
|
160 | + $parts = explode(':', $element, 2); |
|
161 | + if ($parts[0] !== 'principal') { |
|
162 | + return; |
|
163 | + } |
|
164 | + |
|
165 | + // don't share with owner |
|
166 | + if ($shareable->getOwner() === $parts[1]) { |
|
167 | + return; |
|
168 | + } |
|
169 | + |
|
170 | + $query = $this->db->getQueryBuilder(); |
|
171 | + $query->delete('dav_shares') |
|
172 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($shareable->getResourceId()))) |
|
173 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
174 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($parts[1]))) |
|
175 | + ; |
|
176 | + $query->execute(); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Returns the list of people whom this resource is shared with. |
|
181 | + * |
|
182 | + * Every element in this array should have the following properties: |
|
183 | + * * href - Often a mailto: address |
|
184 | + * * commonName - Optional, for example a first + last name |
|
185 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
186 | + * * readOnly - boolean |
|
187 | + * * summary - Optional, a description for the share |
|
188 | + * |
|
189 | + * @param int $resourceId |
|
190 | + * @return array |
|
191 | + */ |
|
192 | + public function getShares($resourceId) { |
|
193 | + $query = $this->db->getQueryBuilder(); |
|
194 | + $result = $query->select(['principaluri', 'access']) |
|
195 | + ->from('dav_shares') |
|
196 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($resourceId))) |
|
197 | + ->andWhere($query->expr()->eq('type', $query->createNamedParameter($this->resourceType))) |
|
198 | + ->groupBy(['principaluri', 'access']) |
|
199 | + ->execute(); |
|
200 | + |
|
201 | + $shares = []; |
|
202 | + while ($row = $result->fetch()) { |
|
203 | + $p = $this->principalBackend->getPrincipalByPath($row['principaluri']); |
|
204 | + $shares[] = [ |
|
205 | + 'href' => "principal:${row['principaluri']}", |
|
206 | + 'commonName' => isset($p['{DAV:}displayname']) ? $p['{DAV:}displayname'] : '', |
|
207 | + 'status' => 1, |
|
208 | + 'readOnly' => (int) $row['access'] === self::ACCESS_READ, |
|
209 | + '{http://owncloud.org/ns}principal' => $row['principaluri'], |
|
210 | + '{http://owncloud.org/ns}group-share' => is_null($p) |
|
211 | + ]; |
|
212 | + } |
|
213 | + |
|
214 | + return $shares; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * For shared resources the sharee is set in the ACL of the resource |
|
219 | + * |
|
220 | + * @param int $resourceId |
|
221 | + * @param array $acl |
|
222 | + * @return array |
|
223 | + */ |
|
224 | + public function applyShareAcl($resourceId, $acl) { |
|
225 | + $shares = $this->getShares($resourceId); |
|
226 | + foreach ($shares as $share) { |
|
227 | + $acl[] = [ |
|
228 | + 'privilege' => '{DAV:}read', |
|
229 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
230 | + 'protected' => true, |
|
231 | + ]; |
|
232 | + if (!$share['readOnly']) { |
|
233 | + $acl[] = [ |
|
234 | + 'privilege' => '{DAV:}write', |
|
235 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
236 | + 'protected' => true, |
|
237 | + ]; |
|
238 | + } elseif ($this->resourceType === 'calendar') { |
|
239 | + // Allow changing the properties of read only calendars, |
|
240 | + // so users can change the visibility. |
|
241 | + $acl[] = [ |
|
242 | + 'privilege' => '{DAV:}write-properties', |
|
243 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
244 | + 'protected' => true, |
|
245 | + ]; |
|
246 | + } |
|
247 | + } |
|
248 | + return $acl; |
|
249 | + } |
|
250 | 250 | } |
@@ -63,7 +63,7 @@ |
||
63 | 63 | private function setupUserFs($userId) { |
64 | 64 | \OC_Util::setupFS($userId); |
65 | 65 | $this->session->close(); |
66 | - return $this->principalPrefix . $userId; |
|
66 | + return $this->principalPrefix.$userId; |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
@@ -31,66 +31,66 @@ |
||
31 | 31 | use Sabre\HTTP\ResponseInterface; |
32 | 32 | |
33 | 33 | class BearerAuth extends AbstractBearer { |
34 | - /** @var IUserSession */ |
|
35 | - private $userSession; |
|
36 | - /** @var ISession */ |
|
37 | - private $session; |
|
38 | - /** @var IRequest */ |
|
39 | - private $request; |
|
40 | - /** @var string */ |
|
41 | - private $principalPrefix; |
|
34 | + /** @var IUserSession */ |
|
35 | + private $userSession; |
|
36 | + /** @var ISession */ |
|
37 | + private $session; |
|
38 | + /** @var IRequest */ |
|
39 | + private $request; |
|
40 | + /** @var string */ |
|
41 | + private $principalPrefix; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param IUserSession $userSession |
|
45 | - * @param ISession $session |
|
46 | - * @param string $principalPrefix |
|
47 | - * @param IRequest $request |
|
48 | - */ |
|
49 | - public function __construct(IUserSession $userSession, |
|
50 | - ISession $session, |
|
51 | - IRequest $request, |
|
52 | - $principalPrefix = 'principals/users/') { |
|
53 | - $this->userSession = $userSession; |
|
54 | - $this->session = $session; |
|
55 | - $this->request = $request; |
|
56 | - $this->principalPrefix = $principalPrefix; |
|
43 | + /** |
|
44 | + * @param IUserSession $userSession |
|
45 | + * @param ISession $session |
|
46 | + * @param string $principalPrefix |
|
47 | + * @param IRequest $request |
|
48 | + */ |
|
49 | + public function __construct(IUserSession $userSession, |
|
50 | + ISession $session, |
|
51 | + IRequest $request, |
|
52 | + $principalPrefix = 'principals/users/') { |
|
53 | + $this->userSession = $userSession; |
|
54 | + $this->session = $session; |
|
55 | + $this->request = $request; |
|
56 | + $this->principalPrefix = $principalPrefix; |
|
57 | 57 | |
58 | - // setup realm |
|
59 | - $defaults = new \OCP\Defaults(); |
|
60 | - $this->realm = $defaults->getName(); |
|
61 | - } |
|
58 | + // setup realm |
|
59 | + $defaults = new \OCP\Defaults(); |
|
60 | + $this->realm = $defaults->getName(); |
|
61 | + } |
|
62 | 62 | |
63 | - private function setupUserFs($userId) { |
|
64 | - \OC_Util::setupFS($userId); |
|
65 | - $this->session->close(); |
|
66 | - return $this->principalPrefix . $userId; |
|
67 | - } |
|
63 | + private function setupUserFs($userId) { |
|
64 | + \OC_Util::setupFS($userId); |
|
65 | + $this->session->close(); |
|
66 | + return $this->principalPrefix . $userId; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * {@inheritdoc} |
|
71 | - */ |
|
72 | - public function validateBearerToken($bearerToken) { |
|
73 | - \OC_Util::setupFS(); |
|
69 | + /** |
|
70 | + * {@inheritdoc} |
|
71 | + */ |
|
72 | + public function validateBearerToken($bearerToken) { |
|
73 | + \OC_Util::setupFS(); |
|
74 | 74 | |
75 | - if (!$this->userSession->isLoggedIn()) { |
|
76 | - $this->userSession->tryTokenLogin($this->request); |
|
77 | - } |
|
78 | - if ($this->userSession->isLoggedIn()) { |
|
79 | - return $this->setupUserFs($this->userSession->getUser()->getUID()); |
|
80 | - } |
|
75 | + if (!$this->userSession->isLoggedIn()) { |
|
76 | + $this->userSession->tryTokenLogin($this->request); |
|
77 | + } |
|
78 | + if ($this->userSession->isLoggedIn()) { |
|
79 | + return $this->setupUserFs($this->userSession->getUser()->getUID()); |
|
80 | + } |
|
81 | 81 | |
82 | - return false; |
|
83 | - } |
|
82 | + return false; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * \Sabre\DAV\Auth\Backend\AbstractBearer::challenge sets an WWW-Authenticate |
|
87 | - * header which some DAV clients can't handle. Thus we override this function |
|
88 | - * and make it simply return a 401. |
|
89 | - * |
|
90 | - * @param RequestInterface $request |
|
91 | - * @param ResponseInterface $response |
|
92 | - */ |
|
93 | - public function challenge(RequestInterface $request, ResponseInterface $response) { |
|
94 | - $response->setStatus(401); |
|
95 | - } |
|
85 | + /** |
|
86 | + * \Sabre\DAV\Auth\Backend\AbstractBearer::challenge sets an WWW-Authenticate |
|
87 | + * header which some DAV clients can't handle. Thus we override this function |
|
88 | + * and make it simply return a 401. |
|
89 | + * |
|
90 | + * @param RequestInterface $request |
|
91 | + * @param ResponseInterface $response |
|
92 | + */ |
|
93 | + public function challenge(RequestInterface $request, ResponseInterface $response) { |
|
94 | + $response->setStatus(401); |
|
95 | + } |
|
96 | 96 | } |
@@ -29,19 +29,19 @@ |
||
29 | 29 | use Exception; |
30 | 30 | |
31 | 31 | class FileLocked extends \Sabre\DAV\Exception { |
32 | - public function __construct($message = "", $code = 0, Exception $previous = null) { |
|
33 | - if ($previous instanceof \OCP\Files\LockNotAcquiredException) { |
|
34 | - $message = sprintf('Target file %s is locked by another process.', $previous->path); |
|
35 | - } |
|
36 | - parent::__construct($message, $code, $previous); |
|
37 | - } |
|
32 | + public function __construct($message = "", $code = 0, Exception $previous = null) { |
|
33 | + if ($previous instanceof \OCP\Files\LockNotAcquiredException) { |
|
34 | + $message = sprintf('Target file %s is locked by another process.', $previous->path); |
|
35 | + } |
|
36 | + parent::__construct($message, $code, $previous); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Returns the HTTP status code for this exception |
|
41 | - * |
|
42 | - * @return int |
|
43 | - */ |
|
44 | - public function getHTTPCode() { |
|
45 | - return 423; |
|
46 | - } |
|
39 | + /** |
|
40 | + * Returns the HTTP status code for this exception |
|
41 | + * |
|
42 | + * @return int |
|
43 | + */ |
|
44 | + public function getHTTPCode() { |
|
45 | + return 423; |
|
46 | + } |
|
47 | 47 | } |