ConfigController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. protected string $userId;
  40. private IConfig $config;
  41. private IUserSession $userSession;
  42. private AccessibilityProvider $accessibilityProvider;
  43. /**
  44. * Config constructor.
  45. */
  46. public function __construct(string $appName,
  47. IRequest $request,
  48. IConfig $config,
  49. IUserSession $userSession,
  50. AccessibilityProvider $accessibilityProvider) {
  51. parent::__construct($appName, $request);
  52. $this->config = $config;
  53. $this->userSession = $userSession;
  54. $this->accessibilityProvider = $accessibilityProvider;
  55. $this->userId = $userSession->getUser()->getUID();
  56. }
  57. /**
  58. * @NoAdminRequired
  59. *
  60. * Get user accessibility config
  61. *
  62. * @param string $key theme or font
  63. * @return DataResponse
  64. */
  65. public function getConfig(): DataResponse {
  66. return new DataResponse([
  67. 'highcontrast' => $this->config->getUserValue($this->userId, $this->appName, 'highcontrast', false),
  68. 'theme' => $this->config->getUserValue($this->userId, $this->appName, 'theme', false),
  69. 'font' => $this->config->getUserValue($this->userId, $this->appName, 'font', false)
  70. ]);
  71. }
  72. /**
  73. * @NoAdminRequired
  74. *
  75. * Set theme or font config
  76. *
  77. * @param string $key the theme or font
  78. * @param string|false $value the value
  79. * @return DataResponse
  80. * @throws OCSBadRequestException|PreConditionNotMetException
  81. */
  82. public function setConfig(string $key, $value): DataResponse {
  83. if ($key === 'theme' || $key === 'font' || $key === 'highcontrast') {
  84. if ($value === false || $value === '') {
  85. throw new OCSBadRequestException('Invalid value: ' . $value);
  86. }
  87. $themes = $this->accessibilityProvider->getThemes();
  88. $highcontrast = [$this->accessibilityProvider->getHighContrast()];
  89. $fonts = $this->accessibilityProvider->getFonts();
  90. $availableOptions = array_map(function ($option): string {
  91. return $option['id'];
  92. }, array_merge($themes, $highcontrast, $fonts));
  93. if (in_array($value, $availableOptions)) {
  94. $this->config->setUserValue($this->userId, $this->appName, $key, $value);
  95. return new DataResponse();
  96. }
  97. throw new OCSBadRequestException('Invalid value: ' . $value);
  98. }
  99. throw new OCSBadRequestException('Invalid key: ' . $key);
  100. }
  101. /**
  102. * @NoAdminRequired
  103. *
  104. * Unset theme or font config
  105. *
  106. * @param string $key theme or font
  107. * @return DataResponse
  108. * @throws OCSBadRequestException
  109. */
  110. public function deleteConfig(string $key): DataResponse {
  111. if ($key === 'theme' || $key === 'font' || $key === 'highcontrast') {
  112. $this->config->deleteUserValue($this->userId, $this->appName, $key);
  113. $userValues = $this->config->getUserKeys($this->userId, $this->appName);
  114. // remove hash if no settings selected
  115. if (count($userValues) === 1 && $userValues[0] === 'icons-css') {
  116. $this->config->deleteUserValue($this->userId, $this->appName, 'icons-css');
  117. }
  118. return new DataResponse();
  119. }
  120. throw new OCSBadRequestException('Invalid key: ' . $key);
  121. }
  122. }