CredentialsCleanup.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\IJob;
  13. use OCP\BackgroundJob\TimedJob;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use OCP\Security\ICredentialsManager;
  17. class CredentialsCleanup extends TimedJob {
  18. private $credentialsManager;
  19. private $userGlobalStoragesService;
  20. private $userManager;
  21. public function __construct(
  22. ITimeFactory $time,
  23. ICredentialsManager $credentialsManager,
  24. UserGlobalStoragesService $userGlobalStoragesService,
  25. IUserManager $userManager
  26. ) {
  27. parent::__construct($time);
  28. $this->credentialsManager = $credentialsManager;
  29. $this->userGlobalStoragesService = $userGlobalStoragesService;
  30. $this->userManager = $userManager;
  31. // run every day
  32. $this->setInterval(24 * 60 * 60);
  33. $this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
  34. }
  35. protected function run($argument) {
  36. $this->userManager->callForSeenUsers(function (IUser $user) {
  37. $storages = $this->userGlobalStoragesService->getAllStoragesForUser($user);
  38. $usesLoginCredentials = array_reduce($storages, function (bool $uses, StorageConfig $storage) {
  39. return $uses || $storage->getAuthMechanism() instanceof LoginCredentials;
  40. }, false);
  41. if (!$usesLoginCredentials) {
  42. $this->credentialsManager->delete($user->getUID(), LoginCredentials::CREDENTIALS_IDENTIFIER);
  43. }
  44. });
  45. }
  46. }