Skip to content

Commit

Permalink
Check node permissions when restoring a version
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Feb 27, 2024
1 parent 941dee9 commit b6ef916
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion apps/files_versions/lib/Versions/LegacyVersionsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OCA\Files_Versions\Versions;

use OC\Files\View;
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\Files_Sharing\SharedStorage;
use OCA\Files_Versions\Storage;
use OCP\Files\File;
Expand All @@ -37,16 +38,20 @@
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;

class LegacyVersionsBackend implements IVersionBackend {
/** @var IRootFolder */
private $rootFolder;
/** @var IUserManager */
private $userManager;
/** @var IUserSession */
private $userSession;

public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IUserSession $userSession) {
$this->rootFolder = $rootFolder;
$this->userManager = $userManager;
$this->userSession = $userSession;
}

public function useBackendForStorage(IStorage $storage): bool {
Expand Down Expand Up @@ -96,6 +101,10 @@ public function createVersion(IUser $user, FileInfo $file) {
}

public function rollback(IVersion $version) {
if (!$this->currentUserHasPermissions($version, \OCP\Constants::PERMISSION_UPDATE)) {
throw new Forbidden('You cannot restore this version because you do not have update permissions on the source file.');
}

return Storage::rollback($version->getVersionPath(), $version->getRevisionId(), $version->getUser());
}

Expand Down Expand Up @@ -125,4 +134,23 @@ public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): Fi
$file = $versionFolder->get($userFolder->getRelativePath($sourceFile->getPath()) . '.v' . $revision);
return $file;
}

private function currentUserHasPermissions(IVersion $version, int $permissions): bool {
$sourceFile = $version->getSourceFile();
$currentUserId = $this->userSession->getUser()?->getUID();

Check failure on line 140 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

ParseError

apps/files_versions/lib/Versions/LegacyVersionsBackend.php:140:50: ParseError: Syntax error, unexpected T_OBJECT_OPERATOR on line 140 (see https://psalm.dev/173)

Check failure on line 140 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedFunction

apps/files_versions/lib/Versions/LegacyVersionsBackend.php:140:52: UndefinedFunction: Function OCA\Files_Versions\Versions\getUID does not exist (see https://psalm.dev/021)

Check failure

Code scanning / Psalm

ParseError Error

Syntax error, unexpected T_OBJECT_OPERATOR on line 140

Check failure

Code scanning / Psalm

UndefinedFunction Error

Function OCA\Files_Versions\Versions\getUID does not exist

Check failure on line 140 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / Psalm

Syntax error, unexpected T_OBJECT_OPERATOR on line 140 (see https://psalm.dev/173)

Check failure on line 140 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / Psalm

Function OCA\Files_Versions\Versions\getUID does not exist (see https://psalm.dev/021)

if ($currentUserId === null) {

Check failure on line 142 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedVariable

apps/files_versions/lib/Versions/LegacyVersionsBackend.php:142:7: UndefinedVariable: Cannot find referenced variable $currentUserId (see https://psalm.dev/024)

Check failure

Code scanning / Psalm

UndefinedVariable Error

Cannot find referenced variable $currentUserId

Check failure on line 142 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / Psalm

Cannot find referenced variable $currentUserId (see https://psalm.dev/024)
throw new NotFoundException("No user logged in");
}

if ($sourceFile->getOwner()?->getUID() !== $currentUserId) {

Check failure on line 146 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

ParseError

apps/files_versions/lib/Versions/LegacyVersionsBackend.php:146:31: ParseError: Syntax error, unexpected T_OBJECT_OPERATOR on line 146 (see https://psalm.dev/173)

Check failure on line 146 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedFunction

apps/files_versions/lib/Versions/LegacyVersionsBackend.php:146:33: UndefinedFunction: Function OCA\Files_Versions\Versions\getUID does not exist (see https://psalm.dev/021)

Check failure on line 146 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

ParseError

apps/files_versions/lib/Versions/LegacyVersionsBackend.php:146:60: ParseError: Syntax error, unexpected ')' on line 146 (see https://psalm.dev/173)

Check failure

Code scanning / Psalm

ParseError Error

Syntax error, unexpected T_OBJECT_OPERATOR on line 146

Check failure

Code scanning / Psalm

UndefinedFunction Error

Function OCA\Files_Versions\Versions\getUID does not exist

Check failure

Code scanning / Psalm

ParseError Error

Syntax error, unexpected ')' on line 146

Check failure on line 146 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / Psalm

Syntax error, unexpected T_OBJECT_OPERATOR on line 146 (see https://psalm.dev/173)

Check failure on line 146 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / Psalm

Function OCA\Files_Versions\Versions\getUID does not exist (see https://psalm.dev/021)

Check failure on line 146 in apps/files_versions/lib/Versions/LegacyVersionsBackend.php

View workflow job for this annotation

GitHub Actions / Psalm

Syntax error, unexpected ')' on line 146 (see https://psalm.dev/173)
$nodes = $this->rootFolder->getUserFolder($currentUserId)->getById($sourceFile->getId());
$sourceFile = array_pop($nodes);
if (!$sourceFile) {
throw new NotFoundException("Version file not accessible by current user");
}
}

return ($sourceFile->getPermissions() & $permissions) === $permissions;
}
}

0 comments on commit b6ef916

Please sign in to comment.