Skip to content

Commit 887363d

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

1 file changed

Lines changed: 75 additions & 7 deletions

File tree

lib/Sharing/DeckShareProvider.php

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\IL10N;
3333
use OCP\Share\Exceptions\GenericShareException;
3434
use OCP\Share\Exceptions\ShareNotFound;
35+
use OCP\Share\IAttributes;
3536
use OCP\Share\IManager;
3637
use OCP\Share\IPartialShareProvider;
3738
use OCP\Share\IShare;
@@ -119,6 +120,11 @@ public function create(IShare $share) {
119120
)
120121
);*/
121122

123+
// set share attributes
124+
$shareAttributes = $this->formatShareAttributes(
125+
$share->getAttributes()
126+
);
127+
122128
$shareId = $this->addShareToDB(
123129
$share->getSharedWith(),
124130
$share->getSharedBy(),
@@ -128,7 +134,8 @@ public function create(IShare $share) {
128134
$share->getTarget(),
129135
$share->getPermissions(),
130136
$share->getToken() ?? '',
131-
$share->getExpirationDate()
137+
$share->getExpirationDate(),
138+
$shareAttributes
132139
);
133140
$data = $this->getRawShare($shareId);
134141

@@ -149,6 +156,7 @@ public function create(IShare $share) {
149156
* @param int $permissions
150157
* @param string $token
151158
* @param \DateTime|null $expirationDate
159+
* @param string|null $attributes
152160
* @return int
153161
*/
154162
private function addShareToDB(
@@ -161,6 +169,7 @@ private function addShareToDB(
161169
int $permissions,
162170
string $token,
163171
?\DateTime $expirationDate,
172+
?string $attributes = null,
164173
): int {
165174
$qb = $this->dbConnection->getQueryBuilder();
166175
$qb->insert('share')
@@ -180,6 +189,10 @@ private function addShareToDB(
180189
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
181190
}
182191

192+
if ($attributes !== null) {
193+
$qb->setValue('attributes', $qb->createNamedParameter($attributes));
194+
}
195+
183196
$qb->executeStatement();
184197

185198
return $qb->getLastInsertId();
@@ -250,6 +263,9 @@ private function createShareObject(array $data): IShare {
250263
$entryData['parent'] = $entryData['f_parent'];
251264
$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, $this->mimeTypeLoader));
252265
}
266+
267+
$share = $this->updateShareAttributes($share, $data['attributes'] ?? null);
268+
253269
return $share;
254270
}
255271

@@ -281,8 +297,14 @@ public function update(IShare $share) {
281297
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
282298
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
283299
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
284-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
285-
->executeStatement();
300+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
301+
302+
$shareAttributes = $this->formatShareAttributes($share->getAttributes());
303+
if ($shareAttributes !== null) {
304+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
305+
}
306+
307+
$qb->executeStatement();
286308

287309
/*
288310
* Update all user defined group shares
@@ -294,8 +316,13 @@ public function update(IShare $share) {
294316
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
295317
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
296318
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
297-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
298-
->executeStatement();
319+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
320+
321+
if ($shareAttributes !== null) {
322+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
323+
}
324+
325+
$qb->executeStatement();
299326

300327
/*
301328
* Now update the permissions for all children that have not set it to 0
@@ -304,8 +331,13 @@ public function update(IShare $share) {
304331
$qb->update('share')
305332
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
306333
->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
307-
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
308-
->executeStatement();
334+
->set('permissions', $qb->createNamedParameter($share->getPermissions()));
335+
336+
if ($shareAttributes !== null) {
337+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
338+
}
339+
340+
$qb->executeStatement();
309341

310342
return $share;
311343
}
@@ -1098,6 +1130,42 @@ public function getAllShares(): iterable {
10981130
$cursor->closeCursor();
10991131
}
11001132

1133+
protected function updateShareAttributes(IShare $share, ?string $data): IShare {
1134+
if ($data !== null && $data !== '') {
1135+
$attributes = $share->getAttributes() ?? $share->newAttributes();
1136+
$compressedAttributes = \json_decode($data, true);
1137+
if ($compressedAttributes === false || $compressedAttributes === null) {
1138+
return $share;
1139+
}
1140+
foreach ($compressedAttributes as $compressedAttribute) {
1141+
$attributes->setAttribute(
1142+
$compressedAttribute[0],
1143+
$compressedAttribute[1],
1144+
$compressedAttribute[2]
1145+
);
1146+
}
1147+
$share->setAttributes($attributes);
1148+
}
1149+
1150+
return $share;
1151+
}
1152+
1153+
protected function formatShareAttributes(?IAttributes $attributes): ?string {
1154+
if ($attributes === null || empty($attributes->toArray())) {
1155+
return null;
1156+
}
1157+
1158+
$compressedAttributes = [];
1159+
foreach ($attributes->toArray() as $attribute) {
1160+
$compressedAttributes[] = [
1161+
0 => $attribute['scope'],
1162+
1 => $attribute['key'],
1163+
2 => $attribute['value']
1164+
];
1165+
}
1166+
return \json_encode($compressedAttributes) ?: null;
1167+
}
1168+
11011169
public function getOrphanedAttachmentShares(): array {
11021170
$allCardIds = $this->cardMapper->getAllCardIds();
11031171

0 commit comments

Comments
 (0)