DeletedUsersIndex.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. namespace OCA\User_LDAP\User;
  7. use OCA\User_LDAP\Mapping\UserMapping;
  8. use OCP\IConfig;
  9. use OCP\PreConditionNotMetException;
  10. use OCP\Share\IManager;
  11. /**
  12. * Class DeletedUsersIndex
  13. * @package OCA\User_LDAP
  14. */
  15. class DeletedUsersIndex {
  16. protected ?array $deletedUsers = null;
  17. public function __construct(
  18. protected IConfig $config,
  19. protected UserMapping $mapping,
  20. private IManager $shareManager,
  21. ) {
  22. }
  23. /**
  24. * reads LDAP users marked as deleted from the database
  25. * @return OfflineUser[]
  26. */
  27. private function fetchDeletedUsers(): array {
  28. $deletedUsers = $this->config->getUsersForUserValue('user_ldap', 'isDeleted', '1');
  29. $userObjects = [];
  30. foreach ($deletedUsers as $user) {
  31. $userObject = new OfflineUser($user, $this->config, $this->mapping, $this->shareManager);
  32. if ($userObject->getLastLogin() > $userObject->getDetectedOn()) {
  33. $userObject->unmark();
  34. } else {
  35. $userObjects[] = $userObject;
  36. }
  37. }
  38. $this->deletedUsers = $userObjects;
  39. return $this->deletedUsers;
  40. }
  41. /**
  42. * returns all LDAP users that are marked as deleted
  43. * @return OfflineUser[]
  44. */
  45. public function getUsers(): array {
  46. if (is_array($this->deletedUsers)) {
  47. return $this->deletedUsers;
  48. }
  49. return $this->fetchDeletedUsers();
  50. }
  51. /**
  52. * whether at least one user was detected as deleted
  53. */
  54. public function hasUsers(): bool {
  55. if (!is_array($this->deletedUsers)) {
  56. $this->fetchDeletedUsers();
  57. }
  58. return is_array($this->deletedUsers) && (count($this->deletedUsers) > 0);
  59. }
  60. /**
  61. * marks a user as deleted
  62. *
  63. * @throws PreConditionNotMetException
  64. */
  65. public function markUser(string $ocName): void {
  66. if ($this->isUserMarked($ocName)) {
  67. // the user is already marked, do not write to DB again
  68. return;
  69. }
  70. $this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
  71. $this->config->setUserValue($ocName, 'user_ldap', 'foundDeleted', (string)time());
  72. $this->deletedUsers = null;
  73. }
  74. public function isUserMarked(string $ocName): bool {
  75. return ($this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0') === '1');
  76. }
  77. }