CredentialsCleanup.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_External\BackgroundJob;
  8. use OCA\Files_External\Lib\Auth\Password\LoginCredentials;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCA\Files_External\Service\UserGlobalStoragesService;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\BackgroundJob\TimedJob;
  13. use OCP\IUser;
  14. use OCP\IUserManager;
  15. use OCP\Security\ICredentialsManager;
  16. class CredentialsCleanup extends TimedJob {
  17. public function __construct(
  18. ITimeFactory $time,
  19. private ICredentialsManager $credentialsManager,
  20. private UserGlobalStoragesService $userGlobalStoragesService,
  21. private IUserManager $userManager,
  22. ) {
  23. parent::__construct($time);
  24. // run every day
  25. $this->setInterval(24 * 60 * 60);
  26. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  27. }
  28. protected function run($argument) {
  29. $this->userManager->callForSeenUsers(function (IUser $user): void {
  30. $storages = $this->userGlobalStoragesService->getAllStoragesForUser($user);
  31. $usesLoginCredentials = array_reduce($storages, function (bool $uses, StorageConfig $storage) {
  32. return $uses || $storage->getAuthMechanism() instanceof LoginCredentials;
  33. }, false);
  34. if (!$usesLoginCredentials) {
  35. $this->credentialsManager->delete($user->getUID(), LoginCredentials::CREDENTIALS_IDENTIFIER);
  36. }
  37. });
  38. }
  39. }