Skip to content

Commit f744d24

Browse files
authored
Merge pull request #7659 from nextcloud/fix/handle-share-attributes
fix: Handle share attributes in the share provider
2 parents d361d98 + 20d6bd9 commit f744d24

1 file changed

Lines changed: 109 additions & 34 deletions

File tree

lib/Sharing/DeckShareProvider.php

Lines changed: 109 additions & 34 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;
@@ -120,6 +121,11 @@ public function create(IShare $share) {
120121
)
121122
);*/
122123

124+
// set share attributes
125+
$shareAttributes = $this->formatShareAttributes(
126+
$share->getAttributes()
127+
);
128+
123129
$shareId = $this->addShareToDB(
124130
$share->getSharedWith(),
125131
$share->getSharedBy(),
@@ -129,7 +135,8 @@ public function create(IShare $share) {
129135
$share->getTarget(),
130136
$share->getPermissions(),
131137
$share->getToken() ?? '',
132-
$share->getExpirationDate()
138+
$share->getExpirationDate(),
139+
$shareAttributes
133140
);
134141
$data = $this->getRawShare($shareId);
135142

@@ -150,6 +157,7 @@ public function create(IShare $share) {
150157
* @param int $permissions
151158
* @param string $token
152159
* @param \DateTime|null $expirationDate
160+
* @param string|null $attributes
153161
* @return int
154162
*/
155163
private function addShareToDB(
@@ -162,6 +170,7 @@ private function addShareToDB(
162170
int $permissions,
163171
string $token,
164172
?\DateTime $expirationDate,
173+
?string $attributes = null,
165174
): int {
166175
$qb = $this->dbConnection->getQueryBuilder();
167176
$qb->insert('share')
@@ -181,6 +190,10 @@ private function addShareToDB(
181190
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
182191
}
183192

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

186199
return $qb->getLastInsertId();
@@ -251,6 +264,9 @@ private function createShareObject(array $data): IShare {
251264
$entryData['parent'] = $entryData['f_parent'];
252265
$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, $this->mimeTypeLoader));
253266
}
267+
268+
$share = $this->updateShareAttributes($share, $data['attributes'] ?? null);
269+
254270
return $share;
255271
}
256272

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

311350
return $share;
312351
}
@@ -1102,6 +1141,42 @@ public function getAllShares(): iterable {
11021141
$cursor->closeCursor();
11031142
}
11041143

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

0 commit comments

Comments
 (0)