@@ -35,49 +35,49 @@ |
||
35 | 35 | * @package OC\AppFramework\Middleware\Security |
36 | 36 | */ |
37 | 37 | class BruteForceMiddleware extends Middleware { |
38 | - /** @var ControllerMethodReflector */ |
|
39 | - private $reflector; |
|
40 | - /** @var Throttler */ |
|
41 | - private $throttler; |
|
42 | - /** @var IRequest */ |
|
43 | - private $request; |
|
38 | + /** @var ControllerMethodReflector */ |
|
39 | + private $reflector; |
|
40 | + /** @var Throttler */ |
|
41 | + private $throttler; |
|
42 | + /** @var IRequest */ |
|
43 | + private $request; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param ControllerMethodReflector $controllerMethodReflector |
|
47 | - * @param Throttler $throttler |
|
48 | - * @param IRequest $request |
|
49 | - */ |
|
50 | - public function __construct(ControllerMethodReflector $controllerMethodReflector, |
|
51 | - Throttler $throttler, |
|
52 | - IRequest $request) { |
|
53 | - $this->reflector = $controllerMethodReflector; |
|
54 | - $this->throttler = $throttler; |
|
55 | - $this->request = $request; |
|
56 | - } |
|
45 | + /** |
|
46 | + * @param ControllerMethodReflector $controllerMethodReflector |
|
47 | + * @param Throttler $throttler |
|
48 | + * @param IRequest $request |
|
49 | + */ |
|
50 | + public function __construct(ControllerMethodReflector $controllerMethodReflector, |
|
51 | + Throttler $throttler, |
|
52 | + IRequest $request) { |
|
53 | + $this->reflector = $controllerMethodReflector; |
|
54 | + $this->throttler = $throttler; |
|
55 | + $this->request = $request; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * {@inheritDoc} |
|
60 | - */ |
|
61 | - public function beforeController($controller, $methodName) { |
|
62 | - parent::beforeController($controller, $methodName); |
|
58 | + /** |
|
59 | + * {@inheritDoc} |
|
60 | + */ |
|
61 | + public function beforeController($controller, $methodName) { |
|
62 | + parent::beforeController($controller, $methodName); |
|
63 | 63 | |
64 | - if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
65 | - $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
66 | - $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
|
67 | - } |
|
68 | - } |
|
64 | + if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
65 | + $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
66 | + $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * {@inheritDoc} |
|
72 | - */ |
|
73 | - public function afterController($controller, $methodName, Response $response) { |
|
74 | - if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
75 | - $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
76 | - $ip = $this->request->getRemoteAddress(); |
|
77 | - $this->throttler->sleepDelay($ip, $action); |
|
78 | - $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata()); |
|
79 | - } |
|
70 | + /** |
|
71 | + * {@inheritDoc} |
|
72 | + */ |
|
73 | + public function afterController($controller, $methodName, Response $response) { |
|
74 | + if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
75 | + $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
76 | + $ip = $this->request->getRemoteAddress(); |
|
77 | + $this->throttler->sleepDelay($ip, $action); |
|
78 | + $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata()); |
|
79 | + } |
|
80 | 80 | |
81 | - return parent::afterController($controller, $methodName, $response); |
|
82 | - } |
|
81 | + return parent::afterController($controller, $methodName, $response); |
|
82 | + } |
|
83 | 83 | } |
@@ -24,7 +24,7 @@ |
||
24 | 24 | use OC\DB\QueryBuilder\QueryFunction; |
25 | 25 | |
26 | 26 | class PgSqlFunctionBuilder extends FunctionBuilder { |
27 | - public function concat($x, $y) { |
|
28 | - return new QueryFunction('(' . $this->helper->quoteColumnName($x) . ' || ' . $this->helper->quoteColumnName($y) . ')'); |
|
29 | - } |
|
27 | + public function concat($x, $y) { |
|
28 | + return new QueryFunction('(' . $this->helper->quoteColumnName($x) . ' || ' . $this->helper->quoteColumnName($y) . ')'); |
|
29 | + } |
|
30 | 30 | } |
@@ -27,72 +27,72 @@ |
||
27 | 27 | |
28 | 28 | class RemoteShare implements ISetting { |
29 | 29 | |
30 | - /** @var IL10N */ |
|
31 | - protected $l; |
|
30 | + /** @var IL10N */ |
|
31 | + protected $l; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param IL10N $l |
|
35 | - */ |
|
36 | - public function __construct(IL10N $l) { |
|
37 | - $this->l = $l; |
|
38 | - } |
|
33 | + /** |
|
34 | + * @param IL10N $l |
|
35 | + */ |
|
36 | + public function __construct(IL10N $l) { |
|
37 | + $this->l = $l; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string Lowercase a-z and underscore only identifier |
|
42 | - * @since 11.0.0 |
|
43 | - */ |
|
44 | - public function getIdentifier() { |
|
45 | - return 'remote_share'; |
|
46 | - } |
|
40 | + /** |
|
41 | + * @return string Lowercase a-z and underscore only identifier |
|
42 | + * @since 11.0.0 |
|
43 | + */ |
|
44 | + public function getIdentifier() { |
|
45 | + return 'remote_share'; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return string A translated string |
|
50 | - * @since 11.0.0 |
|
51 | - */ |
|
52 | - public function getName() { |
|
53 | - return $this->l->t('A file or folder was shared from <strong>another server</strong>'); |
|
54 | - } |
|
48 | + /** |
|
49 | + * @return string A translated string |
|
50 | + * @since 11.0.0 |
|
51 | + */ |
|
52 | + public function getName() { |
|
53 | + return $this->l->t('A file or folder was shared from <strong>another server</strong>'); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return int whether the filter should be rather on the top or bottom of |
|
58 | - * the admin section. The filters are arranged in ascending order of the |
|
59 | - * priority values. It is required to return a value between 0 and 100. |
|
60 | - * @since 11.0.0 |
|
61 | - */ |
|
62 | - public function getPriority() { |
|
63 | - return 11; |
|
64 | - } |
|
56 | + /** |
|
57 | + * @return int whether the filter should be rather on the top or bottom of |
|
58 | + * the admin section. The filters are arranged in ascending order of the |
|
59 | + * priority values. It is required to return a value between 0 and 100. |
|
60 | + * @since 11.0.0 |
|
61 | + */ |
|
62 | + public function getPriority() { |
|
63 | + return 11; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return bool True when the option can be changed for the stream |
|
68 | - * @since 11.0.0 |
|
69 | - */ |
|
70 | - public function canChangeStream() { |
|
71 | - return true; |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return bool True when the option can be changed for the stream |
|
68 | + * @since 11.0.0 |
|
69 | + */ |
|
70 | + public function canChangeStream() { |
|
71 | + return true; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return bool True when the option can be changed for the stream |
|
76 | - * @since 11.0.0 |
|
77 | - */ |
|
78 | - public function isDefaultEnabledStream() { |
|
79 | - return true; |
|
80 | - } |
|
74 | + /** |
|
75 | + * @return bool True when the option can be changed for the stream |
|
76 | + * @since 11.0.0 |
|
77 | + */ |
|
78 | + public function isDefaultEnabledStream() { |
|
79 | + return true; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return bool True when the option can be changed for the mail |
|
84 | - * @since 11.0.0 |
|
85 | - */ |
|
86 | - public function canChangeMail() { |
|
87 | - return true; |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return bool True when the option can be changed for the mail |
|
84 | + * @since 11.0.0 |
|
85 | + */ |
|
86 | + public function canChangeMail() { |
|
87 | + return true; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @return bool True when the option can be changed for the stream |
|
92 | - * @since 11.0.0 |
|
93 | - */ |
|
94 | - public function isDefaultEnabledMail() { |
|
95 | - return false; |
|
96 | - } |
|
90 | + /** |
|
91 | + * @return bool True when the option can be changed for the stream |
|
92 | + * @since 11.0.0 |
|
93 | + */ |
|
94 | + public function isDefaultEnabledMail() { |
|
95 | + return false; |
|
96 | + } |
|
97 | 97 | } |
98 | 98 |
@@ -27,72 +27,72 @@ |
||
27 | 27 | |
28 | 28 | class Shared implements ISetting { |
29 | 29 | |
30 | - /** @var IL10N */ |
|
31 | - protected $l; |
|
30 | + /** @var IL10N */ |
|
31 | + protected $l; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param IL10N $l |
|
35 | - */ |
|
36 | - public function __construct(IL10N $l) { |
|
37 | - $this->l = $l; |
|
38 | - } |
|
33 | + /** |
|
34 | + * @param IL10N $l |
|
35 | + */ |
|
36 | + public function __construct(IL10N $l) { |
|
37 | + $this->l = $l; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string Lowercase a-z and underscore only identifier |
|
42 | - * @since 11.0.0 |
|
43 | - */ |
|
44 | - public function getIdentifier() { |
|
45 | - return 'shared'; |
|
46 | - } |
|
40 | + /** |
|
41 | + * @return string Lowercase a-z and underscore only identifier |
|
42 | + * @since 11.0.0 |
|
43 | + */ |
|
44 | + public function getIdentifier() { |
|
45 | + return 'shared'; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return string A translated string |
|
50 | - * @since 11.0.0 |
|
51 | - */ |
|
52 | - public function getName() { |
|
53 | - return $this->l->t('A file or folder has been <strong>shared</strong>'); |
|
54 | - } |
|
48 | + /** |
|
49 | + * @return string A translated string |
|
50 | + * @since 11.0.0 |
|
51 | + */ |
|
52 | + public function getName() { |
|
53 | + return $this->l->t('A file or folder has been <strong>shared</strong>'); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return int whether the filter should be rather on the top or bottom of |
|
58 | - * the admin section. The filters are arranged in ascending order of the |
|
59 | - * priority values. It is required to return a value between 0 and 100. |
|
60 | - * @since 11.0.0 |
|
61 | - */ |
|
62 | - public function getPriority() { |
|
63 | - return 10; |
|
64 | - } |
|
56 | + /** |
|
57 | + * @return int whether the filter should be rather on the top or bottom of |
|
58 | + * the admin section. The filters are arranged in ascending order of the |
|
59 | + * priority values. It is required to return a value between 0 and 100. |
|
60 | + * @since 11.0.0 |
|
61 | + */ |
|
62 | + public function getPriority() { |
|
63 | + return 10; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return bool True when the option can be changed for the stream |
|
68 | - * @since 11.0.0 |
|
69 | - */ |
|
70 | - public function canChangeStream() { |
|
71 | - return true; |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return bool True when the option can be changed for the stream |
|
68 | + * @since 11.0.0 |
|
69 | + */ |
|
70 | + public function canChangeStream() { |
|
71 | + return true; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return bool True when the option can be changed for the stream |
|
76 | - * @since 11.0.0 |
|
77 | - */ |
|
78 | - public function isDefaultEnabledStream() { |
|
79 | - return true; |
|
80 | - } |
|
74 | + /** |
|
75 | + * @return bool True when the option can be changed for the stream |
|
76 | + * @since 11.0.0 |
|
77 | + */ |
|
78 | + public function isDefaultEnabledStream() { |
|
79 | + return true; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return bool True when the option can be changed for the mail |
|
84 | - * @since 11.0.0 |
|
85 | - */ |
|
86 | - public function canChangeMail() { |
|
87 | - return true; |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return bool True when the option can be changed for the mail |
|
84 | + * @since 11.0.0 |
|
85 | + */ |
|
86 | + public function canChangeMail() { |
|
87 | + return true; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @return bool True when the option can be changed for the stream |
|
92 | - * @since 11.0.0 |
|
93 | - */ |
|
94 | - public function isDefaultEnabledMail() { |
|
95 | - return false; |
|
96 | - } |
|
90 | + /** |
|
91 | + * @return bool True when the option can be changed for the stream |
|
92 | + * @since 11.0.0 |
|
93 | + */ |
|
94 | + public function isDefaultEnabledMail() { |
|
95 | + return false; |
|
96 | + } |
|
97 | 97 | } |
98 | 98 |
@@ -24,18 +24,18 @@ |
||
24 | 24 | use OCP\Files\ObjectStore\IObjectStore; |
25 | 25 | |
26 | 26 | class S3 implements IObjectStore { |
27 | - use S3ConnectionTrait; |
|
28 | - use S3ObjectTrait; |
|
27 | + use S3ConnectionTrait; |
|
28 | + use S3ObjectTrait; |
|
29 | 29 | |
30 | - public function __construct($parameters) { |
|
31 | - $this->parseParams($parameters); |
|
32 | - } |
|
30 | + public function __construct($parameters) { |
|
31 | + $this->parseParams($parameters); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * @return string the container or bucket name where objects are stored |
|
36 | - * @since 7.0.0 |
|
37 | - */ |
|
38 | - public function getStorageId() { |
|
39 | - return $this->id; |
|
40 | - } |
|
34 | + /** |
|
35 | + * @return string the container or bucket name where objects are stored |
|
36 | + * @since 7.0.0 |
|
37 | + */ |
|
38 | + public function getStorageId() { |
|
39 | + return $this->id; |
|
40 | + } |
|
41 | 41 | } |
@@ -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 | - '', |
|
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 | - '', |
|
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 | + '', |
|
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 | + '', |
|
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 | } |
@@ -11,43 +11,43 @@ |
||
11 | 11 | use OCP\Share\IManager; |
12 | 12 | |
13 | 13 | class OCSShareAPIMiddleware extends Middleware { |
14 | - /** @var IManager */ |
|
15 | - private $shareManager; |
|
16 | - /** @var IL10N */ |
|
17 | - private $l; |
|
14 | + /** @var IManager */ |
|
15 | + private $shareManager; |
|
16 | + /** @var IL10N */ |
|
17 | + private $l; |
|
18 | 18 | |
19 | - public function __construct(IManager $shareManager, |
|
20 | - IL10N $l) { |
|
21 | - $this->shareManager = $shareManager; |
|
22 | - $this->l = $l; |
|
23 | - } |
|
19 | + public function __construct(IManager $shareManager, |
|
20 | + IL10N $l) { |
|
21 | + $this->shareManager = $shareManager; |
|
22 | + $this->l = $l; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @param Controller $controller |
|
27 | - * @param string $methodName |
|
28 | - * |
|
29 | - * @throws OCSNotFoundException |
|
30 | - */ |
|
31 | - public function beforeController($controller, $methodName) { |
|
32 | - if ($controller instanceof ShareAPIController) { |
|
33 | - if (!$this->shareManager->shareApiEnabled()) { |
|
34 | - throw new OCSNotFoundException($this->l->t('Share API is disabled')); |
|
35 | - } |
|
36 | - } |
|
37 | - } |
|
25 | + /** |
|
26 | + * @param Controller $controller |
|
27 | + * @param string $methodName |
|
28 | + * |
|
29 | + * @throws OCSNotFoundException |
|
30 | + */ |
|
31 | + public function beforeController($controller, $methodName) { |
|
32 | + if ($controller instanceof ShareAPIController) { |
|
33 | + if (!$this->shareManager->shareApiEnabled()) { |
|
34 | + throw new OCSNotFoundException($this->l->t('Share API is disabled')); |
|
35 | + } |
|
36 | + } |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @param Controller $controller |
|
41 | - * @param string $methodName |
|
42 | - * @param Response $response |
|
43 | - * @return Response |
|
44 | - */ |
|
45 | - public function afterController($controller, $methodName, Response $response) { |
|
46 | - if ($controller instanceof ShareAPIController) { |
|
47 | - /** @var ShareAPIController $controller */ |
|
48 | - $controller->cleanup(); |
|
49 | - } |
|
39 | + /** |
|
40 | + * @param Controller $controller |
|
41 | + * @param string $methodName |
|
42 | + * @param Response $response |
|
43 | + * @return Response |
|
44 | + */ |
|
45 | + public function afterController($controller, $methodName, Response $response) { |
|
46 | + if ($controller instanceof ShareAPIController) { |
|
47 | + /** @var ShareAPIController $controller */ |
|
48 | + $controller->cleanup(); |
|
49 | + } |
|
50 | 50 | |
51 | - return $response; |
|
52 | - } |
|
51 | + return $response; |
|
52 | + } |
|
53 | 53 | } |
@@ -33,161 +33,161 @@ |
||
33 | 33 | |
34 | 34 | class OCIExpressionBuilder extends ExpressionBuilder { |
35 | 35 | |
36 | - /** |
|
37 | - * @param mixed $column |
|
38 | - * @param mixed|null $type |
|
39 | - * @return array|IQueryFunction|string |
|
40 | - */ |
|
41 | - protected function prepareColumn($column, $type) { |
|
42 | - if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) { |
|
43 | - $column = $this->castColumn($column, $type); |
|
44 | - } else { |
|
45 | - $column = $this->helper->quoteColumnNames($column); |
|
46 | - } |
|
47 | - return $column; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * @inheritdoc |
|
52 | - */ |
|
53 | - public function comparison($x, $operator, $y, $type = null) { |
|
54 | - $x = $this->prepareColumn($x, $type); |
|
55 | - $y = $this->prepareColumn($y, $type); |
|
56 | - |
|
57 | - return $this->expressionBuilder->comparison($x, $operator, $y); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @inheritdoc |
|
62 | - */ |
|
63 | - public function eq($x, $y, $type = null) { |
|
64 | - $x = $this->prepareColumn($x, $type); |
|
65 | - $y = $this->prepareColumn($y, $type); |
|
66 | - |
|
67 | - return $this->expressionBuilder->eq($x, $y); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @inheritdoc |
|
72 | - */ |
|
73 | - public function neq($x, $y, $type = null) { |
|
74 | - $x = $this->prepareColumn($x, $type); |
|
75 | - $y = $this->prepareColumn($y, $type); |
|
76 | - |
|
77 | - return $this->expressionBuilder->neq($x, $y); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @inheritdoc |
|
82 | - */ |
|
83 | - public function lt($x, $y, $type = null) { |
|
84 | - $x = $this->prepareColumn($x, $type); |
|
85 | - $y = $this->prepareColumn($y, $type); |
|
86 | - |
|
87 | - return $this->expressionBuilder->lt($x, $y); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @inheritdoc |
|
92 | - */ |
|
93 | - public function lte($x, $y, $type = null) { |
|
94 | - $x = $this->prepareColumn($x, $type); |
|
95 | - $y = $this->prepareColumn($y, $type); |
|
96 | - |
|
97 | - return $this->expressionBuilder->lte($x, $y); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @inheritdoc |
|
102 | - */ |
|
103 | - public function gt($x, $y, $type = null) { |
|
104 | - $x = $this->prepareColumn($x, $type); |
|
105 | - $y = $this->prepareColumn($y, $type); |
|
106 | - |
|
107 | - return $this->expressionBuilder->gt($x, $y); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @inheritdoc |
|
112 | - */ |
|
113 | - public function gte($x, $y, $type = null) { |
|
114 | - $x = $this->prepareColumn($x, $type); |
|
115 | - $y = $this->prepareColumn($y, $type); |
|
116 | - |
|
117 | - return $this->expressionBuilder->gte($x, $y); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @inheritdoc |
|
122 | - */ |
|
123 | - public function in($x, $y, $type = null) { |
|
124 | - $x = $this->prepareColumn($x, $type); |
|
125 | - $y = $this->prepareColumn($y, $type); |
|
126 | - |
|
127 | - return $this->expressionBuilder->in($x, $y); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @inheritdoc |
|
132 | - */ |
|
133 | - public function notIn($x, $y, $type = null) { |
|
134 | - $x = $this->prepareColumn($x, $type); |
|
135 | - $y = $this->prepareColumn($y, $type); |
|
136 | - |
|
137 | - return $this->expressionBuilder->notIn($x, $y); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Creates a $x = '' statement, because Oracle needs a different check |
|
142 | - * |
|
143 | - * @param string $x The field in string format to be inspected by the comparison. |
|
144 | - * @return string |
|
145 | - * @since 13.0.0 |
|
146 | - */ |
|
147 | - public function emptyString($x) { |
|
148 | - return $this->isNull($x); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Creates a `$x <> ''` statement, because Oracle needs a different check |
|
153 | - * |
|
154 | - * @param string $x The field in string format to be inspected by the comparison. |
|
155 | - * @return string |
|
156 | - * @since 13.0.0 |
|
157 | - */ |
|
158 | - public function nonEmptyString($x) { |
|
159 | - return $this->isNotNull($x); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Returns a IQueryFunction that casts the column to the given type |
|
164 | - * |
|
165 | - * @param string $column |
|
166 | - * @param mixed $type One of IQueryBuilder::PARAM_* |
|
167 | - * @return IQueryFunction |
|
168 | - */ |
|
169 | - public function castColumn($column, $type) { |
|
170 | - if ($type === IQueryBuilder::PARAM_STR) { |
|
171 | - $column = $this->helper->quoteColumnName($column); |
|
172 | - return new QueryFunction('to_char(' . $column . ')'); |
|
173 | - } |
|
174 | - |
|
175 | - return parent::castColumn($column, $type); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @inheritdoc |
|
180 | - */ |
|
181 | - public function like($x, $y, $type = null) { |
|
182 | - return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @inheritdoc |
|
187 | - */ |
|
188 | - public function iLike($x, $y, $type = null) { |
|
189 | - $x = $this->helper->quoteColumnName($x); |
|
190 | - $y = $this->helper->quoteColumnName($y); |
|
191 | - return new QueryFunction('REGEXP_LIKE(' . $x . ', \'^\' || REPLACE(REPLACE(' . $y . ', \'%\', \'.*\'), \'_\', \'.\') || \'$\', \'i\')'); |
|
192 | - } |
|
36 | + /** |
|
37 | + * @param mixed $column |
|
38 | + * @param mixed|null $type |
|
39 | + * @return array|IQueryFunction|string |
|
40 | + */ |
|
41 | + protected function prepareColumn($column, $type) { |
|
42 | + if ($type === IQueryBuilder::PARAM_STR && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) { |
|
43 | + $column = $this->castColumn($column, $type); |
|
44 | + } else { |
|
45 | + $column = $this->helper->quoteColumnNames($column); |
|
46 | + } |
|
47 | + return $column; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * @inheritdoc |
|
52 | + */ |
|
53 | + public function comparison($x, $operator, $y, $type = null) { |
|
54 | + $x = $this->prepareColumn($x, $type); |
|
55 | + $y = $this->prepareColumn($y, $type); |
|
56 | + |
|
57 | + return $this->expressionBuilder->comparison($x, $operator, $y); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @inheritdoc |
|
62 | + */ |
|
63 | + public function eq($x, $y, $type = null) { |
|
64 | + $x = $this->prepareColumn($x, $type); |
|
65 | + $y = $this->prepareColumn($y, $type); |
|
66 | + |
|
67 | + return $this->expressionBuilder->eq($x, $y); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @inheritdoc |
|
72 | + */ |
|
73 | + public function neq($x, $y, $type = null) { |
|
74 | + $x = $this->prepareColumn($x, $type); |
|
75 | + $y = $this->prepareColumn($y, $type); |
|
76 | + |
|
77 | + return $this->expressionBuilder->neq($x, $y); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @inheritdoc |
|
82 | + */ |
|
83 | + public function lt($x, $y, $type = null) { |
|
84 | + $x = $this->prepareColumn($x, $type); |
|
85 | + $y = $this->prepareColumn($y, $type); |
|
86 | + |
|
87 | + return $this->expressionBuilder->lt($x, $y); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @inheritdoc |
|
92 | + */ |
|
93 | + public function lte($x, $y, $type = null) { |
|
94 | + $x = $this->prepareColumn($x, $type); |
|
95 | + $y = $this->prepareColumn($y, $type); |
|
96 | + |
|
97 | + return $this->expressionBuilder->lte($x, $y); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @inheritdoc |
|
102 | + */ |
|
103 | + public function gt($x, $y, $type = null) { |
|
104 | + $x = $this->prepareColumn($x, $type); |
|
105 | + $y = $this->prepareColumn($y, $type); |
|
106 | + |
|
107 | + return $this->expressionBuilder->gt($x, $y); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @inheritdoc |
|
112 | + */ |
|
113 | + public function gte($x, $y, $type = null) { |
|
114 | + $x = $this->prepareColumn($x, $type); |
|
115 | + $y = $this->prepareColumn($y, $type); |
|
116 | + |
|
117 | + return $this->expressionBuilder->gte($x, $y); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @inheritdoc |
|
122 | + */ |
|
123 | + public function in($x, $y, $type = null) { |
|
124 | + $x = $this->prepareColumn($x, $type); |
|
125 | + $y = $this->prepareColumn($y, $type); |
|
126 | + |
|
127 | + return $this->expressionBuilder->in($x, $y); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @inheritdoc |
|
132 | + */ |
|
133 | + public function notIn($x, $y, $type = null) { |
|
134 | + $x = $this->prepareColumn($x, $type); |
|
135 | + $y = $this->prepareColumn($y, $type); |
|
136 | + |
|
137 | + return $this->expressionBuilder->notIn($x, $y); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Creates a $x = '' statement, because Oracle needs a different check |
|
142 | + * |
|
143 | + * @param string $x The field in string format to be inspected by the comparison. |
|
144 | + * @return string |
|
145 | + * @since 13.0.0 |
|
146 | + */ |
|
147 | + public function emptyString($x) { |
|
148 | + return $this->isNull($x); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Creates a `$x <> ''` statement, because Oracle needs a different check |
|
153 | + * |
|
154 | + * @param string $x The field in string format to be inspected by the comparison. |
|
155 | + * @return string |
|
156 | + * @since 13.0.0 |
|
157 | + */ |
|
158 | + public function nonEmptyString($x) { |
|
159 | + return $this->isNotNull($x); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Returns a IQueryFunction that casts the column to the given type |
|
164 | + * |
|
165 | + * @param string $column |
|
166 | + * @param mixed $type One of IQueryBuilder::PARAM_* |
|
167 | + * @return IQueryFunction |
|
168 | + */ |
|
169 | + public function castColumn($column, $type) { |
|
170 | + if ($type === IQueryBuilder::PARAM_STR) { |
|
171 | + $column = $this->helper->quoteColumnName($column); |
|
172 | + return new QueryFunction('to_char(' . $column . ')'); |
|
173 | + } |
|
174 | + |
|
175 | + return parent::castColumn($column, $type); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @inheritdoc |
|
180 | + */ |
|
181 | + public function like($x, $y, $type = null) { |
|
182 | + return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @inheritdoc |
|
187 | + */ |
|
188 | + public function iLike($x, $y, $type = null) { |
|
189 | + $x = $this->helper->quoteColumnName($x); |
|
190 | + $y = $this->helper->quoteColumnName($y); |
|
191 | + return new QueryFunction('REGEXP_LIKE(' . $x . ', \'^\' || REPLACE(REPLACE(' . $y . ', \'%\', \'.*\'), \'_\', \'.\') || \'$\', \'i\')'); |
|
192 | + } |
|
193 | 193 | } |
@@ -42,294 +42,294 @@ |
||
42 | 42 | */ |
43 | 43 | class DbHandler { |
44 | 44 | |
45 | - /** @var IDBConnection */ |
|
46 | - private $connection; |
|
47 | - |
|
48 | - /** @var IL10N */ |
|
49 | - private $IL10N; |
|
50 | - |
|
51 | - /** @var string */ |
|
52 | - private $dbTable = 'trusted_servers'; |
|
53 | - |
|
54 | - /** |
|
55 | - * @param IDBConnection $connection |
|
56 | - * @param IL10N $il10n |
|
57 | - */ |
|
58 | - public function __construct( |
|
59 | - IDBConnection $connection, |
|
60 | - IL10N $il10n |
|
61 | - ) { |
|
62 | - $this->connection = $connection; |
|
63 | - $this->IL10N = $il10n; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * add server to the list of trusted servers |
|
68 | - * |
|
69 | - * @param string $url |
|
70 | - * @return int |
|
71 | - * @throws HintException |
|
72 | - */ |
|
73 | - public function addServer($url) { |
|
74 | - $hash = $this->hash($url); |
|
75 | - $url = rtrim($url, '/'); |
|
76 | - $query = $this->connection->getQueryBuilder(); |
|
77 | - $query->insert($this->dbTable) |
|
78 | - ->values( |
|
79 | - [ |
|
80 | - 'url' => $query->createParameter('url'), |
|
81 | - 'url_hash' => $query->createParameter('url_hash'), |
|
82 | - ] |
|
83 | - ) |
|
84 | - ->setParameter('url', $url) |
|
85 | - ->setParameter('url_hash', $hash); |
|
86 | - |
|
87 | - $result = $query->execute(); |
|
88 | - |
|
89 | - if ($result) { |
|
90 | - return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable); |
|
91 | - } |
|
92 | - |
|
93 | - $message = 'Internal failure, Could not add trusted server: ' . $url; |
|
94 | - $message_t = $this->IL10N->t('Could not add server'); |
|
95 | - throw new HintException($message, $message_t); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * remove server from the list of trusted servers |
|
100 | - * |
|
101 | - * @param int $id |
|
102 | - */ |
|
103 | - public function removeServer($id) { |
|
104 | - $query = $this->connection->getQueryBuilder(); |
|
105 | - $query->delete($this->dbTable) |
|
106 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
107 | - ->setParameter('id', $id); |
|
108 | - $query->execute(); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * get trusted server with given ID |
|
113 | - * |
|
114 | - * @param int $id |
|
115 | - * @return array |
|
116 | - * @throws \Exception |
|
117 | - */ |
|
118 | - public function getServerById($id) { |
|
119 | - $query = $this->connection->getQueryBuilder(); |
|
120 | - $query->select('*')->from($this->dbTable) |
|
121 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
122 | - ->setParameter('id', $id); |
|
123 | - $query->execute(); |
|
124 | - $result = $query->execute()->fetchAll(); |
|
125 | - |
|
126 | - if (empty($result)) { |
|
127 | - throw new \Exception('No Server found with ID: ' . $id); |
|
128 | - } |
|
129 | - |
|
130 | - return $result[0]; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * get all trusted servers |
|
135 | - * |
|
136 | - * @return array |
|
137 | - */ |
|
138 | - public function getAllServer() { |
|
139 | - $query = $this->connection->getQueryBuilder(); |
|
140 | - $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token']) |
|
141 | - ->from($this->dbTable); |
|
142 | - $statement = $query->execute(); |
|
143 | - $result = $statement->fetchAll(); |
|
144 | - $statement->closeCursor(); |
|
145 | - return $result; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * check if server already exists in the database table |
|
150 | - * |
|
151 | - * @param string $url |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - public function serverExists($url) { |
|
155 | - $hash = $this->hash($url); |
|
156 | - $query = $this->connection->getQueryBuilder(); |
|
157 | - $query->select('url') |
|
158 | - ->from($this->dbTable) |
|
159 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
160 | - ->setParameter('url_hash', $hash); |
|
161 | - $statement = $query->execute(); |
|
162 | - $result = $statement->fetchAll(); |
|
163 | - $statement->closeCursor(); |
|
164 | - |
|
165 | - return !empty($result); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * write token to database. Token is used to exchange the secret |
|
170 | - * |
|
171 | - * @param string $url |
|
172 | - * @param string $token |
|
173 | - */ |
|
174 | - public function addToken($url, $token) { |
|
175 | - $hash = $this->hash($url); |
|
176 | - $query = $this->connection->getQueryBuilder(); |
|
177 | - $query->update($this->dbTable) |
|
178 | - ->set('token', $query->createParameter('token')) |
|
179 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
180 | - ->setParameter('url_hash', $hash) |
|
181 | - ->setParameter('token', $token); |
|
182 | - $query->execute(); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * get token stored in database |
|
187 | - * |
|
188 | - * @param string $url |
|
189 | - * @return string |
|
190 | - * @throws \Exception |
|
191 | - */ |
|
192 | - public function getToken($url) { |
|
193 | - $hash = $this->hash($url); |
|
194 | - $query = $this->connection->getQueryBuilder(); |
|
195 | - $query->select('token')->from($this->dbTable) |
|
196 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
197 | - ->setParameter('url_hash', $hash); |
|
198 | - |
|
199 | - $statement = $query->execute(); |
|
200 | - $result = $statement->fetch(); |
|
201 | - $statement->closeCursor(); |
|
202 | - |
|
203 | - if (!isset($result['token'])) { |
|
204 | - throw new \Exception('No token found for: ' . $url); |
|
205 | - } |
|
206 | - |
|
207 | - return $result['token']; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * add shared Secret to database |
|
212 | - * |
|
213 | - * @param string $url |
|
214 | - * @param string $sharedSecret |
|
215 | - */ |
|
216 | - public function addSharedSecret($url, $sharedSecret) { |
|
217 | - $hash = $this->hash($url); |
|
218 | - $query = $this->connection->getQueryBuilder(); |
|
219 | - $query->update($this->dbTable) |
|
220 | - ->set('shared_secret', $query->createParameter('sharedSecret')) |
|
221 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
222 | - ->setParameter('url_hash', $hash) |
|
223 | - ->setParameter('sharedSecret', $sharedSecret); |
|
224 | - $query->execute(); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * get shared secret from database |
|
229 | - * |
|
230 | - * @param string $url |
|
231 | - * @return string |
|
232 | - */ |
|
233 | - public function getSharedSecret($url) { |
|
234 | - $hash = $this->hash($url); |
|
235 | - $query = $this->connection->getQueryBuilder(); |
|
236 | - $query->select('shared_secret')->from($this->dbTable) |
|
237 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
238 | - ->setParameter('url_hash', $hash); |
|
239 | - |
|
240 | - $statement = $query->execute(); |
|
241 | - $result = $statement->fetch(); |
|
242 | - $statement->closeCursor(); |
|
243 | - return $result['shared_secret']; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * set server status |
|
248 | - * |
|
249 | - * @param string $url |
|
250 | - * @param int $status |
|
251 | - * @param string|null $token |
|
252 | - */ |
|
253 | - public function setServerStatus($url, $status, $token = null) { |
|
254 | - $hash = $this->hash($url); |
|
255 | - $query = $this->connection->getQueryBuilder(); |
|
256 | - $query->update($this->dbTable) |
|
257 | - ->set('status', $query->createNamedParameter($status)) |
|
258 | - ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); |
|
259 | - if (!is_null($token)) { |
|
260 | - $query->set('sync_token', $query->createNamedParameter($token)); |
|
261 | - } |
|
262 | - $query->execute(); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * get server status |
|
267 | - * |
|
268 | - * @param string $url |
|
269 | - * @return int |
|
270 | - */ |
|
271 | - public function getServerStatus($url) { |
|
272 | - $hash = $this->hash($url); |
|
273 | - $query = $this->connection->getQueryBuilder(); |
|
274 | - $query->select('status')->from($this->dbTable) |
|
275 | - ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
276 | - ->setParameter('url_hash', $hash); |
|
277 | - |
|
278 | - $statement = $query->execute(); |
|
279 | - $result = $statement->fetch(); |
|
280 | - $statement->closeCursor(); |
|
281 | - return (int)$result['status']; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * create hash from URL |
|
286 | - * |
|
287 | - * @param string $url |
|
288 | - * @return string |
|
289 | - */ |
|
290 | - protected function hash($url) { |
|
291 | - $normalized = $this->normalizeUrl($url); |
|
292 | - return sha1($normalized); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * normalize URL, used to create the sha1 hash |
|
297 | - * |
|
298 | - * @param string $url |
|
299 | - * @return string |
|
300 | - */ |
|
301 | - protected function normalizeUrl($url) { |
|
302 | - $normalized = $url; |
|
303 | - |
|
304 | - if (strpos($url, 'https://') === 0) { |
|
305 | - $normalized = substr($url, strlen('https://')); |
|
306 | - } else if (strpos($url, 'http://') === 0) { |
|
307 | - $normalized = substr($url, strlen('http://')); |
|
308 | - } |
|
309 | - |
|
310 | - $normalized = Filesystem::normalizePath($normalized); |
|
311 | - $normalized = trim($normalized, '/'); |
|
312 | - |
|
313 | - return $normalized; |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * @param $username |
|
318 | - * @param $password |
|
319 | - * @return bool |
|
320 | - */ |
|
321 | - public function auth($username, $password) { |
|
322 | - if ($username !== 'system') { |
|
323 | - return false; |
|
324 | - } |
|
325 | - $query = $this->connection->getQueryBuilder(); |
|
326 | - $query->select('url')->from($this->dbTable) |
|
327 | - ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); |
|
328 | - |
|
329 | - $statement = $query->execute(); |
|
330 | - $result = $statement->fetch(); |
|
331 | - $statement->closeCursor(); |
|
332 | - return !empty($result); |
|
333 | - } |
|
45 | + /** @var IDBConnection */ |
|
46 | + private $connection; |
|
47 | + |
|
48 | + /** @var IL10N */ |
|
49 | + private $IL10N; |
|
50 | + |
|
51 | + /** @var string */ |
|
52 | + private $dbTable = 'trusted_servers'; |
|
53 | + |
|
54 | + /** |
|
55 | + * @param IDBConnection $connection |
|
56 | + * @param IL10N $il10n |
|
57 | + */ |
|
58 | + public function __construct( |
|
59 | + IDBConnection $connection, |
|
60 | + IL10N $il10n |
|
61 | + ) { |
|
62 | + $this->connection = $connection; |
|
63 | + $this->IL10N = $il10n; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * add server to the list of trusted servers |
|
68 | + * |
|
69 | + * @param string $url |
|
70 | + * @return int |
|
71 | + * @throws HintException |
|
72 | + */ |
|
73 | + public function addServer($url) { |
|
74 | + $hash = $this->hash($url); |
|
75 | + $url = rtrim($url, '/'); |
|
76 | + $query = $this->connection->getQueryBuilder(); |
|
77 | + $query->insert($this->dbTable) |
|
78 | + ->values( |
|
79 | + [ |
|
80 | + 'url' => $query->createParameter('url'), |
|
81 | + 'url_hash' => $query->createParameter('url_hash'), |
|
82 | + ] |
|
83 | + ) |
|
84 | + ->setParameter('url', $url) |
|
85 | + ->setParameter('url_hash', $hash); |
|
86 | + |
|
87 | + $result = $query->execute(); |
|
88 | + |
|
89 | + if ($result) { |
|
90 | + return (int)$this->connection->lastInsertId('*PREFIX*'.$this->dbTable); |
|
91 | + } |
|
92 | + |
|
93 | + $message = 'Internal failure, Could not add trusted server: ' . $url; |
|
94 | + $message_t = $this->IL10N->t('Could not add server'); |
|
95 | + throw new HintException($message, $message_t); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * remove server from the list of trusted servers |
|
100 | + * |
|
101 | + * @param int $id |
|
102 | + */ |
|
103 | + public function removeServer($id) { |
|
104 | + $query = $this->connection->getQueryBuilder(); |
|
105 | + $query->delete($this->dbTable) |
|
106 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
107 | + ->setParameter('id', $id); |
|
108 | + $query->execute(); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * get trusted server with given ID |
|
113 | + * |
|
114 | + * @param int $id |
|
115 | + * @return array |
|
116 | + * @throws \Exception |
|
117 | + */ |
|
118 | + public function getServerById($id) { |
|
119 | + $query = $this->connection->getQueryBuilder(); |
|
120 | + $query->select('*')->from($this->dbTable) |
|
121 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
122 | + ->setParameter('id', $id); |
|
123 | + $query->execute(); |
|
124 | + $result = $query->execute()->fetchAll(); |
|
125 | + |
|
126 | + if (empty($result)) { |
|
127 | + throw new \Exception('No Server found with ID: ' . $id); |
|
128 | + } |
|
129 | + |
|
130 | + return $result[0]; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * get all trusted servers |
|
135 | + * |
|
136 | + * @return array |
|
137 | + */ |
|
138 | + public function getAllServer() { |
|
139 | + $query = $this->connection->getQueryBuilder(); |
|
140 | + $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token']) |
|
141 | + ->from($this->dbTable); |
|
142 | + $statement = $query->execute(); |
|
143 | + $result = $statement->fetchAll(); |
|
144 | + $statement->closeCursor(); |
|
145 | + return $result; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * check if server already exists in the database table |
|
150 | + * |
|
151 | + * @param string $url |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + public function serverExists($url) { |
|
155 | + $hash = $this->hash($url); |
|
156 | + $query = $this->connection->getQueryBuilder(); |
|
157 | + $query->select('url') |
|
158 | + ->from($this->dbTable) |
|
159 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
160 | + ->setParameter('url_hash', $hash); |
|
161 | + $statement = $query->execute(); |
|
162 | + $result = $statement->fetchAll(); |
|
163 | + $statement->closeCursor(); |
|
164 | + |
|
165 | + return !empty($result); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * write token to database. Token is used to exchange the secret |
|
170 | + * |
|
171 | + * @param string $url |
|
172 | + * @param string $token |
|
173 | + */ |
|
174 | + public function addToken($url, $token) { |
|
175 | + $hash = $this->hash($url); |
|
176 | + $query = $this->connection->getQueryBuilder(); |
|
177 | + $query->update($this->dbTable) |
|
178 | + ->set('token', $query->createParameter('token')) |
|
179 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
180 | + ->setParameter('url_hash', $hash) |
|
181 | + ->setParameter('token', $token); |
|
182 | + $query->execute(); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * get token stored in database |
|
187 | + * |
|
188 | + * @param string $url |
|
189 | + * @return string |
|
190 | + * @throws \Exception |
|
191 | + */ |
|
192 | + public function getToken($url) { |
|
193 | + $hash = $this->hash($url); |
|
194 | + $query = $this->connection->getQueryBuilder(); |
|
195 | + $query->select('token')->from($this->dbTable) |
|
196 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
197 | + ->setParameter('url_hash', $hash); |
|
198 | + |
|
199 | + $statement = $query->execute(); |
|
200 | + $result = $statement->fetch(); |
|
201 | + $statement->closeCursor(); |
|
202 | + |
|
203 | + if (!isset($result['token'])) { |
|
204 | + throw new \Exception('No token found for: ' . $url); |
|
205 | + } |
|
206 | + |
|
207 | + return $result['token']; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * add shared Secret to database |
|
212 | + * |
|
213 | + * @param string $url |
|
214 | + * @param string $sharedSecret |
|
215 | + */ |
|
216 | + public function addSharedSecret($url, $sharedSecret) { |
|
217 | + $hash = $this->hash($url); |
|
218 | + $query = $this->connection->getQueryBuilder(); |
|
219 | + $query->update($this->dbTable) |
|
220 | + ->set('shared_secret', $query->createParameter('sharedSecret')) |
|
221 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
222 | + ->setParameter('url_hash', $hash) |
|
223 | + ->setParameter('sharedSecret', $sharedSecret); |
|
224 | + $query->execute(); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * get shared secret from database |
|
229 | + * |
|
230 | + * @param string $url |
|
231 | + * @return string |
|
232 | + */ |
|
233 | + public function getSharedSecret($url) { |
|
234 | + $hash = $this->hash($url); |
|
235 | + $query = $this->connection->getQueryBuilder(); |
|
236 | + $query->select('shared_secret')->from($this->dbTable) |
|
237 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
238 | + ->setParameter('url_hash', $hash); |
|
239 | + |
|
240 | + $statement = $query->execute(); |
|
241 | + $result = $statement->fetch(); |
|
242 | + $statement->closeCursor(); |
|
243 | + return $result['shared_secret']; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * set server status |
|
248 | + * |
|
249 | + * @param string $url |
|
250 | + * @param int $status |
|
251 | + * @param string|null $token |
|
252 | + */ |
|
253 | + public function setServerStatus($url, $status, $token = null) { |
|
254 | + $hash = $this->hash($url); |
|
255 | + $query = $this->connection->getQueryBuilder(); |
|
256 | + $query->update($this->dbTable) |
|
257 | + ->set('status', $query->createNamedParameter($status)) |
|
258 | + ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash))); |
|
259 | + if (!is_null($token)) { |
|
260 | + $query->set('sync_token', $query->createNamedParameter($token)); |
|
261 | + } |
|
262 | + $query->execute(); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * get server status |
|
267 | + * |
|
268 | + * @param string $url |
|
269 | + * @return int |
|
270 | + */ |
|
271 | + public function getServerStatus($url) { |
|
272 | + $hash = $this->hash($url); |
|
273 | + $query = $this->connection->getQueryBuilder(); |
|
274 | + $query->select('status')->from($this->dbTable) |
|
275 | + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) |
|
276 | + ->setParameter('url_hash', $hash); |
|
277 | + |
|
278 | + $statement = $query->execute(); |
|
279 | + $result = $statement->fetch(); |
|
280 | + $statement->closeCursor(); |
|
281 | + return (int)$result['status']; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * create hash from URL |
|
286 | + * |
|
287 | + * @param string $url |
|
288 | + * @return string |
|
289 | + */ |
|
290 | + protected function hash($url) { |
|
291 | + $normalized = $this->normalizeUrl($url); |
|
292 | + return sha1($normalized); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * normalize URL, used to create the sha1 hash |
|
297 | + * |
|
298 | + * @param string $url |
|
299 | + * @return string |
|
300 | + */ |
|
301 | + protected function normalizeUrl($url) { |
|
302 | + $normalized = $url; |
|
303 | + |
|
304 | + if (strpos($url, 'https://') === 0) { |
|
305 | + $normalized = substr($url, strlen('https://')); |
|
306 | + } else if (strpos($url, 'http://') === 0) { |
|
307 | + $normalized = substr($url, strlen('http://')); |
|
308 | + } |
|
309 | + |
|
310 | + $normalized = Filesystem::normalizePath($normalized); |
|
311 | + $normalized = trim($normalized, '/'); |
|
312 | + |
|
313 | + return $normalized; |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * @param $username |
|
318 | + * @param $password |
|
319 | + * @return bool |
|
320 | + */ |
|
321 | + public function auth($username, $password) { |
|
322 | + if ($username !== 'system') { |
|
323 | + return false; |
|
324 | + } |
|
325 | + $query = $this->connection->getQueryBuilder(); |
|
326 | + $query->select('url')->from($this->dbTable) |
|
327 | + ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password))); |
|
328 | + |
|
329 | + $statement = $query->execute(); |
|
330 | + $result = $statement->fetch(); |
|
331 | + $statement->closeCursor(); |
|
332 | + return !empty($result); |
|
333 | + } |
|
334 | 334 | |
335 | 335 | } |