|
6 | 6 | use OCA\DAV\Connector\Sabre\Node as SabreNode; |
7 | 7 | use OCA\DAV\Connector\Sabre\ObjectTree; |
8 | 8 | use OCA\FilesLock\AppInfo\Application; |
| 9 | +use OCA\FilesLock\Exceptions\LockNotFoundException; |
| 10 | +use OCA\FilesLock\Exceptions\NotFileException; |
| 11 | +use OCA\FilesLock\Exceptions\UnauthorizedUnlockException; |
9 | 12 | use OCA\FilesLock\Model\FileLock; |
10 | 13 | use OCA\FilesLock\Service\AppLockService; |
11 | 14 | use OCA\FilesLock\Service\FileService; |
12 | 15 | use OCA\FilesLock\Service\LockService; |
13 | 16 | use OCP\DirectEditing\IManager; |
14 | 17 | use OCP\DirectEditing\RegisterDirectEditorEvent; |
15 | 18 | use OCP\EventDispatcher\IEventDispatcher; |
| 19 | +use OCP\Files\InvalidPathException; |
16 | 20 | use OCP\Files\Lock\ILock; |
| 21 | +use OCP\Files\Lock\OwnerLockedException; |
| 22 | +use OCP\Files\NotFoundException; |
17 | 23 | use OCP\IUserManager; |
18 | 24 | use OCP\IUserSession; |
19 | 25 | use Sabre\DAV\INode; |
@@ -130,29 +136,72 @@ public function customProperties(PropFind $propFind, INode $node) { |
130 | 136 | }); |
131 | 137 | } |
132 | 138 |
|
| 139 | + private function getLockDisplayName(ILock $lock): ?string { |
| 140 | + $user = $this->userManager->get($lock->getOwner()); |
| 141 | + if ($user !== null) { |
| 142 | + return $user->getDisplayName(); |
| 143 | + } |
| 144 | + |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
133 | 148 | public function httpLock(RequestInterface $request, ResponseInterface $response) { |
134 | 149 | if ($request->getHeader('X-User-Lock')) { |
135 | | - $file = $this->fileService->getFileFromAbsoluteUri($this->server->getRequestUri()); |
136 | | - $this->lockService->lockFileAsUser($file, $this->userSession->getUser()); |
137 | 150 | $response->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
138 | | - //$response->setHeader('Lock-Token', '<opaquelocktoken:'.$lockInfo->token.'>'); |
139 | | - //$response->setStatus($newFile ? 201 : 200); |
140 | | - //$response->setBody($this->generateLockResponse($lockInfo)); |
141 | | - $response->setStatus(200); |
| 151 | + |
| 152 | + try { |
| 153 | + $file = $this->fileService->getFileFromAbsoluteUri($this->server->getRequestUri()); |
| 154 | + $lockInfo = $this->lockService->lockFileAsUser($file, $this->userSession->getUser()); |
| 155 | + $response->setStatus(200); |
| 156 | + $response->setBody($this->server->xml->write('{DAV:}prop', [ |
| 157 | + Application::DAV_PROPERTY_LOCK => true, |
| 158 | + Application::DAV_PROPERTY_LOCK_OWNER_TYPE => $lockInfo->getLockType(), |
| 159 | + Application::DAV_PROPERTY_LOCK_OWNER => $lockInfo->getOwner(), |
| 160 | + Application::DAV_PROPERTY_LOCK_OWNER_DISPLAYNAME => $this->getLockDisplayName($lockInfo), |
| 161 | + Application::DAV_PROPERTY_LOCK_EDITOR => $lockInfo->getOwner(), |
| 162 | + Application::DAV_PROPERTY_LOCK_TIME => $lockInfo->getCreatedAt(), |
| 163 | + ])); |
| 164 | + } catch (OwnerLockedException $e) { |
| 165 | + $lockInfo = $e->getLock(); |
| 166 | + $response->setStatus(423); |
| 167 | + $response->setBody($this->server->xml->write('{DAV:}prop', [ |
| 168 | + Application::DAV_PROPERTY_LOCK => true, |
| 169 | + Application::DAV_PROPERTY_LOCK_OWNER_TYPE => $lockInfo->getLockType(), |
| 170 | + Application::DAV_PROPERTY_LOCK_OWNER => $lockInfo->getOwner(), |
| 171 | + Application::DAV_PROPERTY_LOCK_OWNER_DISPLAYNAME => $this->getLockDisplayName($lockInfo), |
| 172 | + Application::DAV_PROPERTY_LOCK_EDITOR => $lockInfo->getOwner(), |
| 173 | + Application::DAV_PROPERTY_LOCK_TIME => $lockInfo->getCreatedAt(), |
| 174 | + ])); |
| 175 | + } |
| 176 | + |
142 | 177 | return false; |
143 | 178 | } |
144 | 179 | return parent::httpLock($request, $response); |
145 | 180 | } |
146 | 181 |
|
147 | 182 | public function httpUnlock(RequestInterface $request, ResponseInterface $response) { |
148 | 183 | if ($request->getHeader('X-User-Lock')) { |
149 | | - $file = $this->fileService->getFileFromAbsoluteUri($this->server->getRequestUri()); |
150 | | - $this->lockService->unlockFile($file->getId(), $this->userSession->getUser()->getUID()); |
151 | 184 | $response->setHeader('Content-Type', 'application/xml; charset=utf-8'); |
152 | | - //$response->setHeader('Lock-Token', '<opaquelocktoken:'.$lockInfo->token.'>'); |
153 | | - //$response->setStatus($newFile ? 201 : 200); |
154 | | - //$response->setBody($this->generateLockResponse($lockInfo)); |
155 | | - $response->setStatus(200); |
| 185 | + |
| 186 | + try { |
| 187 | + $file = $this->fileService->getFileFromAbsoluteUri($this->server->getRequestUri()); |
| 188 | + $lockInfo = $this->lockService->unlockFile($file->getId(), $this->userSession->getUser()->getUID()); |
| 189 | + $response->setStatus(200); |
| 190 | + $response->setBody($this->server->xml->write('{DAV:}prop', [ |
| 191 | + Application::DAV_PROPERTY_LOCK => false, |
| 192 | + Application::DAV_PROPERTY_LOCK_OWNER_TYPE => null, |
| 193 | + Application::DAV_PROPERTY_LOCK_OWNER => null, |
| 194 | + Application::DAV_PROPERTY_LOCK_OWNER_DISPLAYNAME => null, |
| 195 | + Application::DAV_PROPERTY_LOCK_EDITOR => null, |
| 196 | + Application::DAV_PROPERTY_LOCK_TIME => null, |
| 197 | + ])); |
| 198 | + } catch (LockNotFoundException $e) { |
| 199 | + $response->setStatus(403); |
| 200 | + } catch (UnauthorizedUnlockException $e) { |
| 201 | + $response->setStatus(403); |
| 202 | + } catch (InvalidPathException $e) { |
| 203 | + } catch (NotFoundException $e) { |
| 204 | + } |
156 | 205 | return false; |
157 | 206 | } |
158 | 207 | return parent::httpLock($request, $response); |
|
0 commit comments