Skip to content

Commit 262fee8

Browse files
committed
feat: introduce API for partial share providers
Adds support for retrieval of shares by path Signed-off-by: Salvatore Martire <[email protected]>
1 parent bed17ca commit 262fee8

File tree

6 files changed

+188
-1
lines changed

6 files changed

+188
-1
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@
817817
'OCP\\Share\\Exceptions\\ShareTokenException' => $baseDir . '/lib/public/Share/Exceptions/ShareTokenException.php',
818818
'OCP\\Share\\IAttributes' => $baseDir . '/lib/public/Share/IAttributes.php',
819819
'OCP\\Share\\IManager' => $baseDir . '/lib/public/Share/IManager.php',
820+
'OCP\\Share\\IPartialShareProvider' => $baseDir . '/lib/public/Share/IPartialShareProvider.php',
820821
'OCP\\Share\\IProviderFactory' => $baseDir . '/lib/public/Share/IProviderFactory.php',
821822
'OCP\\Share\\IPublicShareTemplateFactory' => $baseDir . '/lib/public/Share/IPublicShareTemplateFactory.php',
822823
'OCP\\Share\\IPublicShareTemplateProvider' => $baseDir . '/lib/public/Share/IPublicShareTemplateProvider.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
858858
'OCP\\Share\\Exceptions\\ShareTokenException' => __DIR__ . '/../../..' . '/lib/public/Share/Exceptions/ShareTokenException.php',
859859
'OCP\\Share\\IAttributes' => __DIR__ . '/../../..' . '/lib/public/Share/IAttributes.php',
860860
'OCP\\Share\\IManager' => __DIR__ . '/../../..' . '/lib/public/Share/IManager.php',
861+
'OCP\\Share\\IPartialShareProvider' => __DIR__ . '/../../..' . '/lib/public/Share/IPartialShareProvider.php',
861862
'OCP\\Share\\IProviderFactory' => __DIR__ . '/../../..' . '/lib/public/Share/IProviderFactory.php',
862863
'OCP\\Share\\IPublicShareTemplateFactory' => __DIR__ . '/../../..' . '/lib/public/Share/IPublicShareTemplateFactory.php',
863864
'OCP\\Share\\IPublicShareTemplateProvider' => __DIR__ . '/../../..' . '/lib/public/Share/IPublicShareTemplateProvider.php',

