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.

163 lines
3.9 KiB

  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files\Service;
  7. use OCA\Files\AppInfo\Application;
  8. use OCP\IConfig;
  9. use OCP\IUser;
  10. use OCP\IUserSession;
  11. class UserConfig {
  12. public const ALLOWED_CONFIGS = [
  13. [
  14. // Whether to crop the files previews or not in the files list
  15. 'key' => 'crop_image_previews',
  16. 'default' => true,
  17. 'allowed' => [true, false],
  18. ],
  19. [
  20. // Whether to show the "confirm file extension change" warning
  21. 'key' => 'show_dialog_file_extension',
  22. 'default' => true,
  23. 'allowed' => [true, false],
  24. ],
  25. [
  26. // Whether to show the hidden files or not in the files list
  27. 'key' => 'show_hidden',
  28. 'default' => false,
  29. 'allowed' => [true, false],
  30. ],
  31. [
  32. // Whether to sort favorites first in the list or not
  33. 'key' => 'sort_favorites_first',
  34. 'default' => true,
  35. 'allowed' => [true, false],
  36. ],
  37. [
  38. // Whether to sort folders before files in the list or not
  39. 'key' => 'sort_folders_first',
  40. 'default' => true,
  41. 'allowed' => [true, false],
  42. ],
  43. [
  44. // Whether to show the files list in grid view or not
  45. 'key' => 'grid_view',
  46. 'default' => false,
  47. 'allowed' => [true, false],
  48. ],
  49. [
  50. // Whether to show the folder tree
  51. 'key' => 'folder_tree',
  52. 'default' => true,
  53. 'allowed' => [true, false],
  54. ],
  55. [
  56. // Whether to show the mime column or not
  57. 'key' => 'show_mime_column',
  58. 'default' => true,
  59. 'allowed' => [true, false],
  60. ]
  61. ];
  62. protected ?IUser $user = null;
  63. public function __construct(
  64. protected IConfig $config,
  65. IUserSession $userSession,
  66. ) {
  67. $this->user = $userSession->getUser();
  68. }
  69. /**
  70. * Get the list of all allowed user config keys
  71. * @return string[]
  72. */
  73. public function getAllowedConfigKeys(): array {
  74. return array_map(function ($config) {
  75. return $config['key'];
  76. }, self::ALLOWED_CONFIGS);
  77. }
  78. /**
  79. * Get the list of allowed config values for a given key
  80. *
  81. * @param string $key a valid config key
  82. * @return array
  83. */
  84. private function getAllowedConfigValues(string $key): array {
  85. foreach (self::ALLOWED_CONFIGS as $config) {
  86. if ($config['key'] === $key) {
  87. return $config['allowed'];
  88. }
  89. }
  90. return [];
  91. }
  92. /**
  93. * Get the default config value for a given key
  94. *
  95. * @param string $key a valid config key
  96. * @return string|bool
  97. */
  98. private function getDefaultConfigValue(string $key) {
  99. foreach (self::ALLOWED_CONFIGS as $config) {
  100. if ($config['key'] === $key) {
  101. return $config['default'];
  102. }
  103. }
  104. return '';
  105. }
  106. /**
  107. * Set a user config
  108. *
  109. * @param string $key
  110. * @param string|bool $value
  111. * @throws \Exception
  112. * @throws \InvalidArgumentException
  113. */
  114. public function setConfig(string $key, $value): void {
  115. if ($this->user === null) {
  116. throw new \Exception('No user logged in');
  117. }
  118. if (!in_array($key, $this->getAllowedConfigKeys())) {
  119. throw new \InvalidArgumentException('Unknown config key');
  120. }
  121. if (!in_array($value, $this->getAllowedConfigValues($key))) {
  122. throw new \InvalidArgumentException('Invalid config value');
  123. }
  124. if (is_bool($value)) {
  125. $value = $value ? '1' : '0';
  126. }
  127. $this->config->setUserValue($this->user->getUID(), Application::APP_ID, $key, $value);
  128. }
  129. /**
  130. * Get the current user configs array
  131. *
  132. * @return array
  133. */
  134. public function getConfigs(): array {
  135. if ($this->user === null) {
  136. throw new \Exception('No user logged in');
  137. }
  138. $userId = $this->user->getUID();
  139. $userConfigs = array_map(function (string $key) use ($userId) {
  140. $value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key));
  141. // If the default is expected to be a boolean, we need to cast the value
  142. if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) {
  143. return $value === '1';
  144. }
  145. return $value;
  146. }, $this->getAllowedConfigKeys());
  147. return array_combine($this->getAllowedConfigKeys(), $userConfigs);
  148. }
  149. }