1
0

DeletedUsersIndex.php 2.3 KB

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