lib/private/Share20/DefaultShareProvider.php

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\Share\Exceptions\ShareNotFound;
3333
use OCP\Share\IAttributes;
3434
use OCP\Share\IManager;
35+
use OCP\Share\IPartialShareProvider;
3536
use OCP\Share\IShare;
3637
use OCP\Share\IShareProviderSupportsAccept;
3738
use OCP\Share\IShareProviderSupportsAllSharesInFolder;
@@ -44,7 +45,7 @@
4445
*
4546
* @package OC\Share20
4647
*/
47-
class DefaultShareProvider implements IShareProviderWithNotification, IShareProviderSupportsAccept, IShareProviderSupportsAllSharesInFolder {
48+
class DefaultShareProvider implements IShareProviderWithNotification, IShareProviderSupportsAccept, IShareProviderSupportsAllSharesInFolder, IPartialShareProvider {
4849
public function __construct(
4950
private IDBConnection $dbConn,
5051
private IUserManager $userManager,
@@ -948,6 +949,112 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
948949
return $shares;
949950
}
950951

952+
/**
953+
* @inheritDoc
954+
*/
955+
public function getSharedWithByPath(
956+
string $userId,
957+
int $shareType,
958+
string $path,
959+
bool $forChildren,
960+
int $limit,
961+
int $offset
962+
): iterable {
963+
$shares = [];
964+
965+
if ($shareType === IShare::TYPE_USER) {
966+
//Get shares directly with this user
967+
$qb = $this->dbConn->getQueryBuilder();
968+
$qb->select('s.*',
969+
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
970+
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
971+
'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
972+
)
973+
->selectAlias('st.id', 'storage_string_id')
974+
->from('share', 's')
975+
->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
976+
->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'));
977+
978+
// Order by id
979+
$qb->orderBy('s.id');
980+
981+
// Set limit and offset
982+
if ($limit !== -1) {
983+
$qb->setMaxResults($limit);
984+
}
985+
$qb->setFirstResult($offset);
986+
987+
$qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_USER)))
988+
->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
989+
->andWhere($qb->expr()->in('item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)));
990+
991+
if ($forChildren) {
992+
$qb->andWhere($qb->expr()->like('file_target', $qb->createNamedParameter ($this->dbConn->escapeLikeParameter($path) . '_%')));
993+
} else {
994+
$qb->andWhere($qb->expr()->eq('file_target', $qb->createNamedParameter($path)));
995+
}
996+
997+
$cursor = $qb->executeQuery();
998+
999+
while ($data = $cursor->fetch()) {
1000+
if ($data['fileid'] && $data['path'] === null) {
1001+
$data['path'] = (string)$data['path'];
1002+
$data['name'] = (string)$data['name'];
1003+
$data['checksum'] = (string)$data['checksum'];
1004+
}
1005+
if ($this->isAccessibleResult($data)) {
1006+
$shares[] = $this->createShare($data);
1007+
}
1008+
}
1009+
$cursor->closeCursor();
1010+
} elseif ($shareType === IShare::TYPE_GROUP) {
1011+
// get the parent share info (s) along with the child one (s2)
1012+
$qb = $this->dbConn->getQueryBuilder();
1013+
$qb->select('s.*', 's2.permissions AS s2_permissions', 's2.accepted AS s2_accepted', 's2.file_target AS s2_file_target', 's2.parent AS s2_parent',
1014+
'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
1015+
'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
1016+
'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
1017+
)
1018+
->selectAlias('st.id', 'storage_string_id')
1019+
->from('share', 's2')
1020+
->leftJoin('s2', 'filecache', 'f', $qb->expr()->eq('s2.file_source', 'f.fileid'))
1021+
->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
1022+
->leftJoin('s2', 'share', 's', $qb->expr()->eq('s2.parent', 's.id'))
1023+
->where($qb->expr()->eq('s2.share_with', $qb->createNamedParameter($userId)))
1024+
->andWhere($qb->expr()->eq('s2.share_type', $qb->createNamedParameter(IShare::TYPE_USERGROUP)))
1025+
->andWhere($qb->expr()->in('s2.item_type', $qb->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY)))
1026+
->orderBy('s2.id')
1027+
->setFirstResult($offset);
1028+
if ($limit !== -1) {
1029+
$qb->setMaxResults($limit);
1030+
}
1031+
1032+
if ($forChildren) {
1033+
$qb->andWhere($qb->expr()->like('s2.file_target', $qb->createNamedParameter ($this->dbConn->escapeLikeParameter($path) . '_%')));
1034+
} else {
1035+
$qb->andWhere($qb->expr()->eq('s2.file_target', $qb->createNamedParameter($path)));
1036+
}
1037+
1038+
$cursor = $qb->executeQuery();
1039+
while ($data = $cursor->fetch()) {
1040+
if ($this->isAccessibleResult($data)) {
1041+
$share = $this->createShare($data);
1042+
// patch the parent data with the user-specific changes
1043+
$share->setPermissions((int)$data['s2_permissions']);
1044+
$share->setStatus((int)$data['s2_accepted']);
1045+
$share->setTarget($data['s2_file_target']);
1046+
$share->setParent($data['s2_parent']);
1047+
$shares[] = $share;
1048+
}
1049+
}
1050+
$cursor->closeCursor();
1051+
} else {
1052+
throw new BackendError('Invalid backend');
1053+
}
1054+
1055+
return $shares;
1056+
}
1057+
9511058
/**
9521059
* Get a share by token
9531060
*

lib/private/Share20/Manager.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use OCP\Share\Exceptions\ShareNotFound;
5050
use OCP\Share\Exceptions\ShareTokenException;
5151
use OCP\Share\IManager;
52+
use OCP\Share\IPartialShareProvider;
5253
use OCP\Share\IProviderFactory;
5354
use OCP\Share\IShare;
5455
use OCP\Share\IShareProvider;
@@ -1286,6 +1287,40 @@ public function getSharedWith(string $userId, int $shareType, ?Node $node = null
12861287
return $shares;
12871288
}
12881289

1290+
/**
1291+
* @inheritDoc
1292+
*/
1293+
public function getSharedWithByPath(string $userId, int $shareType, string $path, bool $forChildren, int $limit = 50, int $offset = 0): iterable {
1294+
try {
1295+
$provider = $this->factory->getProviderForType($shareType);
1296+
} catch (ProviderException $e) {
1297+
return [];
1298+
}
1299+
1300+
if (!$provider instanceof IPartialShareProvider) {
1301+
return [];
1302+
}
1303+
1304+
$shares = $provider->getSharedWithByPath($userId,
1305+
$shareType,
1306+
$path,
1307+
$forChildren,
1308+
$limit,
1309+
$offset
1310+
);
1311+
1312+
// remove all shares which are already expired
1313+
foreach ($shares as $key => $share) {
1314+
try {
1315+
$this->checkShare($share);
1316+
} catch (ShareNotFound $e) {
1317+
unset($shares[$key]);
1318+
}
1319+
}
1320+
1321+
return $shares;
1322+
}
1323+
12891324
#[Override]
12901325
public function getDeletedSharedWith(string $userId, int $shareType, ?Node $node = null, int $limit = 50, int $offset = 0): array {
12911326
$shares = $this->getSharedWith($userId, $shareType, $node, $limit, $offset);

lib/public/Share/IManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ public function getSharesBy(string $userId, int $shareType, ?Node $path = null,
134134
*/
135135
public function getSharedWith(string $userId, int $shareType, ?Node $node = null, int $limit = 50, int $offset = 0): array;
136136

137+
/**
138+
* Get shares shared with a $user filtering by $path.
139+
*
140+
* @param IShare::TYPE_* $shareType
141+
* @param bool $forChildren if true, results should only include children of $path
142+
* @param int $limit The maximum number of shares returned, -1 for all
143+
*
144+
* @return iterable<IShare>
145+
* @since 33.0.0
146+
*/
147+
public function getSharedWithByPath(string $userId, int $shareType, string $path, bool $forChildren, int $limit = 50, int $offset = 0): iterable;
148+
137149
/**
138150
* Get deleted shares shared with $user.
139151
* Filter by $node if provided
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
namespace OCP\Share;
8+
9+
/**
10+
* Interface IPartialShareProvider
11+
*
12+
* @since 33.0.0
13+
*/
14+
interface IPartialShareProvider extends IShareProvider {
15+
/**
16+
* Get shares received by the given user and filtered by path.
17+
*
18+
* If $forChildren is true, results should only include children of $path
19+
*
20+
* @return iterable<IShare>
21+
* @since 33.0.0
22+
*/
23+
public function getSharedWithByPath(
24+
string $userId,
25+
int $shareType,
26+
string $path,
27+
bool $forChildren,
28+
int $limit,
29+
int $offset
30+
): iterable;
31+
}

0 commit comments

Comments
 (0)