Completed
Push — master ( be3f29...e6d352 )
by Julius
03:56
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 \OCP\AppFramework\Controller;
17
use OCP\AppFramework\Http\JSONResponse;
18
use \OCP\IRequest;
19
use \OCP\IL10N;
20
use OCA\Richdocuments\AppConfig;
21
use OCP\IConfig;
22
use OCP\PreConditionNotMetException;
23
24
class SettingsController extends Controller{
25
	/** @var IL10N */
26
	private $l10n;
27
	/** @var AppConfig */
28
	private $appConfig;
29
	/** @var IConfig */
30
	private $config;
31
	/** @var DiscoveryManager  */
32
	private $discoveryManager;
33
	/** @var string */
34
	private $userId;
35
	/** @var CapabilitiesService */
36
	private $capabilitiesService;
37
38
	/**
39
	 * @param string $appName
40
	 * @param IRequest $request
41
	 * @param IL10N $l10n
42
	 * @param AppConfig $appConfig
43
	 * @param IConfig $config
44
	 * @param DiscoveryManager $discoveryManager
45
	 * @param string $userId
46
	 */
47
	public function __construct($appName,
48
								IRequest $request,
49
								IL10N $l10n,
50
								AppConfig $appConfig,
51
								IConfig $config,
52
								DiscoveryManager $discoveryManager,
53
								$userId,
54
								CapabilitiesService $capabilitiesService) {
55
		parent::__construct($appName, $request);
56
		$this->l10n = $l10n;
57
		$this->appConfig = $appConfig;
58
		$this->config = $config;
59
		$this->discoveryManager = $discoveryManager;
60
		$this->userId = $userId;
61
		$this->capabilitiesService = $capabilitiesService;
62
	}
63
64
	/**
65
	 * @NoAdminRequired
66
	 *
67
	 * @return JSONResponse
68
	 */
69
	public function getSettings() {
70
		return new JSONResponse([
71
			'wopi_url' => $this->appConfig->getAppValue('wopi_url'),
72
			'disable_certificate_verification' => $this->appConfig->getAppValue('disable_certificate_verification'),
73
			'edit_groups' => $this->appConfig->getAppValue('edit_groups'),
74
			'use_groups' => $this->appConfig->getAppValue('use_groups'),
75
			'doc_format' => $this->appConfig->getAppValue('doc_format'),
76
		]);
77
	}
78
79
	/**
80
	 * @param string $wopi_url
81
	 * @param string $disable_certificate_verification
82
	 * @param string $edit_groups
83
	 * @param string $use_groups
84
	 * @param string $doc_format
85
	 * @param string $external_apps
86
	 * @param string $canonical_webroot
87
	 * @return JSONResponse
88
	 */
89
	public function setSettings($wopi_url,
90
	                            $disable_certificate_verification,
91
	                            $edit_groups,
92
	                            $use_groups,
93
	                            $doc_format,
94
	                            $external_apps,
95
	                            $canonical_webroot) {
96
		$message = $this->l10n->t('Saved');
97
98
		if ($wopi_url !== null){
99
			$this->appConfig->setAppValue('wopi_url', $wopi_url);
100
101
			$colon = strpos($wopi_url, ':', 0);
102
			if ($this->request->getServerProtocol() !== substr($wopi_url, 0, $colon)){
103
				$message = $this->l10n->t('Saved with error: Collabora Online should use the same protocol as the server installation.');
104
			}
105
		}
106
107
		if ($disable_certificate_verification !== null) {
108
			$this->appConfig->setAppValue(
109
				'disable_certificate_verification',
110
				$disable_certificate_verification === 'true' ? 'yes' : ''
111
			);
112
		}
113
114
		if ($edit_groups !== null){
115
			$this->appConfig->setAppValue('edit_groups', $edit_groups);
116
		}
117
118
		if ($use_groups !== null){
119
			$this->appConfig->setAppValue('use_groups', $use_groups);
120
		}
121
122
		if ($doc_format !== null) {
123
			$this->appConfig->setAppValue('doc_format', $doc_format);
124
		}
125
126
		if ($external_apps !== null) {
127
			$this->appConfig->setAppValue('external_apps', $external_apps);
128
		}
129
130
		if ($canonical_webroot !== null) {
131
			$this->appConfig->setAppValue('canonical_webroot', $canonical_webroot);
132
		}
133
134
		$this->discoveryManager->refretch();
135
		$this->capabilitiesService->refretch();
136
137
		$response = [
138
			'status' => 'success',
139
			'data' => ['message' => $message]
140
		];
141
142
		return new JSONResponse($response);
143
	}
144
145
	/**
146
	 * @NoAdminRequired
147
	 *
148
	 * @param $key
149
	 * @param $value
150
	 * @return JSONResponse
151
	 */
152
	public function setPersonalSettings($templateFolder) {
153
		$message = $this->l10n->t('Saved');
154
		$status = 'success';
155
156
		if ($templateFolder !== null){
157
			try {
158
				$this->config->setUserValue($this->userId, 'richdocuments', 'templateFolder', $templateFolder);
159
			} catch (PreConditionNotMetException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\PreConditionNotMetException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
160
				$message = $this->l10n->t('Error when saving');
161
				$status = 'error';
162
			}
163
		}
164
165
		$response = [
166
			'status' => $status,
167
			'data' => ['message' => $message]
168
		];
169
170
		return new JSONResponse($response);
171
172
	}
173
}
174