DeletedUsersIndex.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\User_LDAP\User;
  25. use OCA\User_LDAP\Mapping\UserMapping;
  26. use OCP\IConfig;
  27. use OCP\Share\IManager;
  28. /**
  29. * Class DeletedUsersIndex
  30. * @package OCA\User_LDAP
  31. */
  32. class DeletedUsersIndex {
  33. protected IConfig $config;
  34. protected UserMapping $mapping;
  35. protected ?array $deletedUsers = null;
  36. private IManager $shareManager;
  37. public function __construct(
  38. IConfig $config,
  39. UserMapping $mapping,
  40. IManager $shareManager
  41. ) {
  42. $this->config = $config;
  43. $this->mapping = $mapping;
  44. $this->shareManager = $shareManager;
  45. }
  46. /**
  47. * reads LDAP users marked as deleted from the database
  48. * @return OfflineUser[]
  49. */
  50. private function fetchDeletedUsers(): array {
  51. $deletedUsers = $this->config->getUsersForUserValue('user_ldap', 'isDeleted', '1');
  52. $userObjects = [];
  53. foreach ($deletedUsers as $user) {
  54. $userObject = new OfflineUser($user, $this->config, $this->mapping, $this->shareManager);
  55. if ($userObject->getLastLogin() > $userObject->getDetectedOn()) {
  56. $userObject->unmark();
  57. } else {
  58. $userObjects[] = $userObject;
  59. }
  60. }
  61. $this->deletedUsers = $userObjects;
  62. return $this->deletedUsers;
  63. }
  64. /**
  65. * returns all LDAP users that are marked as deleted
  66. * @return OfflineUser[]
  67. */
  68. public function getUsers(): array {
  69. if (is_array($this->deletedUsers)) {
  70. return $this->deletedUsers;
  71. }
  72. return $this->fetchDeletedUsers();
  73. }
  74. /**
  75. * whether at least one user was detected as deleted
  76. */
  77. public function hasUsers(): bool {
  78. if (!is_array($this->deletedUsers)) {
  79. $this->fetchDeletedUsers();
  80. }
  81. return is_array($this->deletedUsers) && (count($this->deletedUsers) > 0);
  82. }
  83. /**
  84. * marks a user as deleted
  85. *
  86. * @throws \OCP\PreConditionNotMetException
  87. */
  88. public function markUser(string $ocName): void {
  89. if ($this->isUserMarked($ocName)) {
  90. // the user is already marked, do not write to DB again
  91. return;
  92. }
  93. $this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
  94. $this->config->setUserValue($ocName, 'user_ldap', 'foundDeleted', (string)time());
  95. $this->deletedUsers = null;
  96. }
  97. public function isUserMarked(string $ocName): bool {
  98. return ($this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0') === '1');
  99. }
  100. }