Passed
Push — master ( a884f3...785682 )
by
unknown
14:57 queued 14s
created

UserThemeController::deleteBackground()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <[email protected]>
7
 * @copyright Copyright (c) 2019 Janis Köhr <[email protected]>
8
 *
9
 * @author Christoph Wurst <[email protected]>
10
 * @author Daniel Kesselberg <[email protected]>
11
 * @author Janis Köhr <[email protected]>
12
 * @author John Molakvoæ <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 *
15
 * @license GNU AGPL version 3 or any later version
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 *
30
 */
31
namespace OCA\Theming\Controller;
32
33
use OCA\Theming\AppInfo\Application;
34
use OCA\Theming\ITheme;
35
use OCA\Theming\Service\BackgroundService;
36
use OCA\Theming\Service\ThemesService;
37
use OCA\Theming\ThemingDefaults;
38
use OCP\AppFramework\Http;
39
use OCP\AppFramework\Http\DataResponse;
40
use OCP\AppFramework\Http\FileDisplayResponse;
41
use OCP\AppFramework\Http\JSONResponse;
42
use OCP\AppFramework\Http\NotFoundResponse;
43
use OCP\AppFramework\OCS\OCSBadRequestException;
44
use OCP\AppFramework\OCS\OCSForbiddenException;
45
use OCP\AppFramework\OCSController;
46
use OCP\IConfig;
47
use OCP\IRequest;
48
use OCP\IUserSession;
49
use OCP\PreConditionNotMetException;
50
51
class UserThemeController extends OCSController {
52
53
	protected string $userId;
54
	private IConfig $config;
55
	private IUserSession $userSession;
56
	private ThemesService $themesService;
57
	private ThemingDefaults $themingDefaults;
58
	private BackgroundService $backgroundService;
59
60
	/**
61
	 * Config constructor.
62
	 */
63
	public function __construct(string $appName,
64
								IRequest $request,
65
								IConfig $config,
66
								IUserSession $userSession,
67
								ThemesService $themesService,
68
								ThemingDefaults $themingDefaults,
69
								BackgroundService $backgroundService) {
70
		parent::__construct($appName, $request);
71
		$this->config = $config;
72
		$this->userSession = $userSession;
73
		$this->themesService = $themesService;
74
		$this->themingDefaults = $themingDefaults;
75
		$this->backgroundService = $backgroundService;
76
		$this->userId = $userSession->getUser()->getUID();
77
	}
78
79
	/**
80
	 * @NoAdminRequired
81
	 *
82
	 * Enable theme
83
	 *
84
	 * @param string $themeId the theme ID
85
	 * @return DataResponse
86
	 * @throws OCSBadRequestException|PreConditionNotMetException
87
	 */
88
	public function enableTheme(string $themeId): DataResponse {
89
		$theme = $this->validateTheme($themeId);
90
91
		// Enable selected theme
92
		$this->themesService->enableTheme($theme);
93
		return new DataResponse();
94
	}
95
96
	/**
97
	 * @NoAdminRequired
98
	 *
99
	 * Disable theme
100
	 *
101
	 * @param string $themeId the theme ID
102
	 * @return DataResponse
103
	 * @throws OCSBadRequestException|PreConditionNotMetException
104
	 */
105
	public function disableTheme(string $themeId): DataResponse {
106
		$theme = $this->validateTheme($themeId);
107
108
		// Enable selected theme
109
		$this->themesService->disableTheme($theme);
110
		return new DataResponse();
111
	}
112
113
	/**
114
	 * Validate and return the matching ITheme
115
	 *
116
	 * Disable theme
117
	 *
118
	 * @param string $themeId the theme ID
119
	 * @return ITheme
120
	 * @throws OCSBadRequestException|PreConditionNotMetException
121
	 */
122
	private function validateTheme(string $themeId): ITheme {
123
		if ($themeId === '' || !$themeId) {
124
			throw new OCSBadRequestException('Invalid theme id: ' . $themeId);
125
		}
126
127
		$themes = $this->themesService->getThemes();
128
		if (!isset($themes[$themeId])) {
129
			throw new OCSBadRequestException('Invalid theme id: ' . $themeId);
130
		}
131
132
		// If trying to toggle another theme but this is enforced
133
		if ($this->config->getSystemValueString('enforce_theme', '') !== ''
134
			&& $themes[$themeId]->getType() === ITheme::TYPE_THEME) {
135
			throw new OCSForbiddenException('Theme switching is disabled');
136
		}
137
138
		return $themes[$themeId];
139
	}
140
141
	/**
142
	 * @NoAdminRequired
143
	 * @NoCSRFRequired
144
	 */
145
	public function getBackground(): Http\Response {
146
		$file = $this->backgroundService->getBackground();
147
		if ($file !== null) {
148
			$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]);
149
			$response->cacheFor(24 * 60 * 60, false, true);
150
			return $response;
151
		}
152
		return new NotFoundResponse();
153
	}
154
155
	/**
156
	 * @NoAdminRequired
157
	 */
158
	public function deleteBackground(): JSONResponse {
159
		$currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');
160
		$this->backgroundService->deleteBackgroundImage();
161
		return new JSONResponse([
162
			'backgroundImage' => null,
163
			'backgroundColor' => $this->themingDefaults->getColorPrimary(),
164
			'version' => $currentVersion,
165
		]);
166
	}
167
168
	/**
169
	 * @NoAdminRequired
170
	 */
171
	public function setBackground(string $type = BackgroundService::BACKGROUND_DEFAULT, string $value = '', string $color = null): JSONResponse {
172
		$currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');
173
174
		// Set color if provided
175
		if ($color) {
176
			$this->backgroundService->setColorBackground($color);
177
		}
178
179
		// Set background image if provided
180
		try {
181
			switch ($type) {
182
				case BackgroundService::BACKGROUND_SHIPPED:
183
					$this->backgroundService->setShippedBackground($value);
184
					break;
185
				case BackgroundService::BACKGROUND_CUSTOM:
186
					$this->backgroundService->setFileBackground($value);
187
					break;
188
				case BackgroundService::BACKGROUND_DEFAULT:
189
					$this->backgroundService->setDefaultBackground();
190
					break;
191
				default:
192
					if (!$color) {
193
						return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST);
194
					}
195
			}
196
		} catch (\InvalidArgumentException $e) {
197
			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
198
		} catch (\Throwable $e) {
199
			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
200
		}
201
202
		$currentVersion++;
203
		$this->config->setUserValue($this->userId, Application::APP_ID, 'userCacheBuster', (string)$currentVersion);
204
205
		return new JSONResponse([
206
			'backgroundImage' => $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT),
207
			'backgroundColor' => $this->themingDefaults->getColorPrimary(),
208
			'version' => $currentVersion,
209
		]);
210
	}
211
}
212