Skip to content
Draft
Show file tree
Hide file tree
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
29 changes: 18 additions & 11 deletions apps/dav/lib/Files/NodeAttributes/SharesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ public function preload(NodeAttributeContext $context, array $nodes, array $attr
return;
}

foreach ($nodes as $node) {
if (!isset($this->shares[$userId][$node->getId()])) {
$this->shares[$userId][$node->getId()] = $this->getShares($userId, $node);
}
$nodes = array_values(array_filter($nodes, fn (Node $node): bool => !isset($this->shares[$userId][$node->getId()])));
if ($nodes !== []) {
$this->shares[$userId] = $this->getShares($userId, $nodes) + ($this->shares[$userId] ?? []);
}
}

Expand Down Expand Up @@ -104,20 +103,28 @@ public function getAttributes(NodeAttributeContext $context, Node $node, array $
}

/**
* Shares the user created on the node, and the ones the user received for it
* Shares the user created on the nodes, and the ones the user received for them
*
* @return list<IShare>
* @param non-empty-list<Node> $nodes
* @return array<int, list<IShare>> by file id
*/
private function getShares(string $userId, Node $node): array {
$shares = [];
private function getShares(string $userId, array $nodes): array {
$shares = array_fill_keys(array_map(static fn (Node $node): int => $node->getId(), $nodes), []);
foreach (self::SHARE_TYPES as $shareType) {
$shares[] = $this->shareManager->getSharesBy($userId, $shareType, $node, false, -1);
$typeShares = [$this->shareManager->getSharesByNodes($userId, $shareType, $nodes)];
try {
$shares[] = $this->shareManager->getSharedWith($userId, $shareType, $node, -1);
$typeShares[] = $this->shareManager->getSharedWithByNodes($userId, $shareType, $nodes);
} catch (BackendError) {
// a share provider that is not available has no shares to show
}
foreach ($typeShares as $sharesByFileId) {
foreach ($sharesByFileId as $fileId => $fileShares) {
if (isset($shares[$fileId])) {
array_push($shares[$fileId], ...$fileShares);
}
}
}
}
return array_values(array_merge(...$shares));
return $shares;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private function shareManager(array $outgoing, array $incoming): IManager {
}, array_filter($shares, static fn (array $data): bool => $data[0] === $type)));

$shareManager = $this->createMock(IManager::class);
$shareManager->method('getSharesBy')->willReturnCallback(static fn (string $userId, int $type): array => $toShares($outgoing, $type));
$shareManager->method('getSharedWith')->willReturnCallback(static fn (string $userId, int $type): array => $toShares($incoming, $type));
$shareManager->method('getSharesByNodes')->willReturnCallback(static fn (string $userId, int $type): array => array_filter([42 => $toShares($outgoing, $type)]));
$shareManager->method('getSharedWithByNodes')->willReturnCallback(static fn (string $userId, int $type): array => array_filter([42 => $toShares($incoming, $type)]));
return $shareManager;
}

