UserConfig.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. protected IConfig $config;
  45. protected ?IUser $user = null;
  46. public function __construct(IConfig $config, IUserSession $userSession) {
  47. $this->config = $config;
  48. $this->user = $userSession->getUser();
  49. }
  50. /**
  51. * Get the list of all allowed user config keys
  52. * @return string[]
  53. */
  54. public function getAllowedConfigKeys(): array {
  55. return array_map(function ($config) {
  56. return $config['key'];
  57. }, self::ALLOWED_CONFIGS);
  58. }
  59. /**
  60. * Get the list of allowed config values for a given key
  61. *
  62. * @param string $key a valid config key
  63. * @return array
  64. */
  65. private function getAllowedConfigValues(string $key): array {
  66. foreach (self::ALLOWED_CONFIGS as $config) {
  67. if ($config['key'] === $key) {
  68. return $config['allowed'];
  69. }
  70. }
  71. return [];
  72. }
  73. /**
  74. * Get the default config value for a given key
  75. *
  76. * @param string $key a valid config key
  77. * @return string|bool
  78. */
  79. private function getDefaultConfigValue(string $key) {
  80. foreach (self::ALLOWED_CONFIGS as $config) {
  81. if ($config['key'] === $key) {
  82. return $config['default'];
  83. }
  84. }
  85. return '';
  86. }
  87. /**
  88. * Set a user config
  89. *
  90. * @param string $key
  91. * @param string|bool $value
  92. * @throws \Exception
  93. * @throws \InvalidArgumentException
  94. */
  95. public function setConfig(string $key, $value): void {
  96. if ($this->user === null) {
  97. throw new \Exception('No user logged in');
  98. }
  99. if (!in_array($key, $this->getAllowedConfigKeys())) {
  100. throw new \InvalidArgumentException('Unknown config key');
  101. }
  102. if (!in_array($value, $this->getAllowedConfigValues($key))) {
  103. throw new \InvalidArgumentException('Invalid config value');
  104. }
  105. if (is_bool($value)) {
  106. $value = $value ? '1' : '0';
  107. }
  108. $this->config->setUserValue($this->user->getUID(), Application::APP_ID, $key, $value);
  109. }
  110. /**
  111. * Get the current user configs array
  112. *
  113. * @return array
  114. */
  115. public function getConfigs(): array {
  116. if ($this->user === null) {
  117. throw new \Exception('No user logged in');
  118. }
  119. $userId = $this->user->getUID();
  120. $userConfigs = array_map(function (string $key) use ($userId) {
  121. $value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key));
  122. // If the default is expected to be a boolean, we need to cast the value
  123. if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) {
  124. return $value === '1';
  125. }
  126. return $value;
  127. }, $this->getAllowedConfigKeys());
  128. return array_combine($this->getAllowedConfigKeys(), $userConfigs);
  129. }
  130. }