Passed
Push — master ( f87717...b73ef3 )
by Roeland
08:54
created

AppConfigController::verifyConfigKey()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 6
nc 4
nop 3
dl 0
loc 11
rs 8.4444
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
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
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Provisioning_API\Controller;
26
27
28
use OCP\AppFramework\Http;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\OCSController;
31
use OCP\IAppConfig;
32
use OCP\IConfig;
33
use OCP\IRequest;
34
35
class AppConfigController extends OCSController {
36
37
	/** @var IConfig */
38
	protected $config;
39
40
	/** @var IAppConfig */
41
	protected $appConfig;
42
43
	/**
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param IConfig $config
47
	 * @param IAppConfig $appConfig
48
	 */
49
	public function __construct(string $appName,
50
								IRequest $request,
51
								IConfig $config,
52
								IAppConfig $appConfig) {
53
		parent::__construct($appName, $request);
54
		$this->config = $config;
55
		$this->appConfig = $appConfig;
56
	}
57
58
	/**
59
	 * @return DataResponse
60
	 */
61
	public function getApps(): DataResponse {
62
		return new DataResponse([
63
			'data' => $this->appConfig->getApps(),
64
		]);
65
	}
66
67
	/**
68
	 * @param string $app
69
	 * @return DataResponse
70
	 */
71
	public function getKeys(string $app): DataResponse {
72
		try {
73
			$this->verifyAppId($app);
74
		} catch (\InvalidArgumentException $e) {
75
			return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
76
		}
77
		return new DataResponse([
78
			'data' => $this->config->getAppKeys($app),
79
		]);
80
	}
81
82
	/**
83
	 * @param string $app
84
	 * @param string $key
85
	 * @param string $defaultValue
86
	 * @return DataResponse
87
	 */
88
	public function getValue(string $app, string $key, string $defaultValue = ''): DataResponse {
89
		try {
90
			$this->verifyAppId($app);
91
		} catch (\InvalidArgumentException $e) {
92
			return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
93
		}
94
		return new DataResponse([
95
			'data' => $this->config->getAppValue($app, $key, $defaultValue),
96
		]);
97
	}
98
99
	/**
100
	 * @PasswordConfirmationRequired
101
	 * @param string $app
102
	 * @param string $key
103
	 * @param string $value
104
	 * @return DataResponse
105
	 */
106
	public function setValue(string $app, string $key, string $value): DataResponse {
107
		try {
108
			$this->verifyAppId($app);
109
			$this->verifyConfigKey($app, $key, $value);
110
		} catch (\InvalidArgumentException $e) {
111
			return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
112
		}
113
114
		$this->config->setAppValue($app, $key, $value);
115
		return new DataResponse();
116
	}
117
118
	/**
119
	 * @PasswordConfirmationRequired
120
	 * @param string $app
121
	 * @param string $key
122
	 * @return DataResponse
123
	 */
124
	public function deleteKey(string $app, string $key): DataResponse {
125
		try {
126
			$this->verifyAppId($app);
127
			$this->verifyConfigKey($app, $key, '');
128
		} catch (\InvalidArgumentException $e) {
129
			return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_FORBIDDEN);
130
		}
131
132
		$this->config->deleteAppValue($app, $key);
133
		return new DataResponse();
134
	}
135
136
	/**
137
	 * @param string $app
138
	 * @throws \InvalidArgumentException
139
	 */
140
	protected function verifyAppId(string $app) {
141
		if (\OC_App::cleanAppId($app) !== $app) {
142
			throw new \InvalidArgumentException('Invalid app id given');
143
		}
144
	}
145
146
	/**
147
	 * @param string $app
148
	 * @param string $key
149
	 * @param string $value
150
	 * @throws \InvalidArgumentException
151
	 */
152
	protected function verifyConfigKey(string $app, string $key, string $value) {
153
		if (in_array($key, ['installed_version', 'enabled', 'types'])) {
154
			throw new \InvalidArgumentException('The given key can not be set');
155
		}
156
157
		if ($app === 'core' && $key === 'encryption_enabled' && $value !== 'yes') {
158
			throw new \InvalidArgumentException('The given key can not be set');
159
		}
160
161
		if ($app === 'core' && (strpos($key, 'public_') === 0 || strpos($key, 'remote_') === 0)) {
162
			throw new \InvalidArgumentException('The given key can not be set');
163
		}
164
	}
165
}
166