Skip to content

Commit 9b66d2b

Browse files
authored
Merge pull request #7728 from nextcloud/backport/7659/stable33
[stable33] fix: Handle share attributes in the share provider
2 parents 6abdf27 + 809a59a commit 9b66d2b

File tree

1 file changed

+109
-34
lines changed

1 file changed

+109
-34
lines changed

lib/Sharing/DeckShareProvider.php

Lines changed: 109 additions & 34 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\IPartialShareProvider;
3637
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

@@ -273,39 +289,62 @@ private function applyBoardPermission($share, $permissions, $userId) {
273289
* @inheritDoc
274290
*/
275291
public function update(IShare $share) {
276-
$qb = $this->dbConnection->getQueryBuilder();
277-
$qb->update('share')
278-
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
279-
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
280-
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
281-
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
282-
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
283-
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
284-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
285-
->executeStatement();
286-
287-
/*
288-
* Update all user defined group shares
289-
*/
290-
$qb = $this->dbConnection->getQueryBuilder();
291-
$qb->update('share')
292-
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
293-
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
294-
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
295-
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
296-
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
297-
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
298-
->executeStatement();
299-
300-
/*
301-
* Now update the permissions for all children that have not set it to 0
302-
*/
303-
$qb = $this->dbConnection->getQueryBuilder();
304-
$qb->update('share')
305-
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
306-
->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
307-
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
308-
->executeStatement();
292+
$this->dbConnection->beginTransaction();
293+
try {
294+
$qb = $this->dbConnection->getQueryBuilder();
295+
$qb->update('share')
296+
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
297+
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
298+
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
299+
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
300+
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
301+
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
302+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
303+
304+
$shareAttributes = $this->formatShareAttributes($share->getAttributes());
305+
if ($shareAttributes !== null) {
306+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
307+
}
308+
309+
$qb->executeStatement();
310+
311+
/*
312+
* Update all user defined group shares
313+
*/
314+
$qb = $this->dbConnection->getQueryBuilder();
315+
$qb->update('share')
316+
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
317+
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
318+
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
319+
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
320+
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
321+
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));
322+
323+
if ($shareAttributes !== null) {
324+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
325+
}
326+
327+
$qb->executeStatement();
328+
329+
/*
330+
* Now update the permissions for all children that have not set it to 0
331+
*/
332+
$qb = $this->dbConnection->getQueryBuilder();
333+
$qb->update('share')
334+
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
335+
->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
336+
->set('permissions', $qb->createNamedParameter($share->getPermissions()));
337+
338+
if ($shareAttributes !== null) {
339+
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
340+
}
341+
342+
$qb->executeStatement();
343+
} catch (\Exception $e) {
344+
$this->dbConnection->rollBack();
345+
throw $e;
346+
}
347+
$this->dbConnection->commit();
309348

310349
return $share;
311350
}
@@ -1101,6 +1140,42 @@ public function getAllShares(): iterable {
11011140
$cursor->closeCursor();
11021141
}
11031142

1143+
protected function updateShareAttributes(IShare $share, ?string $data): IShare {
1144+
if ($data === null || $data === '') {
1145+
return $share;
1146+
}
1147+
$attributes = $share->getAttributes() ?? $share->newAttributes();
1148+
$compressedAttributes = \json_decode($data, true);
1149+
if ($compressedAttributes === false || $compressedAttributes === null) {
1150+
return $share;
1151+
}
1152+
foreach ($compressedAttributes as $compressedAttribute) {
1153+
$attributes->setAttribute(
1154+
$compressedAttribute[0],
1155+
$compressedAttribute[1],
1156+
$compressedAttribute[2]
1157+
);
1158+
}
1159+
$share->setAttributes($attributes);
1160+
return $share;
1161+
}
1162+
1163+
protected function formatShareAttributes(?IAttributes $attributes): ?string {
1164+
if ($attributes === null || empty($attributes->toArray())) {
1165+
return null;
1166+
}
1167+
1168+
$compressedAttributes = [];
1169+
foreach ($attributes->toArray() as $attribute) {
1170+
$compressedAttributes[] = [
1171+
0 => $attribute['scope'],
1172+
1 => $attribute['key'],
1173+
2 => $attribute['value']
1174+
];
1175+
}
1176+
return \json_encode($compressedAttributes) ?: null;
1177+
}
1178+
11041179
public function getOrphanedAttachmentShares(): array {
11051180
$allCardIds = $this->cardMapper->getAllCardIds();
11061181

0 commit comments

Comments
 (0)