Skip to content

Commit 6914802

Browse files
authored
Merge pull request #7732 from nextcloud/backport/7659/stable30
[stable30] fix: Handle share attributes in the share provider
2 parents d3c0338 + f17dcbc commit 6914802

1 file changed

Lines changed: 73 additions & 8 deletions

File tree

lib/Sharing/DeckShareProvider.php

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCP\IL10N;
3232
use OCP\Share\Exceptions\GenericShareException;
3333
use OCP\Share\Exceptions\ShareNotFound;
34+
use OCP\Share\IAttributes;
3435
use OCP\Share\IManager;
3536
use OCP\Share\IShare;
3637

@@ -133,6 +134,11 @@ public function create(IShare $share) {
133134
)
134135
);*/
135136

137+
// set share attributes
138+
$shareAttributes = $this->formatShareAttributes(
139+
$share->getAttributes()
140+
);
141+
136142
$shareId = $this->addShareToDB(
137143
$share->getSharedWith(),
138144
$share->getSharedBy(),
@@ -142,7 +148,8 @@ public function create(IShare $share) {
142148
$share->getTarget(),
143149
$share->getPermissions(),
144150
$share->getToken() ?? '',
145-
$share->getExpirationDate()
151+
$share->getExpirationDate(),
152+
$shareAttributes
146153
);
147154
$data = $this->getRawShare($shareId);
148155

@@ -163,6 +170,7 @@ public function create(IShare $share) {
163170
* @param int $permissions
164171
* @param string $token
165172
* @param \DateTime|null $expirationDate
173+
* @param string|null $attributes
166174
* @return int
167175
*/
168176
private function addShareToDB(
@@ -174,7 +182,8 @@ private function addShareToDB(
174182
string $target,
175183
int $permissions,
176184
string $token,
177-
?\DateTime $expirationDate
185+
?\DateTime $expirationDate,
186+
?string $attributes = null
178187
): int {
179188
$qb = $this->dbConnection->getQueryBuilder();
180189
$qb->insert('share')
@@ -194,6 +203,10 @@ private function addShareToDB(
194203
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
195204
}
196205

206+
if ($attributes !== null) {
207+
$qb->setValue('attributes', $qb->createNamedParameter($attributes));
208+
}
209+
197210
$qb->executeStatement();
198211

199212
return $qb->getLastInsertId();
@@ -264,6 +277,9 @@ private function createShareObject(array $data): IShare {
264277
$entryData['parent'] = $entryData['f_parent'];
265278
$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, $this->mimeTypeLoader));
266279
}
280+
281+
$share = $this->updateShareAttributes($share, $data['attributes'] ?? null);
282+
267283
return $share;
268284
}
269285

@@ -295,8 +311,13 @@ public function update(IShare $share) {
295311
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
296312
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
297313
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
298-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
299-
->execute();
314+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
315+
316+
$shareAttributes = $this->formatShareAttributes($share->getAttributes());
317+
if ($shareAttributes !== null) {
318+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
319+
}
320+
$qb->executeStatement();
300321

301322
/*
302323
* Update all user defined group shares
@@ -308,8 +329,12 @@ public function update(IShare $share) {
308329
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
309330
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
310331
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
311-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
312-
->execute();
332+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
333+
334+
if ($shareAttributes !== null) {
335+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
336+
}
337+
$qb->executeStatement();
313338

314339
/*
315340
* Now update the permissions for all children that have not set it to 0
@@ -318,8 +343,12 @@ public function update(IShare $share) {
318343
$qb->update('share')
319344
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
320345
->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
321-
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
322-
->execute();
346+
->set('permissions', $qb->createNamedParameter($share->getPermissions()));
347+
348+
if ($shareAttributes !== null) {
349+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
350+
}
351+
$qb->executeStatement();
323352

324353
return $share;
325354
}
@@ -1046,6 +1075,42 @@ public function getAllShares(): iterable {
10461075
$cursor->closeCursor();
10471076
}
10481077

1078+
protected function updateShareAttributes(IShare $share, ?string $data): IShare {
1079+
if ($data === null || $data === '') {
1080+
return $share;
1081+
}
1082+
$attributes = $share->getAttributes() ?? $share->newAttributes();
1083+
$compressedAttributes = \json_decode($data, true);
1084+
if ($compressedAttributes === false || $compressedAttributes === null) {
1085+
return $share;
1086+
}
1087+
foreach ($compressedAttributes as $compressedAttribute) {
1088+
$attributes->setAttribute(
1089+
$compressedAttribute[0],
1090+
$compressedAttribute[1],
1091+
$compressedAttribute[2]
1092+
);
1093+
}
1094+
$share->setAttributes($attributes);
1095+
return $share;
1096+
}
1097+
1098+
protected function formatShareAttributes(?IAttributes $attributes): ?string {
1099+
if ($attributes === null || empty($attributes->toArray())) {
1100+
return null;
1101+
}
1102+
1103+
$compressedAttributes = [];
1104+
foreach ($attributes->toArray() as $attribute) {
1105+
$compressedAttributes[] = [
1106+
0 => $attribute['scope'],
1107+
1 => $attribute['key'],
1108+
2 => $attribute['value']
1109+
];
1110+
}
1111+
return \json_encode($compressedAttributes) ?: null;
1112+
}
1113+
10491114
public function getOrphanedAttachmentShares(): array {
10501115
$allCardIds = $this->cardMapper->getAllCardIds();
10511116
$qb = $this->dbConnection->getQueryBuilder();

0 commit comments

Comments
 (0)