Completed
Push — master ( f4635c...83e167 )
by Julius
08:34 queued 07:15
created

SettingsController::setPersonalSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 17
cp 0
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * ownCloud - Richdocuments App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2014 Victor Dubiniuk [email protected]
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later.
10
 */
11
12
namespace OCA\Richdocuments\Controller;
13
14
use OCA\Richdocuments\Service\CapabilitiesService;
15
use OCA\Richdocuments\WOPI\DiscoveryManager;
16
use OCA\Richdocuments\WOPI\Parser;
17
use \OCP\AppFramework\Controller;
18
use OCP\AppFramework\Http;
19
use OCP\AppFramework\Http\DataResponse;
20
use OCP\AppFramework\Http\JSONResponse;
21
use \OCP\IRequest;
22
use \OCP\IL10N;
23
use OCA\Richdocuments\AppConfig;
24
use OCP\IConfig;
25
use OCP\PreConditionNotMetException;
26
27
class SettingsController extends Controller{
28
	/** @var IL10N */
29
	private $l10n;
30
	/** @var AppConfig */
31
	private $appConfig;
32
	/** @var IConfig */
33
	private $config;
34
	/** @var DiscoveryManager  */
35
	private $discoveryManager;
36
	/** @var Parser */
37
	private $wopiParser;
38
	/** @var string */
39
	private $userId;
40
	/** @var CapabilitiesService */
41
	private $capabilitiesService;
42
43
	/**
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param IL10N $l10n
47
	 * @param AppConfig $appConfig
48
	 * @param IConfig $config
49
	 * @param DiscoveryManager $discoveryManager
50
	 * @param Parser $wopiParser
51
	 * @param string $userId
52
	 * @param CapabilitiesService $capabilitiesService
53
	 */
54 View Code Duplication
	public function __construct($appName,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
								IRequest $request,
56
								IL10N $l10n,
57
								AppConfig $appConfig,
58
								IConfig $config,
59
								DiscoveryManager $discoveryManager,
60
								Parser $wopiParser,
61
								$userId,
62
								CapabilitiesService $capabilitiesService) {
63
		parent::__construct($appName, $request);
64
		$this->l10n = $l10n;
65
		$this->appConfig = $appConfig;
66
		$this->config = $config;
67
		$this->discoveryManager = $discoveryManager;
68
		$this->wopiParser = $wopiParser;
69
		$this->userId = $userId;
70
		$this->capabilitiesService = $capabilitiesService;
71
	}
72
73
	/**
74
	 * @PublicPage
75
	 * @NoCSRFRequired
76
	 * @throws \Exception
77
	 */
78
	public function checkSettings() {
79
		try {
80
			$response = $this->discoveryManager->fetchFromRemote();
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
		} catch (\Exception $e) {
82
			return new DataResponse([
83
				'status' => $e->getCode(),
84
				'message' => $e->getMessage()
85
			], Http::STATUS_INTERNAL_SERVER_ERROR);
86
		}
87
88
		return new DataResponse();
89
	}
90
91
	/**
92
	 * @NoAdminRequired
93
	 *
94
	 * @return JSONResponse
95
	 */
96
	public function getSettings() {
97
		return new JSONResponse([
98
			'wopi_url' => $this->appConfig->getAppValue('wopi_url'),
99
			'public_wopi_url' => $this->appConfig->getAppValue('public_wopi_url'),
100
			'disable_certificate_verification' => $this->appConfig->getAppValue('disable_certificate_verification'),
101
			'edit_groups' => $this->appConfig->getAppValue('edit_groups'),
102
			'use_groups' => $this->appConfig->getAppValue('use_groups'),
103
			'doc_format' => $this->appConfig->getAppValue('doc_format'),
104
		]);
105
	}
106
107
	/**
108
	 * @param string $wopi_url
109
	 * @param string $disable_certificate_verification
110
	 * @param string $edit_groups
111
	 * @param string $use_groups
112
	 * @param string $doc_format
113
	 * @param string $external_apps
114
	 * @param string $canonical_webroot
115
	 * @return JSONResponse
116
	 */
117
	public function setSettings($wopi_url,
118
	                            $disable_certificate_verification,
119
	                            $edit_groups,
120
	                            $use_groups,
121
	                            $doc_format,
122
	                            $external_apps,
123
	                            $canonical_webroot) {
124
		$message = $this->l10n->t('Saved');
125
126
		if ($wopi_url !== null){
127
			$this->appConfig->setAppValue('wopi_url', $wopi_url);
128
		}
129
130
		if ($disable_certificate_verification !== null) {
131
			$this->appConfig->setAppValue(
132
				'disable_certificate_verification',
133
				$disable_certificate_verification === 'true' ? 'yes' : ''
134
			);
135
		}
136
137
		if ($edit_groups !== null){
138
			$this->appConfig->setAppValue('edit_groups', $edit_groups);
139
		}
140
141
		if ($use_groups !== null){
142
			$this->appConfig->setAppValue('use_groups', $use_groups);
143
		}
144
145
		if ($doc_format !== null) {
146
			$this->appConfig->setAppValue('doc_format', $doc_format);
147
		}
148
149
		if ($external_apps !== null) {
150
			$this->appConfig->setAppValue('external_apps', $external_apps);
151
		}
152
153
		if ($canonical_webroot !== null) {
154
			$this->appConfig->setAppValue('canonical_webroot', $canonical_webroot);
155
		}
156
157
		$this->discoveryManager->refretch();
158
		try {
159
			$capaUrlSrc = $this->wopiParser->getUrlSrc('Capabilities');
160
			if (is_array($capaUrlSrc) && $capaUrlSrc['action'] === 'getinfo') {
161
				// XXX Works, but probably better to strip trailing '/hosting/capabilities'
162
				$public_wopi_url = $capaUrlSrc['urlsrc'];
163
				if ($public_wopi_url !== null) {
164
					$this->appConfig->setAppValue('public_wopi_url', $public_wopi_url);
165
					$colon = strpos($public_wopi_url, ':', 0);
166
					if ($this->request->getServerProtocol() !== substr($public_wopi_url, 0, $colon)){
167
						$message = $this->l10n->t('Saved with error: Collabora Online should use the same protocol as the server installation.');
168
					}
169
				}
170
			}
171
		} catch (\Exception $e){
172
			// Ignore
173
		}
174
175
		$this->capabilitiesService->clear();
176
		$this->capabilitiesService->refretch();
177
178
		$response = [
179
			'status' => 'success',
180
			'data' => ['message' => $message]
181
		];
182
183
		return new JSONResponse($response);
184
	}
185
186
	/**
187
	 * @NoAdminRequired
188
	 *
189
	 * @param $key
190
	 * @param $value
191
	 * @return JSONResponse
192
	 */
193
	public function setPersonalSettings($templateFolder) {
194
		$message = $this->l10n->t('Saved');
195
		$status = 'success';
196
197
		if ($templateFolder !== null){
198
			try {
199
				$this->config->setUserValue($this->userId, 'richdocuments', 'templateFolder', $templateFolder);
200
			} catch (PreConditionNotMetException $e) {
201
				$message = $this->l10n->t('Error when saving');
202
				$status = 'error';
203
			}
204
		}
205
206
		$response = [
207
			'status' => $status,
208
			'data' => ['message' => $message]
209
		];
210
211
		return new JSONResponse($response);
212
213
	}
214
}
215