-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a method to get all users with access to a file in OCP #38946
base: master
Are you sure you want to change the base?
Changes from all commits
cb2495a
8f0b7ce
fd8c6ac
8977e4e
c1c40ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,14 @@ | |
use OCP\Files\Config\ICachedMountFileInfo; | ||
use OCP\Files\Config\ICachedMountInfo; | ||
use OCP\Files\Config\IUserMountCache; | ||
use OCP\Files\IRootFolder; | ||
use OCP\Files\Mount\IMountPoint; | ||
use OCP\Files\Node; | ||
use OCP\Files\NotFoundException; | ||
use OCP\IDBConnection; | ||
use OCP\IUser; | ||
use OCP\IUserManager; | ||
use OCP\Server; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
|
@@ -380,6 +384,61 @@ public function getMountsForFileId($fileId, $user = null) { | |
}, $filteredMounts); | ||
} | ||
|
||
/** | ||
* Get all nodes that give access to a file | ||
* | ||
* @return array<string, Node[]> Nodes giving access to the given fileId, indexed by user ID | ||
* @since 28.0.0 | ||
*/ | ||
public function getReadableNodesByUserForFileId(int $fileId): array { | ||
$mounts = $this->getMountsForFileId($fileId); | ||
$rootFolder = Server::get(IRootFolder::class); | ||
|
||
$result = []; | ||
foreach ($mounts as $mount) { | ||
$uid = $mount->getUser()->getUID(); | ||
if (!isset($result[$uid])) { | ||
$result[$uid] = []; | ||
} | ||
|
||
$userFolder = $rootFolder->getUserFolder($uid); | ||
$result[$uid] = array_merge($result[$uid], $userFolder->getById($fileId)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel that there should be a more efficient way to do this here since we already know the mount. Perhaps extracting some of the logic from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #40482 (comment) for some alternate discussion around the same problem |
||
} | ||
|
||
return array_filter($result); | ||
} | ||
|
||
/** | ||
* Get all users having read access to a file, with a path they see it as. | ||
* They may see the same file under other paths, | ||
* to get this information use the exhaustive getReadableNodesByUserForFileId | ||
* | ||
* @return array<string,string> Paths giving access to the given fileId, indexed by user ID | ||
|
||
* @since 28.0.0 | ||
*/ | ||
public function getReadablePathByUserForFileId(int $fileId): array { | ||
$mounts = $this->getMountsForFileId($fileId); | ||
$rootFolder = Server::get(IRootFolder::class); | ||
$result = []; | ||
foreach ($mounts as $mount) { | ||
$uid = $mount->getUser()->getUID(); | ||
if (isset($result[$uid])) { | ||
continue; | ||
} | ||
|
||
$userFolder = $rootFolder->getUserFolder($uid); | ||
$nodes = $userFolder->getById($fileId); | ||
$node = reset($nodes); | ||
if ($node) { | ||
$path = $userFolder->getRelativePath($node->getPath()); | ||
if ($path !== null) { | ||
$result[$uid] = $path; | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
|
||
} | ||
|
||
/** | ||
* Remove all cached mounts for a user | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make more sense to move this method to the
RootFolder
instead