Skip to content

Commit 9405999

Browse files
juliusknorrgrnd-alt
authored andcommitted
fix: Handle share attributes in the share provider
Signed-off-by: Julius Knorr <jus@bitgrid.net>
1 parent 4c884d0 commit 9405999

File tree

1 file changed

+80
-12
lines changed

1 file changed

+80
-12
lines changed

lib/Sharing/DeckShareProvider.php

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use OCP\IL10N;
4949
use OCP\Share\Exceptions\GenericShareException;
5050
use OCP\Share\Exceptions\ShareNotFound;
51+
use OCP\Share\IAttributes;
5152
use OCP\Share\IManager;
5253
use OCP\Share\IShare;
5354

@@ -150,6 +151,11 @@ public function create(IShare $share) {
150151
)
151152
);*/
152153

154+
// set share attributes
155+
$shareAttributes = $this->formatShareAttributes(
156+
$share->getAttributes()
157+
);
158+
153159
$shareId = $this->addShareToDB(
154160
$share->getSharedWith(),
155161
$share->getSharedBy(),
@@ -159,7 +165,8 @@ public function create(IShare $share) {
159165
$share->getTarget(),
160166
$share->getPermissions(),
161167
$share->getToken() ?? '',
162-
$share->getExpirationDate()
168+
$share->getExpirationDate(),
169+
$shareAttributes
163170
);
164171
$data = $this->getRawShare($shareId);
165172

@@ -180,6 +187,7 @@ public function create(IShare $share) {
180187
* @param int $permissions
181188
* @param string $token
182189
* @param \DateTime|null $expirationDate
190+
* @param string|null $attributes
183191
* @return int
184192
*/
185193
private function addShareToDB(
@@ -191,7 +199,8 @@ private function addShareToDB(
191199
string $target,
192200
int $permissions,
193201
string $token,
194-
?\DateTime $expirationDate
202+
?\DateTime $expirationDate,
203+
?string $attributes = null
195204
): int {
196205
$qb = $this->dbConnection->getQueryBuilder();
197206
$qb->insert('share')
@@ -211,6 +220,10 @@ private function addShareToDB(
211220
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
212221
}
213222

223+
if ($attributes !== null) {
224+
$qb->setValue('attributes', $qb->createNamedParameter($attributes));
225+
}
226+
214227
$qb->executeStatement();
215228

216229
return $qb->getLastInsertId();
@@ -281,6 +294,9 @@ private function createShareObject(array $data): IShare {
281294
$entryData['parent'] = $entryData['f_parent'];
282295
$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, $this->mimeTypeLoader));
283296
}
297+
298+
$share = $this->updateShareAttributes($share, $data['attributes'] ?? null);
299+
284300
return $share;
285301
}
286302

@@ -312,31 +328,47 @@ public function update(IShare $share) {
312328
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
313329
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
314330
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
315-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
316-
->execute();
331+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
332+
333+
$shareAttributes = $this->formatShareAttributes($share->getAttributes());
334+
if ($shareAttributes !== null) {
335+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
336+
}
337+
338+
$qb->executeStatement();
317339

318340
/*
319-
* Update all user defined group shares
320-
*/
341+
* Update all user defined group shares
342+
*/
321343
$qb = $this->dbConnection->getQueryBuilder();
322344
$qb->update('share')
323345
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
324346
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
325347
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
326348
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
327349
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
328-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
329-
->execute();
350+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
351+
352+
if ($shareAttributes !== null) {
353+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
354+
}
355+
356+
$qb->executeStatement();
330357

331358
/*
332-
* Now update the permissions for all children that have not set it to 0
333-
*/
359+
* Now update the permissions for all children that have not set it to 0
360+
*/
334361
$qb = $this->dbConnection->getQueryBuilder();
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+
371+
$qb->executeStatement();
340372

341373
return $share;
342374
}
@@ -1059,4 +1091,40 @@ public function getAllShares(): iterable {
10591091
}
10601092
$cursor->closeCursor();
10611093
}
1094+
1095+
protected function updateShareAttributes(IShare $share, ?string $data): IShare {
1096+
if ($data === null || $data === '') {
1097+
return $share;
1098+
}
1099+
$attributes = $share->getAttributes() ?? $share->newAttributes();
1100+
$compressedAttributes = \json_decode($data, true);
1101+
if ($compressedAttributes === false || $compressedAttributes === null) {
1102+
return $share;
1103+
}
1104+
foreach ($compressedAttributes as $compressedAttribute) {
1105+
$attributes->setAttribute(
1106+
$compressedAttribute[0],
1107+
$compressedAttribute[1],
1108+
$compressedAttribute[2]
1109+
);
1110+
}
1111+
$share->setAttributes($attributes);
1112+
return $share;
1113+
}
1114+
1115+
protected function formatShareAttributes(?IAttributes $attributes): ?string {
1116+
if ($attributes === null || empty($attributes->toArray())) {
1117+
return null;
1118+
}
1119+
1120+
$compressedAttributes = [];
1121+
foreach ($attributes->toArray() as $attribute) {
1122+
$compressedAttributes[] = [
1123+
0 => $attribute['scope'],
1124+
1 => $attribute['key'],
1125+
2 => $attribute['value']
1126+
];
1127+
}
1128+
return \json_encode($compressedAttributes) ?: null;
1129+
}
10621130
}

0 commit comments

Comments
 (0)