Skip to content

Commit

Permalink
fix(Files): Handle getOwner() returning false
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Sep 17, 2024
1 parent 8ca6fca commit dc13f9c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
10 changes: 4 additions & 6 deletions apps/dav/lib/Storage/PublicOwnerWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

class PublicOwnerWrapper extends Wrapper {

/** @var string */
private $owner;
private string $owner;

/**
* @param array $arguments ['storage' => $storage, 'owner' => $owner]
Expand All @@ -28,11 +27,10 @@ public function __construct($arguments) {

public function getOwner($path): string|false {
$owner = parent::getOwner($path);

if ($owner === null || $owner === false) {
return $this->owner;
if ($owner !== false) {
return $owner;
}

return $owner;
return $this->owner;
}
}
4 changes: 4 additions & 0 deletions apps/files_versions/lib/Versions/LegacyVersionsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public function getVersionsForFile(IUser $user, FileInfo $file): array {

if ($storage->instanceOfStorage(ISharedStorage::class)) {
$owner = $storage->getOwner('');
if ($owner === false) {
throw new NotFoundException('No owner for ' . $file->getPath());
}

$user = $this->userManager->get($owner);

$fileId = $file->getId();
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, IC

$owner = null;
$ownerId = $storage->getOwner($cacheEntry['internalPath']);
if (!empty($ownerId)) {
if ($ownerId !== false) {
// Cache the user manager (for performance)
if ($this->userManager === null) {
$this->userManager = \OCP\Server::get(IUserManager::class);
Expand Down
19 changes: 17 additions & 2 deletions lib/private/Files/Node/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\ICacheFactory;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Server;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -477,9 +478,23 @@ public function getByIdInPath(int $id, string $path): array {
$pathRelativeToMount = substr($internalPath, strlen($rootInternalPath));
$pathRelativeToMount = ltrim($pathRelativeToMount, '/');
$absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/');
$storage = $mount->getStorage();
if ($storage === null) {
return null;
}
$ownerId = $storage->getOwner($pathRelativeToMount);
if ($ownerId !== false) {
$owner = Server::get(IUserManager::class)->get($ownerId);
} else {
$owner = null;
}
return $this->createNode($absolutePath, new FileInfo(
$absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
\OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
$absolutePath,
$storage,
$cacheEntry->getPath(),
$cacheEntry,
$mount,
$owner,
));
}, $mountsContainingFile);

Expand Down
40 changes: 33 additions & 7 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\Files\NotFoundException;
use OCP\Files\ReservedWordException;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\Server;
Expand Down Expand Up @@ -1364,7 +1365,7 @@ public function getFileInfo($path, $includeMountPoints = true) {

$ownerId = $storage->getOwner($internalPath);
$owner = null;
if ($ownerId !== null && $ownerId !== false) {
if ($ownerId !== false) {
// ownerId might be null if files are accessed with an access token without file system access
$owner = $this->getUserObjectForOwner($ownerId);
}
Expand Down Expand Up @@ -1450,7 +1451,12 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
if ($sharingDisabled) {
$content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
}
$owner = $this->getUserObjectForOwner($storage->getOwner($content['path']));
$ownerId = $storage->getOwner($content['path']);
if ($ownerId !== false) {
$owner = $this->getUserObjectForOwner($ownerId);
} else {
$owner = null;
}
return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner);
}, $contents);
$files = array_combine($fileNames, $fileInfos);
Expand Down Expand Up @@ -1527,7 +1533,12 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
$rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
}

$owner = $this->getUserObjectForOwner($subStorage->getOwner(''));
$ownerId = $subStorage->getOwner('');
if ($ownerId !== false) {
$owner = $this->getUserObjectForOwner($ownerId);
} else {
$owner = null;
}
$files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner);
}
}
Expand Down Expand Up @@ -1644,7 +1655,12 @@ private function searchCommon($method, $args) {
$internalPath = $result['path'];
$path = $mountPoint . $result['path'];
$result['path'] = substr($mountPoint . $result['path'], $rootLength);
$owner = $userManager->get($storage->getOwner($internalPath));
$ownerId = $storage->getOwner($internalPath);
if ($ownerId !== false) {
$owner = $userManager->get($ownerId);
} else {
$owner = null;
}
$files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
}
}
Expand All @@ -1663,7 +1679,12 @@ private function searchCommon($method, $args) {
$internalPath = $result['path'];
$result['path'] = rtrim($relativeMountPoint . $result['path'], '/');
$path = rtrim($mountPoint . $internalPath, '/');
$owner = $userManager->get($storage->getOwner($internalPath));
$ownerId = $storage->getOwner($internalPath);
if ($ownerId !== false) {
$owner = $userManager->get($ownerId);
} else {
$owner = null;
}
$files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
}
}
Expand Down Expand Up @@ -1811,7 +1832,12 @@ private function getPartFileInfo(string $path): \OC\Files\FileInfo {
$mount = $this->getMount($path);
$storage = $mount->getStorage();
$internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
$owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath));
$ownerId = $storage->getOwner($internalPath);
if ($ownerId !== false) {
$owner = Server::get(IUserManager::class)->get($ownerId);
} else {
$owner = null;
}
return new FileInfo(
$this->getAbsolutePath($path),
$storage,
Expand Down Expand Up @@ -1846,7 +1872,7 @@ public function verifyPath($path, $fileName, $readonly = false): void {

// Short cut for read-only validation
if ($readonly) {
$validator = \OCP\Server::get(FilenameValidator::class);
$validator = Server::get(FilenameValidator::class);
if ($validator->isForbidden($fileName)) {
$l = \OCP\Util::getL10N('lib');
throw new InvalidPathException($l->t('Filename is a reserved word'));
Expand Down
3 changes: 1 addition & 2 deletions lib/private/legacy/OC_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,9 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
$relative = 0;
}

/** @var string $ownerId */
$ownerId = $storage->getOwner($path);
$ownerDisplayName = '';
if ($ownerId) {
if ($ownerId !== false) {
$ownerDisplayName = \OC::$server->getUserManager()->getDisplayName($ownerId) ?? '';
}

Expand Down

0 comments on commit dc13f9c

Please sign in to comment.