@@ -28,50 +28,50 @@ |
||
28 | 28 | use Symfony\Component\Console\Output\OutputInterface; |
29 | 29 | |
30 | 30 | class Option extends Config { |
31 | - protected function configure() { |
|
32 | - $this |
|
33 | - ->setName('files_external:option') |
|
34 | - ->setDescription('Manage mount options for a mount') |
|
35 | - ->addArgument( |
|
36 | - 'mount_id', |
|
37 | - InputArgument::REQUIRED, |
|
38 | - 'The id of the mount to edit' |
|
39 | - )->addArgument( |
|
40 | - 'key', |
|
41 | - InputArgument::REQUIRED, |
|
42 | - 'key of the mount option to set/get' |
|
43 | - )->addArgument( |
|
44 | - 'value', |
|
45 | - InputArgument::OPTIONAL, |
|
46 | - 'value to set the mount option to, when no value is provided the existing value will be printed' |
|
47 | - ); |
|
48 | - } |
|
31 | + protected function configure() { |
|
32 | + $this |
|
33 | + ->setName('files_external:option') |
|
34 | + ->setDescription('Manage mount options for a mount') |
|
35 | + ->addArgument( |
|
36 | + 'mount_id', |
|
37 | + InputArgument::REQUIRED, |
|
38 | + 'The id of the mount to edit' |
|
39 | + )->addArgument( |
|
40 | + 'key', |
|
41 | + InputArgument::REQUIRED, |
|
42 | + 'key of the mount option to set/get' |
|
43 | + )->addArgument( |
|
44 | + 'value', |
|
45 | + InputArgument::OPTIONAL, |
|
46 | + 'value to set the mount option to, when no value is provided the existing value will be printed' |
|
47 | + ); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param StorageConfig $mount |
|
52 | - * @param string $key |
|
53 | - * @param OutputInterface $output |
|
54 | - */ |
|
55 | - protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { |
|
56 | - $value = $mount->getMountOption($key); |
|
57 | - if (!is_string($value)) { // show bools and objects correctly |
|
58 | - $value = json_encode($value); |
|
59 | - } |
|
60 | - $output->writeln($value); |
|
61 | - } |
|
50 | + /** |
|
51 | + * @param StorageConfig $mount |
|
52 | + * @param string $key |
|
53 | + * @param OutputInterface $output |
|
54 | + */ |
|
55 | + protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { |
|
56 | + $value = $mount->getMountOption($key); |
|
57 | + if (!is_string($value)) { // show bools and objects correctly |
|
58 | + $value = json_encode($value); |
|
59 | + } |
|
60 | + $output->writeln($value); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @param StorageConfig $mount |
|
65 | - * @param string $key |
|
66 | - * @param string $value |
|
67 | - * @param OutputInterface $output |
|
68 | - */ |
|
69 | - protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { |
|
70 | - $decoded = json_decode($value, true); |
|
71 | - if (!is_null($decoded)) { |
|
72 | - $value = $decoded; |
|
73 | - } |
|
74 | - $mount->setMountOption($key, $value); |
|
75 | - $this->globalService->updateStorage($mount); |
|
76 | - } |
|
63 | + /** |
|
64 | + * @param StorageConfig $mount |
|
65 | + * @param string $key |
|
66 | + * @param string $value |
|
67 | + * @param OutputInterface $output |
|
68 | + */ |
|
69 | + protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { |
|
70 | + $decoded = json_decode($value, true); |
|
71 | + if (!is_null($decoded)) { |
|
72 | + $value = $decoded; |
|
73 | + } |
|
74 | + $mount->setMountOption($key, $value); |
|
75 | + $this->globalService->updateStorage($mount); |
|
76 | + } |
|
77 | 77 | } |
@@ -42,182 +42,182 @@ |
||
42 | 42 | use Symfony\Component\Console\Output\OutputInterface; |
43 | 43 | |
44 | 44 | class Create extends Base { |
45 | - /** |
|
46 | - * @var GlobalStoragesService |
|
47 | - */ |
|
48 | - private $globalService; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var UserStoragesService |
|
52 | - */ |
|
53 | - private $userService; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var IUserManager |
|
57 | - */ |
|
58 | - private $userManager; |
|
59 | - |
|
60 | - /** @var BackendService */ |
|
61 | - private $backendService; |
|
62 | - |
|
63 | - /** @var IUserSession */ |
|
64 | - private $userSession; |
|
65 | - |
|
66 | - function __construct(GlobalStoragesService $globalService, |
|
67 | - UserStoragesService $userService, |
|
68 | - IUserManager $userManager, |
|
69 | - IUserSession $userSession, |
|
70 | - BackendService $backendService |
|
71 | - ) { |
|
72 | - parent::__construct(); |
|
73 | - $this->globalService = $globalService; |
|
74 | - $this->userService = $userService; |
|
75 | - $this->userManager = $userManager; |
|
76 | - $this->userSession = $userSession; |
|
77 | - $this->backendService = $backendService; |
|
78 | - } |
|
79 | - |
|
80 | - protected function configure() { |
|
81 | - $this |
|
82 | - ->setName('files_external:create') |
|
83 | - ->setDescription('Create a new mount configuration') |
|
84 | - ->addOption( |
|
85 | - 'user', |
|
86 | - null, |
|
87 | - InputOption::VALUE_OPTIONAL, |
|
88 | - 'user to add the mount configuration for, if not set the mount will be added as system mount' |
|
89 | - ) |
|
90 | - ->addArgument( |
|
91 | - 'mount_point', |
|
92 | - InputArgument::REQUIRED, |
|
93 | - 'mount point for the new mount' |
|
94 | - ) |
|
95 | - ->addArgument( |
|
96 | - 'storage_backend', |
|
97 | - InputArgument::REQUIRED, |
|
98 | - 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values' |
|
99 | - ) |
|
100 | - ->addArgument( |
|
101 | - 'authentication_backend', |
|
102 | - InputArgument::REQUIRED, |
|
103 | - 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values' |
|
104 | - ) |
|
105 | - ->addOption( |
|
106 | - 'config', |
|
107 | - 'c', |
|
108 | - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
109 | - 'Mount configuration option in key=value format' |
|
110 | - ) |
|
111 | - ->addOption( |
|
112 | - 'dry', |
|
113 | - null, |
|
114 | - InputOption::VALUE_NONE, |
|
115 | - 'Don\'t save the created mount, only list the new mount' |
|
116 | - ); |
|
117 | - parent::configure(); |
|
118 | - } |
|
119 | - |
|
120 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
121 | - $user = $input->getOption('user'); |
|
122 | - $mountPoint = $input->getArgument('mount_point'); |
|
123 | - $storageIdentifier = $input->getArgument('storage_backend'); |
|
124 | - $authIdentifier = $input->getArgument('authentication_backend'); |
|
125 | - $configInput = $input->getOption('config'); |
|
126 | - |
|
127 | - $storageBackend = $this->backendService->getBackend($storageIdentifier); |
|
128 | - $authBackend = $this->backendService->getAuthMechanism($authIdentifier); |
|
129 | - |
|
130 | - if (!Filesystem::isValidPath($mountPoint)) { |
|
131 | - $output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>'); |
|
132 | - return 1; |
|
133 | - } |
|
134 | - if (is_null($storageBackend)) { |
|
135 | - $output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>'); |
|
136 | - return 404; |
|
137 | - } |
|
138 | - if (is_null($authBackend)) { |
|
139 | - $output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>'); |
|
140 | - return 404; |
|
141 | - } |
|
142 | - $supportedSchemes = array_keys($storageBackend->getAuthSchemes()); |
|
143 | - if (!in_array($authBackend->getScheme(), $supportedSchemes)) { |
|
144 | - $output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>'); |
|
145 | - return 1; |
|
146 | - } |
|
147 | - |
|
148 | - $config = []; |
|
149 | - foreach ($configInput as $configOption) { |
|
150 | - if (!strpos($configOption, '=')) { |
|
151 | - $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>'); |
|
152 | - return 1; |
|
153 | - } |
|
154 | - list($key, $value) = explode('=', $configOption, 2); |
|
155 | - if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) { |
|
156 | - $output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>'); |
|
157 | - return 1; |
|
158 | - } |
|
159 | - $config[$key] = $value; |
|
160 | - } |
|
161 | - |
|
162 | - $mount = new StorageConfig(); |
|
163 | - $mount->setMountPoint($mountPoint); |
|
164 | - $mount->setBackend($storageBackend); |
|
165 | - $mount->setAuthMechanism($authBackend); |
|
166 | - $mount->setBackendOptions($config); |
|
167 | - |
|
168 | - if ($user) { |
|
169 | - if (!$this->userManager->userExists($user)) { |
|
170 | - $output->writeln('<error>User "' . $user . '" not found</error>'); |
|
171 | - return 1; |
|
172 | - } |
|
173 | - $mount->setApplicableUsers([$user]); |
|
174 | - } |
|
175 | - |
|
176 | - if ($input->getOption('dry')) { |
|
177 | - $this->showMount($user, $mount, $input, $output); |
|
178 | - } else { |
|
179 | - $this->getStorageService($user)->addStorage($mount); |
|
180 | - if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) { |
|
181 | - $output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>'); |
|
182 | - } else { |
|
183 | - $output->writeln($mount->getId()); |
|
184 | - } |
|
185 | - } |
|
186 | - return 0; |
|
187 | - } |
|
188 | - |
|
189 | - private function validateParam($key, &$value, Backend $storageBackend, AuthMechanism $authBackend) { |
|
190 | - $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters()); |
|
191 | - foreach ($params as $param) { |
|
192 | - /** @var DefinitionParameter $param */ |
|
193 | - if ($param->getName() === $key) { |
|
194 | - if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) { |
|
195 | - $value = ($value === 'true'); |
|
196 | - } |
|
197 | - return true; |
|
198 | - } |
|
199 | - } |
|
200 | - return false; |
|
201 | - } |
|
202 | - |
|
203 | - private function showMount($user, StorageConfig $mount, InputInterface $input, OutputInterface $output) { |
|
204 | - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
205 | - $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
206 | - $listInput->setOption('output', $input->getOption('output')); |
|
207 | - $listInput->setOption('show-password', true); |
|
208 | - $listCommand->listMounts($user, [$mount], $listInput, $output); |
|
209 | - } |
|
210 | - |
|
211 | - protected function getStorageService($userId) { |
|
212 | - if (!empty($userId)) { |
|
213 | - $user = $this->userManager->get($userId); |
|
214 | - if (is_null($user)) { |
|
215 | - throw new NoUserException("user $userId not found"); |
|
216 | - } |
|
217 | - $this->userSession->setUser($user); |
|
218 | - return $this->userService; |
|
219 | - } else { |
|
220 | - return $this->globalService; |
|
221 | - } |
|
222 | - } |
|
45 | + /** |
|
46 | + * @var GlobalStoragesService |
|
47 | + */ |
|
48 | + private $globalService; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var UserStoragesService |
|
52 | + */ |
|
53 | + private $userService; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var IUserManager |
|
57 | + */ |
|
58 | + private $userManager; |
|
59 | + |
|
60 | + /** @var BackendService */ |
|
61 | + private $backendService; |
|
62 | + |
|
63 | + /** @var IUserSession */ |
|
64 | + private $userSession; |
|
65 | + |
|
66 | + function __construct(GlobalStoragesService $globalService, |
|
67 | + UserStoragesService $userService, |
|
68 | + IUserManager $userManager, |
|
69 | + IUserSession $userSession, |
|
70 | + BackendService $backendService |
|
71 | + ) { |
|
72 | + parent::__construct(); |
|
73 | + $this->globalService = $globalService; |
|
74 | + $this->userService = $userService; |
|
75 | + $this->userManager = $userManager; |
|
76 | + $this->userSession = $userSession; |
|
77 | + $this->backendService = $backendService; |
|
78 | + } |
|
79 | + |
|
80 | + protected function configure() { |
|
81 | + $this |
|
82 | + ->setName('files_external:create') |
|
83 | + ->setDescription('Create a new mount configuration') |
|
84 | + ->addOption( |
|
85 | + 'user', |
|
86 | + null, |
|
87 | + InputOption::VALUE_OPTIONAL, |
|
88 | + 'user to add the mount configuration for, if not set the mount will be added as system mount' |
|
89 | + ) |
|
90 | + ->addArgument( |
|
91 | + 'mount_point', |
|
92 | + InputArgument::REQUIRED, |
|
93 | + 'mount point for the new mount' |
|
94 | + ) |
|
95 | + ->addArgument( |
|
96 | + 'storage_backend', |
|
97 | + InputArgument::REQUIRED, |
|
98 | + 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values' |
|
99 | + ) |
|
100 | + ->addArgument( |
|
101 | + 'authentication_backend', |
|
102 | + InputArgument::REQUIRED, |
|
103 | + 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values' |
|
104 | + ) |
|
105 | + ->addOption( |
|
106 | + 'config', |
|
107 | + 'c', |
|
108 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
109 | + 'Mount configuration option in key=value format' |
|
110 | + ) |
|
111 | + ->addOption( |
|
112 | + 'dry', |
|
113 | + null, |
|
114 | + InputOption::VALUE_NONE, |
|
115 | + 'Don\'t save the created mount, only list the new mount' |
|
116 | + ); |
|
117 | + parent::configure(); |
|
118 | + } |
|
119 | + |
|
120 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
121 | + $user = $input->getOption('user'); |
|
122 | + $mountPoint = $input->getArgument('mount_point'); |
|
123 | + $storageIdentifier = $input->getArgument('storage_backend'); |
|
124 | + $authIdentifier = $input->getArgument('authentication_backend'); |
|
125 | + $configInput = $input->getOption('config'); |
|
126 | + |
|
127 | + $storageBackend = $this->backendService->getBackend($storageIdentifier); |
|
128 | + $authBackend = $this->backendService->getAuthMechanism($authIdentifier); |
|
129 | + |
|
130 | + if (!Filesystem::isValidPath($mountPoint)) { |
|
131 | + $output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>'); |
|
132 | + return 1; |
|
133 | + } |
|
134 | + if (is_null($storageBackend)) { |
|
135 | + $output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>'); |
|
136 | + return 404; |
|
137 | + } |
|
138 | + if (is_null($authBackend)) { |
|
139 | + $output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>'); |
|
140 | + return 404; |
|
141 | + } |
|
142 | + $supportedSchemes = array_keys($storageBackend->getAuthSchemes()); |
|
143 | + if (!in_array($authBackend->getScheme(), $supportedSchemes)) { |
|
144 | + $output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>'); |
|
145 | + return 1; |
|
146 | + } |
|
147 | + |
|
148 | + $config = []; |
|
149 | + foreach ($configInput as $configOption) { |
|
150 | + if (!strpos($configOption, '=')) { |
|
151 | + $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>'); |
|
152 | + return 1; |
|
153 | + } |
|
154 | + list($key, $value) = explode('=', $configOption, 2); |
|
155 | + if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) { |
|
156 | + $output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>'); |
|
157 | + return 1; |
|
158 | + } |
|
159 | + $config[$key] = $value; |
|
160 | + } |
|
161 | + |
|
162 | + $mount = new StorageConfig(); |
|
163 | + $mount->setMountPoint($mountPoint); |
|
164 | + $mount->setBackend($storageBackend); |
|
165 | + $mount->setAuthMechanism($authBackend); |
|
166 | + $mount->setBackendOptions($config); |
|
167 | + |
|
168 | + if ($user) { |
|
169 | + if (!$this->userManager->userExists($user)) { |
|
170 | + $output->writeln('<error>User "' . $user . '" not found</error>'); |
|
171 | + return 1; |
|
172 | + } |
|
173 | + $mount->setApplicableUsers([$user]); |
|
174 | + } |
|
175 | + |
|
176 | + if ($input->getOption('dry')) { |
|
177 | + $this->showMount($user, $mount, $input, $output); |
|
178 | + } else { |
|
179 | + $this->getStorageService($user)->addStorage($mount); |
|
180 | + if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) { |
|
181 | + $output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>'); |
|
182 | + } else { |
|
183 | + $output->writeln($mount->getId()); |
|
184 | + } |
|
185 | + } |
|
186 | + return 0; |
|
187 | + } |
|
188 | + |
|
189 | + private function validateParam($key, &$value, Backend $storageBackend, AuthMechanism $authBackend) { |
|
190 | + $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters()); |
|
191 | + foreach ($params as $param) { |
|
192 | + /** @var DefinitionParameter $param */ |
|
193 | + if ($param->getName() === $key) { |
|
194 | + if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) { |
|
195 | + $value = ($value === 'true'); |
|
196 | + } |
|
197 | + return true; |
|
198 | + } |
|
199 | + } |
|
200 | + return false; |
|
201 | + } |
|
202 | + |
|
203 | + private function showMount($user, StorageConfig $mount, InputInterface $input, OutputInterface $output) { |
|
204 | + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
205 | + $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
206 | + $listInput->setOption('output', $input->getOption('output')); |
|
207 | + $listInput->setOption('show-password', true); |
|
208 | + $listCommand->listMounts($user, [$mount], $listInput, $output); |
|
209 | + } |
|
210 | + |
|
211 | + protected function getStorageService($userId) { |
|
212 | + if (!empty($userId)) { |
|
213 | + $user = $this->userManager->get($userId); |
|
214 | + if (is_null($user)) { |
|
215 | + throw new NoUserException("user $userId not found"); |
|
216 | + } |
|
217 | + $this->userSession->setUser($user); |
|
218 | + return $this->userService; |
|
219 | + } else { |
|
220 | + return $this->globalService; |
|
221 | + } |
|
222 | + } |
|
223 | 223 | } |
@@ -32,91 +32,91 @@ |
||
32 | 32 | use Symfony\Component\Console\Output\OutputInterface; |
33 | 33 | |
34 | 34 | class Backends extends Base { |
35 | - /** @var BackendService */ |
|
36 | - private $backendService; |
|
35 | + /** @var BackendService */ |
|
36 | + private $backendService; |
|
37 | 37 | |
38 | - function __construct(BackendService $backendService |
|
39 | - ) { |
|
40 | - parent::__construct(); |
|
38 | + function __construct(BackendService $backendService |
|
39 | + ) { |
|
40 | + parent::__construct(); |
|
41 | 41 | |
42 | - $this->backendService = $backendService; |
|
43 | - } |
|
42 | + $this->backendService = $backendService; |
|
43 | + } |
|
44 | 44 | |
45 | - protected function configure() { |
|
46 | - $this |
|
47 | - ->setName('files_external:backends') |
|
48 | - ->setDescription('Show available authentication and storage backends') |
|
49 | - ->addArgument( |
|
50 | - 'type', |
|
51 | - InputArgument::OPTIONAL, |
|
52 | - 'only show backends of a certain type. Possible values are "authentication" or "storage"' |
|
53 | - )->addArgument( |
|
54 | - 'backend', |
|
55 | - InputArgument::OPTIONAL, |
|
56 | - 'only show information of a specific backend' |
|
57 | - ); |
|
58 | - parent::configure(); |
|
59 | - } |
|
45 | + protected function configure() { |
|
46 | + $this |
|
47 | + ->setName('files_external:backends') |
|
48 | + ->setDescription('Show available authentication and storage backends') |
|
49 | + ->addArgument( |
|
50 | + 'type', |
|
51 | + InputArgument::OPTIONAL, |
|
52 | + 'only show backends of a certain type. Possible values are "authentication" or "storage"' |
|
53 | + )->addArgument( |
|
54 | + 'backend', |
|
55 | + InputArgument::OPTIONAL, |
|
56 | + 'only show information of a specific backend' |
|
57 | + ); |
|
58 | + parent::configure(); |
|
59 | + } |
|
60 | 60 | |
61 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
62 | - $authBackends = $this->backendService->getAuthMechanisms(); |
|
63 | - $storageBackends = $this->backendService->getBackends(); |
|
61 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
62 | + $authBackends = $this->backendService->getAuthMechanisms(); |
|
63 | + $storageBackends = $this->backendService->getBackends(); |
|
64 | 64 | |
65 | - $data = [ |
|
66 | - 'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends), |
|
67 | - 'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends) |
|
68 | - ]; |
|
65 | + $data = [ |
|
66 | + 'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends), |
|
67 | + 'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends) |
|
68 | + ]; |
|
69 | 69 | |
70 | - $type = $input->getArgument('type'); |
|
71 | - $backend = $input->getArgument('backend'); |
|
72 | - if ($type) { |
|
73 | - if (!isset($data[$type])) { |
|
74 | - $output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>'); |
|
75 | - return 1; |
|
76 | - } |
|
77 | - $data = $data[$type]; |
|
70 | + $type = $input->getArgument('type'); |
|
71 | + $backend = $input->getArgument('backend'); |
|
72 | + if ($type) { |
|
73 | + if (!isset($data[$type])) { |
|
74 | + $output->writeln('<error>Invalid type "' . $type . '". Possible values are "authentication" or "storage"</error>'); |
|
75 | + return 1; |
|
76 | + } |
|
77 | + $data = $data[$type]; |
|
78 | 78 | |
79 | - if ($backend) { |
|
80 | - if (!isset($data[$backend])) { |
|
81 | - $output->writeln('<error>Unknown backend "' . $backend . '" of type "' . $type . '"</error>'); |
|
82 | - return 1; |
|
83 | - } |
|
84 | - $data = $data[$backend]; |
|
85 | - } |
|
86 | - } |
|
79 | + if ($backend) { |
|
80 | + if (!isset($data[$backend])) { |
|
81 | + $output->writeln('<error>Unknown backend "' . $backend . '" of type "' . $type . '"</error>'); |
|
82 | + return 1; |
|
83 | + } |
|
84 | + $data = $data[$backend]; |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | - $this->writeArrayInOutputFormat($input, $output, $data); |
|
89 | - } |
|
88 | + $this->writeArrayInOutputFormat($input, $output, $data); |
|
89 | + } |
|
90 | 90 | |
91 | - private function serializeAuthBackend(\JsonSerializable $backend) { |
|
92 | - $data = $backend->jsonSerialize(); |
|
93 | - $result = [ |
|
94 | - 'name' => $data['name'], |
|
95 | - 'identifier' => $data['identifier'], |
|
96 | - 'configuration' => $this->formatConfiguration($data['configuration']) |
|
97 | - ]; |
|
98 | - if ($backend instanceof Backend) { |
|
99 | - $result['storage_class'] = $backend->getStorageClass(); |
|
100 | - $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes())); |
|
101 | - $result['supported_authentication_backends'] = array_keys($authBackends); |
|
102 | - $authConfig = array_map(function (AuthMechanism $auth) { |
|
103 | - return $this->serializeAuthBackend($auth)['configuration']; |
|
104 | - }, $authBackends); |
|
105 | - $result['authentication_configuration'] = array_combine(array_keys($authBackends), $authConfig); |
|
106 | - } |
|
107 | - return $result; |
|
108 | - } |
|
91 | + private function serializeAuthBackend(\JsonSerializable $backend) { |
|
92 | + $data = $backend->jsonSerialize(); |
|
93 | + $result = [ |
|
94 | + 'name' => $data['name'], |
|
95 | + 'identifier' => $data['identifier'], |
|
96 | + 'configuration' => $this->formatConfiguration($data['configuration']) |
|
97 | + ]; |
|
98 | + if ($backend instanceof Backend) { |
|
99 | + $result['storage_class'] = $backend->getStorageClass(); |
|
100 | + $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes())); |
|
101 | + $result['supported_authentication_backends'] = array_keys($authBackends); |
|
102 | + $authConfig = array_map(function (AuthMechanism $auth) { |
|
103 | + return $this->serializeAuthBackend($auth)['configuration']; |
|
104 | + }, $authBackends); |
|
105 | + $result['authentication_configuration'] = array_combine(array_keys($authBackends), $authConfig); |
|
106 | + } |
|
107 | + return $result; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * @param DefinitionParameter[] $parameters |
|
112 | - * @return string[] |
|
113 | - */ |
|
114 | - private function formatConfiguration(array $parameters) { |
|
115 | - $configuration = array_filter($parameters, function (DefinitionParameter $parameter) { |
|
116 | - return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN; |
|
117 | - }); |
|
118 | - return array_map(function (DefinitionParameter $parameter) { |
|
119 | - return $parameter->getTypeName(); |
|
120 | - }, $configuration); |
|
121 | - } |
|
110 | + /** |
|
111 | + * @param DefinitionParameter[] $parameters |
|
112 | + * @return string[] |
|
113 | + */ |
|
114 | + private function formatConfiguration(array $parameters) { |
|
115 | + $configuration = array_filter($parameters, function (DefinitionParameter $parameter) { |
|
116 | + return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN; |
|
117 | + }); |
|
118 | + return array_map(function (DefinitionParameter $parameter) { |
|
119 | + return $parameter->getTypeName(); |
|
120 | + }, $configuration); |
|
121 | + } |
|
122 | 122 | } |
@@ -37,76 +37,76 @@ |
||
37 | 37 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
38 | 38 | |
39 | 39 | class Delete extends Base { |
40 | - /** |
|
41 | - * @var GlobalStoragesService |
|
42 | - */ |
|
43 | - protected $globalService; |
|
40 | + /** |
|
41 | + * @var GlobalStoragesService |
|
42 | + */ |
|
43 | + protected $globalService; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @var UserStoragesService |
|
47 | - */ |
|
48 | - protected $userService; |
|
45 | + /** |
|
46 | + * @var UserStoragesService |
|
47 | + */ |
|
48 | + protected $userService; |
|
49 | 49 | |
50 | - /** |
|
51 | - * @var IUserSession |
|
52 | - */ |
|
53 | - protected $userSession; |
|
50 | + /** |
|
51 | + * @var IUserSession |
|
52 | + */ |
|
53 | + protected $userSession; |
|
54 | 54 | |
55 | - /** |
|
56 | - * @var IUserManager |
|
57 | - */ |
|
58 | - protected $userManager; |
|
55 | + /** |
|
56 | + * @var IUserManager |
|
57 | + */ |
|
58 | + protected $userManager; |
|
59 | 59 | |
60 | - function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { |
|
61 | - parent::__construct(); |
|
62 | - $this->globalService = $globalService; |
|
63 | - $this->userService = $userService; |
|
64 | - $this->userSession = $userSession; |
|
65 | - $this->userManager = $userManager; |
|
66 | - } |
|
60 | + function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { |
|
61 | + parent::__construct(); |
|
62 | + $this->globalService = $globalService; |
|
63 | + $this->userService = $userService; |
|
64 | + $this->userSession = $userSession; |
|
65 | + $this->userManager = $userManager; |
|
66 | + } |
|
67 | 67 | |
68 | - protected function configure() { |
|
69 | - $this |
|
70 | - ->setName('files_external:delete') |
|
71 | - ->setDescription('Delete an external mount') |
|
72 | - ->addArgument( |
|
73 | - 'mount_id', |
|
74 | - InputArgument::REQUIRED, |
|
75 | - 'The id of the mount to edit' |
|
76 | - )->addOption( |
|
77 | - 'yes', |
|
78 | - 'y', |
|
79 | - InputOption::VALUE_NONE, |
|
80 | - 'Skip confirmation' |
|
81 | - ); |
|
82 | - parent::configure(); |
|
83 | - } |
|
68 | + protected function configure() { |
|
69 | + $this |
|
70 | + ->setName('files_external:delete') |
|
71 | + ->setDescription('Delete an external mount') |
|
72 | + ->addArgument( |
|
73 | + 'mount_id', |
|
74 | + InputArgument::REQUIRED, |
|
75 | + 'The id of the mount to edit' |
|
76 | + )->addOption( |
|
77 | + 'yes', |
|
78 | + 'y', |
|
79 | + InputOption::VALUE_NONE, |
|
80 | + 'Skip confirmation' |
|
81 | + ); |
|
82 | + parent::configure(); |
|
83 | + } |
|
84 | 84 | |
85 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
86 | - $mountId = $input->getArgument('mount_id'); |
|
87 | - try { |
|
88 | - $mount = $this->globalService->getStorage($mountId); |
|
89 | - } catch (NotFoundException $e) { |
|
90 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
91 | - return 404; |
|
92 | - } |
|
85 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
86 | + $mountId = $input->getArgument('mount_id'); |
|
87 | + try { |
|
88 | + $mount = $this->globalService->getStorage($mountId); |
|
89 | + } catch (NotFoundException $e) { |
|
90 | + $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
91 | + return 404; |
|
92 | + } |
|
93 | 93 | |
94 | - $noConfirm = $input->getOption('yes'); |
|
94 | + $noConfirm = $input->getOption('yes'); |
|
95 | 95 | |
96 | - if (!$noConfirm) { |
|
97 | - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
98 | - $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
99 | - $listInput->setOption('output', $input->getOption('output')); |
|
100 | - $listCommand->listMounts(null, [$mount], $listInput, $output); |
|
96 | + if (!$noConfirm) { |
|
97 | + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
98 | + $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
99 | + $listInput->setOption('output', $input->getOption('output')); |
|
100 | + $listCommand->listMounts(null, [$mount], $listInput, $output); |
|
101 | 101 | |
102 | - $questionHelper = $this->getHelper('question'); |
|
103 | - $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false); |
|
102 | + $questionHelper = $this->getHelper('question'); |
|
103 | + $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false); |
|
104 | 104 | |
105 | - if (!$questionHelper->ask($input, $output, $question)) { |
|
106 | - return; |
|
107 | - } |
|
108 | - } |
|
105 | + if (!$questionHelper->ask($input, $output, $question)) { |
|
106 | + return; |
|
107 | + } |
|
108 | + } |
|
109 | 109 | |
110 | - $this->globalService->removeStorage($mountId); |
|
111 | - } |
|
110 | + $this->globalService->removeStorage($mountId); |
|
111 | + } |
|
112 | 112 | } |
@@ -39,187 +39,187 @@ |
||
39 | 39 | use Symfony\Component\Console\Output\OutputInterface; |
40 | 40 | |
41 | 41 | class Import extends Base { |
42 | - /** |
|
43 | - * @var GlobalStoragesService |
|
44 | - */ |
|
45 | - private $globalService; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var UserStoragesService |
|
49 | - */ |
|
50 | - private $userService; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var IUserSession |
|
54 | - */ |
|
55 | - private $userSession; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var IUserManager |
|
59 | - */ |
|
60 | - private $userManager; |
|
61 | - |
|
62 | - /** @var ImportLegacyStoragesService */ |
|
63 | - private $importLegacyStorageService; |
|
64 | - |
|
65 | - /** @var BackendService */ |
|
66 | - private $backendService; |
|
67 | - |
|
68 | - function __construct(GlobalStoragesService $globalService, |
|
69 | - UserStoragesService $userService, |
|
70 | - IUserSession $userSession, |
|
71 | - IUserManager $userManager, |
|
72 | - ImportLegacyStoragesService $importLegacyStorageService, |
|
73 | - BackendService $backendService |
|
74 | - ) { |
|
75 | - parent::__construct(); |
|
76 | - $this->globalService = $globalService; |
|
77 | - $this->userService = $userService; |
|
78 | - $this->userSession = $userSession; |
|
79 | - $this->userManager = $userManager; |
|
80 | - $this->importLegacyStorageService = $importLegacyStorageService; |
|
81 | - $this->backendService = $backendService; |
|
82 | - } |
|
83 | - |
|
84 | - protected function configure() { |
|
85 | - $this |
|
86 | - ->setName('files_external:import') |
|
87 | - ->setDescription('Import mount configurations') |
|
88 | - ->addOption( |
|
89 | - 'user', |
|
90 | - null, |
|
91 | - InputOption::VALUE_OPTIONAL, |
|
92 | - 'user to add the mount configurations for, if not set the mount will be added as system mount' |
|
93 | - ) |
|
94 | - ->addArgument( |
|
95 | - 'path', |
|
96 | - InputArgument::REQUIRED, |
|
97 | - 'path to a json file containing the mounts to import, use "-" to read from stdin' |
|
98 | - ) |
|
99 | - ->addOption( |
|
100 | - 'dry', |
|
101 | - null, |
|
102 | - InputOption::VALUE_NONE, |
|
103 | - 'Don\'t save the imported mounts, only list the new mounts' |
|
104 | - ); |
|
105 | - parent::configure(); |
|
106 | - } |
|
107 | - |
|
108 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
109 | - $user = $input->getOption('user'); |
|
110 | - $path = $input->getArgument('path'); |
|
111 | - if ($path === '-') { |
|
112 | - $json = file_get_contents('php://stdin'); |
|
113 | - } else { |
|
114 | - if (!file_exists($path)) { |
|
115 | - $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
116 | - return 1; |
|
117 | - } |
|
118 | - $json = file_get_contents($path); |
|
119 | - } |
|
120 | - if (!is_string($json) || strlen($json) < 2) { |
|
121 | - $output->writeln('<error>Error while reading json</error>'); |
|
122 | - return 1; |
|
123 | - } |
|
124 | - $data = json_decode($json, true); |
|
125 | - if (!is_array($data)) { |
|
126 | - $output->writeln('<error>Error while parsing json</error>'); |
|
127 | - return 1; |
|
128 | - } |
|
129 | - |
|
130 | - $isLegacy = isset($data['user']) || isset($data['group']); |
|
131 | - if ($isLegacy) { |
|
132 | - $this->importLegacyStorageService->setData($data); |
|
133 | - $mounts = $this->importLegacyStorageService->getAllStorages(); |
|
134 | - foreach ($mounts as $mount) { |
|
135 | - if ($mount->getBackendOption('password') === false) { |
|
136 | - $output->writeln('<error>Failed to decrypt password</error>'); |
|
137 | - return 1; |
|
138 | - } |
|
139 | - } |
|
140 | - } else { |
|
141 | - if (!isset($data[0])) { //normalize to an array of mounts |
|
142 | - $data = [$data]; |
|
143 | - } |
|
144 | - $mounts = array_map([$this, 'parseData'], $data); |
|
145 | - } |
|
146 | - |
|
147 | - if ($user) { |
|
148 | - // ensure applicables are correct for personal mounts |
|
149 | - foreach ($mounts as $mount) { |
|
150 | - $mount->setApplicableGroups([]); |
|
151 | - $mount->setApplicableUsers([$user]); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - $storageService = $this->getStorageService($user); |
|
156 | - |
|
157 | - $existingMounts = $storageService->getAllStorages(); |
|
158 | - |
|
159 | - foreach ($mounts as $mount) { |
|
160 | - foreach ($existingMounts as $existingMount) { |
|
161 | - if ( |
|
162 | - $existingMount->getMountPoint() === $mount->getMountPoint() && |
|
163 | - $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && |
|
164 | - $existingMount->getApplicableUsers() == $mount->getApplicableUsers() && |
|
165 | - $existingMount->getBackendOptions() == $mount->getBackendOptions() |
|
166 | - ) { |
|
167 | - $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
168 | - return 1; |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - if ($input->getOption('dry')) { |
|
174 | - if (count($mounts) === 0) { |
|
175 | - $output->writeln('<error>No mounts to be imported</error>'); |
|
176 | - return 1; |
|
177 | - } |
|
178 | - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
179 | - $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
180 | - $listInput->setOption('output', $input->getOption('output')); |
|
181 | - $listInput->setOption('show-password', true); |
|
182 | - $listCommand->listMounts($user, $mounts, $listInput, $output); |
|
183 | - } else { |
|
184 | - foreach ($mounts as $mount) { |
|
185 | - $storageService->addStorage($mount); |
|
186 | - } |
|
187 | - } |
|
188 | - return 0; |
|
189 | - } |
|
190 | - |
|
191 | - private function parseData(array $data) { |
|
192 | - $mount = new StorageConfig($data['mount_id']); |
|
193 | - $mount->setMountPoint($data['mount_point']); |
|
194 | - $mount->setBackend($this->getBackendByClass($data['storage'])); |
|
195 | - $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); |
|
196 | - $mount->setAuthMechanism($authBackend); |
|
197 | - $mount->setBackendOptions($data['configuration']); |
|
198 | - $mount->setMountOptions($data['options']); |
|
199 | - $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); |
|
200 | - $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); |
|
201 | - return $mount; |
|
202 | - } |
|
203 | - |
|
204 | - private function getBackendByClass($className) { |
|
205 | - $backends = $this->backendService->getBackends(); |
|
206 | - foreach ($backends as $backend) { |
|
207 | - if ($backend->getStorageClass() === $className) { |
|
208 | - return $backend; |
|
209 | - } |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - protected function getStorageService($userId) { |
|
214 | - if (!empty($userId)) { |
|
215 | - $user = $this->userManager->get($userId); |
|
216 | - if (is_null($user)) { |
|
217 | - throw new NoUserException("user $userId not found"); |
|
218 | - } |
|
219 | - $this->userSession->setUser($user); |
|
220 | - return $this->userService; |
|
221 | - } else { |
|
222 | - return $this->globalService; |
|
223 | - } |
|
224 | - } |
|
42 | + /** |
|
43 | + * @var GlobalStoragesService |
|
44 | + */ |
|
45 | + private $globalService; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var UserStoragesService |
|
49 | + */ |
|
50 | + private $userService; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var IUserSession |
|
54 | + */ |
|
55 | + private $userSession; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var IUserManager |
|
59 | + */ |
|
60 | + private $userManager; |
|
61 | + |
|
62 | + /** @var ImportLegacyStoragesService */ |
|
63 | + private $importLegacyStorageService; |
|
64 | + |
|
65 | + /** @var BackendService */ |
|
66 | + private $backendService; |
|
67 | + |
|
68 | + function __construct(GlobalStoragesService $globalService, |
|
69 | + UserStoragesService $userService, |
|
70 | + IUserSession $userSession, |
|
71 | + IUserManager $userManager, |
|
72 | + ImportLegacyStoragesService $importLegacyStorageService, |
|
73 | + BackendService $backendService |
|
74 | + ) { |
|
75 | + parent::__construct(); |
|
76 | + $this->globalService = $globalService; |
|
77 | + $this->userService = $userService; |
|
78 | + $this->userSession = $userSession; |
|
79 | + $this->userManager = $userManager; |
|
80 | + $this->importLegacyStorageService = $importLegacyStorageService; |
|
81 | + $this->backendService = $backendService; |
|
82 | + } |
|
83 | + |
|
84 | + protected function configure() { |
|
85 | + $this |
|
86 | + ->setName('files_external:import') |
|
87 | + ->setDescription('Import mount configurations') |
|
88 | + ->addOption( |
|
89 | + 'user', |
|
90 | + null, |
|
91 | + InputOption::VALUE_OPTIONAL, |
|
92 | + 'user to add the mount configurations for, if not set the mount will be added as system mount' |
|
93 | + ) |
|
94 | + ->addArgument( |
|
95 | + 'path', |
|
96 | + InputArgument::REQUIRED, |
|
97 | + 'path to a json file containing the mounts to import, use "-" to read from stdin' |
|
98 | + ) |
|
99 | + ->addOption( |
|
100 | + 'dry', |
|
101 | + null, |
|
102 | + InputOption::VALUE_NONE, |
|
103 | + 'Don\'t save the imported mounts, only list the new mounts' |
|
104 | + ); |
|
105 | + parent::configure(); |
|
106 | + } |
|
107 | + |
|
108 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
109 | + $user = $input->getOption('user'); |
|
110 | + $path = $input->getArgument('path'); |
|
111 | + if ($path === '-') { |
|
112 | + $json = file_get_contents('php://stdin'); |
|
113 | + } else { |
|
114 | + if (!file_exists($path)) { |
|
115 | + $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
116 | + return 1; |
|
117 | + } |
|
118 | + $json = file_get_contents($path); |
|
119 | + } |
|
120 | + if (!is_string($json) || strlen($json) < 2) { |
|
121 | + $output->writeln('<error>Error while reading json</error>'); |
|
122 | + return 1; |
|
123 | + } |
|
124 | + $data = json_decode($json, true); |
|
125 | + if (!is_array($data)) { |
|
126 | + $output->writeln('<error>Error while parsing json</error>'); |
|
127 | + return 1; |
|
128 | + } |
|
129 | + |
|
130 | + $isLegacy = isset($data['user']) || isset($data['group']); |
|
131 | + if ($isLegacy) { |
|
132 | + $this->importLegacyStorageService->setData($data); |
|
133 | + $mounts = $this->importLegacyStorageService->getAllStorages(); |
|
134 | + foreach ($mounts as $mount) { |
|
135 | + if ($mount->getBackendOption('password') === false) { |
|
136 | + $output->writeln('<error>Failed to decrypt password</error>'); |
|
137 | + return 1; |
|
138 | + } |
|
139 | + } |
|
140 | + } else { |
|
141 | + if (!isset($data[0])) { //normalize to an array of mounts |
|
142 | + $data = [$data]; |
|
143 | + } |
|
144 | + $mounts = array_map([$this, 'parseData'], $data); |
|
145 | + } |
|
146 | + |
|
147 | + if ($user) { |
|
148 | + // ensure applicables are correct for personal mounts |
|
149 | + foreach ($mounts as $mount) { |
|
150 | + $mount->setApplicableGroups([]); |
|
151 | + $mount->setApplicableUsers([$user]); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + $storageService = $this->getStorageService($user); |
|
156 | + |
|
157 | + $existingMounts = $storageService->getAllStorages(); |
|
158 | + |
|
159 | + foreach ($mounts as $mount) { |
|
160 | + foreach ($existingMounts as $existingMount) { |
|
161 | + if ( |
|
162 | + $existingMount->getMountPoint() === $mount->getMountPoint() && |
|
163 | + $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && |
|
164 | + $existingMount->getApplicableUsers() == $mount->getApplicableUsers() && |
|
165 | + $existingMount->getBackendOptions() == $mount->getBackendOptions() |
|
166 | + ) { |
|
167 | + $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
168 | + return 1; |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + if ($input->getOption('dry')) { |
|
174 | + if (count($mounts) === 0) { |
|
175 | + $output->writeln('<error>No mounts to be imported</error>'); |
|
176 | + return 1; |
|
177 | + } |
|
178 | + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
179 | + $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
180 | + $listInput->setOption('output', $input->getOption('output')); |
|
181 | + $listInput->setOption('show-password', true); |
|
182 | + $listCommand->listMounts($user, $mounts, $listInput, $output); |
|
183 | + } else { |
|
184 | + foreach ($mounts as $mount) { |
|
185 | + $storageService->addStorage($mount); |
|
186 | + } |
|
187 | + } |
|
188 | + return 0; |
|
189 | + } |
|
190 | + |
|
191 | + private function parseData(array $data) { |
|
192 | + $mount = new StorageConfig($data['mount_id']); |
|
193 | + $mount->setMountPoint($data['mount_point']); |
|
194 | + $mount->setBackend($this->getBackendByClass($data['storage'])); |
|
195 | + $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); |
|
196 | + $mount->setAuthMechanism($authBackend); |
|
197 | + $mount->setBackendOptions($data['configuration']); |
|
198 | + $mount->setMountOptions($data['options']); |
|
199 | + $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); |
|
200 | + $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); |
|
201 | + return $mount; |
|
202 | + } |
|
203 | + |
|
204 | + private function getBackendByClass($className) { |
|
205 | + $backends = $this->backendService->getBackends(); |
|
206 | + foreach ($backends as $backend) { |
|
207 | + if ($backend->getStorageClass() === $className) { |
|
208 | + return $backend; |
|
209 | + } |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + protected function getStorageService($userId) { |
|
214 | + if (!empty($userId)) { |
|
215 | + $user = $this->userManager->get($userId); |
|
216 | + if (is_null($user)) { |
|
217 | + throw new NoUserException("user $userId not found"); |
|
218 | + } |
|
219 | + $this->userSession->setUser($user); |
|
220 | + return $this->userService; |
|
221 | + } else { |
|
222 | + return $this->globalService; |
|
223 | + } |
|
224 | + } |
|
225 | 225 | } |
@@ -32,65 +32,65 @@ |
||
32 | 32 | |
33 | 33 | class Admin implements ISettings { |
34 | 34 | |
35 | - /** @var IManager */ |
|
36 | - private $encryptionManager; |
|
35 | + /** @var IManager */ |
|
36 | + private $encryptionManager; |
|
37 | 37 | |
38 | - /** @var GlobalStoragesService */ |
|
39 | - private $globalStoragesService; |
|
38 | + /** @var GlobalStoragesService */ |
|
39 | + private $globalStoragesService; |
|
40 | 40 | |
41 | - /** @var BackendService */ |
|
42 | - private $backendService; |
|
41 | + /** @var BackendService */ |
|
42 | + private $backendService; |
|
43 | 43 | |
44 | - /** @var GlobalAuth */ |
|
45 | - private $globalAuth; |
|
44 | + /** @var GlobalAuth */ |
|
45 | + private $globalAuth; |
|
46 | 46 | |
47 | - public function __construct( |
|
48 | - IManager $encryptionManager, |
|
49 | - GlobalStoragesService $globalStoragesService, |
|
50 | - BackendService $backendService, |
|
51 | - GlobalAuth $globalAuth |
|
52 | - ) { |
|
53 | - $this->encryptionManager = $encryptionManager; |
|
54 | - $this->globalStoragesService = $globalStoragesService; |
|
55 | - $this->backendService = $backendService; |
|
56 | - $this->globalAuth = $globalAuth; |
|
57 | - } |
|
47 | + public function __construct( |
|
48 | + IManager $encryptionManager, |
|
49 | + GlobalStoragesService $globalStoragesService, |
|
50 | + BackendService $backendService, |
|
51 | + GlobalAuth $globalAuth |
|
52 | + ) { |
|
53 | + $this->encryptionManager = $encryptionManager; |
|
54 | + $this->globalStoragesService = $globalStoragesService; |
|
55 | + $this->backendService = $backendService; |
|
56 | + $this->globalAuth = $globalAuth; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @return TemplateResponse |
|
61 | - */ |
|
62 | - public function getForm() { |
|
63 | - $parameters = [ |
|
64 | - 'encryptionEnabled' => $this->encryptionManager->isEnabled(), |
|
65 | - 'visibilityType' => BackendService::VISIBILITY_ADMIN, |
|
66 | - 'storages' => $this->globalStoragesService->getStorages(), |
|
67 | - 'backends' => $this->backendService->getAvailableBackends(), |
|
68 | - 'authMechanisms' => $this->backendService->getAuthMechanisms(), |
|
69 | - 'dependencies' => \OC_Mount_Config::dependencyMessage($this->backendService->getBackends()), |
|
70 | - 'allowUserMounting' => $this->backendService->isUserMountingAllowed(), |
|
71 | - 'globalCredentials' => $this->globalAuth->getAuth(''), |
|
72 | - 'globalCredentialsUid' => '', |
|
73 | - ]; |
|
59 | + /** |
|
60 | + * @return TemplateResponse |
|
61 | + */ |
|
62 | + public function getForm() { |
|
63 | + $parameters = [ |
|
64 | + 'encryptionEnabled' => $this->encryptionManager->isEnabled(), |
|
65 | + 'visibilityType' => BackendService::VISIBILITY_ADMIN, |
|
66 | + 'storages' => $this->globalStoragesService->getStorages(), |
|
67 | + 'backends' => $this->backendService->getAvailableBackends(), |
|
68 | + 'authMechanisms' => $this->backendService->getAuthMechanisms(), |
|
69 | + 'dependencies' => \OC_Mount_Config::dependencyMessage($this->backendService->getBackends()), |
|
70 | + 'allowUserMounting' => $this->backendService->isUserMountingAllowed(), |
|
71 | + 'globalCredentials' => $this->globalAuth->getAuth(''), |
|
72 | + 'globalCredentialsUid' => '', |
|
73 | + ]; |
|
74 | 74 | |
75 | - return new TemplateResponse('files_external', 'settings', $parameters, ''); |
|
76 | - } |
|
75 | + return new TemplateResponse('files_external', 'settings', $parameters, ''); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return string the section ID, e.g. 'sharing' |
|
80 | - */ |
|
81 | - public function getSection() { |
|
82 | - return 'externalstorages'; |
|
83 | - } |
|
78 | + /** |
|
79 | + * @return string the section ID, e.g. 'sharing' |
|
80 | + */ |
|
81 | + public function getSection() { |
|
82 | + return 'externalstorages'; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * @return int whether the form should be rather on the top or bottom of |
|
87 | - * the admin section. The forms are arranged in ascending order of the |
|
88 | - * priority values. It is required to return a value between 0 and 100. |
|
89 | - * |
|
90 | - * E.g.: 70 |
|
91 | - */ |
|
92 | - public function getPriority() { |
|
93 | - return 40; |
|
94 | - } |
|
85 | + /** |
|
86 | + * @return int whether the form should be rather on the top or bottom of |
|
87 | + * the admin section. The forms are arranged in ascending order of the |
|
88 | + * priority values. It is required to return a value between 0 and 100. |
|
89 | + * |
|
90 | + * E.g.: 70 |
|
91 | + */ |
|
92 | + public function getPriority() { |
|
93 | + return 40; |
|
94 | + } |
|
95 | 95 | |
96 | 96 | } |
@@ -29,43 +29,43 @@ |
||
29 | 29 | */ |
30 | 30 | trait LegacyDependencyCheckPolyfill { |
31 | 31 | |
32 | - /** |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - abstract public function getStorageClass(); |
|
32 | + /** |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + abstract public function getStorageClass(); |
|
36 | 36 | |
37 | - /** |
|
38 | - * Check if object is valid for use |
|
39 | - * |
|
40 | - * @return MissingDependency[] Unsatisfied dependencies |
|
41 | - */ |
|
42 | - public function checkDependencies() { |
|
43 | - $ret = []; |
|
37 | + /** |
|
38 | + * Check if object is valid for use |
|
39 | + * |
|
40 | + * @return MissingDependency[] Unsatisfied dependencies |
|
41 | + */ |
|
42 | + public function checkDependencies() { |
|
43 | + $ret = []; |
|
44 | 44 | |
45 | - $result = call_user_func([$this->getStorageClass(), 'checkDependencies']); |
|
46 | - if ($result !== true) { |
|
47 | - if (!is_array($result)) { |
|
48 | - $result = [$result]; |
|
49 | - } |
|
50 | - foreach ($result as $key => $value) { |
|
51 | - if (!($value instanceof MissingDependency)) { |
|
52 | - $module = null; |
|
53 | - $message = null; |
|
54 | - if (is_numeric($key)) { |
|
55 | - $module = $value; |
|
56 | - } else { |
|
57 | - $module = $key; |
|
58 | - $message = $value; |
|
59 | - } |
|
60 | - $value = new MissingDependency($module, $this); |
|
61 | - $value->setMessage($message); |
|
62 | - } |
|
63 | - $ret[] = $value; |
|
64 | - } |
|
65 | - } |
|
45 | + $result = call_user_func([$this->getStorageClass(), 'checkDependencies']); |
|
46 | + if ($result !== true) { |
|
47 | + if (!is_array($result)) { |
|
48 | + $result = [$result]; |
|
49 | + } |
|
50 | + foreach ($result as $key => $value) { |
|
51 | + if (!($value instanceof MissingDependency)) { |
|
52 | + $module = null; |
|
53 | + $message = null; |
|
54 | + if (is_numeric($key)) { |
|
55 | + $module = $value; |
|
56 | + } else { |
|
57 | + $module = $key; |
|
58 | + $message = $value; |
|
59 | + } |
|
60 | + $value = new MissingDependency($module, $this); |
|
61 | + $value->setMessage($message); |
|
62 | + } |
|
63 | + $ret[] = $value; |
|
64 | + } |
|
65 | + } |
|
66 | 66 | |
67 | - return $ret; |
|
68 | - } |
|
67 | + return $ret; |
|
68 | + } |
|
69 | 69 | |
70 | 70 | } |
71 | 71 |
@@ -36,102 +36,102 @@ |
||
36 | 36 | */ |
37 | 37 | trait VisibilityTrait { |
38 | 38 | |
39 | - /** @var int visibility */ |
|
40 | - protected $visibility = BackendService::VISIBILITY_DEFAULT; |
|
41 | - |
|
42 | - /** @var int allowed visibilities */ |
|
43 | - protected $allowedVisibility = BackendService::VISIBILITY_DEFAULT; |
|
44 | - |
|
45 | - /** |
|
46 | - * @return int |
|
47 | - */ |
|
48 | - public function getVisibility() { |
|
49 | - return $this->visibility; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Check if the backend is visible for a user type |
|
54 | - * |
|
55 | - * @param int $visibility |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public function isVisibleFor($visibility) { |
|
59 | - if ($this->visibility & $visibility) { |
|
60 | - return true; |
|
61 | - } |
|
62 | - return false; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @param int $visibility |
|
67 | - * @return self |
|
68 | - */ |
|
69 | - public function setVisibility($visibility) { |
|
70 | - $this->visibility = $visibility; |
|
71 | - $this->allowedVisibility |= $visibility; |
|
72 | - return $this; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param int $visibility |
|
77 | - * @return self |
|
78 | - */ |
|
79 | - public function addVisibility($visibility) { |
|
80 | - return $this->setVisibility($this->visibility | $visibility); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @param int $visibility |
|
85 | - * @return self |
|
86 | - */ |
|
87 | - public function removeVisibility($visibility) { |
|
88 | - return $this->setVisibility($this->visibility & ~$visibility); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @return int |
|
93 | - */ |
|
94 | - public function getAllowedVisibility() { |
|
95 | - return $this->allowedVisibility; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Check if the backend is allowed to be visible for a user type |
|
100 | - * |
|
101 | - * @param int $allowedVisibility |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public function isAllowedVisibleFor($allowedVisibility) { |
|
105 | - if ($this->allowedVisibility & $allowedVisibility) { |
|
106 | - return true; |
|
107 | - } |
|
108 | - return false; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param int $allowedVisibility |
|
113 | - * @return self |
|
114 | - */ |
|
115 | - public function setAllowedVisibility($allowedVisibility) { |
|
116 | - $this->allowedVisibility = $allowedVisibility; |
|
117 | - $this->visibility &= $allowedVisibility; |
|
118 | - return $this; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @param int $allowedVisibility |
|
123 | - * @return self |
|
124 | - */ |
|
125 | - public function addAllowedVisibility($allowedVisibility) { |
|
126 | - return $this->setAllowedVisibility($this->allowedVisibility | $allowedVisibility); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param int $allowedVisibility |
|
131 | - * @return self |
|
132 | - */ |
|
133 | - public function removeAllowedVisibility($allowedVisibility) { |
|
134 | - return $this->setAllowedVisibility($this->allowedVisibility & ~$allowedVisibility); |
|
135 | - } |
|
39 | + /** @var int visibility */ |
|
40 | + protected $visibility = BackendService::VISIBILITY_DEFAULT; |
|
41 | + |
|
42 | + /** @var int allowed visibilities */ |
|
43 | + protected $allowedVisibility = BackendService::VISIBILITY_DEFAULT; |
|
44 | + |
|
45 | + /** |
|
46 | + * @return int |
|
47 | + */ |
|
48 | + public function getVisibility() { |
|
49 | + return $this->visibility; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Check if the backend is visible for a user type |
|
54 | + * |
|
55 | + * @param int $visibility |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public function isVisibleFor($visibility) { |
|
59 | + if ($this->visibility & $visibility) { |
|
60 | + return true; |
|
61 | + } |
|
62 | + return false; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @param int $visibility |
|
67 | + * @return self |
|
68 | + */ |
|
69 | + public function setVisibility($visibility) { |
|
70 | + $this->visibility = $visibility; |
|
71 | + $this->allowedVisibility |= $visibility; |
|
72 | + return $this; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param int $visibility |
|
77 | + * @return self |
|
78 | + */ |
|
79 | + public function addVisibility($visibility) { |
|
80 | + return $this->setVisibility($this->visibility | $visibility); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @param int $visibility |
|
85 | + * @return self |
|
86 | + */ |
|
87 | + public function removeVisibility($visibility) { |
|
88 | + return $this->setVisibility($this->visibility & ~$visibility); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @return int |
|
93 | + */ |
|
94 | + public function getAllowedVisibility() { |
|
95 | + return $this->allowedVisibility; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Check if the backend is allowed to be visible for a user type |
|
100 | + * |
|
101 | + * @param int $allowedVisibility |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public function isAllowedVisibleFor($allowedVisibility) { |
|
105 | + if ($this->allowedVisibility & $allowedVisibility) { |
|
106 | + return true; |
|
107 | + } |
|
108 | + return false; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param int $allowedVisibility |
|
113 | + * @return self |
|
114 | + */ |
|
115 | + public function setAllowedVisibility($allowedVisibility) { |
|
116 | + $this->allowedVisibility = $allowedVisibility; |
|
117 | + $this->visibility &= $allowedVisibility; |
|
118 | + return $this; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @param int $allowedVisibility |
|
123 | + * @return self |
|
124 | + */ |
|
125 | + public function addAllowedVisibility($allowedVisibility) { |
|
126 | + return $this->setAllowedVisibility($this->allowedVisibility | $allowedVisibility); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param int $allowedVisibility |
|
131 | + * @return self |
|
132 | + */ |
|
133 | + public function removeAllowedVisibility($allowedVisibility) { |
|
134 | + return $this->setAllowedVisibility($this->allowedVisibility & ~$allowedVisibility); |
|
135 | + } |
|
136 | 136 | |
137 | 137 | } |
@@ -28,167 +28,167 @@ |
||
28 | 28 | */ |
29 | 29 | class DefinitionParameter implements \JsonSerializable { |
30 | 30 | |
31 | - /** Value constants */ |
|
32 | - const VALUE_TEXT = 0; |
|
33 | - const VALUE_BOOLEAN = 1; |
|
34 | - const VALUE_PASSWORD = 2; |
|
35 | - const VALUE_HIDDEN = 3; |
|
36 | - |
|
37 | - /** Flag constants */ |
|
38 | - const FLAG_NONE = 0; |
|
39 | - const FLAG_OPTIONAL = 1; |
|
40 | - const FLAG_USER_PROVIDED = 2; |
|
41 | - |
|
42 | - /** @var string name of parameter */ |
|
43 | - private $name; |
|
44 | - |
|
45 | - /** @var string human-readable parameter text */ |
|
46 | - private $text; |
|
47 | - |
|
48 | - /** @var int value type, see self::VALUE_* constants */ |
|
49 | - private $type = self::VALUE_TEXT; |
|
50 | - |
|
51 | - /** @var int flags, see self::FLAG_* constants */ |
|
52 | - private $flags = self::FLAG_NONE; |
|
53 | - |
|
54 | - /** |
|
55 | - * @param string $name |
|
56 | - * @param string $text |
|
57 | - */ |
|
58 | - public function __construct($name, $text) { |
|
59 | - $this->name = $name; |
|
60 | - $this->text = $text; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - public function getName() { |
|
67 | - return $this->name; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @return string |
|
72 | - */ |
|
73 | - public function getText() { |
|
74 | - return $this->text; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Get value type |
|
79 | - * |
|
80 | - * @return int |
|
81 | - */ |
|
82 | - public function getType() { |
|
83 | - return $this->type; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Set value type |
|
88 | - * |
|
89 | - * @param int $type |
|
90 | - * @return self |
|
91 | - */ |
|
92 | - public function setType($type) { |
|
93 | - $this->type = $type; |
|
94 | - return $this; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - public function getTypeName() { |
|
101 | - switch ($this->type) { |
|
102 | - case self::VALUE_BOOLEAN: |
|
103 | - return 'boolean'; |
|
104 | - case self::VALUE_TEXT: |
|
105 | - return 'text'; |
|
106 | - case self::VALUE_PASSWORD: |
|
107 | - return 'password'; |
|
108 | - default: |
|
109 | - return 'unknown'; |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @return int |
|
115 | - */ |
|
116 | - public function getFlags() { |
|
117 | - return $this->flags; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @param int $flags |
|
122 | - * @return self |
|
123 | - */ |
|
124 | - public function setFlags($flags) { |
|
125 | - $this->flags = $flags; |
|
126 | - return $this; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @param int $flag |
|
131 | - * @return self |
|
132 | - */ |
|
133 | - public function setFlag($flag) { |
|
134 | - $this->flags |= $flag; |
|
135 | - return $this; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @param int $flag |
|
140 | - * @return bool |
|
141 | - */ |
|
142 | - public function isFlagSet($flag) { |
|
143 | - return (bool)($this->flags & $flag); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Serialize into JSON for client-side JS |
|
148 | - * |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function jsonSerialize() { |
|
152 | - return [ |
|
153 | - 'value' => $this->getText(), |
|
154 | - 'flags' => $this->getFlags(), |
|
155 | - 'type' => $this->getType() |
|
156 | - ]; |
|
157 | - } |
|
158 | - |
|
159 | - public function isOptional() { |
|
160 | - return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Validate a parameter value against this |
|
165 | - * Convert type as necessary |
|
166 | - * |
|
167 | - * @param mixed $value Value to check |
|
168 | - * @return bool success |
|
169 | - */ |
|
170 | - public function validateValue(&$value) { |
|
171 | - switch ($this->getType()) { |
|
172 | - case self::VALUE_BOOLEAN: |
|
173 | - if (!is_bool($value)) { |
|
174 | - switch ($value) { |
|
175 | - case 'true': |
|
176 | - $value = true; |
|
177 | - break; |
|
178 | - case 'false': |
|
179 | - $value = false; |
|
180 | - break; |
|
181 | - default: |
|
182 | - return false; |
|
183 | - } |
|
184 | - } |
|
185 | - break; |
|
186 | - default: |
|
187 | - if (!$value && !$this->isOptional()) { |
|
188 | - return false; |
|
189 | - } |
|
190 | - break; |
|
191 | - } |
|
192 | - return true; |
|
193 | - } |
|
31 | + /** Value constants */ |
|
32 | + const VALUE_TEXT = 0; |
|
33 | + const VALUE_BOOLEAN = 1; |
|
34 | + const VALUE_PASSWORD = 2; |
|
35 | + const VALUE_HIDDEN = 3; |
|
36 | + |
|
37 | + /** Flag constants */ |
|
38 | + const FLAG_NONE = 0; |
|
39 | + const FLAG_OPTIONAL = 1; |
|
40 | + const FLAG_USER_PROVIDED = 2; |
|
41 | + |
|
42 | + /** @var string name of parameter */ |
|
43 | + private $name; |
|
44 | + |
|
45 | + /** @var string human-readable parameter text */ |
|
46 | + private $text; |
|
47 | + |
|
48 | + /** @var int value type, see self::VALUE_* constants */ |
|
49 | + private $type = self::VALUE_TEXT; |
|
50 | + |
|
51 | + /** @var int flags, see self::FLAG_* constants */ |
|
52 | + private $flags = self::FLAG_NONE; |
|
53 | + |
|
54 | + /** |
|
55 | + * @param string $name |
|
56 | + * @param string $text |
|
57 | + */ |
|
58 | + public function __construct($name, $text) { |
|
59 | + $this->name = $name; |
|
60 | + $this->text = $text; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + public function getName() { |
|
67 | + return $this->name; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @return string |
|
72 | + */ |
|
73 | + public function getText() { |
|
74 | + return $this->text; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Get value type |
|
79 | + * |
|
80 | + * @return int |
|
81 | + */ |
|
82 | + public function getType() { |
|
83 | + return $this->type; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Set value type |
|
88 | + * |
|
89 | + * @param int $type |
|
90 | + * @return self |
|
91 | + */ |
|
92 | + public function setType($type) { |
|
93 | + $this->type = $type; |
|
94 | + return $this; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + public function getTypeName() { |
|
101 | + switch ($this->type) { |
|
102 | + case self::VALUE_BOOLEAN: |
|
103 | + return 'boolean'; |
|
104 | + case self::VALUE_TEXT: |
|
105 | + return 'text'; |
|
106 | + case self::VALUE_PASSWORD: |
|
107 | + return 'password'; |
|
108 | + default: |
|
109 | + return 'unknown'; |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @return int |
|
115 | + */ |
|
116 | + public function getFlags() { |
|
117 | + return $this->flags; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @param int $flags |
|
122 | + * @return self |
|
123 | + */ |
|
124 | + public function setFlags($flags) { |
|
125 | + $this->flags = $flags; |
|
126 | + return $this; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @param int $flag |
|
131 | + * @return self |
|
132 | + */ |
|
133 | + public function setFlag($flag) { |
|
134 | + $this->flags |= $flag; |
|
135 | + return $this; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @param int $flag |
|
140 | + * @return bool |
|
141 | + */ |
|
142 | + public function isFlagSet($flag) { |
|
143 | + return (bool)($this->flags & $flag); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Serialize into JSON for client-side JS |
|
148 | + * |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function jsonSerialize() { |
|
152 | + return [ |
|
153 | + 'value' => $this->getText(), |
|
154 | + 'flags' => $this->getFlags(), |
|
155 | + 'type' => $this->getType() |
|
156 | + ]; |
|
157 | + } |
|
158 | + |
|
159 | + public function isOptional() { |
|
160 | + return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Validate a parameter value against this |
|
165 | + * Convert type as necessary |
|
166 | + * |
|
167 | + * @param mixed $value Value to check |
|
168 | + * @return bool success |
|
169 | + */ |
|
170 | + public function validateValue(&$value) { |
|
171 | + switch ($this->getType()) { |
|
172 | + case self::VALUE_BOOLEAN: |
|
173 | + if (!is_bool($value)) { |
|
174 | + switch ($value) { |
|
175 | + case 'true': |
|
176 | + $value = true; |
|
177 | + break; |
|
178 | + case 'false': |
|
179 | + $value = false; |
|
180 | + break; |
|
181 | + default: |
|
182 | + return false; |
|
183 | + } |
|
184 | + } |
|
185 | + break; |
|
186 | + default: |
|
187 | + if (!$value && !$this->isOptional()) { |
|
188 | + return false; |
|
189 | + } |
|
190 | + break; |
|
191 | + } |
|
192 | + return true; |
|
193 | + } |
|
194 | 194 | } |