Expand Down
72 changes: 46 additions & 26 deletions apps/dav/tests/unit/Files/NodeAttributes/SharesProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,23 @@ public static function dataShareTypes(): array {
#[DataProvider('dataShareTypes')]
public function testOutgoingSharesOfANode(array $shareTypes): void {
$node = $this->node(File::class, 11, '/alice/files/report.md');
$this->shareManager->method('getSharesBy')
->willReturnCallback(fn (string $userId, int $type, Node $sharedNode, bool $reshares, int $limit): array => $userId === 'alice' && $sharedNode === $node && !$reshares && $limit === -1 && in_array($type, $shareTypes, true)
? [$this->share($type)]
$this->shareManager->method('getSharesByNodes')
->willReturnCallback(fn (string $userId, int $type, array $nodes): array => $userId === 'alice' && $nodes === [$node] && in_array($type, $shareTypes, true)
? [11 => [$this->share($type)]]
: []);
$this->shareManager->method('getSharedWith')->willReturn([]);
$this->shareManager->method('getSharedWithByNodes')->willReturn([]);

[$types] = $this->getValues($node);
self::assertSame($shareTypes, $types);
}

public function testReceivedSharesAreIncludedForANode(): void {
$node = $this->node(Folder::class, 12, '/alice/files/Shared');
$this->shareManager->method('getSharesBy')
->willReturnCallback(fn (string $userId, int $type): array => $type === IShare::TYPE_LINK ? [$this->share(IShare::TYPE_LINK)] : []);
$this->shareManager->method('getSharedWith')
->willReturnCallback(fn (string $userId, int $type, ?Node $sharedNode, int $limit): array => $userId === 'alice' && $sharedNode === $node && $limit === -1 && $type === IShare::TYPE_GROUP
? [$this->share(IShare::TYPE_GROUP, 'staff', 'Staff')]
$this->shareManager->method('getSharesByNodes')
->willReturnCallback(fn (string $userId, int $type): array => $type === IShare::TYPE_LINK ? [12 => [$this->share(IShare::TYPE_LINK)]] : []);
$this->shareManager->method('getSharedWithByNodes')
->willReturnCallback(fn (string $userId, int $type, array $nodes): array => $userId === 'alice' && $nodes === [$node] && $type === IShare::TYPE_GROUP
? [12 => [$this->share(IShare::TYPE_GROUP, 'staff', 'Staff')]]
: []);

[$types, $sharees] = $this->getValues($node);
Expand All @@ -124,9 +124,9 @@ public function testReceivedSharesAreIncludedForANode(): void {

public function testUnavailableShareProviderIsSkipped(): void {
$node = $this->node(File::class, 11, '/alice/files/report.md');
$this->shareManager->method('getSharesBy')
->willReturnCallback(fn (string $userId, int $type): array => $type === IShare::TYPE_USER ? [$this->share(IShare::TYPE_USER, 'bob', 'Bob')] : []);
$this->shareManager->method('getSharedWith')
$this->shareManager->method('getSharesByNodes')
->willReturnCallback(fn (string $userId, int $type): array => $type === IShare::TYPE_USER ? [11 => [$this->share(IShare::TYPE_USER, 'bob', 'Bob')]] : []);
$this->shareManager->method('getSharedWithByNodes')
->willReturnCallback(static fn (string $userId, int $type): array => $type === IShare::TYPE_ROOM ? throw new BackendError() : []);

[$types] = $this->getValues($node);
Expand All @@ -135,17 +135,37 @@ public function testUnavailableShareProviderIsSkipped(): void {

public function testShareTypesAreUnique(): void {
$node = $this->node(File::class, 11, '/alice/files/report.md');
$this->shareManager->method('getSharesBy')
$this->shareManager->method('getSharesByNodes')
->willReturnCallback(fn (string $userId, int $type): array => $type === IShare::TYPE_USER
? [$this->share(IShare::TYPE_USER, 'bob', 'Bob'), $this->share(IShare::TYPE_USER, 'carol', 'Carol')]
? [11 => [$this->share(IShare::TYPE_USER, 'bob', 'Bob'), $this->share(IShare::TYPE_USER, 'carol', 'Carol')]]
: []);
$this->shareManager->method('getSharedWith')->willReturn([]);
$this->shareManager->method('getSharedWithByNodes')->willReturn([]);

[$types, $sharees] = $this->getValues($node);
self::assertSame([IShare::TYPE_USER], $types);
self::assertSame(['bob', 'carol'], array_column($sharees, 'id'));
}

public function testNodesAreLoadedTogether(): void {
$first = $this->node(File::class, 31, '/alice/files/a.md');
$second = $this->node(File::class, 32, '/alice/files/Other/b.md');

// One call per share type and direction for all the nodes
$this->shareManager->expects(self::exactly(8))
->method('getSharesByNodes')
->with('alice', self::anything(), [$first, $second])
->willReturnCallback(fn (string $userId, int $type): array => $type === IShare::TYPE_LINK ? [32 => [$this->share(IShare::TYPE_LINK)]] : []);
$this->shareManager->expects(self::exactly(8))
->method('getSharedWithByNodes')
->with('alice', self::anything(), [$first, $second])
->willReturn([]);

$this->provider->preload($this->context, [$first, $second], self::ATTRIBUTES);

self::assertSame([], $this->getValues($first)[0]);
self::assertSame([IShare::TYPE_LINK], $this->getValues($second)[0]);
}

public function testFolderListingLoadsTheChildrenAtOnce(): void {
$folder = $this->node(Folder::class, 20, '/alice/files/Projects');
$shared = $this->node(File::class, 21, '/alice/files/Projects/plan.md');
Expand All @@ -157,12 +177,12 @@ public function testFolderListingLoadsTheChildrenAtOnce(): void {
->willReturn([21 => [$this->share(IShare::TYPE_LINK)]]);
// The folder itself is looked up on its own, its children are not
$this->shareManager->expects(self::exactly(8))
->method('getSharesBy')
->with('alice', self::anything(), $folder, false, -1)
->method('getSharesByNodes')
->with('alice', self::anything(), [$folder])
->willReturn([]);
$this->shareManager->expects(self::exactly(8))
->method('getSharedWith')
->with('alice', self::anything(), $folder, -1)
->method('getSharedWithByNodes')
->with('alice', self::anything(), [$folder])
->willReturn([]);

$this->provider->preloadListing($this->context, $folder, [$shared, $notShared], self::ATTRIBUTES);
Expand All @@ -178,10 +198,10 @@ public function testAFolderAndItsChildOutsideOfAListingGetTheirOwnShares(): void
$received = $this->node(Folder::class, 41, '/alice/files/X/Y');

$this->shareManager->expects(self::never())->method('getSharesInFolder');
$this->shareManager->method('getSharesBy')->willReturn([]);
$this->shareManager->method('getSharedWith')
->willReturnCallback(fn (string $userId, int $type, Node $node): array => $node === $received && $type === IShare::TYPE_USER
? [$this->share(IShare::TYPE_USER, 'alice', 'Alice')]
$this->shareManager->method('getSharesByNodes')->willReturn([]);
$this->shareManager->method('getSharedWithByNodes')
->willReturnCallback(fn (string $userId, int $type, array $nodes): array => $nodes === [$folder, $received] && $type === IShare::TYPE_USER
? [41 => [$this->share(IShare::TYPE_USER, 'alice', 'Alice')]]
: []);

$this->provider->preload($this->context, [$folder, $received], self::ATTRIBUTES);
Expand All @@ -192,9 +212,9 @@ public function testAFolderAndItsChildOutsideOfAListingGetTheirOwnShares(): void

public function testPreloadingTwiceOnlyLoadsNewNodes(): void {
$this->shareManager->expects(self::exactly(16))
->method('getSharesBy')
->method('getSharesByNodes')
->willReturn([]);
$this->shareManager->method('getSharedWith')->willReturn([]);
$this->shareManager->method('getSharedWithByNodes')->willReturn([]);

$this->provider->preload($this->context, [$this->node(File::class, 1, '/alice/files/a')], self::ATTRIBUTES);
$this->provider->preload($this->context, [$this->node(File::class, 1, '/alice/files/a')], self::ATTRIBUTES);
Expand All @@ -204,7 +224,7 @@ public function testPreloadingTwiceOnlyLoadsNewNodes(): void {
public function testNothingOnPublicShares(): void {
$context = NodeAttributeContext::forShare($this->createMock(IShare::class));
$node = $this->node(File::class, 11, '/bob/files/Shared/report.md');
$this->shareManager->expects(self::never())->method('getSharesBy');
$this->shareManager->expects(self::never())->method('getSharesByNodes');

$this->provider->preload($context, [$node], self::ATTRIBUTES);
self::assertSame([], $this->provider->getAttributes($context, $node, self::ATTRIBUTES));
Expand Down
37 changes: 32 additions & 5 deletions apps/federatedfilesharing/lib/FederatedShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\Share\IShare;
use OCP\Share\IShareProvider;
use OCP\Share\IShareProviderSupportsAllSharesInFolder;
use OCP\Share\IShareProviderSupportsFileIds;
use Override;
use Psr\Log\LoggerInterface;

Expand All @@ -36,7 +37,7 @@
*
* @package OCA\FederatedFileSharing
*/
class FederatedShareProvider implements IShareProvider, IShareProviderSupportsAllSharesInFolder {
class FederatedShareProvider implements IShareProvider, IShareProviderSupportsAllSharesInFolder, IShareProviderSupportsFileIds {
public const SHARE_TYPE_REMOTE = 6;

/** @var string */
Expand Down Expand Up @@ -559,6 +560,19 @@ private function getSharesInFolderInternal(?string $userId, Folder $node, ?bool
*/
#[\Override]
public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
return $this->getSharesByInternal($userId, $shareType, $node === null ? null : [$node->getId()], $reshares, $limit, $offset);
}

#[\Override]
public function getSharesByFileIds(string $userId, int $shareType, array $fileIds): array {
return $this->getSharesByInternal($userId, $shareType, $fileIds, false, -1, 0);
}

/**
* @param list<int>|null $fileIds
* @return list<IShare>
*/
private function getSharesByInternal($userId, $shareType, ?array $fileIds, $reshares, $limit, $offset): array {
$qb = $this->dbConnection->getQueryBuilder();
$qb->select('*')
->from('share');
Expand Down Expand Up @@ -590,8 +604,8 @@ public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offs
);
}

if ($node !== null) {
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
if ($fileIds !== null) {
$qb->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
}

if ($limit !== -1) {
Expand Down Expand Up @@ -673,6 +687,19 @@ public function getSharesByPath(Node $path) {
*/
#[\Override]
public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
return $this->getSharedWithInternal($userId, $shareType, $node === null ? null : [$node->getId()], $limit, $offset);
}

#[\Override]
public function getSharedWithByFileIds(string $userId, int $shareType, array $fileIds): array {
return array_values($this->getSharedWithInternal($userId, $shareType, $fileIds, -1, 0));
}

/**
* @param list<int>|null $fileIds
* @return IShare[]
*/
private function getSharedWithInternal($userId, $shareType, ?array $fileIds, $limit, $offset): array {
/** @var IShare[] $shares */
$shares = [];

Expand All @@ -695,8 +722,8 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));

// Filter by node if provided
if ($node !== null) {
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
if ($fileIds !== null) {
$qb->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
}

$cursor = $qb->executeQuery();
Expand Down
36 changes: 31 additions & 5 deletions apps/sharebymail/lib/ShareByMailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,19 @@ public function restore(IShare $share, string $recipient): IShare {
*/
#[\Override]
public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset): array {
return $this->getSharesByInternal($userId, $shareType, $node === null ? null : [$node->getId()], $reshares, $limit, $offset);
}

#[\Override]
public function getSharesByFileIds(string $userId, int $shareType, array $fileIds): array {
return $this->getSharesByInternal($userId, $shareType, $fileIds, false, -1, 0);
}

/**
* @param list<int>|null $fileIds
* @return list<IShare>
*/
private function getSharesByInternal($userId, $shareType, ?array $fileIds, $reshares, $limit, $offset): array {
$qb = $this->dbConnection->getQueryBuilder();
$qb->select('*')
->from('share');
Expand All @@ -861,7 +874,7 @@ public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offs
$or1
)
);
} elseif ($node === null) {
} elseif ($fileIds === null) {
$qb->andWhere(
$qb->expr()->orX(
$qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
Expand All @@ -870,8 +883,8 @@ public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offs
);
}

if ($node !== null) {
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
if ($fileIds !== null) {
$qb->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
}

if ($limit !== -1) {
Expand Down Expand Up @@ -954,6 +967,19 @@ public function getSharesByPath(Node $path): array {
*/
#[\Override]
public function getSharedWith($userId, $shareType, $node, $limit, $offset): array {
return $this->getSharedWithInternal($userId, $shareType, $node === null ? null : [$node->getId()], $limit, $offset);
}

#[\Override]
public function getSharedWithByFileIds(string $userId, int $shareType, array $fileIds): array {
return array_values($this->getSharedWithInternal($userId, $shareType, $fileIds, -1, 0));
}

/**
* @param list<int>|null $fileIds
* @return IShare[]
*/
private function getSharedWithInternal($userId, $shareType, ?array $fileIds, $limit, $offset): array {
/** @var IShare[] $shares */
$shares = [];

Expand All @@ -977,8 +1003,8 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset): arra
$qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));

// Filter by node if provided
if ($node !== null) {
$qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
if ($fileIds !== null) {
$qb->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
}

$cursor = $qb->executeQuery();
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@
'OCP\\Share\\IShareProviderGetUsers' => $baseDir . '/lib/public/Share/IShareProviderGetUsers.php',
'OCP\\Share\\IShareProviderSupportsAccept' => $baseDir . '/lib/public/Share/IShareProviderSupportsAccept.php',
'OCP\\Share\\IShareProviderSupportsAllSharesInFolder' => $baseDir . '/lib/public/Share/IShareProviderSupportsAllSharesInFolder.php',
'OCP\\Share\\IShareProviderSupportsFileIds' => $baseDir . '/lib/public/Share/IShareProviderSupportsFileIds.php',
'OCP\\Share\\IShareProviderWithNotification' => $baseDir . '/lib/public/Share/IShareProviderWithNotification.php',
'OCP\\Share\\ShareReview\\Events\\ShareReviewAccessCheckEvent' => $baseDir . '/lib/public/Share/ShareReview/Events/ShareReviewAccessCheckEvent.php',
'OCP\\Share\\ShareReview\\IShareReviewSource' => $baseDir . '/lib/public/Share/ShareReview/IShareReviewSource.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Share\\IShareProviderGetUsers' => __DIR__ . '/../../..' . '/lib/public/Share/IShareProviderGetUsers.php',
'OCP\\Share\\IShareProviderSupportsAccept' => __DIR__ . '/../../..' . '/lib/public/Share/IShareProviderSupportsAccept.php',
'OCP\\Share\\IShareProviderSupportsAllSharesInFolder' => __DIR__ . '/../../..' . '/lib/public/Share/IShareProviderSupportsAllSharesInFolder.php',
'OCP\\Share\\IShareProviderSupportsFileIds' => __DIR__ . '/../../..' . '/lib/public/Share/IShareProviderSupportsFileIds.php',
'OCP\\Share\\IShareProviderWithNotification' => __DIR__ . '/../../..' . '/lib/public/Share/IShareProviderWithNotification.php',
'OCP\\Share\\ShareReview\\Events\\ShareReviewAccessCheckEvent' => __DIR__ . '/../../..' . '/lib/public/Share/ShareReview/Events/ShareReviewAccessCheckEvent.php',
'OCP\\Share\\ShareReview\\IShareReviewSource' => __DIR__ . '/../../..' . '/lib/public/Share/ShareReview/IShareReviewSource.php',
Expand Down
Loading
Loading