1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Robin Appelman <[email protected]> |
4
|
|
|
* @author Roeland Jago Douma <[email protected]> |
5
|
|
|
* @author Stefan Weil <[email protected]> |
6
|
|
|
* @author Thomas Müller <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
9
|
|
|
* @license AGPL-3.0 |
10
|
|
|
* |
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
13
|
|
|
* as published by the Free Software Foundation. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\DAV\Connector\Sabre; |
26
|
|
|
|
27
|
|
|
use OC\Lock\Persistent\LockMapper; |
28
|
|
|
use OCA\DAV\Connector\Sabre\Exception\FileLocked; |
29
|
|
|
use OCP\Lock\ILockingProvider; |
30
|
|
|
use OCP\Lock\LockedException; |
31
|
|
|
use Sabre\DAV\Exception\Forbidden; |
32
|
|
|
use Sabre\DAV\Exception\NotFound; |
33
|
|
|
use Sabre\DAV\Exception\NotImplemented; |
34
|
|
|
use Sabre\DAV\Locks\LockInfo; |
35
|
|
|
use Sabre\DAV\ServerPlugin; |
36
|
|
|
use Sabre\HTTP\RequestInterface; |
37
|
|
|
|
38
|
|
|
class LockPlugin extends ServerPlugin { |
39
|
|
|
/** |
40
|
|
|
* Reference to main server object |
41
|
|
|
* |
42
|
|
|
* @var \Sabre\DAV\Server |
43
|
|
|
*/ |
44
|
|
|
private $server; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function initialize(\Sabre\DAV\Server $server) { |
50
|
|
|
$this->server = $server; |
51
|
|
|
$this->server->on('beforeMethod', [$this, 'getLock'], 50); |
52
|
|
|
$this->server->on('afterMethod', [$this, 'releaseLock'], 50); |
53
|
|
|
$this->server->on('beforeUnlock', [$this, 'beforeUnlock'], 20); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getLock(RequestInterface $request) { |
57
|
|
|
// we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree |
58
|
|
|
// so instead we limit ourselves to the PUT method manually |
59
|
|
|
if ($request->getMethod() !== 'PUT' || \OC_FileChunking::isWebdavChunk()) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
try { |
63
|
|
|
$node = $this->server->tree->getNodeForPath($request->getPath()); |
64
|
|
|
} catch (NotFound $e) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
if ($node instanceof Node) { |
68
|
|
|
try { |
69
|
|
|
$node->acquireLock(ILockingProvider::LOCK_SHARED); |
70
|
|
|
} catch (LockedException $e) { |
71
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function releaseLock(RequestInterface $request) { |
77
|
|
|
if ($request->getMethod() !== 'PUT' || \OC_FileChunking::isWebdavChunk()) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
try { |
81
|
|
|
$node = $this->server->tree->getNodeForPath($request->getPath()); |
82
|
|
|
} catch (NotFound $e) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
if ($node instanceof Node) { |
86
|
|
|
$node->releaseLock(ILockingProvider::LOCK_SHARED); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $uri |
92
|
|
|
* @param LockInfo $lock |
93
|
|
|
* @throws Forbidden |
94
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException |
95
|
|
|
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
96
|
|
|
* @throws \OCP\AppFramework\QueryException |
97
|
|
|
*/ |
98
|
|
|
public function beforeUnlock($uri, LockInfo $lock) { |
99
|
|
|
// no injection of LockMapper and UserSession below because this method |
100
|
|
|
// is only triggered on UNLOCK which is a no that common operation. |
101
|
|
|
/** @var LockMapper $mapper */ |
102
|
|
|
$mapper = \OC::$server->query(LockMapper::class); |
103
|
|
|
|
104
|
|
|
$lock = $mapper->getLockByToken($lock->token); |
105
|
|
|
if ($lock === null || $lock->getOwnerAccountId() === null) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
$currentUser = \OC::$server->getUserSession()->getUser(); |
109
|
|
|
if ($currentUser === null || $lock->getOwnerAccountId() !== $currentUser->getAccountId()) { |
|
|
|
|
110
|
|
|
throw new Forbidden(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|