1
|
|
|
<?php |
2
|
|
|
declare (strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2019 Janis Köhr <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Accessibility\Controller; |
25
|
|
|
|
26
|
|
|
use OCA\Accessibility\AccessibilityProvider; |
27
|
|
|
use OCP\AppFramework\Http; |
28
|
|
|
use OCP\AppFramework\Http\DataResponse; |
29
|
|
|
use OCP\AppFramework\OCSController; |
30
|
|
|
use OCP\AppFramework\OCS\OCSBadRequestException; |
31
|
|
|
use OCP\IConfig; |
32
|
|
|
use OCP\IRequest; |
33
|
|
|
use OCP\IUserSession; |
34
|
|
|
|
35
|
|
|
class ConfigController extends OCSController { |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $appName; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
protected $userId; |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
protected $serverRoot; |
45
|
|
|
|
46
|
|
|
/** @var IConfig */ |
47
|
|
|
private $config; |
48
|
|
|
|
49
|
|
|
/** @var IUserSession */ |
50
|
|
|
private $userSession; |
51
|
|
|
|
52
|
|
|
/** @var AccessibilityProvider */ |
53
|
|
|
private $accessibilityProvider; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Config constructor. |
57
|
|
|
* |
58
|
|
|
* @param string $appName |
59
|
|
|
* @param IRequest $request |
60
|
|
|
* @param IConfig $config |
61
|
|
|
* @param IUserSession $userSession |
62
|
|
|
* @param AccessibilityProvider $accessibilityProvider |
63
|
|
|
*/ |
64
|
|
|
public function __construct(string $appName, |
65
|
|
|
IRequest $request, |
66
|
|
|
IConfig $config, |
67
|
|
|
IUserSession $userSession, |
68
|
|
|
AccessibilityProvider $accessibilityProvider) { |
69
|
|
|
parent::__construct($appName, $request); |
70
|
|
|
$this->appName = $appName; |
71
|
|
|
$this->config = $config; |
72
|
|
|
$this->userSession = $userSession; |
73
|
|
|
$this->accessibilityProvider = $accessibilityProvider; |
74
|
|
|
$this->userId = $userSession->getUser()->getUID(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @NoAdminRequired |
79
|
|
|
* |
80
|
|
|
* Get user accessibility config |
81
|
|
|
* |
82
|
|
|
* @param string $key theme or font |
83
|
|
|
* @return DataResponse |
84
|
|
|
*/ |
85
|
|
|
public function getConfig(): DataResponse { |
86
|
|
|
return new DataResponse([ |
87
|
|
|
'highcontrast' => $this->config->getUserValue($this->userId, $this->appName, 'highcontrast', false), |
88
|
|
|
'theme' => $this->config->getUserValue($this->userId, $this->appName, 'theme', false), |
89
|
|
|
'font' => $this->config->getUserValue($this->userId, $this->appName, 'font', false) |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @NoAdminRequired |
95
|
|
|
* |
96
|
|
|
* Set theme or font config |
97
|
|
|
* |
98
|
|
|
* @param string $key theme or font |
99
|
|
|
* @return DataResponse |
100
|
|
|
* @throws Exception |
101
|
|
|
*/ |
102
|
|
|
public function setConfig(string $key, $value): DataResponse { |
103
|
|
|
if ($key === 'theme' || $key === 'font' || $key === 'highcontrast') { |
104
|
|
|
|
105
|
|
|
if ($value === false || $value === '') { |
106
|
|
|
throw new OCSBadRequestException('Invalid value: ' . $value); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$themes = $this->accessibilityProvider->getThemes(); |
110
|
|
|
$highcontrast = array($this->accessibilityProvider->getHighContrast()); |
111
|
|
|
$fonts = $this->accessibilityProvider->getFonts(); |
112
|
|
|
|
113
|
|
|
$availableOptions = array_map(function($option) { |
114
|
|
|
return $option['id']; |
115
|
|
|
}, array_merge($themes, $highcontrast, $fonts)); |
116
|
|
|
|
117
|
|
|
if (in_array($value, $availableOptions)) { |
118
|
|
|
$this->config->setUserValue($this->userId, $this->appName, $key, $value); |
119
|
|
|
return new DataResponse(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
throw new OCSBadRequestException('Invalid value: ' . $value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
throw new OCSBadRequestException('Invalid key: ' . $key); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @NoAdminRequired |
130
|
|
|
* |
131
|
|
|
* Unset theme or font config |
132
|
|
|
* |
133
|
|
|
* @param string $key theme or font |
134
|
|
|
* @return DataResponse |
135
|
|
|
* @throws Exception |
136
|
|
|
*/ |
137
|
|
|
public function deleteConfig(string $key): DataResponse { |
138
|
|
|
if ($key === 'theme' || $key === 'font' || $key === 'highcontrast') { |
139
|
|
|
|
140
|
|
|
$this->config->deleteUserValue($this->userId, $this->appName, $key); |
141
|
|
|
$userValues = $this->config->getUserKeys($this->userId, $this->appName); |
142
|
|
|
|
143
|
|
|
// remove hash if no settings selected |
144
|
|
|
if (count($userValues) === 1 && $userValues[0] === 'icons-css') { |
145
|
|
|
$this->config->deleteUserValue($this->userId, $this->appName, 'icons-css'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return new DataResponse(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
throw new OCSBadRequestException('Invalid key: ' . $key); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|