1
0

ConfigController.php 3.7 KB

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