Skip to content
Merged
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
74 changes: 31 additions & 43 deletions lib/private/Share20/ShareHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
Expand All @@ -12,28 +14,27 @@
use OCP\Files\NotPermittedException;
use OCP\Share\IManager;
use OCP\Share\IShareHelper;
use Override;

class ShareHelper implements IShareHelper {
public function __construct(
private IManager $shareManager,
private readonly IManager $shareManager,
) {
}

/**
* @param Node $node
* @return array [ users => [Mapping $uid => $pathForUser], remotes => [Mapping $cloudId => $pathToMountRoot]]
*/
public function getPathsForAccessList(Node $node) {
#[Override]
public function getPathsForAccessList(Node $node): array {
$result = [
'users' => [],
'remotes' => [],
];

$accessList = $this->shareManager->getAccessList($node, true, true);
if (!empty($accessList['users'])) {
if (isset($accessList['users']) && $accessList['users'] !== []) {
$result['users'] = $this->getPathsForUsers($node, $accessList['users']);
}
if (!empty($accessList['remote'])) {

if (isset($accessList['remote']) && $accessList['remote'] !== []) {
$result['remotes'] = $this->getPathsForRemotes($node, $accessList['remote']);
}

Expand All @@ -60,20 +61,20 @@ public function getPathsForAccessList(Node $node) {
* 'test3' => '/cat',
* ],
*
* @param Node $node
* @param array[] $users
* @return array
* @param non-empty-array<string, array{node_id: int, node_path: string}> $users
* @return array<string, string>
*/
protected function getPathsForUsers(Node $node, array $users) {
/** @var array[] $byId */
protected function getPathsForUsers(Node $node, array $users): array {
/** @var array<int, array<string, string>> $byId */
$byId = [];
/** @var array[] $results */
/** @var array<string, string> $results */
$results = [];

foreach ($users as $uid => $info) {
if (!isset($byId[$info['node_id']])) {
$byId[$info['node_id']] = [];
}

$byId[$info['node_id']][$uid] = $info['node_path'];
}

Expand All @@ -82,38 +83,33 @@ protected function getPathsForUsers(Node $node, array $users) {
foreach ($byId[$node->getId()] as $uid => $path) {
$results[$uid] = $path;
}

unset($byId[$node->getId()]);
}
} catch (NotFoundException $e) {
return $results;
} catch (InvalidPathException $e) {
} catch (NotFoundException|InvalidPathException) {
return $results;
}

if (empty($byId)) {
if ($byId === []) {
return $results;
}

$item = $node;
$appendix = '/' . $node->getName();
while (!empty($byId)) {
try {
/** @var Node $item */
$item = $item->getParent();

if (!empty($byId[$item->getId()])) {
if ($byId[$item->getId()] !== []) {
foreach ($byId[$item->getId()] as $uid => $path) {
$results[$uid] = $path . $appendix;
}

unset($byId[$item->getId()]);
}

$appendix = '/' . $item->getName() . $appendix;
} catch (NotFoundException $e) {
return $results;
} catch (InvalidPathException $e) {
return $results;
} catch (NotPermittedException $e) {
} catch (NotFoundException|InvalidPathException|NotPermittedException) {
return $results;
}
}
Expand Down Expand Up @@ -141,56 +137,48 @@ protected function getPathsForUsers(Node $node, array $users) {
* 'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
* ],
*
* @param Node $node
* @param array[] $remotes
* @return array
* @param non-empty-array<string, array{node_id: int, token: string}> $remotes
* @return array<string, array{token: string, node_path: string}>
*/
protected function getPathsForRemotes(Node $node, array $remotes) {
/** @var array[] $byId */
protected function getPathsForRemotes(Node $node, array $remotes): array {
/** @var array<int, array<string, string>> $byId */
$byId = [];
/** @var array[] $results */
/** @var array<string, array{token: string, node_path: string}> $results */
$results = [];

foreach ($remotes as $cloudId => $info) {
if (!isset($byId[$info['node_id']])) {
$byId[$info['node_id']] = [];
}

$byId[$info['node_id']][$cloudId] = $info['token'];
}

$item = $node;
while (!empty($byId)) {
try {
if (!empty($byId[$item->getId()])) {
if ($byId[$item->getId()] !== []) {
$path = $this->getMountedPath($item);
foreach ($byId[$item->getId()] as $uid => $token) {
$results[$uid] = [
'node_path' => $path,
'token' => $token,
];
}

unset($byId[$item->getId()]);
}

/** @var Node $item */
$item = $item->getParent();
} catch (NotFoundException $e) {
return $results;
} catch (InvalidPathException $e) {
return $results;
} catch (NotPermittedException $e) {
} catch (NotFoundException|InvalidPathException|NotPermittedException) {
return $results;
}
}

return $results;
}

/**
* @param Node $node
* @return string
*/
protected function getMountedPath(Node $node) {
protected function getMountedPath(Node $node): string {
$path = $node->getPath();
$sections = explode('/', $path, 4);
return '/' . $sections[3];
Expand Down
4 changes: 2 additions & 2 deletions lib/public/Share/IManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ public function userDeletedFromGroup(string $uid, string $gid): void;
* @return ($currentAccess is true
* ? array{
* users?: array<string, array{node_id: int, node_path: string}>,
* remote?: array<string, array{node_id: int, node_path: string}>,
* remote?: array<string, array{node_id: int, token: string}>,
* public?: bool,
* mail?: array<string, array{node_id: int, node_path: string}>
* mail?: array<string, array{node_id: int, token: string}>
* }
* : array{users?: list<string>, remote?: bool, public?: bool, mail?: list<string>})
* @since 12.0.0
Expand Down
11 changes: 7 additions & 4 deletions lib/public/Share/IShareHelper.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\Share;

use OCP\AppFramework\Attribute\Consumable;
use OCP\Files\Node;

/**
* Interface IShareHelper
*
* @since 12
* @since 12.0.0
*/
#[Consumable(since: '12.0.0')]
interface IShareHelper {
/**
* @param Node $node
* @return array [ users => [Mapping $uid => $pathForUser], remotes => [Mapping $cloudId => $pathToMountRoot]]
* @return array{users: array<string, string>, remotes: array<string, array{token: string, node_path: string}>} [ users => [Mapping $uid => $pathForUser], remotes => [Mapping $cloudId => $pathToMountRoot]]
* @since 12
*/
public function getPathsForAccessList(Node $node);
public function getPathsForAccessList(Node $node): array;
}
2 changes: 2 additions & 0 deletions psalm-strict.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<file name="build/psalm/ITypedQueryBuilderTest.php"/>
<file name="lib/private/DB/QueryBuilder/TypedQueryBuilder.php"/>
<file name="lib/public/DB/QueryBuilder/ITypedQueryBuilder.php"/>
<file name="lib/private/Share20/ShareHelper.php"/>
<file name="lib/public/Share/IShareHelper.php"/>
<ignoreFiles>
<directory name="apps/**/composer"/>
<directory name="apps/**/tests"/>
Expand Down
Loading