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.

UserConfig.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  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. namespace OCA\Files\Service;
  24. use OCA\Files\AppInfo\Application;
  25. use OCP\IConfig;
  26. use OCP\IUser;
  27. use OCP\IUserSession;
  28. class UserConfig {
  29. const ALLOWED_CONFIGS = [
  30. [
  31. 'key' => 'crop_image_previews',
  32. 'default' => true,
  33. 'allowed' => [true, false],
  34. ],
  35. [
  36. 'key' => 'show_hidden',
  37. 'default' => false,
  38. 'allowed' => [true, false],
  39. ],
  40. ];
  41. protected IConfig $config;
  42. /** @var \OCP\IUser|null */
  43. protected mixed $user = null;
  44. public function __construct(IConfig $config, IUserSession $userSession) {
  45. $this->config = $config;
  46. $this->user = $userSession->getUser();
  47. }
  48. /**
  49. * Get the list of all allowed user config keys
  50. * @return string[]
  51. */
  52. public function getAllowedConfigKeys(): array {
  53. return array_map(function($config) {
  54. return $config['key'];
  55. }, self::ALLOWED_CONFIGS);
  56. }
  57. /**
  58. * Get the list of allowed config values for a given key
  59. *
  60. * @param string $key a valid config key
  61. * @return array
  62. */
  63. private function getAllowedConfigValues(string $key): array {
  64. foreach (self::ALLOWED_CONFIGS as $config) {
  65. if ($config['key'] === $key) {
  66. return $config['allowed'];
  67. }
  68. }
  69. return [];
  70. }
  71. /**
  72. * Get the default config value for a given key
  73. *
  74. * @param string $key a valid config key
  75. * @return string|bool
  76. */
  77. private function getDefaultConfigValue(string $key): string|bool {
  78. foreach (self::ALLOWED_CONFIGS as $config) {
  79. if ($config['key'] === $key) {
  80. return $config['default'];
  81. }
  82. }
  83. return '';
  84. }
  85. /**
  86. * Set a user config
  87. *
  88. * @param string $key
  89. * @param string $value
  90. * @throws \Exception
  91. * @throws \InvalidArgumentException
  92. */
  93. public function setConfig($key, $value) {
  94. if ($this->user === null) {
  95. throw new \Exception('No user logged in');
  96. }
  97. if (!in_array($key, $this->getAllowedConfigKeys())) {
  98. throw new \InvalidArgumentException('Unknown config key');
  99. }
  100. if (!in_array($value, $this->getAllowedConfigValues($key))) {
  101. throw new \InvalidArgumentException('Invalid config value');
  102. }
  103. if (is_bool($value)) {
  104. $value = $value ? '1' : '0';
  105. }
  106. $this->config->setUserValue($this->user->getUID(), Application::APP_ID, $key, $value);
  107. }
  108. /**
  109. * Get the current user configs array
  110. *
  111. * @return array
  112. */
  113. public function getConfigs(): array {
  114. if ($this->user === null) {
  115. throw new \Exception('No user logged in');
  116. }
  117. $userId = $this->user->getUID();
  118. $userConfigs = array_map(function(string $key) use ($userId): string|bool {
  119. $value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key));
  120. // If the default is expected to be a boolean, we need to cast the value
  121. if (is_bool($this->getDefaultConfigValue($key))) {
  122. return $value === '1';
  123. }
  124. return $value;
  125. }, $this->getAllowedConfigKeys());
  126. return array_combine($this->getAllowedConfigKeys(), $userConfigs);
  127. }
  128. }