Passed
Push — master ( 77db6c...e9432b )
by John
13:58 queued 12s
created

UserThemeController::disableTheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
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\ITheme;
34
use OCA\Theming\Service\ThemesService;
35
use OCP\AppFramework\Http\DataResponse;
36
use OCP\AppFramework\OCS\OCSBadRequestException;
37
use OCP\AppFramework\OCS\OCSForbiddenException;
38
use OCP\AppFramework\OCSController;
39
use OCP\IConfig;
40
use OCP\IRequest;
41
use OCP\IUserSession;
42
use OCP\PreConditionNotMetException;
43
44
class UserThemeController extends OCSController {
45
46
	protected string $userId;
47
	private IConfig $config;
48
	private IUserSession $userSession;
49
	private ThemesService $themesService;
50
51
	/**
52
	 * Config constructor.
53
	 */
54
	public function __construct(string $appName,
55
								IRequest $request,
56
								IConfig $config,
57
								IUserSession $userSession,
58
								ThemesService $themesService) {
59
		parent::__construct($appName, $request);
60
		$this->config = $config;
61
		$this->userSession = $userSession;
62
		$this->themesService = $themesService;
63
		$this->userId = $userSession->getUser()->getUID();
64
	}
65
66
	/**
67
	 * @NoAdminRequired
68
	 *
69
	 * Enable theme
70
	 *
71
	 * @param string $themeId the theme ID
72
	 * @return DataResponse
73
	 * @throws OCSBadRequestException|PreConditionNotMetException
74
	 */
75
	public function enableTheme(string $themeId): DataResponse {
76
		$theme = $this->validateTheme($themeId);
77
78
		// Enable selected theme
79
		$this->themesService->enableTheme($theme);
80
		return new DataResponse();
81
	}
82
83
	/**
84
	 * @NoAdminRequired
85
	 *
86
	 * Disable theme
87
	 *
88
	 * @param string $themeId the theme ID
89
	 * @return DataResponse
90
	 * @throws OCSBadRequestException|PreConditionNotMetException
91
	 */
92
	public function disableTheme(string $themeId): DataResponse {
93
		$theme = $this->validateTheme($themeId);
94
		
95
		// Enable selected theme
96
		$this->themesService->disableTheme($theme);
97
		return new DataResponse();
98
	}
99
100
	/**
101
	 * Validate and return the matching ITheme
102
	 *
103
	 * Disable theme
104
	 *
105
	 * @param string $themeId the theme ID
106
	 * @return ITheme
107
	 * @throws OCSBadRequestException|PreConditionNotMetException
108
	 */
109
	private function validateTheme(string $themeId): ITheme {
110
		if ($themeId === '' || !$themeId) {
111
			throw new OCSBadRequestException('Invalid theme id: ' . $themeId);
112
		}
113
114
		$themes = $this->themesService->getThemes();
115
		if (!isset($themes[$themeId])) {
116
			throw new OCSBadRequestException('Invalid theme id: ' . $themeId);
117
		}
118
119
		// If trying to toggle another theme but this is enforced
120
		if ($this->config->getSystemValueString('enforce_theme', '') !== ''
121
			&& $themes[$themeId]->getType() === ITheme::TYPE_THEME) {
122
			throw new OCSForbiddenException('Theme switching is disabled');
123
		}
124
125
		return $themes[$themeId];
126
	}
127
}
128