@@ -55,549 +55,549 @@ |
||
55 | 55 | use Psr\Log\LoggerInterface; |
56 | 56 | |
57 | 57 | class SetupManager { |
58 | - private bool $rootSetup = false; |
|
59 | - // List of users for which at least one mount is setup |
|
60 | - private array $setupUsers = []; |
|
61 | - // List of users for which all mounts are setup |
|
62 | - private array $setupUsersComplete = []; |
|
63 | - /** @var array<string, string[]> */ |
|
64 | - private array $setupUserMountProviders = []; |
|
65 | - private ICache $cache; |
|
66 | - private bool $listeningForProviders; |
|
67 | - private array $fullSetupRequired = []; |
|
68 | - private bool $setupBuiltinWrappersDone = false; |
|
69 | - private bool $forceFullSetup = false; |
|
70 | - |
|
71 | - public function __construct( |
|
72 | - private IEventLogger $eventLogger, |
|
73 | - private MountProviderCollection $mountProviderCollection, |
|
74 | - private IMountManager $mountManager, |
|
75 | - private IUserManager $userManager, |
|
76 | - private IEventDispatcher $eventDispatcher, |
|
77 | - private IUserMountCache $userMountCache, |
|
78 | - private ILockdownManager $lockdownManager, |
|
79 | - private IUserSession $userSession, |
|
80 | - ICacheFactory $cacheFactory, |
|
81 | - private LoggerInterface $logger, |
|
82 | - private IConfig $config, |
|
83 | - private ShareDisableChecker $shareDisableChecker, |
|
84 | - private IAppManager $appManager, |
|
85 | - ) { |
|
86 | - $this->cache = $cacheFactory->createDistributed('setupmanager::'); |
|
87 | - $this->listeningForProviders = false; |
|
88 | - $this->forceFullSetup = $this->config->getSystemValueBool('debug.force-full-fs-setup'); |
|
89 | - |
|
90 | - $this->setupListeners(); |
|
91 | - } |
|
92 | - |
|
93 | - private function isSetupStarted(IUser $user): bool { |
|
94 | - return in_array($user->getUID(), $this->setupUsers, true); |
|
95 | - } |
|
96 | - |
|
97 | - public function isSetupComplete(IUser $user): bool { |
|
98 | - return in_array($user->getUID(), $this->setupUsersComplete, true); |
|
99 | - } |
|
100 | - |
|
101 | - private function setupBuiltinWrappers() { |
|
102 | - if ($this->setupBuiltinWrappersDone) { |
|
103 | - return; |
|
104 | - } |
|
105 | - $this->setupBuiltinWrappersDone = true; |
|
106 | - |
|
107 | - // load all filesystem apps before, so no setup-hook gets lost |
|
108 | - $this->appManager->loadApps(['filesystem']); |
|
109 | - $prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
110 | - |
|
111 | - Filesystem::addStorageWrapper('mount_options', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
112 | - if ($storage->instanceOfStorage(Common::class)) { |
|
113 | - $options = array_merge($mount->getOptions(), ['mount_point' => $mountPoint]); |
|
114 | - $storage->setMountOptions($options); |
|
115 | - } |
|
116 | - return $storage; |
|
117 | - }); |
|
118 | - |
|
119 | - $reSharingEnabled = Share::isResharingAllowed(); |
|
120 | - $user = $this->userSession->getUser(); |
|
121 | - $sharingEnabledForUser = $user ? !$this->shareDisableChecker->sharingDisabledForUser($user->getUID()) : true; |
|
122 | - Filesystem::addStorageWrapper( |
|
123 | - 'sharing_mask', |
|
124 | - function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEnabled, $sharingEnabledForUser) { |
|
125 | - $sharingEnabledForMount = $mount->getOption('enable_sharing', true); |
|
126 | - $isShared = $mount instanceof ISharedMountPoint; |
|
127 | - if (!$sharingEnabledForMount || !$sharingEnabledForUser || (!$reSharingEnabled && $isShared)) { |
|
128 | - return new PermissionsMask([ |
|
129 | - 'storage' => $storage, |
|
130 | - 'mask' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE, |
|
131 | - ]); |
|
132 | - } |
|
133 | - return $storage; |
|
134 | - } |
|
135 | - ); |
|
136 | - |
|
137 | - // install storage availability wrapper, before most other wrappers |
|
138 | - Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
139 | - $externalMount = $mount instanceof ExternalMountPoint || $mount instanceof Mount; |
|
140 | - if ($externalMount && !$storage->isLocal()) { |
|
141 | - return new Availability(['storage' => $storage]); |
|
142 | - } |
|
143 | - return $storage; |
|
144 | - }); |
|
145 | - |
|
146 | - Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
147 | - if ($mount->getOption('encoding_compatibility', false) && !$mount instanceof SharedMount) { |
|
148 | - return new Encoding(['storage' => $storage]); |
|
149 | - } |
|
150 | - return $storage; |
|
151 | - }); |
|
152 | - |
|
153 | - $quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false); |
|
154 | - Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) { |
|
155 | - // set up quota for home storages, even for other users |
|
156 | - // which can happen when using sharing |
|
157 | - if ($mount instanceof HomeMountPoint) { |
|
158 | - $user = $mount->getUser(); |
|
159 | - return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) { |
|
160 | - return $user->getQuotaBytes(); |
|
161 | - }, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]); |
|
162 | - } |
|
163 | - |
|
164 | - return $storage; |
|
165 | - }); |
|
166 | - |
|
167 | - Filesystem::addStorageWrapper('readonly', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
168 | - /* |
|
58 | + private bool $rootSetup = false; |
|
59 | + // List of users for which at least one mount is setup |
|
60 | + private array $setupUsers = []; |
|
61 | + // List of users for which all mounts are setup |
|
62 | + private array $setupUsersComplete = []; |
|
63 | + /** @var array<string, string[]> */ |
|
64 | + private array $setupUserMountProviders = []; |
|
65 | + private ICache $cache; |
|
66 | + private bool $listeningForProviders; |
|
67 | + private array $fullSetupRequired = []; |
|
68 | + private bool $setupBuiltinWrappersDone = false; |
|
69 | + private bool $forceFullSetup = false; |
|
70 | + |
|
71 | + public function __construct( |
|
72 | + private IEventLogger $eventLogger, |
|
73 | + private MountProviderCollection $mountProviderCollection, |
|
74 | + private IMountManager $mountManager, |
|
75 | + private IUserManager $userManager, |
|
76 | + private IEventDispatcher $eventDispatcher, |
|
77 | + private IUserMountCache $userMountCache, |
|
78 | + private ILockdownManager $lockdownManager, |
|
79 | + private IUserSession $userSession, |
|
80 | + ICacheFactory $cacheFactory, |
|
81 | + private LoggerInterface $logger, |
|
82 | + private IConfig $config, |
|
83 | + private ShareDisableChecker $shareDisableChecker, |
|
84 | + private IAppManager $appManager, |
|
85 | + ) { |
|
86 | + $this->cache = $cacheFactory->createDistributed('setupmanager::'); |
|
87 | + $this->listeningForProviders = false; |
|
88 | + $this->forceFullSetup = $this->config->getSystemValueBool('debug.force-full-fs-setup'); |
|
89 | + |
|
90 | + $this->setupListeners(); |
|
91 | + } |
|
92 | + |
|
93 | + private function isSetupStarted(IUser $user): bool { |
|
94 | + return in_array($user->getUID(), $this->setupUsers, true); |
|
95 | + } |
|
96 | + |
|
97 | + public function isSetupComplete(IUser $user): bool { |
|
98 | + return in_array($user->getUID(), $this->setupUsersComplete, true); |
|
99 | + } |
|
100 | + |
|
101 | + private function setupBuiltinWrappers() { |
|
102 | + if ($this->setupBuiltinWrappersDone) { |
|
103 | + return; |
|
104 | + } |
|
105 | + $this->setupBuiltinWrappersDone = true; |
|
106 | + |
|
107 | + // load all filesystem apps before, so no setup-hook gets lost |
|
108 | + $this->appManager->loadApps(['filesystem']); |
|
109 | + $prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
110 | + |
|
111 | + Filesystem::addStorageWrapper('mount_options', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
112 | + if ($storage->instanceOfStorage(Common::class)) { |
|
113 | + $options = array_merge($mount->getOptions(), ['mount_point' => $mountPoint]); |
|
114 | + $storage->setMountOptions($options); |
|
115 | + } |
|
116 | + return $storage; |
|
117 | + }); |
|
118 | + |
|
119 | + $reSharingEnabled = Share::isResharingAllowed(); |
|
120 | + $user = $this->userSession->getUser(); |
|
121 | + $sharingEnabledForUser = $user ? !$this->shareDisableChecker->sharingDisabledForUser($user->getUID()) : true; |
|
122 | + Filesystem::addStorageWrapper( |
|
123 | + 'sharing_mask', |
|
124 | + function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEnabled, $sharingEnabledForUser) { |
|
125 | + $sharingEnabledForMount = $mount->getOption('enable_sharing', true); |
|
126 | + $isShared = $mount instanceof ISharedMountPoint; |
|
127 | + if (!$sharingEnabledForMount || !$sharingEnabledForUser || (!$reSharingEnabled && $isShared)) { |
|
128 | + return new PermissionsMask([ |
|
129 | + 'storage' => $storage, |
|
130 | + 'mask' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE, |
|
131 | + ]); |
|
132 | + } |
|
133 | + return $storage; |
|
134 | + } |
|
135 | + ); |
|
136 | + |
|
137 | + // install storage availability wrapper, before most other wrappers |
|
138 | + Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
139 | + $externalMount = $mount instanceof ExternalMountPoint || $mount instanceof Mount; |
|
140 | + if ($externalMount && !$storage->isLocal()) { |
|
141 | + return new Availability(['storage' => $storage]); |
|
142 | + } |
|
143 | + return $storage; |
|
144 | + }); |
|
145 | + |
|
146 | + Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
147 | + if ($mount->getOption('encoding_compatibility', false) && !$mount instanceof SharedMount) { |
|
148 | + return new Encoding(['storage' => $storage]); |
|
149 | + } |
|
150 | + return $storage; |
|
151 | + }); |
|
152 | + |
|
153 | + $quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false); |
|
154 | + Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) { |
|
155 | + // set up quota for home storages, even for other users |
|
156 | + // which can happen when using sharing |
|
157 | + if ($mount instanceof HomeMountPoint) { |
|
158 | + $user = $mount->getUser(); |
|
159 | + return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) { |
|
160 | + return $user->getQuotaBytes(); |
|
161 | + }, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]); |
|
162 | + } |
|
163 | + |
|
164 | + return $storage; |
|
165 | + }); |
|
166 | + |
|
167 | + Filesystem::addStorageWrapper('readonly', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
168 | + /* |
|
169 | 169 | * Do not allow any operations that modify the storage |
170 | 170 | */ |
171 | - if ($mount->getOption('readonly', false)) { |
|
172 | - return new PermissionsMask([ |
|
173 | - 'storage' => $storage, |
|
174 | - 'mask' => Constants::PERMISSION_ALL & ~( |
|
175 | - Constants::PERMISSION_UPDATE |
|
176 | - | Constants::PERMISSION_CREATE |
|
177 | - | Constants::PERMISSION_DELETE |
|
178 | - ), |
|
179 | - ]); |
|
180 | - } |
|
181 | - return $storage; |
|
182 | - }); |
|
183 | - |
|
184 | - Filesystem::logWarningWhenAddingStorageWrapper($prevLogging); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Setup the full filesystem for the specified user |
|
189 | - */ |
|
190 | - public function setupForUser(IUser $user): void { |
|
191 | - if ($this->isSetupComplete($user)) { |
|
192 | - return; |
|
193 | - } |
|
194 | - $this->setupUsersComplete[] = $user->getUID(); |
|
195 | - |
|
196 | - $this->eventLogger->start('fs:setup:user:full', 'Setup full filesystem for user'); |
|
197 | - |
|
198 | - if (!isset($this->setupUserMountProviders[$user->getUID()])) { |
|
199 | - $this->setupUserMountProviders[$user->getUID()] = []; |
|
200 | - } |
|
201 | - |
|
202 | - $previouslySetupProviders = $this->setupUserMountProviders[$user->getUID()]; |
|
203 | - |
|
204 | - $this->setupForUserWith($user, function () use ($user) { |
|
205 | - $this->mountProviderCollection->addMountForUser($user, $this->mountManager, function ( |
|
206 | - IMountProvider $provider, |
|
207 | - ) use ($user) { |
|
208 | - return !in_array(get_class($provider), $this->setupUserMountProviders[$user->getUID()]); |
|
209 | - }); |
|
210 | - }); |
|
211 | - $this->afterUserFullySetup($user, $previouslySetupProviders); |
|
212 | - $this->eventLogger->end('fs:setup:user:full'); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * part of the user setup that is run only once per user |
|
217 | - */ |
|
218 | - private function oneTimeUserSetup(IUser $user) { |
|
219 | - if ($this->isSetupStarted($user)) { |
|
220 | - return; |
|
221 | - } |
|
222 | - $this->setupUsers[] = $user->getUID(); |
|
223 | - |
|
224 | - $this->setupRoot(); |
|
225 | - |
|
226 | - $this->eventLogger->start('fs:setup:user:onetime', 'Onetime filesystem for user'); |
|
227 | - |
|
228 | - $this->setupBuiltinWrappers(); |
|
229 | - |
|
230 | - $prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
231 | - |
|
232 | - // TODO remove hook |
|
233 | - OC_Hook::emit('OC_Filesystem', 'preSetup', ['user' => $user->getUID()]); |
|
234 | - |
|
235 | - $event = new BeforeFileSystemSetupEvent($user); |
|
236 | - $this->eventDispatcher->dispatchTyped($event); |
|
237 | - |
|
238 | - Filesystem::logWarningWhenAddingStorageWrapper($prevLogging); |
|
239 | - |
|
240 | - $userDir = '/' . $user->getUID() . '/files'; |
|
241 | - |
|
242 | - Filesystem::initInternal($userDir); |
|
243 | - |
|
244 | - if ($this->lockdownManager->canAccessFilesystem()) { |
|
245 | - $this->eventLogger->start('fs:setup:user:home', 'Setup home filesystem for user'); |
|
246 | - // home mounts are handled separate since we need to ensure this is mounted before we call the other mount providers |
|
247 | - $homeMount = $this->mountProviderCollection->getHomeMountForUser($user); |
|
248 | - $this->mountManager->addMount($homeMount); |
|
249 | - |
|
250 | - if ($homeMount->getStorageRootId() === -1) { |
|
251 | - $this->eventLogger->start('fs:setup:user:home:scan', 'Scan home filesystem for user'); |
|
252 | - $homeMount->getStorage()->mkdir(''); |
|
253 | - $homeMount->getStorage()->getScanner()->scan(''); |
|
254 | - $this->eventLogger->end('fs:setup:user:home:scan'); |
|
255 | - } |
|
256 | - $this->eventLogger->end('fs:setup:user:home'); |
|
257 | - } else { |
|
258 | - $this->mountManager->addMount(new MountPoint( |
|
259 | - new NullStorage([]), |
|
260 | - '/' . $user->getUID() |
|
261 | - )); |
|
262 | - $this->mountManager->addMount(new MountPoint( |
|
263 | - new NullStorage([]), |
|
264 | - '/' . $user->getUID() . '/files' |
|
265 | - )); |
|
266 | - $this->setupUsersComplete[] = $user->getUID(); |
|
267 | - } |
|
268 | - |
|
269 | - $this->listenForNewMountProviders(); |
|
270 | - |
|
271 | - $this->eventLogger->end('fs:setup:user:onetime'); |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Final housekeeping after a user has been fully setup |
|
276 | - */ |
|
277 | - private function afterUserFullySetup(IUser $user, array $previouslySetupProviders): void { |
|
278 | - $this->eventLogger->start('fs:setup:user:full:post', 'Housekeeping after user is setup'); |
|
279 | - $userRoot = '/' . $user->getUID() . '/'; |
|
280 | - $mounts = $this->mountManager->getAll(); |
|
281 | - $mounts = array_filter($mounts, function (IMountPoint $mount) use ($userRoot) { |
|
282 | - return str_starts_with($mount->getMountPoint(), $userRoot); |
|
283 | - }); |
|
284 | - $allProviders = array_map(function (IMountProvider|IHomeMountProvider|IRootMountProvider $provider) { |
|
285 | - return get_class($provider); |
|
286 | - }, array_merge( |
|
287 | - $this->mountProviderCollection->getProviders(), |
|
288 | - $this->mountProviderCollection->getHomeProviders(), |
|
289 | - $this->mountProviderCollection->getRootProviders(), |
|
290 | - )); |
|
291 | - $newProviders = array_diff($allProviders, $previouslySetupProviders); |
|
292 | - $mounts = array_filter($mounts, function (IMountPoint $mount) use ($previouslySetupProviders) { |
|
293 | - return !in_array($mount->getMountProvider(), $previouslySetupProviders); |
|
294 | - }); |
|
295 | - $this->userMountCache->registerMounts($user, $mounts, $newProviders); |
|
296 | - |
|
297 | - $cacheDuration = $this->config->getSystemValueInt('fs_mount_cache_duration', 5 * 60); |
|
298 | - if ($cacheDuration > 0) { |
|
299 | - $this->cache->set($user->getUID(), true, $cacheDuration); |
|
300 | - $this->fullSetupRequired[$user->getUID()] = false; |
|
301 | - } |
|
302 | - $this->eventLogger->end('fs:setup:user:full:post'); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @param IUser $user |
|
307 | - * @param IMountPoint $mounts |
|
308 | - * @return void |
|
309 | - * @throws \OCP\HintException |
|
310 | - * @throws \OC\ServerNotAvailableException |
|
311 | - */ |
|
312 | - private function setupForUserWith(IUser $user, callable $mountCallback): void { |
|
313 | - $this->oneTimeUserSetup($user); |
|
314 | - |
|
315 | - if ($this->lockdownManager->canAccessFilesystem()) { |
|
316 | - $mountCallback(); |
|
317 | - } |
|
318 | - $this->eventLogger->start('fs:setup:user:post-init-mountpoint', 'post_initMountPoints legacy hook'); |
|
319 | - \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', ['user' => $user->getUID()]); |
|
320 | - $this->eventLogger->end('fs:setup:user:post-init-mountpoint'); |
|
321 | - |
|
322 | - $userDir = '/' . $user->getUID() . '/files'; |
|
323 | - $this->eventLogger->start('fs:setup:user:setup-hook', 'setup legacy hook'); |
|
324 | - OC_Hook::emit('OC_Filesystem', 'setup', ['user' => $user->getUID(), 'user_dir' => $userDir]); |
|
325 | - $this->eventLogger->end('fs:setup:user:setup-hook'); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Set up the root filesystem |
|
330 | - */ |
|
331 | - public function setupRoot(): void { |
|
332 | - //setting up the filesystem twice can only lead to trouble |
|
333 | - if ($this->rootSetup) { |
|
334 | - return; |
|
335 | - } |
|
336 | - |
|
337 | - $this->setupBuiltinWrappers(); |
|
338 | - |
|
339 | - $this->rootSetup = true; |
|
340 | - |
|
341 | - $this->eventLogger->start('fs:setup:root', 'Setup root filesystem'); |
|
342 | - |
|
343 | - $rootMounts = $this->mountProviderCollection->getRootMounts(); |
|
344 | - foreach ($rootMounts as $rootMountProvider) { |
|
345 | - $this->mountManager->addMount($rootMountProvider); |
|
346 | - } |
|
347 | - |
|
348 | - $this->eventLogger->end('fs:setup:root'); |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Get the user to setup for a path or `null` if the root needs to be setup |
|
353 | - * |
|
354 | - * @param string $path |
|
355 | - * @return IUser|null |
|
356 | - */ |
|
357 | - private function getUserForPath(string $path) { |
|
358 | - if (str_starts_with($path, '/__groupfolders')) { |
|
359 | - return null; |
|
360 | - } elseif (substr_count($path, '/') < 2) { |
|
361 | - if ($user = $this->userSession->getUser()) { |
|
362 | - return $user; |
|
363 | - } else { |
|
364 | - return null; |
|
365 | - } |
|
366 | - } elseif (str_starts_with($path, '/appdata_' . \OC_Util::getInstanceId()) || str_starts_with($path, '/files_external/')) { |
|
367 | - return null; |
|
368 | - } else { |
|
369 | - [, $userId] = explode('/', $path); |
|
370 | - } |
|
371 | - |
|
372 | - return $this->userManager->get($userId); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Set up the filesystem for the specified path |
|
377 | - */ |
|
378 | - public function setupForPath(string $path, bool $includeChildren = false): void { |
|
379 | - $user = $this->getUserForPath($path); |
|
380 | - if (!$user) { |
|
381 | - $this->setupRoot(); |
|
382 | - return; |
|
383 | - } |
|
384 | - |
|
385 | - if ($this->isSetupComplete($user)) { |
|
386 | - return; |
|
387 | - } |
|
388 | - |
|
389 | - if ($this->fullSetupRequired($user)) { |
|
390 | - $this->setupForUser($user); |
|
391 | - return; |
|
392 | - } |
|
393 | - |
|
394 | - // for the user's home folder, and includes children we need everything always |
|
395 | - if (rtrim($path) === '/' . $user->getUID() . '/files' && $includeChildren) { |
|
396 | - $this->setupForUser($user); |
|
397 | - return; |
|
398 | - } |
|
399 | - |
|
400 | - if (!isset($this->setupUserMountProviders[$user->getUID()])) { |
|
401 | - $this->setupUserMountProviders[$user->getUID()] = []; |
|
402 | - } |
|
403 | - $setupProviders = &$this->setupUserMountProviders[$user->getUID()]; |
|
404 | - $currentProviders = []; |
|
405 | - |
|
406 | - try { |
|
407 | - $cachedMount = $this->userMountCache->getMountForPath($user, $path); |
|
408 | - } catch (NotFoundException $e) { |
|
409 | - $this->setupForUser($user); |
|
410 | - return; |
|
411 | - } |
|
412 | - |
|
413 | - $this->oneTimeUserSetup($user); |
|
414 | - |
|
415 | - $this->eventLogger->start('fs:setup:user:path', "Setup $path filesystem for user"); |
|
416 | - $this->eventLogger->start('fs:setup:user:path:find', "Find mountpoint for $path"); |
|
417 | - |
|
418 | - $mounts = []; |
|
419 | - if (!in_array($cachedMount->getMountProvider(), $setupProviders)) { |
|
420 | - $currentProviders[] = $cachedMount->getMountProvider(); |
|
421 | - if ($cachedMount->getMountProvider()) { |
|
422 | - $setupProviders[] = $cachedMount->getMountProvider(); |
|
423 | - $mounts = $this->mountProviderCollection->getUserMountsForProviderClasses($user, [$cachedMount->getMountProvider()]); |
|
424 | - } else { |
|
425 | - $this->logger->debug('mount at ' . $cachedMount->getMountPoint() . ' has no provider set, performing full setup'); |
|
426 | - $this->eventLogger->end('fs:setup:user:path:find'); |
|
427 | - $this->setupForUser($user); |
|
428 | - $this->eventLogger->end('fs:setup:user:path'); |
|
429 | - return; |
|
430 | - } |
|
431 | - } |
|
432 | - |
|
433 | - if ($includeChildren) { |
|
434 | - $subCachedMounts = $this->userMountCache->getMountsInPath($user, $path); |
|
435 | - $this->eventLogger->end('fs:setup:user:path:find'); |
|
436 | - |
|
437 | - $needsFullSetup = array_reduce($subCachedMounts, function (bool $needsFullSetup, ICachedMountInfo $cachedMountInfo) { |
|
438 | - return $needsFullSetup || $cachedMountInfo->getMountProvider() === ''; |
|
439 | - }, false); |
|
440 | - |
|
441 | - if ($needsFullSetup) { |
|
442 | - $this->logger->debug('mount has no provider set, performing full setup'); |
|
443 | - $this->setupForUser($user); |
|
444 | - $this->eventLogger->end('fs:setup:user:path'); |
|
445 | - return; |
|
446 | - } else { |
|
447 | - foreach ($subCachedMounts as $cachedMount) { |
|
448 | - if (!in_array($cachedMount->getMountProvider(), $setupProviders)) { |
|
449 | - $currentProviders[] = $cachedMount->getMountProvider(); |
|
450 | - $setupProviders[] = $cachedMount->getMountProvider(); |
|
451 | - $mounts = array_merge($mounts, $this->mountProviderCollection->getUserMountsForProviderClasses($user, [$cachedMount->getMountProvider()])); |
|
452 | - } |
|
453 | - } |
|
454 | - } |
|
455 | - } else { |
|
456 | - $this->eventLogger->end('fs:setup:user:path:find'); |
|
457 | - } |
|
458 | - |
|
459 | - if (count($mounts)) { |
|
460 | - $this->userMountCache->registerMounts($user, $mounts, $currentProviders); |
|
461 | - $this->setupForUserWith($user, function () use ($mounts) { |
|
462 | - array_walk($mounts, [$this->mountManager, 'addMount']); |
|
463 | - }); |
|
464 | - } elseif (!$this->isSetupStarted($user)) { |
|
465 | - $this->oneTimeUserSetup($user); |
|
466 | - } |
|
467 | - $this->eventLogger->end('fs:setup:user:path'); |
|
468 | - } |
|
469 | - |
|
470 | - private function fullSetupRequired(IUser $user): bool { |
|
471 | - if ($this->forceFullSetup) { |
|
472 | - return true; |
|
473 | - } |
|
474 | - |
|
475 | - // we perform a "cached" setup only after having done the full setup recently |
|
476 | - // this is also used to trigger a full setup after handling events that are likely |
|
477 | - // to change the available mounts |
|
478 | - if (!isset($this->fullSetupRequired[$user->getUID()])) { |
|
479 | - $this->fullSetupRequired[$user->getUID()] = !$this->cache->get($user->getUID()); |
|
480 | - } |
|
481 | - return $this->fullSetupRequired[$user->getUID()]; |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * @param string $path |
|
486 | - * @param string[] $providers |
|
487 | - */ |
|
488 | - public function setupForProvider(string $path, array $providers): void { |
|
489 | - $user = $this->getUserForPath($path); |
|
490 | - if (!$user) { |
|
491 | - $this->setupRoot(); |
|
492 | - return; |
|
493 | - } |
|
494 | - |
|
495 | - if ($this->isSetupComplete($user)) { |
|
496 | - return; |
|
497 | - } |
|
498 | - |
|
499 | - if ($this->fullSetupRequired($user)) { |
|
500 | - $this->setupForUser($user); |
|
501 | - return; |
|
502 | - } |
|
503 | - |
|
504 | - $this->eventLogger->start('fs:setup:user:providers', 'Setup filesystem for ' . implode(', ', $providers)); |
|
505 | - |
|
506 | - $this->oneTimeUserSetup($user); |
|
507 | - |
|
508 | - // home providers are always used |
|
509 | - $providers = array_filter($providers, function (string $provider) { |
|
510 | - return !is_subclass_of($provider, IHomeMountProvider::class); |
|
511 | - }); |
|
512 | - |
|
513 | - if (in_array('', $providers)) { |
|
514 | - $this->setupForUser($user); |
|
515 | - return; |
|
516 | - } |
|
517 | - $setupProviders = $this->setupUserMountProviders[$user->getUID()] ?? []; |
|
518 | - |
|
519 | - $providers = array_diff($providers, $setupProviders); |
|
520 | - if (count($providers) === 0) { |
|
521 | - if (!$this->isSetupStarted($user)) { |
|
522 | - $this->oneTimeUserSetup($user); |
|
523 | - } |
|
524 | - $this->eventLogger->end('fs:setup:user:providers'); |
|
525 | - return; |
|
526 | - } else { |
|
527 | - $this->setupUserMountProviders[$user->getUID()] = array_merge($setupProviders, $providers); |
|
528 | - $mounts = $this->mountProviderCollection->getUserMountsForProviderClasses($user, $providers); |
|
529 | - } |
|
530 | - |
|
531 | - $this->userMountCache->registerMounts($user, $mounts, $providers); |
|
532 | - $this->setupForUserWith($user, function () use ($mounts) { |
|
533 | - array_walk($mounts, [$this->mountManager, 'addMount']); |
|
534 | - }); |
|
535 | - $this->eventLogger->end('fs:setup:user:providers'); |
|
536 | - } |
|
537 | - |
|
538 | - public function tearDown() { |
|
539 | - $this->setupUsers = []; |
|
540 | - $this->setupUsersComplete = []; |
|
541 | - $this->setupUserMountProviders = []; |
|
542 | - $this->fullSetupRequired = []; |
|
543 | - $this->rootSetup = false; |
|
544 | - $this->mountManager->clear(); |
|
545 | - $this->eventDispatcher->dispatchTyped(new FilesystemTornDownEvent()); |
|
546 | - } |
|
547 | - |
|
548 | - /** |
|
549 | - * Get mounts from mount providers that are registered after setup |
|
550 | - */ |
|
551 | - private function listenForNewMountProviders() { |
|
552 | - if (!$this->listeningForProviders) { |
|
553 | - $this->listeningForProviders = true; |
|
554 | - $this->mountProviderCollection->listen('\OC\Files\Config', 'registerMountProvider', function ( |
|
555 | - IMountProvider $provider, |
|
556 | - ) { |
|
557 | - foreach ($this->setupUsers as $userId) { |
|
558 | - $user = $this->userManager->get($userId); |
|
559 | - if ($user) { |
|
560 | - $mounts = $provider->getMountsForUser($user, Filesystem::getLoader()); |
|
561 | - array_walk($mounts, [$this->mountManager, 'addMount']); |
|
562 | - } |
|
563 | - } |
|
564 | - }); |
|
565 | - } |
|
566 | - } |
|
567 | - |
|
568 | - private function setupListeners() { |
|
569 | - // note that this event handling is intentionally pessimistic |
|
570 | - // clearing the cache to often is better than not enough |
|
571 | - |
|
572 | - $this->eventDispatcher->addListener(UserAddedEvent::class, function (UserAddedEvent $event) { |
|
573 | - $this->cache->remove($event->getUser()->getUID()); |
|
574 | - }); |
|
575 | - $this->eventDispatcher->addListener(UserRemovedEvent::class, function (UserRemovedEvent $event) { |
|
576 | - $this->cache->remove($event->getUser()->getUID()); |
|
577 | - }); |
|
578 | - $this->eventDispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) { |
|
579 | - $this->cache->remove($event->getShare()->getSharedWith()); |
|
580 | - }); |
|
581 | - $this->eventDispatcher->addListener(InvalidateMountCacheEvent::class, function (InvalidateMountCacheEvent $event, |
|
582 | - ) { |
|
583 | - if ($user = $event->getUser()) { |
|
584 | - $this->cache->remove($user->getUID()); |
|
585 | - } else { |
|
586 | - $this->cache->clear(); |
|
587 | - } |
|
588 | - }); |
|
589 | - |
|
590 | - $genericEvents = [ |
|
591 | - 'OCA\Circles\Events\CreatingCircleEvent', |
|
592 | - 'OCA\Circles\Events\DestroyingCircleEvent', |
|
593 | - 'OCA\Circles\Events\AddingCircleMemberEvent', |
|
594 | - 'OCA\Circles\Events\RemovingCircleMemberEvent', |
|
595 | - ]; |
|
596 | - |
|
597 | - foreach ($genericEvents as $genericEvent) { |
|
598 | - $this->eventDispatcher->addListener($genericEvent, function ($event) { |
|
599 | - $this->cache->clear(); |
|
600 | - }); |
|
601 | - } |
|
602 | - } |
|
171 | + if ($mount->getOption('readonly', false)) { |
|
172 | + return new PermissionsMask([ |
|
173 | + 'storage' => $storage, |
|
174 | + 'mask' => Constants::PERMISSION_ALL & ~( |
|
175 | + Constants::PERMISSION_UPDATE |
|
176 | + | Constants::PERMISSION_CREATE |
|
177 | + | Constants::PERMISSION_DELETE |
|
178 | + ), |
|
179 | + ]); |
|
180 | + } |
|
181 | + return $storage; |
|
182 | + }); |
|
183 | + |
|
184 | + Filesystem::logWarningWhenAddingStorageWrapper($prevLogging); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Setup the full filesystem for the specified user |
|
189 | + */ |
|
190 | + public function setupForUser(IUser $user): void { |
|
191 | + if ($this->isSetupComplete($user)) { |
|
192 | + return; |
|
193 | + } |
|
194 | + $this->setupUsersComplete[] = $user->getUID(); |
|
195 | + |
|
196 | + $this->eventLogger->start('fs:setup:user:full', 'Setup full filesystem for user'); |
|
197 | + |
|
198 | + if (!isset($this->setupUserMountProviders[$user->getUID()])) { |
|
199 | + $this->setupUserMountProviders[$user->getUID()] = []; |
|
200 | + } |
|
201 | + |
|
202 | + $previouslySetupProviders = $this->setupUserMountProviders[$user->getUID()]; |
|
203 | + |
|
204 | + $this->setupForUserWith($user, function () use ($user) { |
|
205 | + $this->mountProviderCollection->addMountForUser($user, $this->mountManager, function ( |
|
206 | + IMountProvider $provider, |
|
207 | + ) use ($user) { |
|
208 | + return !in_array(get_class($provider), $this->setupUserMountProviders[$user->getUID()]); |
|
209 | + }); |
|
210 | + }); |
|
211 | + $this->afterUserFullySetup($user, $previouslySetupProviders); |
|
212 | + $this->eventLogger->end('fs:setup:user:full'); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * part of the user setup that is run only once per user |
|
217 | + */ |
|
218 | + private function oneTimeUserSetup(IUser $user) { |
|
219 | + if ($this->isSetupStarted($user)) { |
|
220 | + return; |
|
221 | + } |
|
222 | + $this->setupUsers[] = $user->getUID(); |
|
223 | + |
|
224 | + $this->setupRoot(); |
|
225 | + |
|
226 | + $this->eventLogger->start('fs:setup:user:onetime', 'Onetime filesystem for user'); |
|
227 | + |
|
228 | + $this->setupBuiltinWrappers(); |
|
229 | + |
|
230 | + $prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
231 | + |
|
232 | + // TODO remove hook |
|
233 | + OC_Hook::emit('OC_Filesystem', 'preSetup', ['user' => $user->getUID()]); |
|
234 | + |
|
235 | + $event = new BeforeFileSystemSetupEvent($user); |
|
236 | + $this->eventDispatcher->dispatchTyped($event); |
|
237 | + |
|
238 | + Filesystem::logWarningWhenAddingStorageWrapper($prevLogging); |
|
239 | + |
|
240 | + $userDir = '/' . $user->getUID() . '/files'; |
|
241 | + |
|
242 | + Filesystem::initInternal($userDir); |
|
243 | + |
|
244 | + if ($this->lockdownManager->canAccessFilesystem()) { |
|
245 | + $this->eventLogger->start('fs:setup:user:home', 'Setup home filesystem for user'); |
|
246 | + // home mounts are handled separate since we need to ensure this is mounted before we call the other mount providers |
|
247 | + $homeMount = $this->mountProviderCollection->getHomeMountForUser($user); |
|
248 | + $this->mountManager->addMount($homeMount); |
|
249 | + |
|
250 | + if ($homeMount->getStorageRootId() === -1) { |
|
251 | + $this->eventLogger->start('fs:setup:user:home:scan', 'Scan home filesystem for user'); |
|
252 | + $homeMount->getStorage()->mkdir(''); |
|
253 | + $homeMount->getStorage()->getScanner()->scan(''); |
|
254 | + $this->eventLogger->end('fs:setup:user:home:scan'); |
|
255 | + } |
|
256 | + $this->eventLogger->end('fs:setup:user:home'); |
|
257 | + } else { |
|
258 | + $this->mountManager->addMount(new MountPoint( |
|
259 | + new NullStorage([]), |
|
260 | + '/' . $user->getUID() |
|
261 | + )); |
|
262 | + $this->mountManager->addMount(new MountPoint( |
|
263 | + new NullStorage([]), |
|
264 | + '/' . $user->getUID() . '/files' |
|
265 | + )); |
|
266 | + $this->setupUsersComplete[] = $user->getUID(); |
|
267 | + } |
|
268 | + |
|
269 | + $this->listenForNewMountProviders(); |
|
270 | + |
|
271 | + $this->eventLogger->end('fs:setup:user:onetime'); |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Final housekeeping after a user has been fully setup |
|
276 | + */ |
|
277 | + private function afterUserFullySetup(IUser $user, array $previouslySetupProviders): void { |
|
278 | + $this->eventLogger->start('fs:setup:user:full:post', 'Housekeeping after user is setup'); |
|
279 | + $userRoot = '/' . $user->getUID() . '/'; |
|
280 | + $mounts = $this->mountManager->getAll(); |
|
281 | + $mounts = array_filter($mounts, function (IMountPoint $mount) use ($userRoot) { |
|
282 | + return str_starts_with($mount->getMountPoint(), $userRoot); |
|
283 | + }); |
|
284 | + $allProviders = array_map(function (IMountProvider|IHomeMountProvider|IRootMountProvider $provider) { |
|
285 | + return get_class($provider); |
|
286 | + }, array_merge( |
|
287 | + $this->mountProviderCollection->getProviders(), |
|
288 | + $this->mountProviderCollection->getHomeProviders(), |
|
289 | + $this->mountProviderCollection->getRootProviders(), |
|
290 | + )); |
|
291 | + $newProviders = array_diff($allProviders, $previouslySetupProviders); |
|
292 | + $mounts = array_filter($mounts, function (IMountPoint $mount) use ($previouslySetupProviders) { |
|
293 | + return !in_array($mount->getMountProvider(), $previouslySetupProviders); |
|
294 | + }); |
|
295 | + $this->userMountCache->registerMounts($user, $mounts, $newProviders); |
|
296 | + |
|
297 | + $cacheDuration = $this->config->getSystemValueInt('fs_mount_cache_duration', 5 * 60); |
|
298 | + if ($cacheDuration > 0) { |
|
299 | + $this->cache->set($user->getUID(), true, $cacheDuration); |
|
300 | + $this->fullSetupRequired[$user->getUID()] = false; |
|
301 | + } |
|
302 | + $this->eventLogger->end('fs:setup:user:full:post'); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @param IUser $user |
|
307 | + * @param IMountPoint $mounts |
|
308 | + * @return void |
|
309 | + * @throws \OCP\HintException |
|
310 | + * @throws \OC\ServerNotAvailableException |
|
311 | + */ |
|
312 | + private function setupForUserWith(IUser $user, callable $mountCallback): void { |
|
313 | + $this->oneTimeUserSetup($user); |
|
314 | + |
|
315 | + if ($this->lockdownManager->canAccessFilesystem()) { |
|
316 | + $mountCallback(); |
|
317 | + } |
|
318 | + $this->eventLogger->start('fs:setup:user:post-init-mountpoint', 'post_initMountPoints legacy hook'); |
|
319 | + \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', ['user' => $user->getUID()]); |
|
320 | + $this->eventLogger->end('fs:setup:user:post-init-mountpoint'); |
|
321 | + |
|
322 | + $userDir = '/' . $user->getUID() . '/files'; |
|
323 | + $this->eventLogger->start('fs:setup:user:setup-hook', 'setup legacy hook'); |
|
324 | + OC_Hook::emit('OC_Filesystem', 'setup', ['user' => $user->getUID(), 'user_dir' => $userDir]); |
|
325 | + $this->eventLogger->end('fs:setup:user:setup-hook'); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Set up the root filesystem |
|
330 | + */ |
|
331 | + public function setupRoot(): void { |
|
332 | + //setting up the filesystem twice can only lead to trouble |
|
333 | + if ($this->rootSetup) { |
|
334 | + return; |
|
335 | + } |
|
336 | + |
|
337 | + $this->setupBuiltinWrappers(); |
|
338 | + |
|
339 | + $this->rootSetup = true; |
|
340 | + |
|
341 | + $this->eventLogger->start('fs:setup:root', 'Setup root filesystem'); |
|
342 | + |
|
343 | + $rootMounts = $this->mountProviderCollection->getRootMounts(); |
|
344 | + foreach ($rootMounts as $rootMountProvider) { |
|
345 | + $this->mountManager->addMount($rootMountProvider); |
|
346 | + } |
|
347 | + |
|
348 | + $this->eventLogger->end('fs:setup:root'); |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Get the user to setup for a path or `null` if the root needs to be setup |
|
353 | + * |
|
354 | + * @param string $path |
|
355 | + * @return IUser|null |
|
356 | + */ |
|
357 | + private function getUserForPath(string $path) { |
|
358 | + if (str_starts_with($path, '/__groupfolders')) { |
|
359 | + return null; |
|
360 | + } elseif (substr_count($path, '/') < 2) { |
|
361 | + if ($user = $this->userSession->getUser()) { |
|
362 | + return $user; |
|
363 | + } else { |
|
364 | + return null; |
|
365 | + } |
|
366 | + } elseif (str_starts_with($path, '/appdata_' . \OC_Util::getInstanceId()) || str_starts_with($path, '/files_external/')) { |
|
367 | + return null; |
|
368 | + } else { |
|
369 | + [, $userId] = explode('/', $path); |
|
370 | + } |
|
371 | + |
|
372 | + return $this->userManager->get($userId); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Set up the filesystem for the specified path |
|
377 | + */ |
|
378 | + public function setupForPath(string $path, bool $includeChildren = false): void { |
|
379 | + $user = $this->getUserForPath($path); |
|
380 | + if (!$user) { |
|
381 | + $this->setupRoot(); |
|
382 | + return; |
|
383 | + } |
|
384 | + |
|
385 | + if ($this->isSetupComplete($user)) { |
|
386 | + return; |
|
387 | + } |
|
388 | + |
|
389 | + if ($this->fullSetupRequired($user)) { |
|
390 | + $this->setupForUser($user); |
|
391 | + return; |
|
392 | + } |
|
393 | + |
|
394 | + // for the user's home folder, and includes children we need everything always |
|
395 | + if (rtrim($path) === '/' . $user->getUID() . '/files' && $includeChildren) { |
|
396 | + $this->setupForUser($user); |
|
397 | + return; |
|
398 | + } |
|
399 | + |
|
400 | + if (!isset($this->setupUserMountProviders[$user->getUID()])) { |
|
401 | + $this->setupUserMountProviders[$user->getUID()] = []; |
|
402 | + } |
|
403 | + $setupProviders = &$this->setupUserMountProviders[$user->getUID()]; |
|
404 | + $currentProviders = []; |
|
405 | + |
|
406 | + try { |
|
407 | + $cachedMount = $this->userMountCache->getMountForPath($user, $path); |
|
408 | + } catch (NotFoundException $e) { |
|
409 | + $this->setupForUser($user); |
|
410 | + return; |
|
411 | + } |
|
412 | + |
|
413 | + $this->oneTimeUserSetup($user); |
|
414 | + |
|
415 | + $this->eventLogger->start('fs:setup:user:path', "Setup $path filesystem for user"); |
|
416 | + $this->eventLogger->start('fs:setup:user:path:find', "Find mountpoint for $path"); |
|
417 | + |
|
418 | + $mounts = []; |
|
419 | + if (!in_array($cachedMount->getMountProvider(), $setupProviders)) { |
|
420 | + $currentProviders[] = $cachedMount->getMountProvider(); |
|
421 | + if ($cachedMount->getMountProvider()) { |
|
422 | + $setupProviders[] = $cachedMount->getMountProvider(); |
|
423 | + $mounts = $this->mountProviderCollection->getUserMountsForProviderClasses($user, [$cachedMount->getMountProvider()]); |
|
424 | + } else { |
|
425 | + $this->logger->debug('mount at ' . $cachedMount->getMountPoint() . ' has no provider set, performing full setup'); |
|
426 | + $this->eventLogger->end('fs:setup:user:path:find'); |
|
427 | + $this->setupForUser($user); |
|
428 | + $this->eventLogger->end('fs:setup:user:path'); |
|
429 | + return; |
|
430 | + } |
|
431 | + } |
|
432 | + |
|
433 | + if ($includeChildren) { |
|
434 | + $subCachedMounts = $this->userMountCache->getMountsInPath($user, $path); |
|
435 | + $this->eventLogger->end('fs:setup:user:path:find'); |
|
436 | + |
|
437 | + $needsFullSetup = array_reduce($subCachedMounts, function (bool $needsFullSetup, ICachedMountInfo $cachedMountInfo) { |
|
438 | + return $needsFullSetup || $cachedMountInfo->getMountProvider() === ''; |
|
439 | + }, false); |
|
440 | + |
|
441 | + if ($needsFullSetup) { |
|
442 | + $this->logger->debug('mount has no provider set, performing full setup'); |
|
443 | + $this->setupForUser($user); |
|
444 | + $this->eventLogger->end('fs:setup:user:path'); |
|
445 | + return; |
|
446 | + } else { |
|
447 | + foreach ($subCachedMounts as $cachedMount) { |
|
448 | + if (!in_array($cachedMount->getMountProvider(), $setupProviders)) { |
|
449 | + $currentProviders[] = $cachedMount->getMountProvider(); |
|
450 | + $setupProviders[] = $cachedMount->getMountProvider(); |
|
451 | + $mounts = array_merge($mounts, $this->mountProviderCollection->getUserMountsForProviderClasses($user, [$cachedMount->getMountProvider()])); |
|
452 | + } |
|
453 | + } |
|
454 | + } |
|
455 | + } else { |
|
456 | + $this->eventLogger->end('fs:setup:user:path:find'); |
|
457 | + } |
|
458 | + |
|
459 | + if (count($mounts)) { |
|
460 | + $this->userMountCache->registerMounts($user, $mounts, $currentProviders); |
|
461 | + $this->setupForUserWith($user, function () use ($mounts) { |
|
462 | + array_walk($mounts, [$this->mountManager, 'addMount']); |
|
463 | + }); |
|
464 | + } elseif (!$this->isSetupStarted($user)) { |
|
465 | + $this->oneTimeUserSetup($user); |
|
466 | + } |
|
467 | + $this->eventLogger->end('fs:setup:user:path'); |
|
468 | + } |
|
469 | + |
|
470 | + private function fullSetupRequired(IUser $user): bool { |
|
471 | + if ($this->forceFullSetup) { |
|
472 | + return true; |
|
473 | + } |
|
474 | + |
|
475 | + // we perform a "cached" setup only after having done the full setup recently |
|
476 | + // this is also used to trigger a full setup after handling events that are likely |
|
477 | + // to change the available mounts |
|
478 | + if (!isset($this->fullSetupRequired[$user->getUID()])) { |
|
479 | + $this->fullSetupRequired[$user->getUID()] = !$this->cache->get($user->getUID()); |
|
480 | + } |
|
481 | + return $this->fullSetupRequired[$user->getUID()]; |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * @param string $path |
|
486 | + * @param string[] $providers |
|
487 | + */ |
|
488 | + public function setupForProvider(string $path, array $providers): void { |
|
489 | + $user = $this->getUserForPath($path); |
|
490 | + if (!$user) { |
|
491 | + $this->setupRoot(); |
|
492 | + return; |
|
493 | + } |
|
494 | + |
|
495 | + if ($this->isSetupComplete($user)) { |
|
496 | + return; |
|
497 | + } |
|
498 | + |
|
499 | + if ($this->fullSetupRequired($user)) { |
|
500 | + $this->setupForUser($user); |
|
501 | + return; |
|
502 | + } |
|
503 | + |
|
504 | + $this->eventLogger->start('fs:setup:user:providers', 'Setup filesystem for ' . implode(', ', $providers)); |
|
505 | + |
|
506 | + $this->oneTimeUserSetup($user); |
|
507 | + |
|
508 | + // home providers are always used |
|
509 | + $providers = array_filter($providers, function (string $provider) { |
|
510 | + return !is_subclass_of($provider, IHomeMountProvider::class); |
|
511 | + }); |
|
512 | + |
|
513 | + if (in_array('', $providers)) { |
|
514 | + $this->setupForUser($user); |
|
515 | + return; |
|
516 | + } |
|
517 | + $setupProviders = $this->setupUserMountProviders[$user->getUID()] ?? []; |
|
518 | + |
|
519 | + $providers = array_diff($providers, $setupProviders); |
|
520 | + if (count($providers) === 0) { |
|
521 | + if (!$this->isSetupStarted($user)) { |
|
522 | + $this->oneTimeUserSetup($user); |
|
523 | + } |
|
524 | + $this->eventLogger->end('fs:setup:user:providers'); |
|
525 | + return; |
|
526 | + } else { |
|
527 | + $this->setupUserMountProviders[$user->getUID()] = array_merge($setupProviders, $providers); |
|
528 | + $mounts = $this->mountProviderCollection->getUserMountsForProviderClasses($user, $providers); |
|
529 | + } |
|
530 | + |
|
531 | + $this->userMountCache->registerMounts($user, $mounts, $providers); |
|
532 | + $this->setupForUserWith($user, function () use ($mounts) { |
|
533 | + array_walk($mounts, [$this->mountManager, 'addMount']); |
|
534 | + }); |
|
535 | + $this->eventLogger->end('fs:setup:user:providers'); |
|
536 | + } |
|
537 | + |
|
538 | + public function tearDown() { |
|
539 | + $this->setupUsers = []; |
|
540 | + $this->setupUsersComplete = []; |
|
541 | + $this->setupUserMountProviders = []; |
|
542 | + $this->fullSetupRequired = []; |
|
543 | + $this->rootSetup = false; |
|
544 | + $this->mountManager->clear(); |
|
545 | + $this->eventDispatcher->dispatchTyped(new FilesystemTornDownEvent()); |
|
546 | + } |
|
547 | + |
|
548 | + /** |
|
549 | + * Get mounts from mount providers that are registered after setup |
|
550 | + */ |
|
551 | + private function listenForNewMountProviders() { |
|
552 | + if (!$this->listeningForProviders) { |
|
553 | + $this->listeningForProviders = true; |
|
554 | + $this->mountProviderCollection->listen('\OC\Files\Config', 'registerMountProvider', function ( |
|
555 | + IMountProvider $provider, |
|
556 | + ) { |
|
557 | + foreach ($this->setupUsers as $userId) { |
|
558 | + $user = $this->userManager->get($userId); |
|
559 | + if ($user) { |
|
560 | + $mounts = $provider->getMountsForUser($user, Filesystem::getLoader()); |
|
561 | + array_walk($mounts, [$this->mountManager, 'addMount']); |
|
562 | + } |
|
563 | + } |
|
564 | + }); |
|
565 | + } |
|
566 | + } |
|
567 | + |
|
568 | + private function setupListeners() { |
|
569 | + // note that this event handling is intentionally pessimistic |
|
570 | + // clearing the cache to often is better than not enough |
|
571 | + |
|
572 | + $this->eventDispatcher->addListener(UserAddedEvent::class, function (UserAddedEvent $event) { |
|
573 | + $this->cache->remove($event->getUser()->getUID()); |
|
574 | + }); |
|
575 | + $this->eventDispatcher->addListener(UserRemovedEvent::class, function (UserRemovedEvent $event) { |
|
576 | + $this->cache->remove($event->getUser()->getUID()); |
|
577 | + }); |
|
578 | + $this->eventDispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) { |
|
579 | + $this->cache->remove($event->getShare()->getSharedWith()); |
|
580 | + }); |
|
581 | + $this->eventDispatcher->addListener(InvalidateMountCacheEvent::class, function (InvalidateMountCacheEvent $event, |
|
582 | + ) { |
|
583 | + if ($user = $event->getUser()) { |
|
584 | + $this->cache->remove($user->getUID()); |
|
585 | + } else { |
|
586 | + $this->cache->clear(); |
|
587 | + } |
|
588 | + }); |
|
589 | + |
|
590 | + $genericEvents = [ |
|
591 | + 'OCA\Circles\Events\CreatingCircleEvent', |
|
592 | + 'OCA\Circles\Events\DestroyingCircleEvent', |
|
593 | + 'OCA\Circles\Events\AddingCircleMemberEvent', |
|
594 | + 'OCA\Circles\Events\RemovingCircleMemberEvent', |
|
595 | + ]; |
|
596 | + |
|
597 | + foreach ($genericEvents as $genericEvent) { |
|
598 | + $this->eventDispatcher->addListener($genericEvent, function ($event) { |
|
599 | + $this->cache->clear(); |
|
600 | + }); |
|
601 | + } |
|
602 | + } |
|
603 | 603 | } |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | $this->appManager->loadApps(['filesystem']); |
109 | 109 | $prevLogging = Filesystem::logWarningWhenAddingStorageWrapper(false); |
110 | 110 | |
111 | - Filesystem::addStorageWrapper('mount_options', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
111 | + Filesystem::addStorageWrapper('mount_options', function($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
112 | 112 | if ($storage->instanceOfStorage(Common::class)) { |
113 | 113 | $options = array_merge($mount->getOptions(), ['mount_point' => $mountPoint]); |
114 | 114 | $storage->setMountOptions($options); |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | $sharingEnabledForUser = $user ? !$this->shareDisableChecker->sharingDisabledForUser($user->getUID()) : true; |
122 | 122 | Filesystem::addStorageWrapper( |
123 | 123 | 'sharing_mask', |
124 | - function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEnabled, $sharingEnabledForUser) { |
|
124 | + function($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEnabled, $sharingEnabledForUser) { |
|
125 | 125 | $sharingEnabledForMount = $mount->getOption('enable_sharing', true); |
126 | 126 | $isShared = $mount instanceof ISharedMountPoint; |
127 | 127 | if (!$sharingEnabledForMount || !$sharingEnabledForUser || (!$reSharingEnabled && $isShared)) { |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | ); |
136 | 136 | |
137 | 137 | // install storage availability wrapper, before most other wrappers |
138 | - Filesystem::addStorageWrapper('oc_availability', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
138 | + Filesystem::addStorageWrapper('oc_availability', function($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
139 | 139 | $externalMount = $mount instanceof ExternalMountPoint || $mount instanceof Mount; |
140 | 140 | if ($externalMount && !$storage->isLocal()) { |
141 | 141 | return new Availability(['storage' => $storage]); |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | return $storage; |
144 | 144 | }); |
145 | 145 | |
146 | - Filesystem::addStorageWrapper('oc_encoding', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
146 | + Filesystem::addStorageWrapper('oc_encoding', function($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
147 | 147 | if ($mount->getOption('encoding_compatibility', false) && !$mount instanceof SharedMount) { |
148 | 148 | return new Encoding(['storage' => $storage]); |
149 | 149 | } |
@@ -151,12 +151,12 @@ discard block |
||
151 | 151 | }); |
152 | 152 | |
153 | 153 | $quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false); |
154 | - Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) { |
|
154 | + Filesystem::addStorageWrapper('oc_quota', function($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) { |
|
155 | 155 | // set up quota for home storages, even for other users |
156 | 156 | // which can happen when using sharing |
157 | 157 | if ($mount instanceof HomeMountPoint) { |
158 | 158 | $user = $mount->getUser(); |
159 | - return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) { |
|
159 | + return new Quota(['storage' => $storage, 'quotaCallback' => function() use ($user) { |
|
160 | 160 | return $user->getQuotaBytes(); |
161 | 161 | }, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]); |
162 | 162 | } |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | return $storage; |
165 | 165 | }); |
166 | 166 | |
167 | - Filesystem::addStorageWrapper('readonly', function ($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
167 | + Filesystem::addStorageWrapper('readonly', function($mountPoint, IStorage $storage, IMountPoint $mount) { |
|
168 | 168 | /* |
169 | 169 | * Do not allow any operations that modify the storage |
170 | 170 | */ |
@@ -201,8 +201,8 @@ discard block |
||
201 | 201 | |
202 | 202 | $previouslySetupProviders = $this->setupUserMountProviders[$user->getUID()]; |
203 | 203 | |
204 | - $this->setupForUserWith($user, function () use ($user) { |
|
205 | - $this->mountProviderCollection->addMountForUser($user, $this->mountManager, function ( |
|
204 | + $this->setupForUserWith($user, function() use ($user) { |
|
205 | + $this->mountProviderCollection->addMountForUser($user, $this->mountManager, function( |
|
206 | 206 | IMountProvider $provider, |
207 | 207 | ) use ($user) { |
208 | 208 | return !in_array(get_class($provider), $this->setupUserMountProviders[$user->getUID()]); |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | |
238 | 238 | Filesystem::logWarningWhenAddingStorageWrapper($prevLogging); |
239 | 239 | |
240 | - $userDir = '/' . $user->getUID() . '/files'; |
|
240 | + $userDir = '/'.$user->getUID().'/files'; |
|
241 | 241 | |
242 | 242 | Filesystem::initInternal($userDir); |
243 | 243 | |
@@ -257,11 +257,11 @@ discard block |
||
257 | 257 | } else { |
258 | 258 | $this->mountManager->addMount(new MountPoint( |
259 | 259 | new NullStorage([]), |
260 | - '/' . $user->getUID() |
|
260 | + '/'.$user->getUID() |
|
261 | 261 | )); |
262 | 262 | $this->mountManager->addMount(new MountPoint( |
263 | 263 | new NullStorage([]), |
264 | - '/' . $user->getUID() . '/files' |
|
264 | + '/'.$user->getUID().'/files' |
|
265 | 265 | )); |
266 | 266 | $this->setupUsersComplete[] = $user->getUID(); |
267 | 267 | } |
@@ -276,12 +276,12 @@ discard block |
||
276 | 276 | */ |
277 | 277 | private function afterUserFullySetup(IUser $user, array $previouslySetupProviders): void { |
278 | 278 | $this->eventLogger->start('fs:setup:user:full:post', 'Housekeeping after user is setup'); |
279 | - $userRoot = '/' . $user->getUID() . '/'; |
|
279 | + $userRoot = '/'.$user->getUID().'/'; |
|
280 | 280 | $mounts = $this->mountManager->getAll(); |
281 | - $mounts = array_filter($mounts, function (IMountPoint $mount) use ($userRoot) { |
|
281 | + $mounts = array_filter($mounts, function(IMountPoint $mount) use ($userRoot) { |
|
282 | 282 | return str_starts_with($mount->getMountPoint(), $userRoot); |
283 | 283 | }); |
284 | - $allProviders = array_map(function (IMountProvider|IHomeMountProvider|IRootMountProvider $provider) { |
|
284 | + $allProviders = array_map(function(IMountProvider | IHomeMountProvider | IRootMountProvider $provider) { |
|
285 | 285 | return get_class($provider); |
286 | 286 | }, array_merge( |
287 | 287 | $this->mountProviderCollection->getProviders(), |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | $this->mountProviderCollection->getRootProviders(), |
290 | 290 | )); |
291 | 291 | $newProviders = array_diff($allProviders, $previouslySetupProviders); |
292 | - $mounts = array_filter($mounts, function (IMountPoint $mount) use ($previouslySetupProviders) { |
|
292 | + $mounts = array_filter($mounts, function(IMountPoint $mount) use ($previouslySetupProviders) { |
|
293 | 293 | return !in_array($mount->getMountProvider(), $previouslySetupProviders); |
294 | 294 | }); |
295 | 295 | $this->userMountCache->registerMounts($user, $mounts, $newProviders); |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', ['user' => $user->getUID()]); |
320 | 320 | $this->eventLogger->end('fs:setup:user:post-init-mountpoint'); |
321 | 321 | |
322 | - $userDir = '/' . $user->getUID() . '/files'; |
|
322 | + $userDir = '/'.$user->getUID().'/files'; |
|
323 | 323 | $this->eventLogger->start('fs:setup:user:setup-hook', 'setup legacy hook'); |
324 | 324 | OC_Hook::emit('OC_Filesystem', 'setup', ['user' => $user->getUID(), 'user_dir' => $userDir]); |
325 | 325 | $this->eventLogger->end('fs:setup:user:setup-hook'); |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | } else { |
364 | 364 | return null; |
365 | 365 | } |
366 | - } elseif (str_starts_with($path, '/appdata_' . \OC_Util::getInstanceId()) || str_starts_with($path, '/files_external/')) { |
|
366 | + } elseif (str_starts_with($path, '/appdata_'.\OC_Util::getInstanceId()) || str_starts_with($path, '/files_external/')) { |
|
367 | 367 | return null; |
368 | 368 | } else { |
369 | 369 | [, $userId] = explode('/', $path); |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | } |
393 | 393 | |
394 | 394 | // for the user's home folder, and includes children we need everything always |
395 | - if (rtrim($path) === '/' . $user->getUID() . '/files' && $includeChildren) { |
|
395 | + if (rtrim($path) === '/'.$user->getUID().'/files' && $includeChildren) { |
|
396 | 396 | $this->setupForUser($user); |
397 | 397 | return; |
398 | 398 | } |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | $setupProviders[] = $cachedMount->getMountProvider(); |
423 | 423 | $mounts = $this->mountProviderCollection->getUserMountsForProviderClasses($user, [$cachedMount->getMountProvider()]); |
424 | 424 | } else { |
425 | - $this->logger->debug('mount at ' . $cachedMount->getMountPoint() . ' has no provider set, performing full setup'); |
|
425 | + $this->logger->debug('mount at '.$cachedMount->getMountPoint().' has no provider set, performing full setup'); |
|
426 | 426 | $this->eventLogger->end('fs:setup:user:path:find'); |
427 | 427 | $this->setupForUser($user); |
428 | 428 | $this->eventLogger->end('fs:setup:user:path'); |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | $subCachedMounts = $this->userMountCache->getMountsInPath($user, $path); |
435 | 435 | $this->eventLogger->end('fs:setup:user:path:find'); |
436 | 436 | |
437 | - $needsFullSetup = array_reduce($subCachedMounts, function (bool $needsFullSetup, ICachedMountInfo $cachedMountInfo) { |
|
437 | + $needsFullSetup = array_reduce($subCachedMounts, function(bool $needsFullSetup, ICachedMountInfo $cachedMountInfo) { |
|
438 | 438 | return $needsFullSetup || $cachedMountInfo->getMountProvider() === ''; |
439 | 439 | }, false); |
440 | 440 | |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | |
459 | 459 | if (count($mounts)) { |
460 | 460 | $this->userMountCache->registerMounts($user, $mounts, $currentProviders); |
461 | - $this->setupForUserWith($user, function () use ($mounts) { |
|
461 | + $this->setupForUserWith($user, function() use ($mounts) { |
|
462 | 462 | array_walk($mounts, [$this->mountManager, 'addMount']); |
463 | 463 | }); |
464 | 464 | } elseif (!$this->isSetupStarted($user)) { |
@@ -501,12 +501,12 @@ discard block |
||
501 | 501 | return; |
502 | 502 | } |
503 | 503 | |
504 | - $this->eventLogger->start('fs:setup:user:providers', 'Setup filesystem for ' . implode(', ', $providers)); |
|
504 | + $this->eventLogger->start('fs:setup:user:providers', 'Setup filesystem for '.implode(', ', $providers)); |
|
505 | 505 | |
506 | 506 | $this->oneTimeUserSetup($user); |
507 | 507 | |
508 | 508 | // home providers are always used |
509 | - $providers = array_filter($providers, function (string $provider) { |
|
509 | + $providers = array_filter($providers, function(string $provider) { |
|
510 | 510 | return !is_subclass_of($provider, IHomeMountProvider::class); |
511 | 511 | }); |
512 | 512 | |
@@ -529,7 +529,7 @@ discard block |
||
529 | 529 | } |
530 | 530 | |
531 | 531 | $this->userMountCache->registerMounts($user, $mounts, $providers); |
532 | - $this->setupForUserWith($user, function () use ($mounts) { |
|
532 | + $this->setupForUserWith($user, function() use ($mounts) { |
|
533 | 533 | array_walk($mounts, [$this->mountManager, 'addMount']); |
534 | 534 | }); |
535 | 535 | $this->eventLogger->end('fs:setup:user:providers'); |
@@ -551,7 +551,7 @@ discard block |
||
551 | 551 | private function listenForNewMountProviders() { |
552 | 552 | if (!$this->listeningForProviders) { |
553 | 553 | $this->listeningForProviders = true; |
554 | - $this->mountProviderCollection->listen('\OC\Files\Config', 'registerMountProvider', function ( |
|
554 | + $this->mountProviderCollection->listen('\OC\Files\Config', 'registerMountProvider', function( |
|
555 | 555 | IMountProvider $provider, |
556 | 556 | ) { |
557 | 557 | foreach ($this->setupUsers as $userId) { |
@@ -569,16 +569,16 @@ discard block |
||
569 | 569 | // note that this event handling is intentionally pessimistic |
570 | 570 | // clearing the cache to often is better than not enough |
571 | 571 | |
572 | - $this->eventDispatcher->addListener(UserAddedEvent::class, function (UserAddedEvent $event) { |
|
572 | + $this->eventDispatcher->addListener(UserAddedEvent::class, function(UserAddedEvent $event) { |
|
573 | 573 | $this->cache->remove($event->getUser()->getUID()); |
574 | 574 | }); |
575 | - $this->eventDispatcher->addListener(UserRemovedEvent::class, function (UserRemovedEvent $event) { |
|
575 | + $this->eventDispatcher->addListener(UserRemovedEvent::class, function(UserRemovedEvent $event) { |
|
576 | 576 | $this->cache->remove($event->getUser()->getUID()); |
577 | 577 | }); |
578 | - $this->eventDispatcher->addListener(ShareCreatedEvent::class, function (ShareCreatedEvent $event) { |
|
578 | + $this->eventDispatcher->addListener(ShareCreatedEvent::class, function(ShareCreatedEvent $event) { |
|
579 | 579 | $this->cache->remove($event->getShare()->getSharedWith()); |
580 | 580 | }); |
581 | - $this->eventDispatcher->addListener(InvalidateMountCacheEvent::class, function (InvalidateMountCacheEvent $event, |
|
581 | + $this->eventDispatcher->addListener(InvalidateMountCacheEvent::class, function(InvalidateMountCacheEvent $event, |
|
582 | 582 | ) { |
583 | 583 | if ($user = $event->getUser()) { |
584 | 584 | $this->cache->remove($user->getUID()); |
@@ -595,7 +595,7 @@ discard block |
||
595 | 595 | ]; |
596 | 596 | |
597 | 597 | foreach ($genericEvents as $genericEvent) { |
598 | - $this->eventDispatcher->addListener($genericEvent, function ($event) { |
|
598 | + $this->eventDispatcher->addListener($genericEvent, function($event) { |
|
599 | 599 | $this->cache->clear(); |
600 | 600 | }); |
601 | 601 | } |
@@ -23,43 +23,43 @@ |
||
23 | 23 | use Psr\Log\LoggerInterface; |
24 | 24 | |
25 | 25 | class SetupManagerFactory { |
26 | - private ?SetupManager $setupManager; |
|
26 | + private ?SetupManager $setupManager; |
|
27 | 27 | |
28 | - public function __construct( |
|
29 | - private IEventLogger $eventLogger, |
|
30 | - private IMountProviderCollection $mountProviderCollection, |
|
31 | - private IUserManager $userManager, |
|
32 | - private IEventDispatcher $eventDispatcher, |
|
33 | - private IUserMountCache $userMountCache, |
|
34 | - private ILockdownManager $lockdownManager, |
|
35 | - private IUserSession $userSession, |
|
36 | - private ICacheFactory $cacheFactory, |
|
37 | - private LoggerInterface $logger, |
|
38 | - private IConfig $config, |
|
39 | - private ShareDisableChecker $shareDisableChecker, |
|
40 | - private IAppManager $appManager, |
|
41 | - ) { |
|
42 | - $this->setupManager = null; |
|
43 | - } |
|
28 | + public function __construct( |
|
29 | + private IEventLogger $eventLogger, |
|
30 | + private IMountProviderCollection $mountProviderCollection, |
|
31 | + private IUserManager $userManager, |
|
32 | + private IEventDispatcher $eventDispatcher, |
|
33 | + private IUserMountCache $userMountCache, |
|
34 | + private ILockdownManager $lockdownManager, |
|
35 | + private IUserSession $userSession, |
|
36 | + private ICacheFactory $cacheFactory, |
|
37 | + private LoggerInterface $logger, |
|
38 | + private IConfig $config, |
|
39 | + private ShareDisableChecker $shareDisableChecker, |
|
40 | + private IAppManager $appManager, |
|
41 | + ) { |
|
42 | + $this->setupManager = null; |
|
43 | + } |
|
44 | 44 | |
45 | - public function create(IMountManager $mountManager): SetupManager { |
|
46 | - if (!$this->setupManager) { |
|
47 | - $this->setupManager = new SetupManager( |
|
48 | - $this->eventLogger, |
|
49 | - $this->mountProviderCollection, |
|
50 | - $mountManager, |
|
51 | - $this->userManager, |
|
52 | - $this->eventDispatcher, |
|
53 | - $this->userMountCache, |
|
54 | - $this->lockdownManager, |
|
55 | - $this->userSession, |
|
56 | - $this->cacheFactory, |
|
57 | - $this->logger, |
|
58 | - $this->config, |
|
59 | - $this->shareDisableChecker, |
|
60 | - $this->appManager, |
|
61 | - ); |
|
62 | - } |
|
63 | - return $this->setupManager; |
|
64 | - } |
|
45 | + public function create(IMountManager $mountManager): SetupManager { |
|
46 | + if (!$this->setupManager) { |
|
47 | + $this->setupManager = new SetupManager( |
|
48 | + $this->eventLogger, |
|
49 | + $this->mountProviderCollection, |
|
50 | + $mountManager, |
|
51 | + $this->userManager, |
|
52 | + $this->eventDispatcher, |
|
53 | + $this->userMountCache, |
|
54 | + $this->lockdownManager, |
|
55 | + $this->userSession, |
|
56 | + $this->cacheFactory, |
|
57 | + $this->logger, |
|
58 | + $this->config, |
|
59 | + $this->shareDisableChecker, |
|
60 | + $this->appManager, |
|
61 | + ); |
|
62 | + } |
|
63 | + return $this->setupManager; |
|
64 | + } |
|
65 | 65 | } |
@@ -34,432 +34,432 @@ |
||
34 | 34 | use function preg_quote; |
35 | 35 | |
36 | 36 | class ConvertType extends Command implements CompletionAwareInterface { |
37 | - protected array $columnTypes = []; |
|
38 | - |
|
39 | - public function __construct( |
|
40 | - protected IConfig $config, |
|
41 | - protected ConnectionFactory $connectionFactory, |
|
42 | - protected IAppManager $appManager, |
|
43 | - ) { |
|
44 | - parent::__construct(); |
|
45 | - } |
|
46 | - |
|
47 | - protected function configure() { |
|
48 | - $this |
|
49 | - ->setName('db:convert-type') |
|
50 | - ->setDescription('Convert the Nextcloud database to the newly configured one') |
|
51 | - ->addArgument( |
|
52 | - 'type', |
|
53 | - InputArgument::REQUIRED, |
|
54 | - 'the type of the database to convert to' |
|
55 | - ) |
|
56 | - ->addArgument( |
|
57 | - 'username', |
|
58 | - InputArgument::REQUIRED, |
|
59 | - 'the username of the database to convert to' |
|
60 | - ) |
|
61 | - ->addArgument( |
|
62 | - 'hostname', |
|
63 | - InputArgument::REQUIRED, |
|
64 | - 'the hostname of the database to convert to' |
|
65 | - ) |
|
66 | - ->addArgument( |
|
67 | - 'database', |
|
68 | - InputArgument::REQUIRED, |
|
69 | - 'the name of the database to convert to' |
|
70 | - ) |
|
71 | - ->addOption( |
|
72 | - 'port', |
|
73 | - null, |
|
74 | - InputOption::VALUE_REQUIRED, |
|
75 | - 'the port of the database to convert to' |
|
76 | - ) |
|
77 | - ->addOption( |
|
78 | - 'password', |
|
79 | - null, |
|
80 | - InputOption::VALUE_REQUIRED, |
|
81 | - 'the password of the database to convert to. Will be asked when not specified. Can also be passed via stdin.' |
|
82 | - ) |
|
83 | - ->addOption( |
|
84 | - 'clear-schema', |
|
85 | - null, |
|
86 | - InputOption::VALUE_NONE, |
|
87 | - 'remove all tables from the destination database' |
|
88 | - ) |
|
89 | - ->addOption( |
|
90 | - 'all-apps', |
|
91 | - null, |
|
92 | - InputOption::VALUE_NONE, |
|
93 | - 'whether to create schema for all apps instead of only installed apps' |
|
94 | - ) |
|
95 | - ->addOption( |
|
96 | - 'chunk-size', |
|
97 | - null, |
|
98 | - InputOption::VALUE_REQUIRED, |
|
99 | - 'the maximum number of database rows to handle in a single query, bigger tables will be handled in chunks of this size. Lower this if the process runs out of memory during conversion.', |
|
100 | - '1000' |
|
101 | - ) |
|
102 | - ; |
|
103 | - } |
|
104 | - |
|
105 | - protected function validateInput(InputInterface $input, OutputInterface $output) { |
|
106 | - $type = $this->connectionFactory->normalizeType($input->getArgument('type')); |
|
107 | - if ($type === 'sqlite3') { |
|
108 | - throw new \InvalidArgumentException( |
|
109 | - 'Converting to SQLite (sqlite3) is currently not supported.' |
|
110 | - ); |
|
111 | - } |
|
112 | - if ($type === $this->config->getSystemValue('dbtype', '')) { |
|
113 | - throw new \InvalidArgumentException(sprintf( |
|
114 | - 'Can not convert from %1$s to %1$s.', |
|
115 | - $type |
|
116 | - )); |
|
117 | - } |
|
118 | - if ($type === 'oci' && $input->getOption('clear-schema')) { |
|
119 | - // Doctrine unconditionally tries (at least in version 2.3) |
|
120 | - // to drop sequence triggers when dropping a table, even though |
|
121 | - // such triggers may not exist. This results in errors like |
|
122 | - // "ORA-04080: trigger 'OC_STORAGES_AI_PK' does not exist". |
|
123 | - throw new \InvalidArgumentException( |
|
124 | - 'The --clear-schema option is not supported when converting to Oracle (oci).' |
|
125 | - ); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - protected function readPassword(InputInterface $input, OutputInterface $output) { |
|
130 | - // Explicitly specified password |
|
131 | - if ($input->getOption('password')) { |
|
132 | - return; |
|
133 | - } |
|
134 | - |
|
135 | - // Read from stdin. stream_set_blocking is used to prevent blocking |
|
136 | - // when nothing is passed via stdin. |
|
137 | - stream_set_blocking(STDIN, 0); |
|
138 | - $password = file_get_contents('php://stdin'); |
|
139 | - stream_set_blocking(STDIN, 1); |
|
140 | - if (trim($password) !== '') { |
|
141 | - $input->setOption('password', $password); |
|
142 | - return; |
|
143 | - } |
|
144 | - |
|
145 | - // Read password by interacting |
|
146 | - if ($input->isInteractive()) { |
|
147 | - /** @var QuestionHelper $helper */ |
|
148 | - $helper = $this->getHelper('question'); |
|
149 | - $question = new Question('What is the database password (press <enter> for none)? '); |
|
150 | - $question->setHidden(true); |
|
151 | - $question->setHiddenFallback(false); |
|
152 | - $password = $helper->ask($input, $output, $question); |
|
153 | - if ($password === null) { |
|
154 | - $password = ''; // possibly unnecessary |
|
155 | - } |
|
156 | - $input->setOption('password', $password); |
|
157 | - return; |
|
158 | - } |
|
159 | - } |
|
160 | - |
|
161 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
162 | - $this->validateInput($input, $output); |
|
163 | - $this->readPassword($input, $output); |
|
164 | - |
|
165 | - /** @var Connection $fromDB */ |
|
166 | - $fromDB = Server::get(Connection::class); |
|
167 | - $toDB = $this->getToDBConnection($input, $output); |
|
168 | - |
|
169 | - if ($input->getOption('clear-schema')) { |
|
170 | - $this->clearSchema($toDB, $input, $output); |
|
171 | - } |
|
172 | - |
|
173 | - $this->createSchema($fromDB, $toDB, $input, $output); |
|
174 | - |
|
175 | - $toTables = $this->getTables($toDB); |
|
176 | - $fromTables = $this->getTables($fromDB); |
|
177 | - |
|
178 | - // warn/fail if there are more tables in 'from' database |
|
179 | - $extraFromTables = array_diff($fromTables, $toTables); |
|
180 | - if (!empty($extraFromTables)) { |
|
181 | - $output->writeln('<comment>The following tables will not be converted:</comment>'); |
|
182 | - $output->writeln($extraFromTables); |
|
183 | - if (!$input->getOption('all-apps')) { |
|
184 | - $output->writeln('<comment>Please note that tables belonging to disabled (but not removed) apps</comment>'); |
|
185 | - $output->writeln('<comment>can be included by specifying the --all-apps option.</comment>'); |
|
186 | - } |
|
187 | - |
|
188 | - $continueConversion = !$input->isInteractive(); // assume yes for --no-interaction and no otherwise. |
|
189 | - $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', $continueConversion); |
|
190 | - |
|
191 | - /** @var QuestionHelper $helper */ |
|
192 | - $helper = $this->getHelper('question'); |
|
193 | - |
|
194 | - if (!$helper->ask($input, $output, $question)) { |
|
195 | - return 1; |
|
196 | - } |
|
197 | - } |
|
198 | - $intersectingTables = array_intersect($toTables, $fromTables); |
|
199 | - $this->convertDB($fromDB, $toDB, $intersectingTables, $input, $output); |
|
200 | - return 0; |
|
201 | - } |
|
202 | - |
|
203 | - protected function createSchema(Connection $fromDB, Connection $toDB, InputInterface $input, OutputInterface $output) { |
|
204 | - $output->writeln('<info>Creating schema in new database</info>'); |
|
205 | - |
|
206 | - $fromMS = new MigrationService('core', $fromDB); |
|
207 | - $currentMigration = $fromMS->getMigration('current'); |
|
208 | - if ($currentMigration !== '0') { |
|
209 | - $toMS = new MigrationService('core', $toDB); |
|
210 | - $toMS->migrate($currentMigration); |
|
211 | - } |
|
212 | - |
|
213 | - $apps = $input->getOption('all-apps') |
|
214 | - ? $this->appManager->getAllAppsInAppsFolders() |
|
215 | - : $this->appManager->getEnabledApps(); |
|
216 | - foreach ($apps as $app) { |
|
217 | - $output->writeln('<info> - ' . $app . '</info>'); |
|
218 | - // Make sure autoloading works... |
|
219 | - $this->appManager->loadApp($app); |
|
220 | - $fromMS = new MigrationService($app, $fromDB); |
|
221 | - $currentMigration = $fromMS->getMigration('current'); |
|
222 | - if ($currentMigration !== '0') { |
|
223 | - $toMS = new MigrationService($app, $toDB); |
|
224 | - $toMS->migrate($currentMigration, true); |
|
225 | - } |
|
226 | - } |
|
227 | - } |
|
228 | - |
|
229 | - protected function getToDBConnection(InputInterface $input, OutputInterface $output) { |
|
230 | - $type = $input->getArgument('type'); |
|
231 | - $connectionParams = $this->connectionFactory->createConnectionParams(type: $type); |
|
232 | - $connectionParams = array_merge($connectionParams, [ |
|
233 | - 'host' => $input->getArgument('hostname'), |
|
234 | - 'user' => $input->getArgument('username'), |
|
235 | - 'password' => $input->getOption('password'), |
|
236 | - 'dbname' => $input->getArgument('database'), |
|
237 | - ]); |
|
238 | - |
|
239 | - // parse port |
|
240 | - if ($input->getOption('port')) { |
|
241 | - $connectionParams['port'] = $input->getOption('port'); |
|
242 | - } |
|
243 | - |
|
244 | - // parse hostname for unix socket |
|
245 | - if (preg_match('/^(.+)(:(\d+|[^:]+))?$/', $input->getArgument('hostname'), $matches)) { |
|
246 | - $connectionParams['host'] = $matches[1]; |
|
247 | - if (isset($matches[3])) { |
|
248 | - if (is_numeric($matches[3])) { |
|
249 | - $connectionParams['port'] = $matches[3]; |
|
250 | - } else { |
|
251 | - $connectionParams['unix_socket'] = $matches[3]; |
|
252 | - } |
|
253 | - } |
|
254 | - } |
|
255 | - |
|
256 | - return $this->connectionFactory->getConnection($type, $connectionParams); |
|
257 | - } |
|
258 | - |
|
259 | - protected function clearSchema(Connection $db, InputInterface $input, OutputInterface $output) { |
|
260 | - $toTables = $this->getTables($db); |
|
261 | - if (!empty($toTables)) { |
|
262 | - $output->writeln('<info>Clearing schema in new database</info>'); |
|
263 | - } |
|
264 | - foreach ($toTables as $table) { |
|
265 | - $db->createSchemaManager()->dropTable($table); |
|
266 | - } |
|
267 | - } |
|
268 | - |
|
269 | - protected function getTables(Connection $db) { |
|
270 | - $db->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
271 | - /** @var string|AbstractAsset $asset */ |
|
272 | - $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
273 | - if ($asset instanceof AbstractAsset) { |
|
274 | - return preg_match($filterExpression, $asset->getName()) !== false; |
|
275 | - } |
|
276 | - return preg_match($filterExpression, $asset) !== false; |
|
277 | - }); |
|
278 | - return $db->createSchemaManager()->listTableNames(); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @param Connection $fromDB |
|
283 | - * @param Connection $toDB |
|
284 | - * @param Table $table |
|
285 | - * @param InputInterface $input |
|
286 | - * @param OutputInterface $output |
|
287 | - */ |
|
288 | - protected function copyTable(Connection $fromDB, Connection $toDB, Table $table, InputInterface $input, OutputInterface $output) { |
|
289 | - if ($table->getName() === $toDB->getPrefix() . 'migrations') { |
|
290 | - $output->writeln('<comment>Skipping migrations table because it was already filled by running the migrations</comment>'); |
|
291 | - return; |
|
292 | - } |
|
293 | - |
|
294 | - $chunkSize = (int)$input->getOption('chunk-size'); |
|
295 | - |
|
296 | - $query = $fromDB->getQueryBuilder(); |
|
297 | - $query->automaticTablePrefix(false); |
|
298 | - $query->select($query->func()->count('*', 'num_entries')) |
|
299 | - ->from($table->getName()); |
|
300 | - $result = $query->executeQuery(); |
|
301 | - $count = $result->fetchOne(); |
|
302 | - $result->closeCursor(); |
|
303 | - |
|
304 | - $numChunks = ceil($count / $chunkSize); |
|
305 | - if ($numChunks > 1) { |
|
306 | - $output->writeln('chunked query, ' . $numChunks . ' chunks'); |
|
307 | - } |
|
308 | - |
|
309 | - $progress = new ProgressBar($output, $count); |
|
310 | - $progress->setFormat('very_verbose'); |
|
311 | - $progress->start(); |
|
312 | - $redraw = $count > $chunkSize ? 100 : ($count > 100 ? 5 : 1); |
|
313 | - $progress->setRedrawFrequency($redraw); |
|
314 | - |
|
315 | - $query = $fromDB->getQueryBuilder(); |
|
316 | - $query->automaticTablePrefix(false); |
|
317 | - $query->select('*') |
|
318 | - ->from($table->getName()) |
|
319 | - ->setMaxResults($chunkSize); |
|
320 | - |
|
321 | - try { |
|
322 | - $orderColumns = $table->getPrimaryKeyColumns(); |
|
323 | - } catch (Exception $e) { |
|
324 | - $orderColumns = $table->getColumns(); |
|
325 | - } |
|
326 | - |
|
327 | - foreach ($orderColumns as $column) { |
|
328 | - $query->addOrderBy($column->getName()); |
|
329 | - } |
|
330 | - |
|
331 | - $insertQuery = $toDB->getQueryBuilder(); |
|
332 | - $insertQuery->automaticTablePrefix(false); |
|
333 | - $insertQuery->insert($table->getName()); |
|
334 | - $parametersCreated = false; |
|
335 | - |
|
336 | - for ($chunk = 0; $chunk < $numChunks; $chunk++) { |
|
337 | - $query->setFirstResult($chunk * $chunkSize); |
|
338 | - |
|
339 | - $result = $query->executeQuery(); |
|
340 | - |
|
341 | - try { |
|
342 | - $toDB->beginTransaction(); |
|
343 | - |
|
344 | - while ($row = $result->fetch()) { |
|
345 | - $progress->advance(); |
|
346 | - if (!$parametersCreated) { |
|
347 | - foreach ($row as $key => $value) { |
|
348 | - $insertQuery->setValue($key, $insertQuery->createParameter($key)); |
|
349 | - } |
|
350 | - $parametersCreated = true; |
|
351 | - } |
|
352 | - |
|
353 | - foreach ($row as $key => $value) { |
|
354 | - $type = $this->getColumnType($table, $key); |
|
355 | - if ($type !== false) { |
|
356 | - $insertQuery->setParameter($key, $value, $type); |
|
357 | - } else { |
|
358 | - $insertQuery->setParameter($key, $value); |
|
359 | - } |
|
360 | - } |
|
361 | - $insertQuery->execute(); |
|
362 | - } |
|
363 | - $result->closeCursor(); |
|
364 | - |
|
365 | - $toDB->commit(); |
|
366 | - } catch (\Throwable $e) { |
|
367 | - $toDB->rollBack(); |
|
368 | - throw $e; |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - $progress->finish(); |
|
373 | - $output->writeln(''); |
|
374 | - } |
|
375 | - |
|
376 | - protected function getColumnType(Table $table, $columnName) { |
|
377 | - $tableName = $table->getName(); |
|
378 | - if (isset($this->columnTypes[$tableName][$columnName])) { |
|
379 | - return $this->columnTypes[$tableName][$columnName]; |
|
380 | - } |
|
381 | - |
|
382 | - $type = $table->getColumn($columnName)->getType()->getName(); |
|
383 | - |
|
384 | - switch ($type) { |
|
385 | - case Types::BLOB: |
|
386 | - case Types::TEXT: |
|
387 | - $this->columnTypes[$tableName][$columnName] = IQueryBuilder::PARAM_LOB; |
|
388 | - break; |
|
389 | - case Types::BOOLEAN: |
|
390 | - $this->columnTypes[$tableName][$columnName] = IQueryBuilder::PARAM_BOOL; |
|
391 | - break; |
|
392 | - default: |
|
393 | - $this->columnTypes[$tableName][$columnName] = false; |
|
394 | - } |
|
395 | - |
|
396 | - return $this->columnTypes[$tableName][$columnName]; |
|
397 | - } |
|
398 | - |
|
399 | - protected function convertDB(Connection $fromDB, Connection $toDB, array $tables, InputInterface $input, OutputInterface $output) { |
|
400 | - $this->config->setSystemValue('maintenance', true); |
|
401 | - $schema = $fromDB->createSchema(); |
|
402 | - |
|
403 | - try { |
|
404 | - // copy table rows |
|
405 | - foreach ($tables as $table) { |
|
406 | - $output->writeln('<info> - ' . $table . '</info>'); |
|
407 | - $this->copyTable($fromDB, $toDB, $schema->getTable($table), $input, $output); |
|
408 | - } |
|
409 | - if ($input->getArgument('type') === 'pgsql') { |
|
410 | - $tools = new PgSqlTools($this->config); |
|
411 | - $tools->resynchronizeDatabaseSequences($toDB); |
|
412 | - } |
|
413 | - // save new database config |
|
414 | - $this->saveDBInfo($input); |
|
415 | - } catch (\Exception $e) { |
|
416 | - $this->config->setSystemValue('maintenance', false); |
|
417 | - throw $e; |
|
418 | - } |
|
419 | - $this->config->setSystemValue('maintenance', false); |
|
420 | - } |
|
421 | - |
|
422 | - protected function saveDBInfo(InputInterface $input) { |
|
423 | - $type = $input->getArgument('type'); |
|
424 | - $username = $input->getArgument('username'); |
|
425 | - $dbHost = $input->getArgument('hostname'); |
|
426 | - $dbName = $input->getArgument('database'); |
|
427 | - $password = $input->getOption('password'); |
|
428 | - if ($input->getOption('port')) { |
|
429 | - $dbHost .= ':' . $input->getOption('port'); |
|
430 | - } |
|
431 | - |
|
432 | - $this->config->setSystemValues([ |
|
433 | - 'dbtype' => $type, |
|
434 | - 'dbname' => $dbName, |
|
435 | - 'dbhost' => $dbHost, |
|
436 | - 'dbuser' => $username, |
|
437 | - 'dbpassword' => $password, |
|
438 | - ]); |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Return possible values for the named option |
|
443 | - * |
|
444 | - * @param string $optionName |
|
445 | - * @param CompletionContext $context |
|
446 | - * @return string[] |
|
447 | - */ |
|
448 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
449 | - return []; |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Return possible values for the named argument |
|
454 | - * |
|
455 | - * @param string $argumentName |
|
456 | - * @param CompletionContext $context |
|
457 | - * @return string[] |
|
458 | - */ |
|
459 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
460 | - if ($argumentName === 'type') { |
|
461 | - return ['mysql', 'oci', 'pgsql']; |
|
462 | - } |
|
463 | - return []; |
|
464 | - } |
|
37 | + protected array $columnTypes = []; |
|
38 | + |
|
39 | + public function __construct( |
|
40 | + protected IConfig $config, |
|
41 | + protected ConnectionFactory $connectionFactory, |
|
42 | + protected IAppManager $appManager, |
|
43 | + ) { |
|
44 | + parent::__construct(); |
|
45 | + } |
|
46 | + |
|
47 | + protected function configure() { |
|
48 | + $this |
|
49 | + ->setName('db:convert-type') |
|
50 | + ->setDescription('Convert the Nextcloud database to the newly configured one') |
|
51 | + ->addArgument( |
|
52 | + 'type', |
|
53 | + InputArgument::REQUIRED, |
|
54 | + 'the type of the database to convert to' |
|
55 | + ) |
|
56 | + ->addArgument( |
|
57 | + 'username', |
|
58 | + InputArgument::REQUIRED, |
|
59 | + 'the username of the database to convert to' |
|
60 | + ) |
|
61 | + ->addArgument( |
|
62 | + 'hostname', |
|
63 | + InputArgument::REQUIRED, |
|
64 | + 'the hostname of the database to convert to' |
|
65 | + ) |
|
66 | + ->addArgument( |
|
67 | + 'database', |
|
68 | + InputArgument::REQUIRED, |
|
69 | + 'the name of the database to convert to' |
|
70 | + ) |
|
71 | + ->addOption( |
|
72 | + 'port', |
|
73 | + null, |
|
74 | + InputOption::VALUE_REQUIRED, |
|
75 | + 'the port of the database to convert to' |
|
76 | + ) |
|
77 | + ->addOption( |
|
78 | + 'password', |
|
79 | + null, |
|
80 | + InputOption::VALUE_REQUIRED, |
|
81 | + 'the password of the database to convert to. Will be asked when not specified. Can also be passed via stdin.' |
|
82 | + ) |
|
83 | + ->addOption( |
|
84 | + 'clear-schema', |
|
85 | + null, |
|
86 | + InputOption::VALUE_NONE, |
|
87 | + 'remove all tables from the destination database' |
|
88 | + ) |
|
89 | + ->addOption( |
|
90 | + 'all-apps', |
|
91 | + null, |
|
92 | + InputOption::VALUE_NONE, |
|
93 | + 'whether to create schema for all apps instead of only installed apps' |
|
94 | + ) |
|
95 | + ->addOption( |
|
96 | + 'chunk-size', |
|
97 | + null, |
|
98 | + InputOption::VALUE_REQUIRED, |
|
99 | + 'the maximum number of database rows to handle in a single query, bigger tables will be handled in chunks of this size. Lower this if the process runs out of memory during conversion.', |
|
100 | + '1000' |
|
101 | + ) |
|
102 | + ; |
|
103 | + } |
|
104 | + |
|
105 | + protected function validateInput(InputInterface $input, OutputInterface $output) { |
|
106 | + $type = $this->connectionFactory->normalizeType($input->getArgument('type')); |
|
107 | + if ($type === 'sqlite3') { |
|
108 | + throw new \InvalidArgumentException( |
|
109 | + 'Converting to SQLite (sqlite3) is currently not supported.' |
|
110 | + ); |
|
111 | + } |
|
112 | + if ($type === $this->config->getSystemValue('dbtype', '')) { |
|
113 | + throw new \InvalidArgumentException(sprintf( |
|
114 | + 'Can not convert from %1$s to %1$s.', |
|
115 | + $type |
|
116 | + )); |
|
117 | + } |
|
118 | + if ($type === 'oci' && $input->getOption('clear-schema')) { |
|
119 | + // Doctrine unconditionally tries (at least in version 2.3) |
|
120 | + // to drop sequence triggers when dropping a table, even though |
|
121 | + // such triggers may not exist. This results in errors like |
|
122 | + // "ORA-04080: trigger 'OC_STORAGES_AI_PK' does not exist". |
|
123 | + throw new \InvalidArgumentException( |
|
124 | + 'The --clear-schema option is not supported when converting to Oracle (oci).' |
|
125 | + ); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + protected function readPassword(InputInterface $input, OutputInterface $output) { |
|
130 | + // Explicitly specified password |
|
131 | + if ($input->getOption('password')) { |
|
132 | + return; |
|
133 | + } |
|
134 | + |
|
135 | + // Read from stdin. stream_set_blocking is used to prevent blocking |
|
136 | + // when nothing is passed via stdin. |
|
137 | + stream_set_blocking(STDIN, 0); |
|
138 | + $password = file_get_contents('php://stdin'); |
|
139 | + stream_set_blocking(STDIN, 1); |
|
140 | + if (trim($password) !== '') { |
|
141 | + $input->setOption('password', $password); |
|
142 | + return; |
|
143 | + } |
|
144 | + |
|
145 | + // Read password by interacting |
|
146 | + if ($input->isInteractive()) { |
|
147 | + /** @var QuestionHelper $helper */ |
|
148 | + $helper = $this->getHelper('question'); |
|
149 | + $question = new Question('What is the database password (press <enter> for none)? '); |
|
150 | + $question->setHidden(true); |
|
151 | + $question->setHiddenFallback(false); |
|
152 | + $password = $helper->ask($input, $output, $question); |
|
153 | + if ($password === null) { |
|
154 | + $password = ''; // possibly unnecessary |
|
155 | + } |
|
156 | + $input->setOption('password', $password); |
|
157 | + return; |
|
158 | + } |
|
159 | + } |
|
160 | + |
|
161 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
162 | + $this->validateInput($input, $output); |
|
163 | + $this->readPassword($input, $output); |
|
164 | + |
|
165 | + /** @var Connection $fromDB */ |
|
166 | + $fromDB = Server::get(Connection::class); |
|
167 | + $toDB = $this->getToDBConnection($input, $output); |
|
168 | + |
|
169 | + if ($input->getOption('clear-schema')) { |
|
170 | + $this->clearSchema($toDB, $input, $output); |
|
171 | + } |
|
172 | + |
|
173 | + $this->createSchema($fromDB, $toDB, $input, $output); |
|
174 | + |
|
175 | + $toTables = $this->getTables($toDB); |
|
176 | + $fromTables = $this->getTables($fromDB); |
|
177 | + |
|
178 | + // warn/fail if there are more tables in 'from' database |
|
179 | + $extraFromTables = array_diff($fromTables, $toTables); |
|
180 | + if (!empty($extraFromTables)) { |
|
181 | + $output->writeln('<comment>The following tables will not be converted:</comment>'); |
|
182 | + $output->writeln($extraFromTables); |
|
183 | + if (!$input->getOption('all-apps')) { |
|
184 | + $output->writeln('<comment>Please note that tables belonging to disabled (but not removed) apps</comment>'); |
|
185 | + $output->writeln('<comment>can be included by specifying the --all-apps option.</comment>'); |
|
186 | + } |
|
187 | + |
|
188 | + $continueConversion = !$input->isInteractive(); // assume yes for --no-interaction and no otherwise. |
|
189 | + $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', $continueConversion); |
|
190 | + |
|
191 | + /** @var QuestionHelper $helper */ |
|
192 | + $helper = $this->getHelper('question'); |
|
193 | + |
|
194 | + if (!$helper->ask($input, $output, $question)) { |
|
195 | + return 1; |
|
196 | + } |
|
197 | + } |
|
198 | + $intersectingTables = array_intersect($toTables, $fromTables); |
|
199 | + $this->convertDB($fromDB, $toDB, $intersectingTables, $input, $output); |
|
200 | + return 0; |
|
201 | + } |
|
202 | + |
|
203 | + protected function createSchema(Connection $fromDB, Connection $toDB, InputInterface $input, OutputInterface $output) { |
|
204 | + $output->writeln('<info>Creating schema in new database</info>'); |
|
205 | + |
|
206 | + $fromMS = new MigrationService('core', $fromDB); |
|
207 | + $currentMigration = $fromMS->getMigration('current'); |
|
208 | + if ($currentMigration !== '0') { |
|
209 | + $toMS = new MigrationService('core', $toDB); |
|
210 | + $toMS->migrate($currentMigration); |
|
211 | + } |
|
212 | + |
|
213 | + $apps = $input->getOption('all-apps') |
|
214 | + ? $this->appManager->getAllAppsInAppsFolders() |
|
215 | + : $this->appManager->getEnabledApps(); |
|
216 | + foreach ($apps as $app) { |
|
217 | + $output->writeln('<info> - ' . $app . '</info>'); |
|
218 | + // Make sure autoloading works... |
|
219 | + $this->appManager->loadApp($app); |
|
220 | + $fromMS = new MigrationService($app, $fromDB); |
|
221 | + $currentMigration = $fromMS->getMigration('current'); |
|
222 | + if ($currentMigration !== '0') { |
|
223 | + $toMS = new MigrationService($app, $toDB); |
|
224 | + $toMS->migrate($currentMigration, true); |
|
225 | + } |
|
226 | + } |
|
227 | + } |
|
228 | + |
|
229 | + protected function getToDBConnection(InputInterface $input, OutputInterface $output) { |
|
230 | + $type = $input->getArgument('type'); |
|
231 | + $connectionParams = $this->connectionFactory->createConnectionParams(type: $type); |
|
232 | + $connectionParams = array_merge($connectionParams, [ |
|
233 | + 'host' => $input->getArgument('hostname'), |
|
234 | + 'user' => $input->getArgument('username'), |
|
235 | + 'password' => $input->getOption('password'), |
|
236 | + 'dbname' => $input->getArgument('database'), |
|
237 | + ]); |
|
238 | + |
|
239 | + // parse port |
|
240 | + if ($input->getOption('port')) { |
|
241 | + $connectionParams['port'] = $input->getOption('port'); |
|
242 | + } |
|
243 | + |
|
244 | + // parse hostname for unix socket |
|
245 | + if (preg_match('/^(.+)(:(\d+|[^:]+))?$/', $input->getArgument('hostname'), $matches)) { |
|
246 | + $connectionParams['host'] = $matches[1]; |
|
247 | + if (isset($matches[3])) { |
|
248 | + if (is_numeric($matches[3])) { |
|
249 | + $connectionParams['port'] = $matches[3]; |
|
250 | + } else { |
|
251 | + $connectionParams['unix_socket'] = $matches[3]; |
|
252 | + } |
|
253 | + } |
|
254 | + } |
|
255 | + |
|
256 | + return $this->connectionFactory->getConnection($type, $connectionParams); |
|
257 | + } |
|
258 | + |
|
259 | + protected function clearSchema(Connection $db, InputInterface $input, OutputInterface $output) { |
|
260 | + $toTables = $this->getTables($db); |
|
261 | + if (!empty($toTables)) { |
|
262 | + $output->writeln('<info>Clearing schema in new database</info>'); |
|
263 | + } |
|
264 | + foreach ($toTables as $table) { |
|
265 | + $db->createSchemaManager()->dropTable($table); |
|
266 | + } |
|
267 | + } |
|
268 | + |
|
269 | + protected function getTables(Connection $db) { |
|
270 | + $db->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
271 | + /** @var string|AbstractAsset $asset */ |
|
272 | + $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
273 | + if ($asset instanceof AbstractAsset) { |
|
274 | + return preg_match($filterExpression, $asset->getName()) !== false; |
|
275 | + } |
|
276 | + return preg_match($filterExpression, $asset) !== false; |
|
277 | + }); |
|
278 | + return $db->createSchemaManager()->listTableNames(); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @param Connection $fromDB |
|
283 | + * @param Connection $toDB |
|
284 | + * @param Table $table |
|
285 | + * @param InputInterface $input |
|
286 | + * @param OutputInterface $output |
|
287 | + */ |
|
288 | + protected function copyTable(Connection $fromDB, Connection $toDB, Table $table, InputInterface $input, OutputInterface $output) { |
|
289 | + if ($table->getName() === $toDB->getPrefix() . 'migrations') { |
|
290 | + $output->writeln('<comment>Skipping migrations table because it was already filled by running the migrations</comment>'); |
|
291 | + return; |
|
292 | + } |
|
293 | + |
|
294 | + $chunkSize = (int)$input->getOption('chunk-size'); |
|
295 | + |
|
296 | + $query = $fromDB->getQueryBuilder(); |
|
297 | + $query->automaticTablePrefix(false); |
|
298 | + $query->select($query->func()->count('*', 'num_entries')) |
|
299 | + ->from($table->getName()); |
|
300 | + $result = $query->executeQuery(); |
|
301 | + $count = $result->fetchOne(); |
|
302 | + $result->closeCursor(); |
|
303 | + |
|
304 | + $numChunks = ceil($count / $chunkSize); |
|
305 | + if ($numChunks > 1) { |
|
306 | + $output->writeln('chunked query, ' . $numChunks . ' chunks'); |
|
307 | + } |
|
308 | + |
|
309 | + $progress = new ProgressBar($output, $count); |
|
310 | + $progress->setFormat('very_verbose'); |
|
311 | + $progress->start(); |
|
312 | + $redraw = $count > $chunkSize ? 100 : ($count > 100 ? 5 : 1); |
|
313 | + $progress->setRedrawFrequency($redraw); |
|
314 | + |
|
315 | + $query = $fromDB->getQueryBuilder(); |
|
316 | + $query->automaticTablePrefix(false); |
|
317 | + $query->select('*') |
|
318 | + ->from($table->getName()) |
|
319 | + ->setMaxResults($chunkSize); |
|
320 | + |
|
321 | + try { |
|
322 | + $orderColumns = $table->getPrimaryKeyColumns(); |
|
323 | + } catch (Exception $e) { |
|
324 | + $orderColumns = $table->getColumns(); |
|
325 | + } |
|
326 | + |
|
327 | + foreach ($orderColumns as $column) { |
|
328 | + $query->addOrderBy($column->getName()); |
|
329 | + } |
|
330 | + |
|
331 | + $insertQuery = $toDB->getQueryBuilder(); |
|
332 | + $insertQuery->automaticTablePrefix(false); |
|
333 | + $insertQuery->insert($table->getName()); |
|
334 | + $parametersCreated = false; |
|
335 | + |
|
336 | + for ($chunk = 0; $chunk < $numChunks; $chunk++) { |
|
337 | + $query->setFirstResult($chunk * $chunkSize); |
|
338 | + |
|
339 | + $result = $query->executeQuery(); |
|
340 | + |
|
341 | + try { |
|
342 | + $toDB->beginTransaction(); |
|
343 | + |
|
344 | + while ($row = $result->fetch()) { |
|
345 | + $progress->advance(); |
|
346 | + if (!$parametersCreated) { |
|
347 | + foreach ($row as $key => $value) { |
|
348 | + $insertQuery->setValue($key, $insertQuery->createParameter($key)); |
|
349 | + } |
|
350 | + $parametersCreated = true; |
|
351 | + } |
|
352 | + |
|
353 | + foreach ($row as $key => $value) { |
|
354 | + $type = $this->getColumnType($table, $key); |
|
355 | + if ($type !== false) { |
|
356 | + $insertQuery->setParameter($key, $value, $type); |
|
357 | + } else { |
|
358 | + $insertQuery->setParameter($key, $value); |
|
359 | + } |
|
360 | + } |
|
361 | + $insertQuery->execute(); |
|
362 | + } |
|
363 | + $result->closeCursor(); |
|
364 | + |
|
365 | + $toDB->commit(); |
|
366 | + } catch (\Throwable $e) { |
|
367 | + $toDB->rollBack(); |
|
368 | + throw $e; |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + $progress->finish(); |
|
373 | + $output->writeln(''); |
|
374 | + } |
|
375 | + |
|
376 | + protected function getColumnType(Table $table, $columnName) { |
|
377 | + $tableName = $table->getName(); |
|
378 | + if (isset($this->columnTypes[$tableName][$columnName])) { |
|
379 | + return $this->columnTypes[$tableName][$columnName]; |
|
380 | + } |
|
381 | + |
|
382 | + $type = $table->getColumn($columnName)->getType()->getName(); |
|
383 | + |
|
384 | + switch ($type) { |
|
385 | + case Types::BLOB: |
|
386 | + case Types::TEXT: |
|
387 | + $this->columnTypes[$tableName][$columnName] = IQueryBuilder::PARAM_LOB; |
|
388 | + break; |
|
389 | + case Types::BOOLEAN: |
|
390 | + $this->columnTypes[$tableName][$columnName] = IQueryBuilder::PARAM_BOOL; |
|
391 | + break; |
|
392 | + default: |
|
393 | + $this->columnTypes[$tableName][$columnName] = false; |
|
394 | + } |
|
395 | + |
|
396 | + return $this->columnTypes[$tableName][$columnName]; |
|
397 | + } |
|
398 | + |
|
399 | + protected function convertDB(Connection $fromDB, Connection $toDB, array $tables, InputInterface $input, OutputInterface $output) { |
|
400 | + $this->config->setSystemValue('maintenance', true); |
|
401 | + $schema = $fromDB->createSchema(); |
|
402 | + |
|
403 | + try { |
|
404 | + // copy table rows |
|
405 | + foreach ($tables as $table) { |
|
406 | + $output->writeln('<info> - ' . $table . '</info>'); |
|
407 | + $this->copyTable($fromDB, $toDB, $schema->getTable($table), $input, $output); |
|
408 | + } |
|
409 | + if ($input->getArgument('type') === 'pgsql') { |
|
410 | + $tools = new PgSqlTools($this->config); |
|
411 | + $tools->resynchronizeDatabaseSequences($toDB); |
|
412 | + } |
|
413 | + // save new database config |
|
414 | + $this->saveDBInfo($input); |
|
415 | + } catch (\Exception $e) { |
|
416 | + $this->config->setSystemValue('maintenance', false); |
|
417 | + throw $e; |
|
418 | + } |
|
419 | + $this->config->setSystemValue('maintenance', false); |
|
420 | + } |
|
421 | + |
|
422 | + protected function saveDBInfo(InputInterface $input) { |
|
423 | + $type = $input->getArgument('type'); |
|
424 | + $username = $input->getArgument('username'); |
|
425 | + $dbHost = $input->getArgument('hostname'); |
|
426 | + $dbName = $input->getArgument('database'); |
|
427 | + $password = $input->getOption('password'); |
|
428 | + if ($input->getOption('port')) { |
|
429 | + $dbHost .= ':' . $input->getOption('port'); |
|
430 | + } |
|
431 | + |
|
432 | + $this->config->setSystemValues([ |
|
433 | + 'dbtype' => $type, |
|
434 | + 'dbname' => $dbName, |
|
435 | + 'dbhost' => $dbHost, |
|
436 | + 'dbuser' => $username, |
|
437 | + 'dbpassword' => $password, |
|
438 | + ]); |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Return possible values for the named option |
|
443 | + * |
|
444 | + * @param string $optionName |
|
445 | + * @param CompletionContext $context |
|
446 | + * @return string[] |
|
447 | + */ |
|
448 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
449 | + return []; |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Return possible values for the named argument |
|
454 | + * |
|
455 | + * @param string $argumentName |
|
456 | + * @param CompletionContext $context |
|
457 | + * @return string[] |
|
458 | + */ |
|
459 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
460 | + if ($argumentName === 'type') { |
|
461 | + return ['mysql', 'oci', 'pgsql']; |
|
462 | + } |
|
463 | + return []; |
|
464 | + } |
|
465 | 465 | } |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | ? $this->appManager->getAllAppsInAppsFolders() |
215 | 215 | : $this->appManager->getEnabledApps(); |
216 | 216 | foreach ($apps as $app) { |
217 | - $output->writeln('<info> - ' . $app . '</info>'); |
|
217 | + $output->writeln('<info> - '.$app.'</info>'); |
|
218 | 218 | // Make sure autoloading works... |
219 | 219 | $this->appManager->loadApp($app); |
220 | 220 | $fromMS = new MigrationService($app, $fromDB); |
@@ -267,9 +267,9 @@ discard block |
||
267 | 267 | } |
268 | 268 | |
269 | 269 | protected function getTables(Connection $db) { |
270 | - $db->getConfiguration()->setSchemaAssetsFilter(function ($asset) { |
|
270 | + $db->getConfiguration()->setSchemaAssetsFilter(function($asset) { |
|
271 | 271 | /** @var string|AbstractAsset $asset */ |
272 | - $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
272 | + $filterExpression = '/^'.preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')).'/'; |
|
273 | 273 | if ($asset instanceof AbstractAsset) { |
274 | 274 | return preg_match($filterExpression, $asset->getName()) !== false; |
275 | 275 | } |
@@ -286,12 +286,12 @@ discard block |
||
286 | 286 | * @param OutputInterface $output |
287 | 287 | */ |
288 | 288 | protected function copyTable(Connection $fromDB, Connection $toDB, Table $table, InputInterface $input, OutputInterface $output) { |
289 | - if ($table->getName() === $toDB->getPrefix() . 'migrations') { |
|
289 | + if ($table->getName() === $toDB->getPrefix().'migrations') { |
|
290 | 290 | $output->writeln('<comment>Skipping migrations table because it was already filled by running the migrations</comment>'); |
291 | 291 | return; |
292 | 292 | } |
293 | 293 | |
294 | - $chunkSize = (int)$input->getOption('chunk-size'); |
|
294 | + $chunkSize = (int) $input->getOption('chunk-size'); |
|
295 | 295 | |
296 | 296 | $query = $fromDB->getQueryBuilder(); |
297 | 297 | $query->automaticTablePrefix(false); |
@@ -303,7 +303,7 @@ discard block |
||
303 | 303 | |
304 | 304 | $numChunks = ceil($count / $chunkSize); |
305 | 305 | if ($numChunks > 1) { |
306 | - $output->writeln('chunked query, ' . $numChunks . ' chunks'); |
|
306 | + $output->writeln('chunked query, '.$numChunks.' chunks'); |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | $progress = new ProgressBar($output, $count); |
@@ -403,7 +403,7 @@ discard block |
||
403 | 403 | try { |
404 | 404 | // copy table rows |
405 | 405 | foreach ($tables as $table) { |
406 | - $output->writeln('<info> - ' . $table . '</info>'); |
|
406 | + $output->writeln('<info> - '.$table.'</info>'); |
|
407 | 407 | $this->copyTable($fromDB, $toDB, $schema->getTable($table), $input, $output); |
408 | 408 | } |
409 | 409 | if ($input->getArgument('type') === 'pgsql') { |
@@ -426,7 +426,7 @@ discard block |
||
426 | 426 | $dbName = $input->getArgument('database'); |
427 | 427 | $password = $input->getOption('password'); |
428 | 428 | if ($input->getOption('port')) { |
429 | - $dbHost .= ':' . $input->getOption('port'); |
|
429 | + $dbHost .= ':'.$input->getOption('port'); |
|
430 | 430 | } |
431 | 431 | |
432 | 432 | $this->config->setSystemValues([ |
@@ -11,7 +11,7 @@ |
||
11 | 11 | use OCP\Server; |
12 | 12 | |
13 | 13 | if (!defined('PHPUNIT_RUN')) { |
14 | - define('PHPUNIT_RUN', 1); |
|
14 | + define('PHPUNIT_RUN', 1); |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | require_once __DIR__ . '/../../../lib/base.php'; |
@@ -14,7 +14,7 @@ |
||
14 | 14 | define('PHPUNIT_RUN', 1); |
15 | 15 | } |
16 | 16 | |
17 | -require_once __DIR__ . '/../../../lib/base.php'; |
|
18 | -require_once __DIR__ . '/../../../tests/autoload.php'; |
|
17 | +require_once __DIR__.'/../../../lib/base.php'; |
|
18 | +require_once __DIR__.'/../../../tests/autoload.php'; |
|
19 | 19 | |
20 | 20 | Server::get(IAppManager::class)->loadApp('user_status'); |
@@ -103,180 +103,180 @@ |
||
103 | 103 | use function is_null; |
104 | 104 | |
105 | 105 | class Application extends App implements IBootstrap { |
106 | - public const APP_ID = 'dav'; |
|
106 | + public const APP_ID = 'dav'; |
|
107 | 107 | |
108 | - public function __construct() { |
|
109 | - parent::__construct(self::APP_ID); |
|
110 | - } |
|
108 | + public function __construct() { |
|
109 | + parent::__construct(self::APP_ID); |
|
110 | + } |
|
111 | 111 | |
112 | - public function register(IRegistrationContext $context): void { |
|
113 | - $context->registerServiceAlias('CardDAVSyncService', SyncService::class); |
|
114 | - $context->registerService(AppCalendarPlugin::class, function (ContainerInterface $c) { |
|
115 | - return new AppCalendarPlugin( |
|
116 | - $c->get(ICalendarManager::class), |
|
117 | - $c->get(LoggerInterface::class) |
|
118 | - ); |
|
119 | - }); |
|
112 | + public function register(IRegistrationContext $context): void { |
|
113 | + $context->registerServiceAlias('CardDAVSyncService', SyncService::class); |
|
114 | + $context->registerService(AppCalendarPlugin::class, function (ContainerInterface $c) { |
|
115 | + return new AppCalendarPlugin( |
|
116 | + $c->get(ICalendarManager::class), |
|
117 | + $c->get(LoggerInterface::class) |
|
118 | + ); |
|
119 | + }); |
|
120 | 120 | |
121 | - /* |
|
121 | + /* |
|
122 | 122 | * Register capabilities |
123 | 123 | */ |
124 | - $context->registerCapability(Capabilities::class); |
|
124 | + $context->registerCapability(Capabilities::class); |
|
125 | 125 | |
126 | - /* |
|
126 | + /* |
|
127 | 127 | * Register Search Providers |
128 | 128 | */ |
129 | - $context->registerSearchProvider(ContactsSearchProvider::class); |
|
130 | - $context->registerSearchProvider(EventsSearchProvider::class); |
|
131 | - $context->registerSearchProvider(TasksSearchProvider::class); |
|
129 | + $context->registerSearchProvider(ContactsSearchProvider::class); |
|
130 | + $context->registerSearchProvider(EventsSearchProvider::class); |
|
131 | + $context->registerSearchProvider(TasksSearchProvider::class); |
|
132 | 132 | |
133 | - /** |
|
134 | - * Register event listeners |
|
135 | - */ |
|
136 | - $context->registerEventListener(AddMissingIndicesEvent::class, AddMissingIndicesListener::class); |
|
133 | + /** |
|
134 | + * Register event listeners |
|
135 | + */ |
|
136 | + $context->registerEventListener(AddMissingIndicesEvent::class, AddMissingIndicesListener::class); |
|
137 | 137 | |
138 | - $context->registerEventListener(CalendarCreatedEvent::class, ActivityUpdaterListener::class); |
|
139 | - $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class); |
|
140 | - $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
141 | - $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class); |
|
142 | - $context->registerEventListener(CalendarMovedToTrashEvent::class, ActivityUpdaterListener::class); |
|
143 | - $context->registerEventListener(CalendarMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
144 | - $context->registerEventListener(CalendarUpdatedEvent::class, ActivityUpdaterListener::class); |
|
145 | - $context->registerEventListener(CalendarRestoredEvent::class, ActivityUpdaterListener::class); |
|
146 | - $context->registerEventListener(CalendarRestoredEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
147 | - $context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class); |
|
148 | - $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class); |
|
149 | - $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
150 | - $context->registerEventListener(CalendarObjectUpdatedEvent::class, ActivityUpdaterListener::class); |
|
151 | - $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class); |
|
152 | - $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
153 | - $context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class); |
|
154 | - $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
155 | - $context->registerEventListener(CalendarObjectMovedEvent::class, ActivityUpdaterListener::class); |
|
156 | - $context->registerEventListener(CalendarObjectMovedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
157 | - $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, ActivityUpdaterListener::class); |
|
158 | - $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
159 | - $context->registerEventListener(CalendarObjectRestoredEvent::class, ActivityUpdaterListener::class); |
|
160 | - $context->registerEventListener(CalendarObjectRestoredEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
161 | - $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class); |
|
162 | - $context->registerEventListener(CalendarPublishedEvent::class, CalendarPublicationListener::class); |
|
163 | - $context->registerEventListener(CalendarUnpublishedEvent::class, CalendarPublicationListener::class); |
|
164 | - $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarShareUpdateListener::class); |
|
138 | + $context->registerEventListener(CalendarCreatedEvent::class, ActivityUpdaterListener::class); |
|
139 | + $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class); |
|
140 | + $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
141 | + $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class); |
|
142 | + $context->registerEventListener(CalendarMovedToTrashEvent::class, ActivityUpdaterListener::class); |
|
143 | + $context->registerEventListener(CalendarMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
144 | + $context->registerEventListener(CalendarUpdatedEvent::class, ActivityUpdaterListener::class); |
|
145 | + $context->registerEventListener(CalendarRestoredEvent::class, ActivityUpdaterListener::class); |
|
146 | + $context->registerEventListener(CalendarRestoredEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
147 | + $context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class); |
|
148 | + $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class); |
|
149 | + $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
150 | + $context->registerEventListener(CalendarObjectUpdatedEvent::class, ActivityUpdaterListener::class); |
|
151 | + $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class); |
|
152 | + $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
153 | + $context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class); |
|
154 | + $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
155 | + $context->registerEventListener(CalendarObjectMovedEvent::class, ActivityUpdaterListener::class); |
|
156 | + $context->registerEventListener(CalendarObjectMovedEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
157 | + $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, ActivityUpdaterListener::class); |
|
158 | + $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
159 | + $context->registerEventListener(CalendarObjectRestoredEvent::class, ActivityUpdaterListener::class); |
|
160 | + $context->registerEventListener(CalendarObjectRestoredEvent::class, CalendarObjectReminderUpdaterListener::class); |
|
161 | + $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class); |
|
162 | + $context->registerEventListener(CalendarPublishedEvent::class, CalendarPublicationListener::class); |
|
163 | + $context->registerEventListener(CalendarUnpublishedEvent::class, CalendarPublicationListener::class); |
|
164 | + $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarShareUpdateListener::class); |
|
165 | 165 | |
166 | - $context->registerEventListener(SubscriptionCreatedEvent::class, SubscriptionListener::class); |
|
167 | - $context->registerEventListener(SubscriptionDeletedEvent::class, SubscriptionListener::class); |
|
166 | + $context->registerEventListener(SubscriptionCreatedEvent::class, SubscriptionListener::class); |
|
167 | + $context->registerEventListener(SubscriptionDeletedEvent::class, SubscriptionListener::class); |
|
168 | 168 | |
169 | 169 | |
170 | - $context->registerEventListener(AddressBookCreatedEvent::class, AddressbookListener::class); |
|
171 | - $context->registerEventListener(AddressBookDeletedEvent::class, AddressbookListener::class); |
|
172 | - $context->registerEventListener(AddressBookUpdatedEvent::class, AddressbookListener::class); |
|
173 | - $context->registerEventListener(AddressBookShareUpdatedEvent::class, AddressbookListener::class); |
|
174 | - $context->registerEventListener(CardCreatedEvent::class, CardListener::class); |
|
175 | - $context->registerEventListener(CardDeletedEvent::class, CardListener::class); |
|
176 | - $context->registerEventListener(CardUpdatedEvent::class, CardListener::class); |
|
177 | - $context->registerEventListener(CardCreatedEvent::class, BirthdayListener::class); |
|
178 | - $context->registerEventListener(CardDeletedEvent::class, BirthdayListener::class); |
|
179 | - $context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class); |
|
180 | - $context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class); |
|
181 | - $context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class); |
|
182 | - $context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class); |
|
170 | + $context->registerEventListener(AddressBookCreatedEvent::class, AddressbookListener::class); |
|
171 | + $context->registerEventListener(AddressBookDeletedEvent::class, AddressbookListener::class); |
|
172 | + $context->registerEventListener(AddressBookUpdatedEvent::class, AddressbookListener::class); |
|
173 | + $context->registerEventListener(AddressBookShareUpdatedEvent::class, AddressbookListener::class); |
|
174 | + $context->registerEventListener(CardCreatedEvent::class, CardListener::class); |
|
175 | + $context->registerEventListener(CardDeletedEvent::class, CardListener::class); |
|
176 | + $context->registerEventListener(CardUpdatedEvent::class, CardListener::class); |
|
177 | + $context->registerEventListener(CardCreatedEvent::class, BirthdayListener::class); |
|
178 | + $context->registerEventListener(CardDeletedEvent::class, BirthdayListener::class); |
|
179 | + $context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class); |
|
180 | + $context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class); |
|
181 | + $context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class); |
|
182 | + $context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class); |
|
183 | 183 | |
184 | - $context->registerEventListener(BeforePreferenceDeletedEvent::class, UserPreferenceListener::class); |
|
185 | - $context->registerEventListener(BeforePreferenceSetEvent::class, UserPreferenceListener::class); |
|
184 | + $context->registerEventListener(BeforePreferenceDeletedEvent::class, UserPreferenceListener::class); |
|
185 | + $context->registerEventListener(BeforePreferenceSetEvent::class, UserPreferenceListener::class); |
|
186 | 186 | |
187 | - $context->registerEventListener(OutOfOfficeChangedEvent::class, OutOfOfficeListener::class); |
|
188 | - $context->registerEventListener(OutOfOfficeClearedEvent::class, OutOfOfficeListener::class); |
|
189 | - $context->registerEventListener(OutOfOfficeScheduledEvent::class, OutOfOfficeListener::class); |
|
187 | + $context->registerEventListener(OutOfOfficeChangedEvent::class, OutOfOfficeListener::class); |
|
188 | + $context->registerEventListener(OutOfOfficeClearedEvent::class, OutOfOfficeListener::class); |
|
189 | + $context->registerEventListener(OutOfOfficeScheduledEvent::class, OutOfOfficeListener::class); |
|
190 | 190 | |
191 | - $context->registerEventListener(UserFirstTimeLoggedInEvent::class, UserEventsListener::class); |
|
192 | - $context->registerEventListener(UserIdAssignedEvent::class, UserEventsListener::class); |
|
193 | - $context->registerEventListener(BeforeUserIdUnassignedEvent::class, UserEventsListener::class); |
|
194 | - $context->registerEventListener(UserIdUnassignedEvent::class, UserEventsListener::class); |
|
195 | - $context->registerEventListener(BeforeUserDeletedEvent::class, UserEventsListener::class); |
|
196 | - $context->registerEventListener(UserDeletedEvent::class, UserEventsListener::class); |
|
197 | - $context->registerEventListener(UserCreatedEvent::class, UserEventsListener::class); |
|
198 | - $context->registerEventListener(UserChangedEvent::class, UserEventsListener::class); |
|
199 | - $context->registerEventListener(UserUpdatedEvent::class, UserEventsListener::class); |
|
191 | + $context->registerEventListener(UserFirstTimeLoggedInEvent::class, UserEventsListener::class); |
|
192 | + $context->registerEventListener(UserIdAssignedEvent::class, UserEventsListener::class); |
|
193 | + $context->registerEventListener(BeforeUserIdUnassignedEvent::class, UserEventsListener::class); |
|
194 | + $context->registerEventListener(UserIdUnassignedEvent::class, UserEventsListener::class); |
|
195 | + $context->registerEventListener(BeforeUserDeletedEvent::class, UserEventsListener::class); |
|
196 | + $context->registerEventListener(UserDeletedEvent::class, UserEventsListener::class); |
|
197 | + $context->registerEventListener(UserCreatedEvent::class, UserEventsListener::class); |
|
198 | + $context->registerEventListener(UserChangedEvent::class, UserEventsListener::class); |
|
199 | + $context->registerEventListener(UserUpdatedEvent::class, UserEventsListener::class); |
|
200 | 200 | |
201 | - $context->registerNotifierService(Notifier::class); |
|
201 | + $context->registerNotifierService(Notifier::class); |
|
202 | 202 | |
203 | - $context->registerCalendarProvider(CalendarProvider::class); |
|
204 | - $context->registerCalendarProvider(CachedSubscriptionProvider::class); |
|
203 | + $context->registerCalendarProvider(CalendarProvider::class); |
|
204 | + $context->registerCalendarProvider(CachedSubscriptionProvider::class); |
|
205 | 205 | |
206 | - $context->registerUserMigrator(CalendarMigrator::class); |
|
207 | - $context->registerUserMigrator(ContactsMigrator::class); |
|
206 | + $context->registerUserMigrator(CalendarMigrator::class); |
|
207 | + $context->registerUserMigrator(ContactsMigrator::class); |
|
208 | 208 | |
209 | - $context->registerSetupCheck(NeedsSystemAddressBookSync::class); |
|
210 | - $context->registerSetupCheck(WebdavEndpoint::class); |
|
209 | + $context->registerSetupCheck(NeedsSystemAddressBookSync::class); |
|
210 | + $context->registerSetupCheck(WebdavEndpoint::class); |
|
211 | 211 | |
212 | - // register admin settings form and listener(s) |
|
213 | - $context->registerDeclarativeSettings(SystemAddressBookSettings::class); |
|
214 | - $context->registerEventListener(DeclarativeSettingsGetValueEvent::class, DavAdminSettingsListener::class); |
|
215 | - $context->registerEventListener(DeclarativeSettingsSetValueEvent::class, DavAdminSettingsListener::class); |
|
212 | + // register admin settings form and listener(s) |
|
213 | + $context->registerDeclarativeSettings(SystemAddressBookSettings::class); |
|
214 | + $context->registerEventListener(DeclarativeSettingsGetValueEvent::class, DavAdminSettingsListener::class); |
|
215 | + $context->registerEventListener(DeclarativeSettingsSetValueEvent::class, DavAdminSettingsListener::class); |
|
216 | 216 | |
217 | - } |
|
217 | + } |
|
218 | 218 | |
219 | - public function boot(IBootContext $context): void { |
|
220 | - // Load all dav apps |
|
221 | - $context->getServerContainer()->get(IAppManager::class)->loadApps(['dav']); |
|
219 | + public function boot(IBootContext $context): void { |
|
220 | + // Load all dav apps |
|
221 | + $context->getServerContainer()->get(IAppManager::class)->loadApps(['dav']); |
|
222 | 222 | |
223 | - $context->injectFn($this->registerContactsManager(...)); |
|
224 | - $context->injectFn($this->registerCalendarManager(...)); |
|
225 | - $context->injectFn($this->registerCalendarReminders(...)); |
|
226 | - } |
|
223 | + $context->injectFn($this->registerContactsManager(...)); |
|
224 | + $context->injectFn($this->registerCalendarManager(...)); |
|
225 | + $context->injectFn($this->registerCalendarReminders(...)); |
|
226 | + } |
|
227 | 227 | |
228 | - public function registerContactsManager(IContactsManager $cm, IAppContainer $container): void { |
|
229 | - $cm->register(function () use ($container, $cm): void { |
|
230 | - $user = Server::get(IUserSession::class)->getUser(); |
|
231 | - if (!is_null($user)) { |
|
232 | - $this->setupContactsProvider($cm, $container, $user->getUID()); |
|
233 | - } else { |
|
234 | - $this->setupSystemContactsProvider($cm, $container); |
|
235 | - } |
|
236 | - }); |
|
237 | - } |
|
228 | + public function registerContactsManager(IContactsManager $cm, IAppContainer $container): void { |
|
229 | + $cm->register(function () use ($container, $cm): void { |
|
230 | + $user = Server::get(IUserSession::class)->getUser(); |
|
231 | + if (!is_null($user)) { |
|
232 | + $this->setupContactsProvider($cm, $container, $user->getUID()); |
|
233 | + } else { |
|
234 | + $this->setupSystemContactsProvider($cm, $container); |
|
235 | + } |
|
236 | + }); |
|
237 | + } |
|
238 | 238 | |
239 | - private function setupContactsProvider(IContactsManager $contactsManager, |
|
240 | - IAppContainer $container, |
|
241 | - string $userID): void { |
|
242 | - /** @var ContactsManager $cm */ |
|
243 | - $cm = $container->query(ContactsManager::class); |
|
244 | - $urlGenerator = $container->getServer()->getURLGenerator(); |
|
245 | - $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator); |
|
246 | - } |
|
239 | + private function setupContactsProvider(IContactsManager $contactsManager, |
|
240 | + IAppContainer $container, |
|
241 | + string $userID): void { |
|
242 | + /** @var ContactsManager $cm */ |
|
243 | + $cm = $container->query(ContactsManager::class); |
|
244 | + $urlGenerator = $container->getServer()->getURLGenerator(); |
|
245 | + $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator); |
|
246 | + } |
|
247 | 247 | |
248 | - private function setupSystemContactsProvider(IContactsManager $contactsManager, IAppContainer $container): void { |
|
249 | - /** @var ContactsManager $cm */ |
|
250 | - $cm = $container->query(ContactsManager::class); |
|
251 | - $urlGenerator = $container->getServer()->getURLGenerator(); |
|
252 | - $cm->setupSystemContactsProvider($contactsManager, null, $urlGenerator); |
|
253 | - } |
|
248 | + private function setupSystemContactsProvider(IContactsManager $contactsManager, IAppContainer $container): void { |
|
249 | + /** @var ContactsManager $cm */ |
|
250 | + $cm = $container->query(ContactsManager::class); |
|
251 | + $urlGenerator = $container->getServer()->getURLGenerator(); |
|
252 | + $cm->setupSystemContactsProvider($contactsManager, null, $urlGenerator); |
|
253 | + } |
|
254 | 254 | |
255 | - public function registerCalendarManager(ICalendarManager $calendarManager, |
|
256 | - IAppContainer $container): void { |
|
257 | - $calendarManager->register(function () use ($container, $calendarManager): void { |
|
258 | - $user = Server::get(IUserSession::class)->getUser(); |
|
259 | - if ($user !== null) { |
|
260 | - $this->setupCalendarProvider($calendarManager, $container, $user->getUID()); |
|
261 | - } |
|
262 | - }); |
|
263 | - } |
|
255 | + public function registerCalendarManager(ICalendarManager $calendarManager, |
|
256 | + IAppContainer $container): void { |
|
257 | + $calendarManager->register(function () use ($container, $calendarManager): void { |
|
258 | + $user = Server::get(IUserSession::class)->getUser(); |
|
259 | + if ($user !== null) { |
|
260 | + $this->setupCalendarProvider($calendarManager, $container, $user->getUID()); |
|
261 | + } |
|
262 | + }); |
|
263 | + } |
|
264 | 264 | |
265 | - private function setupCalendarProvider(ICalendarManager $calendarManager, |
|
266 | - IAppContainer $container, |
|
267 | - $userId) { |
|
268 | - $cm = $container->query(CalendarManager::class); |
|
269 | - $cm->setupCalendarProvider($calendarManager, $userId); |
|
270 | - } |
|
265 | + private function setupCalendarProvider(ICalendarManager $calendarManager, |
|
266 | + IAppContainer $container, |
|
267 | + $userId) { |
|
268 | + $cm = $container->query(CalendarManager::class); |
|
269 | + $cm->setupCalendarProvider($calendarManager, $userId); |
|
270 | + } |
|
271 | 271 | |
272 | - public function registerCalendarReminders(NotificationProviderManager $manager, |
|
273 | - LoggerInterface $logger): void { |
|
274 | - try { |
|
275 | - $manager->registerProvider(AudioProvider::class); |
|
276 | - $manager->registerProvider(EmailProvider::class); |
|
277 | - $manager->registerProvider(PushProvider::class); |
|
278 | - } catch (Throwable $ex) { |
|
279 | - $logger->error($ex->getMessage(), ['exception' => $ex]); |
|
280 | - } |
|
281 | - } |
|
272 | + public function registerCalendarReminders(NotificationProviderManager $manager, |
|
273 | + LoggerInterface $logger): void { |
|
274 | + try { |
|
275 | + $manager->registerProvider(AudioProvider::class); |
|
276 | + $manager->registerProvider(EmailProvider::class); |
|
277 | + $manager->registerProvider(PushProvider::class); |
|
278 | + } catch (Throwable $ex) { |
|
279 | + $logger->error($ex->getMessage(), ['exception' => $ex]); |
|
280 | + } |
|
281 | + } |
|
282 | 282 | } |
@@ -12,7 +12,7 @@ |
||
12 | 12 | use OCP\Server; |
13 | 13 | |
14 | 14 | if (!defined('PHPUNIT_RUN')) { |
15 | - define('PHPUNIT_RUN', 1); |
|
15 | + define('PHPUNIT_RUN', 1); |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | require_once __DIR__ . '/../../../../lib/base.php'; |
@@ -15,7 +15,7 @@ |
||
15 | 15 | define('PHPUNIT_RUN', 1); |
16 | 16 | } |
17 | 17 | |
18 | -require_once __DIR__ . '/../../../../lib/base.php'; |
|
19 | -require_once __DIR__ . '/../../../../tests/autoload.php'; |
|
18 | +require_once __DIR__.'/../../../../lib/base.php'; |
|
19 | +require_once __DIR__.'/../../../../tests/autoload.php'; |
|
20 | 20 | |
21 | 21 | Server::get(IAppManager::class)->loadApp('dav'); |
@@ -23,143 +23,143 @@ |
||
23 | 23 | * |
24 | 24 | */ |
25 | 25 | class ExceptionOnLostConnection { |
26 | - /** @var string */ |
|
27 | - private $ldapHost; |
|
28 | - |
|
29 | - /** @var LDAP */ |
|
30 | - private $ldap; |
|
31 | - |
|
32 | - /** @var bool */ |
|
33 | - private $originalProxyState; |
|
34 | - |
|
35 | - /** |
|
36 | - * @param string $toxiProxyHost host of toxiproxy as url, like http://localhost:8474 |
|
37 | - * @param string $toxiProxyName name of the LDAP proxy service as configured in toxiProxy |
|
38 | - * @param string $ldapBase any valid LDAP base DN |
|
39 | - * @param null $ldapBindDN optional, bind DN if anonymous bind is not possible |
|
40 | - * @param null $ldapBindPwd optional |
|
41 | - */ |
|
42 | - public function __construct( |
|
43 | - private $toxiProxyHost, |
|
44 | - private $toxiProxyName, |
|
45 | - private $ldapBase, |
|
46 | - private $ldapBindDN = null, |
|
47 | - private $ldapBindPwd = null, |
|
48 | - ) { |
|
49 | - $this->setUp(); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * destructor |
|
54 | - */ |
|
55 | - public function __destruct() { |
|
56 | - $this->cleanUp(); |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * prepares everything for the test run. Includes loading Nextcloud and |
|
61 | - * the LDAP backend, as well as getting information about toxiproxy. |
|
62 | - * Also creates an instance of the LDAP class, the testee |
|
63 | - * |
|
64 | - * @throws \Exception |
|
65 | - */ |
|
66 | - public function setUp(): void { |
|
67 | - require_once __DIR__ . '/../../../../lib/base.php'; |
|
68 | - Server::get(IAppManager::class)->loadApps(['user_ldap']); |
|
69 | - |
|
70 | - $ch = $this->getCurl(); |
|
71 | - $proxyInfoJson = curl_exec($ch); |
|
72 | - $this->checkCurlResult($ch, $proxyInfoJson); |
|
73 | - $proxyInfo = json_decode($proxyInfoJson, true); |
|
74 | - $this->originalProxyState = $proxyInfo['enabled']; |
|
75 | - $this->ldapHost = 'ldap://' . $proxyInfo['listen']; // contains port as well |
|
76 | - |
|
77 | - $this->ldap = new LDAP(); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * restores original state of the LDAP proxy, if necessary |
|
82 | - */ |
|
83 | - public function cleanUp() { |
|
84 | - if ($this->originalProxyState === true) { |
|
85 | - $this->setProxyState(true); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * runs the test and prints the result. Exit code is 0 if successful, 1 on |
|
91 | - * fail |
|
92 | - */ |
|
93 | - public function run() { |
|
94 | - if ($this->originalProxyState === false) { |
|
95 | - $this->setProxyState(true); |
|
96 | - } |
|
97 | - //host contains port, 2nd parameter will be ignored |
|
98 | - $cr = $this->ldap->connect($this->ldapHost, 0); |
|
99 | - $this->ldap->bind($cr, $this->ldapBindDN, $this->ldapBindPwd); |
|
100 | - $this->ldap->search($cr, $this->ldapBase, 'objectClass=*', ['dn'], true, 5); |
|
101 | - |
|
102 | - // disable LDAP, will cause lost connection |
|
103 | - $this->setProxyState(false); |
|
104 | - try { |
|
105 | - $this->ldap->search($cr, $this->ldapBase, 'objectClass=*', ['dn'], true, 5); |
|
106 | - } catch (ServerNotAvailableException $e) { |
|
107 | - print('Test PASSED' . PHP_EOL); |
|
108 | - exit(0); |
|
109 | - } |
|
110 | - print('Test FAILED' . PHP_EOL); |
|
111 | - exit(1); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * tests whether a curl operation ran successfully. If not, an exception |
|
116 | - * is thrown |
|
117 | - * |
|
118 | - * @param resource|\CurlHandle $ch |
|
119 | - * @param mixed $result |
|
120 | - * @throws \Exception |
|
121 | - */ |
|
122 | - private function checkCurlResult($ch, $result) { |
|
123 | - if ($result === false) { |
|
124 | - $error = curl_error($ch); |
|
125 | - curl_close($ch); |
|
126 | - throw new \Exception($error); |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * enables or disabled the LDAP proxy service in toxiproxy |
|
132 | - * |
|
133 | - * @param bool $isEnabled whether is should be enabled or disables |
|
134 | - * @throws \Exception |
|
135 | - */ |
|
136 | - private function setProxyState($isEnabled) { |
|
137 | - if (!is_bool($isEnabled)) { |
|
138 | - throw new \InvalidArgumentException('Bool expected'); |
|
139 | - } |
|
140 | - $postData = json_encode(['enabled' => $isEnabled]); |
|
141 | - $ch = $this->getCurl(); |
|
142 | - curl_setopt($ch, CURLOPT_POST, true); |
|
143 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); |
|
144 | - curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
|
145 | - 'Content-Type: application/json', |
|
146 | - 'Content-Length: ' . strlen($postData)] |
|
147 | - ); |
|
148 | - $recvd = curl_exec($ch); |
|
149 | - $this->checkCurlResult($ch, $recvd); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * initializes a curl handler towards the toxiproxy LDAP proxy service |
|
154 | - * @return resource|\CurlHandle |
|
155 | - */ |
|
156 | - private function getCurl() { |
|
157 | - $ch = curl_init(); |
|
158 | - curl_setopt($ch, CURLOPT_URL, $this->toxiProxyHost . '/proxies/' . $this->toxiProxyName); |
|
159 | - curl_setopt($ch, CURLOPT_HEADER, false); |
|
160 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
161 | - return $ch; |
|
162 | - } |
|
26 | + /** @var string */ |
|
27 | + private $ldapHost; |
|
28 | + |
|
29 | + /** @var LDAP */ |
|
30 | + private $ldap; |
|
31 | + |
|
32 | + /** @var bool */ |
|
33 | + private $originalProxyState; |
|
34 | + |
|
35 | + /** |
|
36 | + * @param string $toxiProxyHost host of toxiproxy as url, like http://localhost:8474 |
|
37 | + * @param string $toxiProxyName name of the LDAP proxy service as configured in toxiProxy |
|
38 | + * @param string $ldapBase any valid LDAP base DN |
|
39 | + * @param null $ldapBindDN optional, bind DN if anonymous bind is not possible |
|
40 | + * @param null $ldapBindPwd optional |
|
41 | + */ |
|
42 | + public function __construct( |
|
43 | + private $toxiProxyHost, |
|
44 | + private $toxiProxyName, |
|
45 | + private $ldapBase, |
|
46 | + private $ldapBindDN = null, |
|
47 | + private $ldapBindPwd = null, |
|
48 | + ) { |
|
49 | + $this->setUp(); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * destructor |
|
54 | + */ |
|
55 | + public function __destruct() { |
|
56 | + $this->cleanUp(); |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * prepares everything for the test run. Includes loading Nextcloud and |
|
61 | + * the LDAP backend, as well as getting information about toxiproxy. |
|
62 | + * Also creates an instance of the LDAP class, the testee |
|
63 | + * |
|
64 | + * @throws \Exception |
|
65 | + */ |
|
66 | + public function setUp(): void { |
|
67 | + require_once __DIR__ . '/../../../../lib/base.php'; |
|
68 | + Server::get(IAppManager::class)->loadApps(['user_ldap']); |
|
69 | + |
|
70 | + $ch = $this->getCurl(); |
|
71 | + $proxyInfoJson = curl_exec($ch); |
|
72 | + $this->checkCurlResult($ch, $proxyInfoJson); |
|
73 | + $proxyInfo = json_decode($proxyInfoJson, true); |
|
74 | + $this->originalProxyState = $proxyInfo['enabled']; |
|
75 | + $this->ldapHost = 'ldap://' . $proxyInfo['listen']; // contains port as well |
|
76 | + |
|
77 | + $this->ldap = new LDAP(); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * restores original state of the LDAP proxy, if necessary |
|
82 | + */ |
|
83 | + public function cleanUp() { |
|
84 | + if ($this->originalProxyState === true) { |
|
85 | + $this->setProxyState(true); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * runs the test and prints the result. Exit code is 0 if successful, 1 on |
|
91 | + * fail |
|
92 | + */ |
|
93 | + public function run() { |
|
94 | + if ($this->originalProxyState === false) { |
|
95 | + $this->setProxyState(true); |
|
96 | + } |
|
97 | + //host contains port, 2nd parameter will be ignored |
|
98 | + $cr = $this->ldap->connect($this->ldapHost, 0); |
|
99 | + $this->ldap->bind($cr, $this->ldapBindDN, $this->ldapBindPwd); |
|
100 | + $this->ldap->search($cr, $this->ldapBase, 'objectClass=*', ['dn'], true, 5); |
|
101 | + |
|
102 | + // disable LDAP, will cause lost connection |
|
103 | + $this->setProxyState(false); |
|
104 | + try { |
|
105 | + $this->ldap->search($cr, $this->ldapBase, 'objectClass=*', ['dn'], true, 5); |
|
106 | + } catch (ServerNotAvailableException $e) { |
|
107 | + print('Test PASSED' . PHP_EOL); |
|
108 | + exit(0); |
|
109 | + } |
|
110 | + print('Test FAILED' . PHP_EOL); |
|
111 | + exit(1); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * tests whether a curl operation ran successfully. If not, an exception |
|
116 | + * is thrown |
|
117 | + * |
|
118 | + * @param resource|\CurlHandle $ch |
|
119 | + * @param mixed $result |
|
120 | + * @throws \Exception |
|
121 | + */ |
|
122 | + private function checkCurlResult($ch, $result) { |
|
123 | + if ($result === false) { |
|
124 | + $error = curl_error($ch); |
|
125 | + curl_close($ch); |
|
126 | + throw new \Exception($error); |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * enables or disabled the LDAP proxy service in toxiproxy |
|
132 | + * |
|
133 | + * @param bool $isEnabled whether is should be enabled or disables |
|
134 | + * @throws \Exception |
|
135 | + */ |
|
136 | + private function setProxyState($isEnabled) { |
|
137 | + if (!is_bool($isEnabled)) { |
|
138 | + throw new \InvalidArgumentException('Bool expected'); |
|
139 | + } |
|
140 | + $postData = json_encode(['enabled' => $isEnabled]); |
|
141 | + $ch = $this->getCurl(); |
|
142 | + curl_setopt($ch, CURLOPT_POST, true); |
|
143 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); |
|
144 | + curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
|
145 | + 'Content-Type: application/json', |
|
146 | + 'Content-Length: ' . strlen($postData)] |
|
147 | + ); |
|
148 | + $recvd = curl_exec($ch); |
|
149 | + $this->checkCurlResult($ch, $recvd); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * initializes a curl handler towards the toxiproxy LDAP proxy service |
|
154 | + * @return resource|\CurlHandle |
|
155 | + */ |
|
156 | + private function getCurl() { |
|
157 | + $ch = curl_init(); |
|
158 | + curl_setopt($ch, CURLOPT_URL, $this->toxiProxyHost . '/proxies/' . $this->toxiProxyName); |
|
159 | + curl_setopt($ch, CURLOPT_HEADER, false); |
|
160 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
161 | + return $ch; |
|
162 | + } |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | $test = new ExceptionOnLostConnection('http://localhost:8474', 'ldap', 'dc=owncloud,dc=bzoc'); |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | * @throws \Exception |
65 | 65 | */ |
66 | 66 | public function setUp(): void { |
67 | - require_once __DIR__ . '/../../../../lib/base.php'; |
|
67 | + require_once __DIR__.'/../../../../lib/base.php'; |
|
68 | 68 | Server::get(IAppManager::class)->loadApps(['user_ldap']); |
69 | 69 | |
70 | 70 | $ch = $this->getCurl(); |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | $this->checkCurlResult($ch, $proxyInfoJson); |
73 | 73 | $proxyInfo = json_decode($proxyInfoJson, true); |
74 | 74 | $this->originalProxyState = $proxyInfo['enabled']; |
75 | - $this->ldapHost = 'ldap://' . $proxyInfo['listen']; // contains port as well |
|
75 | + $this->ldapHost = 'ldap://'.$proxyInfo['listen']; // contains port as well |
|
76 | 76 | |
77 | 77 | $this->ldap = new LDAP(); |
78 | 78 | } |
@@ -104,10 +104,10 @@ discard block |
||
104 | 104 | try { |
105 | 105 | $this->ldap->search($cr, $this->ldapBase, 'objectClass=*', ['dn'], true, 5); |
106 | 106 | } catch (ServerNotAvailableException $e) { |
107 | - print('Test PASSED' . PHP_EOL); |
|
107 | + print('Test PASSED'.PHP_EOL); |
|
108 | 108 | exit(0); |
109 | 109 | } |
110 | - print('Test FAILED' . PHP_EOL); |
|
110 | + print('Test FAILED'.PHP_EOL); |
|
111 | 111 | exit(1); |
112 | 112 | } |
113 | 113 | |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); |
144 | 144 | curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
145 | 145 | 'Content-Type: application/json', |
146 | - 'Content-Length: ' . strlen($postData)] |
|
146 | + 'Content-Length: '.strlen($postData)] |
|
147 | 147 | ); |
148 | 148 | $recvd = curl_exec($ch); |
149 | 149 | $this->checkCurlResult($ch, $recvd); |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | */ |
156 | 156 | private function getCurl() { |
157 | 157 | $ch = curl_init(); |
158 | - curl_setopt($ch, CURLOPT_URL, $this->toxiProxyHost . '/proxies/' . $this->toxiProxyName); |
|
158 | + curl_setopt($ch, CURLOPT_URL, $this->toxiProxyHost.'/proxies/'.$this->toxiProxyName); |
|
159 | 159 | curl_setopt($ch, CURLOPT_HEADER, false); |
160 | 160 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
161 | 161 | return $ch; |
@@ -29,11 +29,11 @@ discard block |
||
29 | 29 | use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
30 | 30 | |
31 | 31 | if (Util::needUpgrade() |
32 | - || Server::get(IConfig::class)->getSystemValueBool('maintenance')) { |
|
33 | - // since the behavior of apps or remotes are unpredictable during |
|
34 | - // an upgrade, return a 503 directly |
|
35 | - ApiHelper::respond(503, 'Service unavailable', ['X-Nextcloud-Maintenance-Mode' => '1'], 503); |
|
36 | - exit; |
|
32 | + || Server::get(IConfig::class)->getSystemValueBool('maintenance')) { |
|
33 | + // since the behavior of apps or remotes are unpredictable during |
|
34 | + // an upgrade, return a 503 directly |
|
35 | + ApiHelper::respond(503, 'Service unavailable', ['X-Nextcloud-Maintenance-Mode' => '1'], 503); |
|
36 | + exit; |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | |
@@ -41,45 +41,45 @@ discard block |
||
41 | 41 | * Try the appframework routes |
42 | 42 | */ |
43 | 43 | try { |
44 | - $appManager = Server::get(IAppManager::class); |
|
45 | - $appManager->loadApps(['session']); |
|
46 | - $appManager->loadApps(['authentication']); |
|
47 | - $appManager->loadApps(['extended_authentication']); |
|
44 | + $appManager = Server::get(IAppManager::class); |
|
45 | + $appManager->loadApps(['session']); |
|
46 | + $appManager->loadApps(['authentication']); |
|
47 | + $appManager->loadApps(['extended_authentication']); |
|
48 | 48 | |
49 | - // load all apps to get all api routes properly setup |
|
50 | - // FIXME: this should ideally appear after handleLogin but will cause |
|
51 | - // side effects in existing apps |
|
52 | - $appManager->loadApps(); |
|
49 | + // load all apps to get all api routes properly setup |
|
50 | + // FIXME: this should ideally appear after handleLogin but will cause |
|
51 | + // side effects in existing apps |
|
52 | + $appManager->loadApps(); |
|
53 | 53 | |
54 | - $request = Server::get(IRequest::class); |
|
55 | - $request->throwDecodingExceptionIfAny(); |
|
54 | + $request = Server::get(IRequest::class); |
|
55 | + $request->throwDecodingExceptionIfAny(); |
|
56 | 56 | |
57 | - if (!Server::get(IUserSession::class)->isLoggedIn()) { |
|
58 | - OC::handleLogin($request); |
|
59 | - } |
|
57 | + if (!Server::get(IUserSession::class)->isLoggedIn()) { |
|
58 | + OC::handleLogin($request); |
|
59 | + } |
|
60 | 60 | |
61 | - Server::get(Router::class)->match('/ocsapp' . $request->getRawPathInfo()); |
|
61 | + Server::get(Router::class)->match('/ocsapp' . $request->getRawPathInfo()); |
|
62 | 62 | } catch (MaxDelayReached $ex) { |
63 | - ApiHelper::respond(Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage()); |
|
63 | + ApiHelper::respond(Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage()); |
|
64 | 64 | } catch (ResourceNotFoundException $e) { |
65 | - $txt = 'Invalid query, please check the syntax. API specifications are here:' |
|
66 | - . ' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.' . "\n"; |
|
67 | - ApiHelper::respond(OCSController::RESPOND_NOT_FOUND, $txt); |
|
65 | + $txt = 'Invalid query, please check the syntax. API specifications are here:' |
|
66 | + . ' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.' . "\n"; |
|
67 | + ApiHelper::respond(OCSController::RESPOND_NOT_FOUND, $txt); |
|
68 | 68 | } catch (MethodNotAllowedException $e) { |
69 | - ApiHelper::setContentType(); |
|
70 | - http_response_code(405); |
|
69 | + ApiHelper::setContentType(); |
|
70 | + http_response_code(405); |
|
71 | 71 | } catch (LoginException $e) { |
72 | - ApiHelper::respond(OCSController::RESPOND_UNAUTHORISED, 'Unauthorised'); |
|
72 | + ApiHelper::respond(OCSController::RESPOND_UNAUTHORISED, 'Unauthorised'); |
|
73 | 73 | } catch (\Exception $e) { |
74 | - Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]); |
|
74 | + Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]); |
|
75 | 75 | |
76 | - $txt = 'Internal Server Error' . "\n"; |
|
77 | - try { |
|
78 | - if (Server::get(SystemConfig::class)->getValue('debug', false)) { |
|
79 | - $txt .= $e->getMessage(); |
|
80 | - } |
|
81 | - } catch (\Throwable $e) { |
|
82 | - // Just to be save |
|
83 | - } |
|
84 | - ApiHelper::respond(OCSController::RESPOND_SERVER_ERROR, $txt); |
|
76 | + $txt = 'Internal Server Error' . "\n"; |
|
77 | + try { |
|
78 | + if (Server::get(SystemConfig::class)->getValue('debug', false)) { |
|
79 | + $txt .= $e->getMessage(); |
|
80 | + } |
|
81 | + } catch (\Throwable $e) { |
|
82 | + // Just to be save |
|
83 | + } |
|
84 | + ApiHelper::respond(OCSController::RESPOND_SERVER_ERROR, $txt); |
|
85 | 85 | } |
@@ -8,8 +8,8 @@ discard block |
||
8 | 8 | * SPDX-License-Identifier: AGPL-3.0-only |
9 | 9 | */ |
10 | 10 | |
11 | -require_once __DIR__ . '/../lib/versioncheck.php'; |
|
12 | -require_once __DIR__ . '/../lib/base.php'; |
|
11 | +require_once __DIR__.'/../lib/versioncheck.php'; |
|
12 | +require_once __DIR__.'/../lib/base.php'; |
|
13 | 13 | |
14 | 14 | use OC\OCS\ApiHelper; |
15 | 15 | use OC\Route\Router; |
@@ -58,12 +58,12 @@ discard block |
||
58 | 58 | OC::handleLogin($request); |
59 | 59 | } |
60 | 60 | |
61 | - Server::get(Router::class)->match('/ocsapp' . $request->getRawPathInfo()); |
|
61 | + Server::get(Router::class)->match('/ocsapp'.$request->getRawPathInfo()); |
|
62 | 62 | } catch (MaxDelayReached $ex) { |
63 | 63 | ApiHelper::respond(Http::STATUS_TOO_MANY_REQUESTS, $ex->getMessage()); |
64 | 64 | } catch (ResourceNotFoundException $e) { |
65 | 65 | $txt = 'Invalid query, please check the syntax. API specifications are here:' |
66 | - . ' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.' . "\n"; |
|
66 | + . ' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services.'."\n"; |
|
67 | 67 | ApiHelper::respond(OCSController::RESPOND_NOT_FOUND, $txt); |
68 | 68 | } catch (MethodNotAllowedException $e) { |
69 | 69 | ApiHelper::setContentType(); |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | } catch (\Exception $e) { |
74 | 74 | Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]); |
75 | 75 | |
76 | - $txt = 'Internal Server Error' . "\n"; |
|
76 | + $txt = 'Internal Server Error'."\n"; |
|
77 | 77 | try { |
78 | 78 | if (Server::get(SystemConfig::class)->getValue('debug', false)) { |
79 | 79 | $txt .= $e->getMessage(); |
@@ -27,99 +27,99 @@ |
||
27 | 27 | * Enables encryption |
28 | 28 | */ |
29 | 29 | trait EncryptionTrait { |
30 | - // from MountProviderTrait |
|
31 | - abstract protected function registerStorageWrapper($name, $wrapper); |
|
32 | - |
|
33 | - // from phpunit |
|
34 | - abstract protected static function markTestSkipped(string $message = ''): void; |
|
35 | - abstract protected static function assertTrue($condition, string $message = ''): void; |
|
36 | - |
|
37 | - private $encryptionWasEnabled; |
|
38 | - |
|
39 | - private $originalEncryptionModule; |
|
40 | - |
|
41 | - /** @var IUserManager */ |
|
42 | - private $userManager; |
|
43 | - /** @var SetupManager */ |
|
44 | - private $setupManager; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var IConfig |
|
48 | - */ |
|
49 | - private $config; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var Application |
|
53 | - */ |
|
54 | - private $encryptionApp; |
|
55 | - |
|
56 | - protected function loginWithEncryption($user = '') { |
|
57 | - \OC_Util::tearDownFS(); |
|
58 | - \OC_User::setUserId(''); |
|
59 | - // needed for fully logout |
|
60 | - Server::get(IUserSession::class)->setUser(null); |
|
61 | - |
|
62 | - $this->setupManager->tearDown(); |
|
63 | - |
|
64 | - \OC_User::setUserId($user); |
|
65 | - $this->postLogin(); |
|
66 | - \OC_Util::setupFS($user); |
|
67 | - if ($this->userManager->userExists($user)) { |
|
68 | - \OC::$server->getUserFolder($user); |
|
69 | - } |
|
70 | - } |
|
71 | - |
|
72 | - protected function setupForUser($name, $password) { |
|
73 | - $this->setupManager->tearDown(); |
|
74 | - $this->setupManager->setupForUser($this->userManager->get($name)); |
|
75 | - |
|
76 | - $container = $this->encryptionApp->getContainer(); |
|
77 | - /** @var KeyManager $keyManager */ |
|
78 | - $keyManager = $container->query(KeyManager::class); |
|
79 | - /** @var Setup $userSetup */ |
|
80 | - $userSetup = $container->query(Setup::class); |
|
81 | - $userSetup->setupUser($name, $password); |
|
82 | - $encryptionManager = $container->query(IManager::class); |
|
83 | - $this->encryptionApp->setUp($encryptionManager); |
|
84 | - $keyManager->init($name, $password); |
|
85 | - } |
|
86 | - |
|
87 | - protected function postLogin() { |
|
88 | - $encryptionWrapper = new EncryptionWrapper( |
|
89 | - new ArrayCache(), |
|
90 | - Server::get(\OCP\Encryption\IManager::class), |
|
91 | - Server::get(LoggerInterface::class) |
|
92 | - ); |
|
93 | - |
|
94 | - $this->registerStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage']); |
|
95 | - } |
|
96 | - |
|
97 | - protected function setUpEncryptionTrait() { |
|
98 | - $isReady = Server::get(\OCP\Encryption\IManager::class)->isReady(); |
|
99 | - if (!$isReady) { |
|
100 | - $this->markTestSkipped('Encryption not ready'); |
|
101 | - } |
|
102 | - |
|
103 | - $this->userManager = Server::get(IUserManager::class); |
|
104 | - $this->setupManager = Server::get(SetupManager::class); |
|
105 | - |
|
106 | - Server::get(IAppManager::class)->loadApp('encryption'); |
|
107 | - |
|
108 | - $this->encryptionApp = new Application([], $isReady); |
|
109 | - |
|
110 | - $this->config = Server::get(IConfig::class); |
|
111 | - $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); |
|
112 | - $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module'); |
|
113 | - $this->config->setAppValue('core', 'default_encryption_module', Encryption::ID); |
|
114 | - $this->config->setAppValue('core', 'encryption_enabled', 'yes'); |
|
115 | - $this->assertTrue(Server::get(\OCP\Encryption\IManager::class)->isEnabled()); |
|
116 | - } |
|
117 | - |
|
118 | - protected function tearDownEncryptionTrait() { |
|
119 | - if ($this->config) { |
|
120 | - $this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled); |
|
121 | - $this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule); |
|
122 | - $this->config->deleteAppValue('encryption', 'useMasterKey'); |
|
123 | - } |
|
124 | - } |
|
30 | + // from MountProviderTrait |
|
31 | + abstract protected function registerStorageWrapper($name, $wrapper); |
|
32 | + |
|
33 | + // from phpunit |
|
34 | + abstract protected static function markTestSkipped(string $message = ''): void; |
|
35 | + abstract protected static function assertTrue($condition, string $message = ''): void; |
|
36 | + |
|
37 | + private $encryptionWasEnabled; |
|
38 | + |
|
39 | + private $originalEncryptionModule; |
|
40 | + |
|
41 | + /** @var IUserManager */ |
|
42 | + private $userManager; |
|
43 | + /** @var SetupManager */ |
|
44 | + private $setupManager; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var IConfig |
|
48 | + */ |
|
49 | + private $config; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var Application |
|
53 | + */ |
|
54 | + private $encryptionApp; |
|
55 | + |
|
56 | + protected function loginWithEncryption($user = '') { |
|
57 | + \OC_Util::tearDownFS(); |
|
58 | + \OC_User::setUserId(''); |
|
59 | + // needed for fully logout |
|
60 | + Server::get(IUserSession::class)->setUser(null); |
|
61 | + |
|
62 | + $this->setupManager->tearDown(); |
|
63 | + |
|
64 | + \OC_User::setUserId($user); |
|
65 | + $this->postLogin(); |
|
66 | + \OC_Util::setupFS($user); |
|
67 | + if ($this->userManager->userExists($user)) { |
|
68 | + \OC::$server->getUserFolder($user); |
|
69 | + } |
|
70 | + } |
|
71 | + |
|
72 | + protected function setupForUser($name, $password) { |
|
73 | + $this->setupManager->tearDown(); |
|
74 | + $this->setupManager->setupForUser($this->userManager->get($name)); |
|
75 | + |
|
76 | + $container = $this->encryptionApp->getContainer(); |
|
77 | + /** @var KeyManager $keyManager */ |
|
78 | + $keyManager = $container->query(KeyManager::class); |
|
79 | + /** @var Setup $userSetup */ |
|
80 | + $userSetup = $container->query(Setup::class); |
|
81 | + $userSetup->setupUser($name, $password); |
|
82 | + $encryptionManager = $container->query(IManager::class); |
|
83 | + $this->encryptionApp->setUp($encryptionManager); |
|
84 | + $keyManager->init($name, $password); |
|
85 | + } |
|
86 | + |
|
87 | + protected function postLogin() { |
|
88 | + $encryptionWrapper = new EncryptionWrapper( |
|
89 | + new ArrayCache(), |
|
90 | + Server::get(\OCP\Encryption\IManager::class), |
|
91 | + Server::get(LoggerInterface::class) |
|
92 | + ); |
|
93 | + |
|
94 | + $this->registerStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage']); |
|
95 | + } |
|
96 | + |
|
97 | + protected function setUpEncryptionTrait() { |
|
98 | + $isReady = Server::get(\OCP\Encryption\IManager::class)->isReady(); |
|
99 | + if (!$isReady) { |
|
100 | + $this->markTestSkipped('Encryption not ready'); |
|
101 | + } |
|
102 | + |
|
103 | + $this->userManager = Server::get(IUserManager::class); |
|
104 | + $this->setupManager = Server::get(SetupManager::class); |
|
105 | + |
|
106 | + Server::get(IAppManager::class)->loadApp('encryption'); |
|
107 | + |
|
108 | + $this->encryptionApp = new Application([], $isReady); |
|
109 | + |
|
110 | + $this->config = Server::get(IConfig::class); |
|
111 | + $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); |
|
112 | + $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module'); |
|
113 | + $this->config->setAppValue('core', 'default_encryption_module', Encryption::ID); |
|
114 | + $this->config->setAppValue('core', 'encryption_enabled', 'yes'); |
|
115 | + $this->assertTrue(Server::get(\OCP\Encryption\IManager::class)->isEnabled()); |
|
116 | + } |
|
117 | + |
|
118 | + protected function tearDownEncryptionTrait() { |
|
119 | + if ($this->config) { |
|
120 | + $this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled); |
|
121 | + $this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule); |
|
122 | + $this->config->deleteAppValue('encryption', 'useMasterKey'); |
|
123 | + } |
|
124 | + } |
|
125 | 125 | } |