UserConfig.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 hidden files or not in the files list
  21. 'key' => 'show_hidden',
  22. 'default' => false,
  23. 'allowed' => [true, false],
  24. ],
  25. [
  26. // Whether to sort favorites first in the list or not
  27. 'key' => 'sort_favorites_first',
  28. 'default' => true,
  29. 'allowed' => [true, false],
  30. ],
  31. [
  32. // Whether to sort folders before files in the list or not
  33. 'key' => 'sort_folders_first',
  34. 'default' => true,
  35. 'allowed' => [true, false],
  36. ],
  37. [
  38. // Whether to show the files list in grid view or not
  39. 'key' => 'grid_view',
  40. 'default' => false,
  41. 'allowed' => [true, false],
  42. ],
  43. [
  44. // Whether to show the folder tree
  45. 'key' => 'folder_tree',
  46. 'default' => true,
  47. 'allowed' => [true, false],
  48. ],
  49. ];
  50. protected ?IUser $user = null;
  51. public function __construct(
  52. protected IConfig $config,
  53. IUserSession $userSession,
  54. ) {
  55. $this->user = $userSession->getUser();
  56. }
  57. /**
  58. * Get the list of all allowed user config keys
  59. * @return string[]
  60. */
  61. public function getAllowedConfigKeys(): array {
  62. return array_map(function ($config) {
  63. return $config['key'];
  64. }, self::ALLOWED_CONFIGS);
  65. }
  66. /**
  67. * Get the list of allowed config values for a given key
  68. *
  69. * @param string $key a valid config key
  70. * @return array
  71. */
  72. private function getAllowedConfigValues(string $key): array {
  73. foreach (self::ALLOWED_CONFIGS as $config) {
  74. if ($config['key'] === $key) {
  75. return $config['allowed'];
  76. }
  77. }
  78. return [];
  79. }
  80. /**
  81. * Get the default config value for a given key
  82. *
  83. * @param string $key a valid config key
  84. * @return string|bool
  85. */
  86. private function getDefaultConfigValue(string $key) {
  87. foreach (self::ALLOWED_CONFIGS as $config) {
  88. if ($config['key'] === $key) {
  89. return $config['default'];
  90. }
  91. }
  92. return '';
  93. }
  94. /**
  95. * Set a user config
  96. *
  97. * @param string $key
  98. * @param string|bool $value
  99. * @throws \Exception
  100. * @throws \InvalidArgumentException
  101. */
  102. public function setConfig(string $key, $value): void {
  103. if ($this->user === null) {
  104. throw new \Exception('No user logged in');
  105. }
  106. if (!in_array($key, $this->getAllowedConfigKeys())) {
  107. throw new \InvalidArgumentException('Unknown config key');
  108. }
  109. if (!in_array($value, $this->getAllowedConfigValues($key))) {
  110. throw new \InvalidArgumentException('Invalid config value');
  111. }
  112. if (is_bool($value)) {
  113. $value = $value ? '1' : '0';
  114. }
  115. $this->config->setUserValue($this->user->getUID(), Application::APP_ID, $key, $value);
  116. }
  117. /**
  118. * Get the current user configs array
  119. *
  120. * @return array
  121. */
  122. public function getConfigs(): array {
  123. if ($this->user === null) {
  124. throw new \Exception('No user logged in');
  125. }
  126. $userId = $this->user->getUID();
  127. $userConfigs = array_map(function (string $key) use ($userId) {
  128. $value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key));
  129. // If the default is expected to be a boolean, we need to cast the value
  130. if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) {
  131. return $value === '1';
  132. }
  133. return $value;
  134. }, $this->getAllowedConfigKeys());
  135. return array_combine($this->getAllowedConfigKeys(), $userConfigs);
  136. }
  137. }