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.6KB

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