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