|
27 | 27 | namespace OCA\Files_Versions\Versions; |
28 | 28 |
|
29 | 29 | use OC\Files\View; |
| 30 | +use OCA\DAV\Connector\Sabre\Exception\Forbidden; |
30 | 31 | use OCA\Files_Sharing\SharedStorage; |
31 | 32 | use OCA\Files_Versions\Storage; |
32 | 33 | use OCP\Files\File; |
|
37 | 38 | use OCP\Files\Storage\IStorage; |
38 | 39 | use OCP\IUser; |
39 | 40 | use OCP\IUserManager; |
| 41 | +use OCP\IUserSession; |
40 | 42 |
|
41 | 43 | class LegacyVersionsBackend implements IVersionBackend { |
42 | 44 | /** @var IRootFolder */ |
43 | 45 | private $rootFolder; |
44 | 46 | /** @var IUserManager */ |
45 | 47 | private $userManager; |
| 48 | + /** @var IUserSession */ |
| 49 | + private $userSession; |
46 | 50 |
|
47 | | - public function __construct(IRootFolder $rootFolder, IUserManager $userManager) { |
| 51 | + public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IUserSession $userSession) { |
48 | 52 | $this->rootFolder = $rootFolder; |
49 | 53 | $this->userManager = $userManager; |
| 54 | + $this->userSession = $userSession; |
50 | 55 | } |
51 | 56 |
|
52 | 57 | public function useBackendForStorage(IStorage $storage): bool { |
@@ -96,6 +101,10 @@ public function createVersion(IUser $user, FileInfo $file) { |
96 | 101 | } |
97 | 102 |
|
98 | 103 | public function rollback(IVersion $version) { |
| 104 | + if (!$this->currentUserHasPermissions($version, \OCP\Constants::PERMISSION_UPDATE)) { |
| 105 | + throw new Forbidden('You cannot restore this version because you do not have update permissions on the source file.'); |
| 106 | + } |
| 107 | + |
99 | 108 | return Storage::rollback($version->getVersionPath(), $version->getRevisionId(), $version->getUser()); |
100 | 109 | } |
101 | 110 |
|
@@ -125,4 +134,23 @@ public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): Fi |
125 | 134 | $file = $versionFolder->get($userFolder->getRelativePath($sourceFile->getPath()) . '.v' . $revision); |
126 | 135 | return $file; |
127 | 136 | } |
| 137 | + |
| 138 | + private function currentUserHasPermissions(IVersion $version, int $permissions): bool { |
| 139 | + $sourceFile = $version->getSourceFile(); |
| 140 | + $currentUserId = $this->userSession->getUser()->getUID(); |
| 141 | + |
| 142 | + if ($currentUserId === null) { |
| 143 | + throw new NotFoundException("No user logged in"); |
| 144 | + } |
| 145 | + |
| 146 | + if ($sourceFile->getOwner()->getUID() !== $currentUserId) { |
| 147 | + $nodes = $this->rootFolder->getUserFolder($currentUserId)->getById($sourceFile->getId()); |
| 148 | + $sourceFile = array_pop($nodes); |
| 149 | + if (!$sourceFile) { |
| 150 | + throw new NotFoundException("Version file not accessible by current user"); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return ($sourceFile->getPermissions() & $permissions) === $permissions; |
| 155 | + } |
128 | 156 | } |
0 commit comments