You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigController.php 4.7KB

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