@@ -35,122 +35,122 @@ |
||
35 | 35 | use Symfony\Component\Console\Output\OutputInterface; |
36 | 36 | |
37 | 37 | class Applicable extends Base { |
38 | - /** |
|
39 | - * @var GlobalStoragesService |
|
40 | - */ |
|
41 | - protected $globalService; |
|
38 | + /** |
|
39 | + * @var GlobalStoragesService |
|
40 | + */ |
|
41 | + protected $globalService; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @var IUserManager |
|
45 | - */ |
|
46 | - private $userManager; |
|
43 | + /** |
|
44 | + * @var IUserManager |
|
45 | + */ |
|
46 | + private $userManager; |
|
47 | 47 | |
48 | - /** |
|
49 | - * @var IGroupManager |
|
50 | - */ |
|
51 | - private $groupManager; |
|
48 | + /** |
|
49 | + * @var IGroupManager |
|
50 | + */ |
|
51 | + private $groupManager; |
|
52 | 52 | |
53 | - function __construct( |
|
54 | - GlobalStoragesService $globalService, |
|
55 | - IUserManager $userManager, |
|
56 | - IGroupManager $groupManager |
|
57 | - ) { |
|
58 | - parent::__construct(); |
|
59 | - $this->globalService = $globalService; |
|
60 | - $this->userManager = $userManager; |
|
61 | - $this->groupManager = $groupManager; |
|
62 | - } |
|
53 | + function __construct( |
|
54 | + GlobalStoragesService $globalService, |
|
55 | + IUserManager $userManager, |
|
56 | + IGroupManager $groupManager |
|
57 | + ) { |
|
58 | + parent::__construct(); |
|
59 | + $this->globalService = $globalService; |
|
60 | + $this->userManager = $userManager; |
|
61 | + $this->groupManager = $groupManager; |
|
62 | + } |
|
63 | 63 | |
64 | - protected function configure() { |
|
65 | - $this |
|
66 | - ->setName('files_external:applicable') |
|
67 | - ->setDescription('Manage applicable users and groups for a mount') |
|
68 | - ->addArgument( |
|
69 | - 'mount_id', |
|
70 | - InputArgument::REQUIRED, |
|
71 | - 'The id of the mount to edit' |
|
72 | - )->addOption( |
|
73 | - 'add-user', |
|
74 | - '', |
|
75 | - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
76 | - 'user to add as applicable' |
|
77 | - )->addOption( |
|
78 | - 'remove-user', |
|
79 | - '', |
|
80 | - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
81 | - 'user to remove as applicable' |
|
82 | - )->addOption( |
|
83 | - 'add-group', |
|
84 | - '', |
|
85 | - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
86 | - 'group to add as applicable' |
|
87 | - )->addOption( |
|
88 | - 'remove-group', |
|
89 | - '', |
|
90 | - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
91 | - 'group to remove as applicable' |
|
92 | - )->addOption( |
|
93 | - 'remove-all', |
|
94 | - '', |
|
95 | - InputOption::VALUE_NONE, |
|
96 | - 'Set the mount to be globally applicable' |
|
97 | - ); |
|
98 | - parent::configure(); |
|
99 | - } |
|
64 | + protected function configure() { |
|
65 | + $this |
|
66 | + ->setName('files_external:applicable') |
|
67 | + ->setDescription('Manage applicable users and groups for a mount') |
|
68 | + ->addArgument( |
|
69 | + 'mount_id', |
|
70 | + InputArgument::REQUIRED, |
|
71 | + 'The id of the mount to edit' |
|
72 | + )->addOption( |
|
73 | + 'add-user', |
|
74 | + '', |
|
75 | + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
76 | + 'user to add as applicable' |
|
77 | + )->addOption( |
|
78 | + 'remove-user', |
|
79 | + '', |
|
80 | + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
81 | + 'user to remove as applicable' |
|
82 | + )->addOption( |
|
83 | + 'add-group', |
|
84 | + '', |
|
85 | + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
86 | + 'group to add as applicable' |
|
87 | + )->addOption( |
|
88 | + 'remove-group', |
|
89 | + '', |
|
90 | + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
91 | + 'group to remove as applicable' |
|
92 | + )->addOption( |
|
93 | + 'remove-all', |
|
94 | + '', |
|
95 | + InputOption::VALUE_NONE, |
|
96 | + 'Set the mount to be globally applicable' |
|
97 | + ); |
|
98 | + parent::configure(); |
|
99 | + } |
|
100 | 100 | |
101 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
102 | - $mountId = $input->getArgument('mount_id'); |
|
103 | - try { |
|
104 | - $mount = $this->globalService->getStorage($mountId); |
|
105 | - } catch (NotFoundException $e) { |
|
106 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>'); |
|
107 | - return 404; |
|
108 | - } |
|
101 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
102 | + $mountId = $input->getArgument('mount_id'); |
|
103 | + try { |
|
104 | + $mount = $this->globalService->getStorage($mountId); |
|
105 | + } catch (NotFoundException $e) { |
|
106 | + $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>'); |
|
107 | + return 404; |
|
108 | + } |
|
109 | 109 | |
110 | - if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { |
|
111 | - $output->writeln('<error>Can\'t change applicables on personal mounts</error>'); |
|
112 | - return 1; |
|
113 | - } |
|
110 | + if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { |
|
111 | + $output->writeln('<error>Can\'t change applicables on personal mounts</error>'); |
|
112 | + return 1; |
|
113 | + } |
|
114 | 114 | |
115 | - $addUsers = $input->getOption('add-user'); |
|
116 | - $removeUsers = $input->getOption('remove-user'); |
|
117 | - $addGroups = $input->getOption('add-group'); |
|
118 | - $removeGroups = $input->getOption('remove-group'); |
|
115 | + $addUsers = $input->getOption('add-user'); |
|
116 | + $removeUsers = $input->getOption('remove-user'); |
|
117 | + $addGroups = $input->getOption('add-group'); |
|
118 | + $removeGroups = $input->getOption('remove-group'); |
|
119 | 119 | |
120 | - $applicableUsers = $mount->getApplicableUsers(); |
|
121 | - $applicableGroups = $mount->getApplicableGroups(); |
|
120 | + $applicableUsers = $mount->getApplicableUsers(); |
|
121 | + $applicableGroups = $mount->getApplicableGroups(); |
|
122 | 122 | |
123 | - if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) { |
|
124 | - foreach ($addUsers as $addUser) { |
|
125 | - if (!$this->userManager->userExists($addUser)) { |
|
126 | - $output->writeln('<error>User "' . $addUser . '" not found</error>'); |
|
127 | - return 404; |
|
128 | - } |
|
129 | - } |
|
130 | - foreach ($addGroups as $addGroup) { |
|
131 | - if (!$this->groupManager->groupExists($addGroup)) { |
|
132 | - $output->writeln('<error>Group "' . $addGroup . '" not found</error>'); |
|
133 | - return 404; |
|
134 | - } |
|
135 | - } |
|
123 | + if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) { |
|
124 | + foreach ($addUsers as $addUser) { |
|
125 | + if (!$this->userManager->userExists($addUser)) { |
|
126 | + $output->writeln('<error>User "' . $addUser . '" not found</error>'); |
|
127 | + return 404; |
|
128 | + } |
|
129 | + } |
|
130 | + foreach ($addGroups as $addGroup) { |
|
131 | + if (!$this->groupManager->groupExists($addGroup)) { |
|
132 | + $output->writeln('<error>Group "' . $addGroup . '" not found</error>'); |
|
133 | + return 404; |
|
134 | + } |
|
135 | + } |
|
136 | 136 | |
137 | - if ($input->getOption('remove-all')) { |
|
138 | - $applicableUsers = []; |
|
139 | - $applicableGroups = []; |
|
140 | - } else { |
|
141 | - $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers)); |
|
142 | - $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers)); |
|
143 | - $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups)); |
|
144 | - $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups)); |
|
145 | - } |
|
146 | - $mount->setApplicableUsers($applicableUsers); |
|
147 | - $mount->setApplicableGroups($applicableGroups); |
|
148 | - $this->globalService->updateStorage($mount); |
|
149 | - } |
|
137 | + if ($input->getOption('remove-all')) { |
|
138 | + $applicableUsers = []; |
|
139 | + $applicableGroups = []; |
|
140 | + } else { |
|
141 | + $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers)); |
|
142 | + $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers)); |
|
143 | + $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups)); |
|
144 | + $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups)); |
|
145 | + } |
|
146 | + $mount->setApplicableUsers($applicableUsers); |
|
147 | + $mount->setApplicableGroups($applicableGroups); |
|
148 | + $this->globalService->updateStorage($mount); |
|
149 | + } |
|
150 | 150 | |
151 | - $this->writeArrayInOutputFormat($input, $output, [ |
|
152 | - 'users' => $applicableUsers, |
|
153 | - 'groups' => $applicableGroups |
|
154 | - ]); |
|
155 | - } |
|
151 | + $this->writeArrayInOutputFormat($input, $output, [ |
|
152 | + 'users' => $applicableUsers, |
|
153 | + 'groups' => $applicableGroups |
|
154 | + ]); |
|
155 | + } |
|
156 | 156 | } |
@@ -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 | - '', |
|
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 | - '', |
|
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((string)$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 | + '', |
|
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 | + '', |
|
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((string)$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 | } |
@@ -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 null; |
|
107 | - } |
|
108 | - } |
|
105 | + if (!$questionHelper->ask($input, $output, $question)) { |
|
106 | + return null; |
|
107 | + } |
|
108 | + } |
|
109 | 109 | |
110 | - $this->globalService->removeStorage($mountId); |
|
111 | - } |
|
110 | + $this->globalService->removeStorage($mountId); |
|
111 | + } |
|
112 | 112 | } |
@@ -34,318 +34,318 @@ |
||
34 | 34 | |
35 | 35 | class Manager implements IManager { |
36 | 36 | |
37 | - /** @var IStorage */ |
|
38 | - protected $storage; |
|
39 | - |
|
40 | - /** @var string */ |
|
41 | - protected $path; |
|
42 | - |
|
43 | - /** @var array[] */ |
|
44 | - protected $operations = []; |
|
45 | - |
|
46 | - /** @var array[] */ |
|
47 | - protected $checks = []; |
|
48 | - |
|
49 | - /** @var IDBConnection */ |
|
50 | - protected $connection; |
|
51 | - |
|
52 | - /** @var IServerContainer|\OC\Server */ |
|
53 | - protected $container; |
|
54 | - |
|
55 | - /** @var IL10N */ |
|
56 | - protected $l; |
|
57 | - |
|
58 | - /** |
|
59 | - * @param IDBConnection $connection |
|
60 | - * @param IServerContainer $container |
|
61 | - * @param IL10N $l |
|
62 | - */ |
|
63 | - public function __construct(IDBConnection $connection, IServerContainer $container, IL10N $l) { |
|
64 | - $this->connection = $connection; |
|
65 | - $this->container = $container; |
|
66 | - $this->l = $l; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @inheritdoc |
|
71 | - */ |
|
72 | - public function setFileInfo(IStorage $storage, $path) { |
|
73 | - $this->storage = $storage; |
|
74 | - $this->path = $path; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @inheritdoc |
|
79 | - */ |
|
80 | - public function getMatchingOperations($class, $returnFirstMatchingOperationOnly = true) { |
|
81 | - $operations = $this->getOperations($class); |
|
82 | - |
|
83 | - $matches = []; |
|
84 | - foreach ($operations as $operation) { |
|
85 | - $checkIds = json_decode($operation['checks'], true); |
|
86 | - $checks = $this->getChecks($checkIds); |
|
87 | - |
|
88 | - foreach ($checks as $check) { |
|
89 | - if (!$this->check($check)) { |
|
90 | - // Check did not match, continue with the next operation |
|
91 | - continue 2; |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - if ($returnFirstMatchingOperationOnly) { |
|
96 | - return $operation; |
|
97 | - } |
|
98 | - $matches[] = $operation; |
|
99 | - } |
|
100 | - |
|
101 | - return $matches; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @param array $check |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - protected function check(array $check) { |
|
109 | - try { |
|
110 | - $checkInstance = $this->container->query($check['class']); |
|
111 | - } catch (QueryException $e) { |
|
112 | - // Check does not exist, assume it matches. |
|
113 | - return true; |
|
114 | - } |
|
115 | - |
|
116 | - if ($checkInstance instanceof ICheck) { |
|
117 | - $checkInstance->setFileInfo($this->storage, $this->path); |
|
118 | - return $checkInstance->executeCheck($check['operator'], $check['value']); |
|
119 | - } else { |
|
120 | - // Check is invalid |
|
121 | - throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class'])); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $class |
|
127 | - * @return array[] |
|
128 | - */ |
|
129 | - public function getOperations($class) { |
|
130 | - if (isset($this->operations[$class])) { |
|
131 | - return $this->operations[$class]; |
|
132 | - } |
|
133 | - |
|
134 | - $query = $this->connection->getQueryBuilder(); |
|
135 | - |
|
136 | - $query->select('*') |
|
137 | - ->from('flow_operations') |
|
138 | - ->where($query->expr()->eq('class', $query->createNamedParameter($class))); |
|
139 | - $result = $query->execute(); |
|
140 | - |
|
141 | - $this->operations[$class] = []; |
|
142 | - while ($row = $result->fetch()) { |
|
143 | - $this->operations[$class][] = $row; |
|
144 | - } |
|
145 | - $result->closeCursor(); |
|
146 | - |
|
147 | - return $this->operations[$class]; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param int $id |
|
152 | - * @return array |
|
153 | - * @throws \UnexpectedValueException |
|
154 | - */ |
|
155 | - protected function getOperation($id) { |
|
156 | - $query = $this->connection->getQueryBuilder(); |
|
157 | - $query->select('*') |
|
158 | - ->from('flow_operations') |
|
159 | - ->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
|
160 | - $result = $query->execute(); |
|
161 | - $row = $result->fetch(); |
|
162 | - $result->closeCursor(); |
|
163 | - |
|
164 | - if ($row) { |
|
165 | - return $row; |
|
166 | - } |
|
167 | - |
|
168 | - throw new \UnexpectedValueException($this->l->t('Operation #%s does not exist', [$id])); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @param string $class |
|
173 | - * @param string $name |
|
174 | - * @param array[] $checks |
|
175 | - * @param string $operation |
|
176 | - * @return array The added operation |
|
177 | - * @throws \UnexpectedValueException |
|
178 | - */ |
|
179 | - public function addOperation($class, $name, array $checks, $operation) { |
|
180 | - $this->validateOperation($class, $name, $checks, $operation); |
|
181 | - |
|
182 | - $checkIds = []; |
|
183 | - foreach ($checks as $check) { |
|
184 | - $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']); |
|
185 | - } |
|
186 | - |
|
187 | - $query = $this->connection->getQueryBuilder(); |
|
188 | - $query->insert('flow_operations') |
|
189 | - ->values([ |
|
190 | - 'class' => $query->createNamedParameter($class), |
|
191 | - 'name' => $query->createNamedParameter($name), |
|
192 | - 'checks' => $query->createNamedParameter(json_encode(array_unique($checkIds))), |
|
193 | - 'operation' => $query->createNamedParameter($operation), |
|
194 | - ]); |
|
195 | - $query->execute(); |
|
196 | - |
|
197 | - $id = $query->getLastInsertId(); |
|
198 | - return $this->getOperation($id); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * @param int $id |
|
203 | - * @param string $name |
|
204 | - * @param array[] $checks |
|
205 | - * @param string $operation |
|
206 | - * @return array The updated operation |
|
207 | - * @throws \UnexpectedValueException |
|
208 | - */ |
|
209 | - public function updateOperation($id, $name, array $checks, $operation) { |
|
210 | - $row = $this->getOperation($id); |
|
211 | - $this->validateOperation($row['class'], $name, $checks, $operation); |
|
212 | - |
|
213 | - $checkIds = []; |
|
214 | - foreach ($checks as $check) { |
|
215 | - $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']); |
|
216 | - } |
|
217 | - |
|
218 | - $query = $this->connection->getQueryBuilder(); |
|
219 | - $query->update('flow_operations') |
|
220 | - ->set('name', $query->createNamedParameter($name)) |
|
221 | - ->set('checks', $query->createNamedParameter(json_encode(array_unique($checkIds)))) |
|
222 | - ->set('operation', $query->createNamedParameter($operation)) |
|
223 | - ->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
|
224 | - $query->execute(); |
|
225 | - |
|
226 | - return $this->getOperation($id); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * @param int $id |
|
231 | - * @return bool |
|
232 | - * @throws \UnexpectedValueException |
|
233 | - */ |
|
234 | - public function deleteOperation($id) { |
|
235 | - $query = $this->connection->getQueryBuilder(); |
|
236 | - $query->delete('flow_operations') |
|
237 | - ->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
|
238 | - return (bool) $query->execute(); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * @param string $class |
|
243 | - * @param string $name |
|
244 | - * @param array[] $checks |
|
245 | - * @param string $operation |
|
246 | - * @throws \UnexpectedValueException |
|
247 | - */ |
|
248 | - protected function validateOperation($class, $name, array $checks, $operation) { |
|
249 | - try { |
|
250 | - /** @var IOperation $instance */ |
|
251 | - $instance = $this->container->query($class); |
|
252 | - } catch (QueryException $e) { |
|
253 | - throw new \UnexpectedValueException($this->l->t('Operation %s does not exist', [$class])); |
|
254 | - } |
|
255 | - |
|
256 | - if (!($instance instanceof IOperation)) { |
|
257 | - throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class])); |
|
258 | - } |
|
259 | - |
|
260 | - $instance->validateOperation($name, $checks, $operation); |
|
261 | - |
|
262 | - foreach ($checks as $check) { |
|
263 | - try { |
|
264 | - /** @var ICheck $instance */ |
|
265 | - $instance = $this->container->query($check['class']); |
|
266 | - } catch (QueryException $e) { |
|
267 | - throw new \UnexpectedValueException($this->l->t('Check %s does not exist', [$class])); |
|
268 | - } |
|
269 | - |
|
270 | - if (!($instance instanceof ICheck)) { |
|
271 | - throw new \UnexpectedValueException($this->l->t('Check %s is invalid', [$class])); |
|
272 | - } |
|
273 | - |
|
274 | - $instance->validateCheck($check['operator'], $check['value']); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param int[] $checkIds |
|
280 | - * @return array[] |
|
281 | - */ |
|
282 | - public function getChecks(array $checkIds) { |
|
283 | - $checkIds = array_map('intval', $checkIds); |
|
284 | - |
|
285 | - $checks = []; |
|
286 | - foreach ($checkIds as $i => $checkId) { |
|
287 | - if (isset($this->checks[$checkId])) { |
|
288 | - $checks[$checkId] = $this->checks[$checkId]; |
|
289 | - unset($checkIds[$i]); |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - if (empty($checkIds)) { |
|
294 | - return $checks; |
|
295 | - } |
|
296 | - |
|
297 | - $query = $this->connection->getQueryBuilder(); |
|
298 | - $query->select('*') |
|
299 | - ->from('flow_checks') |
|
300 | - ->where($query->expr()->in('id', $query->createNamedParameter($checkIds, IQueryBuilder::PARAM_INT_ARRAY))); |
|
301 | - $result = $query->execute(); |
|
302 | - |
|
303 | - while ($row = $result->fetch()) { |
|
304 | - $this->checks[(int) $row['id']] = $row; |
|
305 | - $checks[(int) $row['id']] = $row; |
|
306 | - } |
|
307 | - $result->closeCursor(); |
|
308 | - |
|
309 | - $checkIds = array_diff($checkIds, array_keys($checks)); |
|
310 | - |
|
311 | - if (!empty($checkIds)) { |
|
312 | - $missingCheck = array_pop($checkIds); |
|
313 | - throw new \UnexpectedValueException($this->l->t('Check #%s does not exist', $missingCheck)); |
|
314 | - } |
|
315 | - |
|
316 | - return $checks; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * @param string $class |
|
321 | - * @param string $operator |
|
322 | - * @param string $value |
|
323 | - * @return int Check unique ID |
|
324 | - */ |
|
325 | - protected function addCheck($class, $operator, $value) { |
|
326 | - $hash = md5($class . '::' . $operator . '::' . $value); |
|
327 | - |
|
328 | - $query = $this->connection->getQueryBuilder(); |
|
329 | - $query->select('id') |
|
330 | - ->from('flow_checks') |
|
331 | - ->where($query->expr()->eq('hash', $query->createNamedParameter($hash))); |
|
332 | - $result = $query->execute(); |
|
333 | - |
|
334 | - if ($row = $result->fetch()) { |
|
335 | - $result->closeCursor(); |
|
336 | - return (int) $row['id']; |
|
337 | - } |
|
338 | - |
|
339 | - $query = $this->connection->getQueryBuilder(); |
|
340 | - $query->insert('flow_checks') |
|
341 | - ->values([ |
|
342 | - 'class' => $query->createNamedParameter($class), |
|
343 | - 'operator' => $query->createNamedParameter($operator), |
|
344 | - 'value' => $query->createNamedParameter($value), |
|
345 | - 'hash' => $query->createNamedParameter($hash), |
|
346 | - ]); |
|
347 | - $query->execute(); |
|
348 | - |
|
349 | - return $query->getLastInsertId(); |
|
350 | - } |
|
37 | + /** @var IStorage */ |
|
38 | + protected $storage; |
|
39 | + |
|
40 | + /** @var string */ |
|
41 | + protected $path; |
|
42 | + |
|
43 | + /** @var array[] */ |
|
44 | + protected $operations = []; |
|
45 | + |
|
46 | + /** @var array[] */ |
|
47 | + protected $checks = []; |
|
48 | + |
|
49 | + /** @var IDBConnection */ |
|
50 | + protected $connection; |
|
51 | + |
|
52 | + /** @var IServerContainer|\OC\Server */ |
|
53 | + protected $container; |
|
54 | + |
|
55 | + /** @var IL10N */ |
|
56 | + protected $l; |
|
57 | + |
|
58 | + /** |
|
59 | + * @param IDBConnection $connection |
|
60 | + * @param IServerContainer $container |
|
61 | + * @param IL10N $l |
|
62 | + */ |
|
63 | + public function __construct(IDBConnection $connection, IServerContainer $container, IL10N $l) { |
|
64 | + $this->connection = $connection; |
|
65 | + $this->container = $container; |
|
66 | + $this->l = $l; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @inheritdoc |
|
71 | + */ |
|
72 | + public function setFileInfo(IStorage $storage, $path) { |
|
73 | + $this->storage = $storage; |
|
74 | + $this->path = $path; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @inheritdoc |
|
79 | + */ |
|
80 | + public function getMatchingOperations($class, $returnFirstMatchingOperationOnly = true) { |
|
81 | + $operations = $this->getOperations($class); |
|
82 | + |
|
83 | + $matches = []; |
|
84 | + foreach ($operations as $operation) { |
|
85 | + $checkIds = json_decode($operation['checks'], true); |
|
86 | + $checks = $this->getChecks($checkIds); |
|
87 | + |
|
88 | + foreach ($checks as $check) { |
|
89 | + if (!$this->check($check)) { |
|
90 | + // Check did not match, continue with the next operation |
|
91 | + continue 2; |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + if ($returnFirstMatchingOperationOnly) { |
|
96 | + return $operation; |
|
97 | + } |
|
98 | + $matches[] = $operation; |
|
99 | + } |
|
100 | + |
|
101 | + return $matches; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @param array $check |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + protected function check(array $check) { |
|
109 | + try { |
|
110 | + $checkInstance = $this->container->query($check['class']); |
|
111 | + } catch (QueryException $e) { |
|
112 | + // Check does not exist, assume it matches. |
|
113 | + return true; |
|
114 | + } |
|
115 | + |
|
116 | + if ($checkInstance instanceof ICheck) { |
|
117 | + $checkInstance->setFileInfo($this->storage, $this->path); |
|
118 | + return $checkInstance->executeCheck($check['operator'], $check['value']); |
|
119 | + } else { |
|
120 | + // Check is invalid |
|
121 | + throw new \UnexpectedValueException($this->l->t('Check %s is invalid or does not exist', $check['class'])); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $class |
|
127 | + * @return array[] |
|
128 | + */ |
|
129 | + public function getOperations($class) { |
|
130 | + if (isset($this->operations[$class])) { |
|
131 | + return $this->operations[$class]; |
|
132 | + } |
|
133 | + |
|
134 | + $query = $this->connection->getQueryBuilder(); |
|
135 | + |
|
136 | + $query->select('*') |
|
137 | + ->from('flow_operations') |
|
138 | + ->where($query->expr()->eq('class', $query->createNamedParameter($class))); |
|
139 | + $result = $query->execute(); |
|
140 | + |
|
141 | + $this->operations[$class] = []; |
|
142 | + while ($row = $result->fetch()) { |
|
143 | + $this->operations[$class][] = $row; |
|
144 | + } |
|
145 | + $result->closeCursor(); |
|
146 | + |
|
147 | + return $this->operations[$class]; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param int $id |
|
152 | + * @return array |
|
153 | + * @throws \UnexpectedValueException |
|
154 | + */ |
|
155 | + protected function getOperation($id) { |
|
156 | + $query = $this->connection->getQueryBuilder(); |
|
157 | + $query->select('*') |
|
158 | + ->from('flow_operations') |
|
159 | + ->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
|
160 | + $result = $query->execute(); |
|
161 | + $row = $result->fetch(); |
|
162 | + $result->closeCursor(); |
|
163 | + |
|
164 | + if ($row) { |
|
165 | + return $row; |
|
166 | + } |
|
167 | + |
|
168 | + throw new \UnexpectedValueException($this->l->t('Operation #%s does not exist', [$id])); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @param string $class |
|
173 | + * @param string $name |
|
174 | + * @param array[] $checks |
|
175 | + * @param string $operation |
|
176 | + * @return array The added operation |
|
177 | + * @throws \UnexpectedValueException |
|
178 | + */ |
|
179 | + public function addOperation($class, $name, array $checks, $operation) { |
|
180 | + $this->validateOperation($class, $name, $checks, $operation); |
|
181 | + |
|
182 | + $checkIds = []; |
|
183 | + foreach ($checks as $check) { |
|
184 | + $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']); |
|
185 | + } |
|
186 | + |
|
187 | + $query = $this->connection->getQueryBuilder(); |
|
188 | + $query->insert('flow_operations') |
|
189 | + ->values([ |
|
190 | + 'class' => $query->createNamedParameter($class), |
|
191 | + 'name' => $query->createNamedParameter($name), |
|
192 | + 'checks' => $query->createNamedParameter(json_encode(array_unique($checkIds))), |
|
193 | + 'operation' => $query->createNamedParameter($operation), |
|
194 | + ]); |
|
195 | + $query->execute(); |
|
196 | + |
|
197 | + $id = $query->getLastInsertId(); |
|
198 | + return $this->getOperation($id); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * @param int $id |
|
203 | + * @param string $name |
|
204 | + * @param array[] $checks |
|
205 | + * @param string $operation |
|
206 | + * @return array The updated operation |
|
207 | + * @throws \UnexpectedValueException |
|
208 | + */ |
|
209 | + public function updateOperation($id, $name, array $checks, $operation) { |
|
210 | + $row = $this->getOperation($id); |
|
211 | + $this->validateOperation($row['class'], $name, $checks, $operation); |
|
212 | + |
|
213 | + $checkIds = []; |
|
214 | + foreach ($checks as $check) { |
|
215 | + $checkIds[] = $this->addCheck($check['class'], $check['operator'], $check['value']); |
|
216 | + } |
|
217 | + |
|
218 | + $query = $this->connection->getQueryBuilder(); |
|
219 | + $query->update('flow_operations') |
|
220 | + ->set('name', $query->createNamedParameter($name)) |
|
221 | + ->set('checks', $query->createNamedParameter(json_encode(array_unique($checkIds)))) |
|
222 | + ->set('operation', $query->createNamedParameter($operation)) |
|
223 | + ->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
|
224 | + $query->execute(); |
|
225 | + |
|
226 | + return $this->getOperation($id); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * @param int $id |
|
231 | + * @return bool |
|
232 | + * @throws \UnexpectedValueException |
|
233 | + */ |
|
234 | + public function deleteOperation($id) { |
|
235 | + $query = $this->connection->getQueryBuilder(); |
|
236 | + $query->delete('flow_operations') |
|
237 | + ->where($query->expr()->eq('id', $query->createNamedParameter($id))); |
|
238 | + return (bool) $query->execute(); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * @param string $class |
|
243 | + * @param string $name |
|
244 | + * @param array[] $checks |
|
245 | + * @param string $operation |
|
246 | + * @throws \UnexpectedValueException |
|
247 | + */ |
|
248 | + protected function validateOperation($class, $name, array $checks, $operation) { |
|
249 | + try { |
|
250 | + /** @var IOperation $instance */ |
|
251 | + $instance = $this->container->query($class); |
|
252 | + } catch (QueryException $e) { |
|
253 | + throw new \UnexpectedValueException($this->l->t('Operation %s does not exist', [$class])); |
|
254 | + } |
|
255 | + |
|
256 | + if (!($instance instanceof IOperation)) { |
|
257 | + throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class])); |
|
258 | + } |
|
259 | + |
|
260 | + $instance->validateOperation($name, $checks, $operation); |
|
261 | + |
|
262 | + foreach ($checks as $check) { |
|
263 | + try { |
|
264 | + /** @var ICheck $instance */ |
|
265 | + $instance = $this->container->query($check['class']); |
|
266 | + } catch (QueryException $e) { |
|
267 | + throw new \UnexpectedValueException($this->l->t('Check %s does not exist', [$class])); |
|
268 | + } |
|
269 | + |
|
270 | + if (!($instance instanceof ICheck)) { |
|
271 | + throw new \UnexpectedValueException($this->l->t('Check %s is invalid', [$class])); |
|
272 | + } |
|
273 | + |
|
274 | + $instance->validateCheck($check['operator'], $check['value']); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param int[] $checkIds |
|
280 | + * @return array[] |
|
281 | + */ |
|
282 | + public function getChecks(array $checkIds) { |
|
283 | + $checkIds = array_map('intval', $checkIds); |
|
284 | + |
|
285 | + $checks = []; |
|
286 | + foreach ($checkIds as $i => $checkId) { |
|
287 | + if (isset($this->checks[$checkId])) { |
|
288 | + $checks[$checkId] = $this->checks[$checkId]; |
|
289 | + unset($checkIds[$i]); |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + if (empty($checkIds)) { |
|
294 | + return $checks; |
|
295 | + } |
|
296 | + |
|
297 | + $query = $this->connection->getQueryBuilder(); |
|
298 | + $query->select('*') |
|
299 | + ->from('flow_checks') |
|
300 | + ->where($query->expr()->in('id', $query->createNamedParameter($checkIds, IQueryBuilder::PARAM_INT_ARRAY))); |
|
301 | + $result = $query->execute(); |
|
302 | + |
|
303 | + while ($row = $result->fetch()) { |
|
304 | + $this->checks[(int) $row['id']] = $row; |
|
305 | + $checks[(int) $row['id']] = $row; |
|
306 | + } |
|
307 | + $result->closeCursor(); |
|
308 | + |
|
309 | + $checkIds = array_diff($checkIds, array_keys($checks)); |
|
310 | + |
|
311 | + if (!empty($checkIds)) { |
|
312 | + $missingCheck = array_pop($checkIds); |
|
313 | + throw new \UnexpectedValueException($this->l->t('Check #%s does not exist', $missingCheck)); |
|
314 | + } |
|
315 | + |
|
316 | + return $checks; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * @param string $class |
|
321 | + * @param string $operator |
|
322 | + * @param string $value |
|
323 | + * @return int Check unique ID |
|
324 | + */ |
|
325 | + protected function addCheck($class, $operator, $value) { |
|
326 | + $hash = md5($class . '::' . $operator . '::' . $value); |
|
327 | + |
|
328 | + $query = $this->connection->getQueryBuilder(); |
|
329 | + $query->select('id') |
|
330 | + ->from('flow_checks') |
|
331 | + ->where($query->expr()->eq('hash', $query->createNamedParameter($hash))); |
|
332 | + $result = $query->execute(); |
|
333 | + |
|
334 | + if ($row = $result->fetch()) { |
|
335 | + $result->closeCursor(); |
|
336 | + return (int) $row['id']; |
|
337 | + } |
|
338 | + |
|
339 | + $query = $this->connection->getQueryBuilder(); |
|
340 | + $query->insert('flow_checks') |
|
341 | + ->values([ |
|
342 | + 'class' => $query->createNamedParameter($class), |
|
343 | + 'operator' => $query->createNamedParameter($operator), |
|
344 | + 'value' => $query->createNamedParameter($value), |
|
345 | + 'hash' => $query->createNamedParameter($hash), |
|
346 | + ]); |
|
347 | + $query->execute(); |
|
348 | + |
|
349 | + return $query->getLastInsertId(); |
|
350 | + } |
|
351 | 351 | } |
@@ -21,62 +21,62 @@ |
||
21 | 21 | */ |
22 | 22 | |
23 | 23 | return [ |
24 | - 'routes' => [ |
|
25 | - [ |
|
26 | - 'name' => 'RateLimitTest#userAndAnonProtected', |
|
27 | - 'url' => '/userAndAnonProtected', |
|
28 | - 'verb' => 'GET', |
|
29 | - ], |
|
30 | - [ |
|
31 | - 'name' => 'RateLimitTest#onlyAnonProtected', |
|
32 | - 'url' => '/anonProtected', |
|
33 | - 'verb' => 'GET', |
|
34 | - ], |
|
35 | - ], |
|
24 | + 'routes' => [ |
|
25 | + [ |
|
26 | + 'name' => 'RateLimitTest#userAndAnonProtected', |
|
27 | + 'url' => '/userAndAnonProtected', |
|
28 | + 'verb' => 'GET', |
|
29 | + ], |
|
30 | + [ |
|
31 | + 'name' => 'RateLimitTest#onlyAnonProtected', |
|
32 | + 'url' => '/anonProtected', |
|
33 | + 'verb' => 'GET', |
|
34 | + ], |
|
35 | + ], |
|
36 | 36 | |
37 | - 'ocs' => [ |
|
38 | - [ |
|
39 | - 'name' => 'Config#setAppValue', |
|
40 | - 'url' => '/api/v1/app/{appid}/{configkey}', |
|
41 | - 'verb' => 'POST', |
|
42 | - ], |
|
43 | - [ |
|
44 | - 'name' => 'Config#deleteAppValue', |
|
45 | - 'url' => '/api/v1/app/{appid}/{configkey}', |
|
46 | - 'verb' => 'DELETE', |
|
47 | - ], |
|
48 | - [ |
|
49 | - 'name' => 'Locking#isLockingEnabled', |
|
50 | - 'url' => '/api/v1/lockprovisioning', |
|
51 | - 'verb' => 'GET', |
|
52 | - ], |
|
53 | - [ |
|
54 | - 'name' => 'Locking#isLocked', |
|
55 | - 'url' => '/api/v1/lockprovisioning/{type}/{user}', |
|
56 | - 'verb' => 'GET', |
|
57 | - ], |
|
58 | - [ |
|
59 | - 'name' => 'Locking#acquireLock', |
|
60 | - 'url' => '/api/v1/lockprovisioning/{type}/{user}', |
|
61 | - 'verb' => 'POST', |
|
62 | - ], |
|
63 | - [ |
|
64 | - 'name' => 'Locking#changeLock', |
|
65 | - 'url' => '/api/v1/lockprovisioning/{type}/{user}', |
|
66 | - 'verb' => 'PUT', |
|
67 | - ], |
|
68 | - [ |
|
69 | - 'name' => 'Locking#releaseLock', |
|
70 | - 'url' => '/api/v1/lockprovisioning/{type}/{user}', |
|
71 | - 'verb' => 'DELETE', |
|
72 | - ], |
|
73 | - [ |
|
74 | - 'name' => 'Locking#releaseAll', |
|
75 | - 'url' => '/api/v1/lockprovisioning/{type}', |
|
76 | - 'verb' => 'DELETE', |
|
77 | - 'defaults' => [ |
|
78 | - 'type' => null |
|
79 | - ] |
|
80 | - ], |
|
81 | - ], |
|
37 | + 'ocs' => [ |
|
38 | + [ |
|
39 | + 'name' => 'Config#setAppValue', |
|
40 | + 'url' => '/api/v1/app/{appid}/{configkey}', |
|
41 | + 'verb' => 'POST', |
|
42 | + ], |
|
43 | + [ |
|
44 | + 'name' => 'Config#deleteAppValue', |
|
45 | + 'url' => '/api/v1/app/{appid}/{configkey}', |
|
46 | + 'verb' => 'DELETE', |
|
47 | + ], |
|
48 | + [ |
|
49 | + 'name' => 'Locking#isLockingEnabled', |
|
50 | + 'url' => '/api/v1/lockprovisioning', |
|
51 | + 'verb' => 'GET', |
|
52 | + ], |
|
53 | + [ |
|
54 | + 'name' => 'Locking#isLocked', |
|
55 | + 'url' => '/api/v1/lockprovisioning/{type}/{user}', |
|
56 | + 'verb' => 'GET', |
|
57 | + ], |
|
58 | + [ |
|
59 | + 'name' => 'Locking#acquireLock', |
|
60 | + 'url' => '/api/v1/lockprovisioning/{type}/{user}', |
|
61 | + 'verb' => 'POST', |
|
62 | + ], |
|
63 | + [ |
|
64 | + 'name' => 'Locking#changeLock', |
|
65 | + 'url' => '/api/v1/lockprovisioning/{type}/{user}', |
|
66 | + 'verb' => 'PUT', |
|
67 | + ], |
|
68 | + [ |
|
69 | + 'name' => 'Locking#releaseLock', |
|
70 | + 'url' => '/api/v1/lockprovisioning/{type}/{user}', |
|
71 | + 'verb' => 'DELETE', |
|
72 | + ], |
|
73 | + [ |
|
74 | + 'name' => 'Locking#releaseAll', |
|
75 | + 'url' => '/api/v1/lockprovisioning/{type}', |
|
76 | + 'verb' => 'DELETE', |
|
77 | + 'defaults' => [ |
|
78 | + 'type' => null |
|
79 | + ] |
|
80 | + ], |
|
81 | + ], |
|
82 | 82 | ]; |
@@ -32,14 +32,14 @@ |
||
32 | 32 | * @since 9.1.0 |
33 | 33 | */ |
34 | 34 | class OCSNotFoundException extends OCSException { |
35 | - /** |
|
36 | - * OCSNotFoundException constructor. |
|
37 | - * |
|
38 | - * @param string $message |
|
39 | - * @param Exception|null $previous |
|
40 | - * @since 9.1.0 |
|
41 | - */ |
|
42 | - public function __construct($message = '', Exception $previous = null) { |
|
43 | - parent::__construct($message, Http::STATUS_NOT_FOUND, $previous); |
|
44 | - } |
|
35 | + /** |
|
36 | + * OCSNotFoundException constructor. |
|
37 | + * |
|
38 | + * @param string $message |
|
39 | + * @param Exception|null $previous |
|
40 | + * @since 9.1.0 |
|
41 | + */ |
|
42 | + public function __construct($message = '', Exception $previous = null) { |
|
43 | + parent::__construct($message, Http::STATUS_NOT_FOUND, $previous); |
|
44 | + } |
|
45 | 45 | } |
@@ -32,14 +32,14 @@ |
||
32 | 32 | * @since 9.1.0 |
33 | 33 | */ |
34 | 34 | class OCSForbiddenException extends OCSException { |
35 | - /** |
|
36 | - * OCSForbiddenException constructor. |
|
37 | - * |
|
38 | - * @param string $message |
|
39 | - * @param Exception|null $previous |
|
40 | - * @since 9.1.0 |
|
41 | - */ |
|
42 | - public function __construct($message = '', Exception $previous = null) { |
|
43 | - parent::__construct($message, Http::STATUS_FORBIDDEN, $previous); |
|
44 | - } |
|
35 | + /** |
|
36 | + * OCSForbiddenException constructor. |
|
37 | + * |
|
38 | + * @param string $message |
|
39 | + * @param Exception|null $previous |
|
40 | + * @since 9.1.0 |
|
41 | + */ |
|
42 | + public function __construct($message = '', Exception $previous = null) { |
|
43 | + parent::__construct($message, Http::STATUS_FORBIDDEN, $previous); |
|
44 | + } |
|
45 | 45 | } |
@@ -32,14 +32,14 @@ |
||
32 | 32 | * @since 9.1.0 |
33 | 33 | */ |
34 | 34 | class OCSBadRequestException extends OCSException { |
35 | - /** |
|
36 | - * OCSBadRequestException constructor. |
|
37 | - * |
|
38 | - * @param string $message |
|
39 | - * @param Exception|null $previous |
|
40 | - * @since 9.1.0 |
|
41 | - */ |
|
42 | - public function __construct($message = '', Exception $previous = null) { |
|
43 | - parent::__construct($message, Http::STATUS_BAD_REQUEST, $previous); |
|
44 | - } |
|
35 | + /** |
|
36 | + * OCSBadRequestException constructor. |
|
37 | + * |
|
38 | + * @param string $message |
|
39 | + * @param Exception|null $previous |
|
40 | + * @since 9.1.0 |
|
41 | + */ |
|
42 | + public function __construct($message = '', Exception $previous = null) { |
|
43 | + parent::__construct($message, Http::STATUS_BAD_REQUEST, $previous); |
|
44 | + } |
|
45 | 45 | } |
@@ -31,33 +31,33 @@ |
||
31 | 31 | |
32 | 32 | class EMailProvider implements IProvider { |
33 | 33 | |
34 | - /** @var IActionFactory */ |
|
35 | - private $actionFactory; |
|
36 | - |
|
37 | - /** @var IURLGenerator */ |
|
38 | - private $urlGenerator; |
|
39 | - |
|
40 | - /** |
|
41 | - * @param IActionFactory $actionFactory |
|
42 | - * @param IURLGenerator $urlGenerator |
|
43 | - */ |
|
44 | - public function __construct(IActionFactory $actionFactory, IURLGenerator $urlGenerator) { |
|
45 | - $this->actionFactory = $actionFactory; |
|
46 | - $this->urlGenerator = $urlGenerator; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @param IEntry $entry |
|
51 | - */ |
|
52 | - public function process(IEntry $entry) { |
|
53 | - $iconUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/mail.svg')); |
|
54 | - foreach ($entry->getEMailAddresses() as $address) { |
|
55 | - if (empty($address)) { |
|
56 | - // Skip |
|
57 | - continue; |
|
58 | - } |
|
59 | - $action = $this->actionFactory->newEMailAction($iconUrl, $address, $address); |
|
60 | - $entry->addAction($action); |
|
61 | - } |
|
62 | - } |
|
34 | + /** @var IActionFactory */ |
|
35 | + private $actionFactory; |
|
36 | + |
|
37 | + /** @var IURLGenerator */ |
|
38 | + private $urlGenerator; |
|
39 | + |
|
40 | + /** |
|
41 | + * @param IActionFactory $actionFactory |
|
42 | + * @param IURLGenerator $urlGenerator |
|
43 | + */ |
|
44 | + public function __construct(IActionFactory $actionFactory, IURLGenerator $urlGenerator) { |
|
45 | + $this->actionFactory = $actionFactory; |
|
46 | + $this->urlGenerator = $urlGenerator; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @param IEntry $entry |
|
51 | + */ |
|
52 | + public function process(IEntry $entry) { |
|
53 | + $iconUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/mail.svg')); |
|
54 | + foreach ($entry->getEMailAddresses() as $address) { |
|
55 | + if (empty($address)) { |
|
56 | + // Skip |
|
57 | + continue; |
|
58 | + } |
|
59 | + $action = $this->actionFactory->newEMailAction($iconUrl, $address, $address); |
|
60 | + $entry->addAction($action); |
|
61 | + } |
|
62 | + } |
|
63 | 63 | } |