1
0

UserConfig.php 4.2 KB

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