Completed
Push — master ( 63676d...3faef6 )
by Lukas
28:10 queued 12:28
created

LockingController::releaseAll()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 12
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 19
rs 8.2222
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Testing\Controller;
24
25
use OC\Lock\DBLockingProvider;
26
use OC\User\NoUserException;
27
use OCA\Testing\Locking\FakeDBLockingProvider;
28
use OCP\AppFramework\Http;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\OCS\OCSException;
31
use OCP\AppFramework\OCSController;
32
use OCP\Files\IRootFolder;
33
use OCP\Files\NotFoundException;
34
use OCP\IConfig;
35
use OCP\IDBConnection;
36
use OCP\IRequest;
37
use OCP\Lock\ILockingProvider;
38
use OCP\Lock\LockedException;
39
40
class LockingController extends OCSController {
41
42
	/** @var ILockingProvider */
43
	protected $lockingProvider;
44
45
	/** @var FakeDBLockingProvider */
46
	protected $fakeDBLockingProvider;
47
48
	/** @var IDBConnection */
49
	protected $connection;
50
51
	/** @var IConfig */
52
	protected $config;
53
54
	/** @var IRootFolder */
55
	protected $rootFolder;
56
57
	/**
58
	 * @param string $appName
59
	 * @param IRequest $request
60
	 * @param ILockingProvider $lockingProvider
61
	 * @param FakeDBLockingProvider $fakeDBLockingProvider
62
	 * @param IDBConnection $connection
63
	 * @param IConfig $config
64
	 * @param IRootFolder $rootFolder
65
	 */
66
	public function __construct($appName,
67
								IRequest $request,
68
								ILockingProvider $lockingProvider,
69
								FakeDBLockingProvider $fakeDBLockingProvider,
70
								IDBConnection $connection,
71
								IConfig $config,
72
								IRootFolder $rootFolder) {
73
		parent::__construct($appName, $request);
74
75
		$this->lockingProvider = $lockingProvider;
76
		$this->fakeDBLockingProvider = $fakeDBLockingProvider;
77
		$this->connection = $connection;
78
		$this->config = $config;
79
		$this->rootFolder = $rootFolder;
80
	}
81
82
	/**
83
	 * @return ILockingProvider
84
	 * @throws \RuntimeException
85
	 */
86
	protected function getLockingProvider() {
87
		if ($this->lockingProvider instanceof DBLockingProvider) {
88
			return $this->fakeDBLockingProvider;
89
		}
90
		throw new \RuntimeException('Lock provisioning is only possible using the DBLockingProvider');
91
	}
92
93
	/**
94
	 * @param string $user
95
	 * @param string $path
96
	 * @return string
97
	 * @throws NotFoundException
98
	 */
99
	protected function getPath($user, $path) {
100
		$node = $this->rootFolder->getUserFolder($user)->get($path);
101
		return 'files/' . md5($node->getStorage()->getId() . '::' . trim($node->getInternalPath(), '/'));
102
	}
103
104
	/**
105
	 * @return DataResponse
106
	 * @throws OCSException
107
	 */
108
	public function isLockingEnabled() {
109
		try {
110
			$this->getLockingProvider();
111
			return new DataResponse();
112
		} catch (\RuntimeException $e) {
113
			throw new OCSException($e->getMessage(), Http::STATUS_NOT_IMPLEMENTED, $e);
114
		}
115
	}
116
117
	/**
118
	 * @param int $type
119
	 * @param string $user
120
	 * @param string $path
121
	 * @return DataResponse
122
	 * @throws OCSException
123
	 */
124 View Code Duplication
	public function acquireLock($type, $user, $path) {
125
		try {
126
			$path = $this->getPath($user, $path);
127
		} catch (NoUserException $e) {
128
			throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
129
		} catch (NotFoundException $e) {
130
			throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
131
		}
132
133
		$lockingProvider = $this->getLockingProvider();
134
135
		try {
136
			$lockingProvider->acquireLock($path, $type);
137
			$this->config->setAppValue('testing', 'locking_' . $path, $type);
138
			return new DataResponse();
139
		} catch (LockedException $e) {
140
			throw new OCSException('', Http::STATUS_LOCKED, $e);
141
		}
142
	}
143
144
	/**
145
	 * @param int $type
146
	 * @param string $user
147
	 * @param string $path
148
	 * @return DataResponse
149
	 * @throws OCSException
150
	 */
151 View Code Duplication
	public function changeLock($type, $user, $path) {
152
		try {
153
			$path = $this->getPath($user, $path);
154
		} catch (NoUserException $e) {
155
			throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
156
		} catch (NotFoundException $e) {
157
			throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
158
		}
159
160
		$lockingProvider = $this->getLockingProvider();
161
162
		try {
163
			$lockingProvider->changeLock($path, $type);
164
			$this->config->setAppValue('testing', 'locking_' . $path, $type);
165
			return new DataResponse();
166
		} catch (LockedException $e) {
167
			throw new OCSException('', Http::STATUS_LOCKED, $e);
168
		}
169
	}
170
171
	/**
172
	 * @param int $type
173
	 * @param string $user
174
	 * @param string $path
175
	 * @return DataResponse
176
	 * @throws OCSException
177
	 */
178 View Code Duplication
	public function releaseLock($type, $user, $path) {
179
		try {
180
			$path = $this->getPath($user, $path);
181
		} catch (NoUserException $e) {
182
			throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
183
		} catch (NotFoundException $e) {
184
			throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
185
		}
186
187
		$lockingProvider = $this->getLockingProvider();
188
189
		try {
190
			$lockingProvider->releaseLock($path, $type);
191
			$this->config->deleteAppValue('testing', 'locking_' . $path);
192
			return new DataResponse();
193
		} catch (LockedException $e) {
194
			throw new OCSException('', Http::STATUS_LOCKED, $e);
195
		}
196
	}
197
198
	/**
199
	 * @param int $type
200
	 * @param string $user
201
	 * @param string $path
202
	 * @return DataResponse
203
	 * @throws OCSException
204
	 */
205
	public function isLocked($type, $user, $path) {
206
		try {
207
			$path = $this->getPath($user, $path);
208
		} catch (NoUserException $e) {
209
			throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e);
210
		} catch (NotFoundException $e) {
211
			throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e);
212
		}
213
214
		$lockingProvider = $this->getLockingProvider();
215
216
		if ($lockingProvider->isLocked($path, $type)) {
217
			return new DataResponse();
218
		}
219
220
		throw new OCSException('', Http::STATUS_LOCKED);
221
	}
222
223
	/**
224
	 * @param int $type
225
	 * @return DataResponse
226
	 */
227
	public function releaseAll($type = null) {
228
		$lockingProvider = $this->getLockingProvider();
229
230
		foreach ($this->config->getAppKeys('testing') as $lock) {
231
			if (strpos($lock, 'locking_') === 0) {
232
				$path = substr($lock, strlen('locking_'));
233
234
				if ($type === ILockingProvider::LOCK_EXCLUSIVE && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_EXCLUSIVE) {
235
					$lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock));
236
				} else if ($type === ILockingProvider::LOCK_SHARED && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_SHARED) {
237
					$lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock));
238
				} else {
239
					$lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock));
240
				}
241
			}
242
		}
243
244
		return new DataResponse();
245
	}
246
}
247