Skip to content

Commit 3b843ac

Browse files
authored
Merge pull request #7729 from nextcloud/backport/7659/stable29
[stable29] fix: Handle share attributes in the share provider
2 parents 5806f5c + 1ab748a commit 3b843ac

1 file changed

Lines changed: 76 additions & 9 deletions

File tree

lib/Sharing/DeckShareProvider.php

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use OC\Files\Cache\Cache;
3030
use OCA\Deck\Cache\AttachmentCacheHelper;
3131
use OCA\Deck\Db\Acl;
32-
use OCA\Deck\Db\Board;
3332
use OCA\Deck\Db\BoardMapper;
3433
use OCA\Deck\Db\CardMapper;
3534
use OCA\Deck\Db\User;
@@ -48,6 +47,7 @@
4847
use OCP\IL10N;
4948
use OCP\Share\Exceptions\GenericShareException;
5049
use OCP\Share\Exceptions\ShareNotFound;
50+
use OCP\Share\IAttributes;
5151
use OCP\Share\IManager;
5252
use OCP\Share\IShare;
5353

@@ -150,6 +150,11 @@ public function create(IShare $share) {
150150
)
151151
);*/
152152

153+
// set share attributes
154+
$shareAttributes = $this->formatShareAttributes(
155+
$share->getAttributes()
156+
);
157+
153158
$shareId = $this->addShareToDB(
154159
$share->getSharedWith(),
155160
$share->getSharedBy(),
@@ -159,7 +164,8 @@ public function create(IShare $share) {
159164
$share->getTarget(),
160165
$share->getPermissions(),
161166
$share->getToken() ?? '',
162-
$share->getExpirationDate()
167+
$share->getExpirationDate(),
168+
$shareAttributes
163169
);
164170
$data = $this->getRawShare($shareId);
165171

@@ -180,6 +186,7 @@ public function create(IShare $share) {
180186
* @param int $permissions
181187
* @param string $token
182188
* @param \DateTime|null $expirationDate
189+
* @param string|null $attributes
183190
* @return int
184191
*/
185192
private function addShareToDB(
@@ -191,7 +198,8 @@ private function addShareToDB(
191198
string $target,
192199
int $permissions,
193200
string $token,
194-
?\DateTime $expirationDate
201+
?\DateTime $expirationDate,
202+
?string $attributes = null
195203
): int {
196204
$qb = $this->dbConnection->getQueryBuilder();
197205
$qb->insert('share')
@@ -211,6 +219,10 @@ private function addShareToDB(
211219
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
212220
}
213221

222+
if ($attributes !== null) {
223+
$qb->setValue('attributes', $qb->createNamedParameter($attributes));
224+
}
225+
214226
$qb->executeStatement();
215227

216228
return $qb->getLastInsertId();
@@ -281,6 +293,9 @@ private function createShareObject(array $data): IShare {
281293
$entryData['parent'] = $entryData['f_parent'];
282294
$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, $this->mimeTypeLoader));
283295
}
296+
297+
$share = $this->updateShareAttributes($share, $data['attributes'] ?? null);
298+
284299
return $share;
285300
}
286301

@@ -312,8 +327,14 @@ public function update(IShare $share) {
312327
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
313328
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
314329
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
315-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
316-
->execute();
330+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
331+
332+
$shareAttributes = $this->formatShareAttributes($share->getAttributes());
333+
if ($shareAttributes !== null) {
334+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
335+
}
336+
337+
$qb->execute();
317338

318339
/*
319340
* Update all user defined group shares
@@ -325,8 +346,14 @@ public function update(IShare $share) {
325346
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
326347
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
327348
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
328-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
329-
->execute();
349+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
350+
351+
352+
if ($shareAttributes !== null) {
353+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
354+
}
355+
356+
$qb->execute();
330357

331358
/*
332359
* Now update the permissions for all children that have not set it to 0
@@ -335,12 +362,52 @@ public function update(IShare $share) {
335362
$qb->update('share')
336363
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
337364
->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
338-
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
339-
->execute();
365+
->set('permissions', $qb->createNamedParameter($share->getPermissions()));
366+
367+
if ($shareAttributes !== null) {
368+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
369+
}
370+
$qb->execute();
340371

341372
return $share;
342373
}
343374

375+
protected function updateShareAttributes(IShare $share, ?string $data): IShare {
376+
if ($data === null || $data === '') {
377+
return $share;
378+
}
379+
$attributes = $share->getAttributes() ?? $share->newAttributes();
380+
$compressedAttributes = \json_decode($data, true);
381+
if ($compressedAttributes === false || $compressedAttributes === null) {
382+
return $share;
383+
}
384+
foreach ($compressedAttributes as $compressedAttribute) {
385+
$attributes->setAttribute(
386+
$compressedAttribute[0],
387+
$compressedAttribute[1],
388+
$compressedAttribute[2]
389+
);
390+
}
391+
$share->setAttributes($attributes);
392+
return $share;
393+
}
394+
395+
protected function formatShareAttributes(?IAttributes $attributes): ?string {
396+
if ($attributes === null || empty($attributes->toArray())) {
397+
return null;
398+
}
399+
400+
$compressedAttributes = [];
401+
foreach ($attributes->toArray() as $attribute) {
402+
$compressedAttributes[] = [
403+
0 => $attribute['scope'],
404+
1 => $attribute['key'],
405+
2 => $attribute['value']
406+
];
407+
}
408+
return \json_encode($compressedAttributes) ?: null;
409+
}
410+
344411
/**
345412
* @inheritDoc
346413
*/

0 commit comments

Comments
 (0)