Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 75 additions & 7 deletions lib/Sharing/DeckShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\IL10N;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IAttributes;
use OCP\Share\IManager;
use OCP\Share\IShare;

Expand Down Expand Up @@ -113,6 +114,11 @@ public function create(IShare $share) {
)
);*/

// set share attributes
$shareAttributes = $this->formatShareAttributes(
$share->getAttributes()
);

$shareId = $this->addShareToDB(
$share->getSharedWith(),
$share->getSharedBy(),
Expand All @@ -122,7 +128,8 @@ public function create(IShare $share) {
$share->getTarget(),
$share->getPermissions(),
$share->getToken() ?? '',
$share->getExpirationDate()
$share->getExpirationDate(),
$shareAttributes
);
$data = $this->getRawShare($shareId);

Expand All @@ -143,6 +150,7 @@ public function create(IShare $share) {
* @param int $permissions
* @param string $token
* @param \DateTime|null $expirationDate
* @param string|null $attributes
* @return int
*/
private function addShareToDB(
Expand All @@ -155,6 +163,7 @@ private function addShareToDB(
int $permissions,
string $token,
?\DateTime $expirationDate,
?string $attributes = null,
): int {
$qb = $this->dbConnection->getQueryBuilder();
$qb->insert('share')
Expand All @@ -174,6 +183,10 @@ private function addShareToDB(
$qb->setValue('expiration', $qb->createNamedParameter($expirationDate, 'datetime'));
}

if ($attributes !== null) {
$qb->setValue('attributes', $qb->createNamedParameter($attributes));
}

$qb->executeStatement();

return $qb->getLastInsertId();
Expand Down Expand Up @@ -244,6 +257,9 @@ private function createShareObject(array $data): IShare {
$entryData['parent'] = $entryData['f_parent'];
$share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData, $this->mimeTypeLoader));
}

$share = $this->updateShareAttributes($share, $data['attributes'] ?? null);

return $share;
}

Expand Down Expand Up @@ -275,8 +291,14 @@ public function update(IShare $share) {
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
->execute();
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));

$shareAttributes = $this->formatShareAttributes($share->getAttributes());
if ($shareAttributes !== null) {
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
}

$qb->executeStatement();

/*
* Update all user defined group shares
Expand All @@ -288,8 +310,13 @@ public function update(IShare $share) {
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
->execute();
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE));

if ($shareAttributes !== null) {
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
}

$qb->executeStatement();

/*
* Now update the permissions for all children that have not set it to 0
Expand All @@ -298,8 +325,13 @@ public function update(IShare $share) {
$qb->update('share')
->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
->execute();
->set('permissions', $qb->createNamedParameter($share->getPermissions()));

if ($shareAttributes !== null) {
$qb->set('attributes', $qb->createNamedParameter($shareAttributes));
}

$qb->executeStatement();

return $share;
}
Expand Down Expand Up @@ -1026,6 +1058,42 @@ public function getAllShares(): iterable {
$cursor->closeCursor();
}

protected function updateShareAttributes(IShare $share, ?string $data): IShare {
if ($data !== null && $data !== '') {
$attributes = $share->getAttributes() ?? $share->newAttributes();
$compressedAttributes = \json_decode($data, true);
if ($compressedAttributes === false || $compressedAttributes === null) {
return $share;
}
foreach ($compressedAttributes as $compressedAttribute) {
$attributes->setAttribute(
$compressedAttribute[0],
$compressedAttribute[1],
$compressedAttribute[2]
);
}
$share->setAttributes($attributes);
}

return $share;
}

protected function formatShareAttributes(?IAttributes $attributes): ?string {
if ($attributes === null || empty($attributes->toArray())) {
return null;
}

$compressedAttributes = [];
foreach ($attributes->toArray() as $attribute) {
$compressedAttributes[] = [
0 => $attribute['scope'],
1 => $attribute['key'],
2 => $attribute['value']
];
}
return \json_encode($compressedAttributes) ?: null;
}

public function getOrphanedAttachmentShares(): array {
$allCardIds = $this->cardMapper->getAllCardIds();

Expand Down
Loading