DeletedUsersIndex.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $userObjects[] = new OfflineUser($user, $this->config, $this->mapping, $this->shareManager);
  55. }
  56. $this->deletedUsers = $userObjects;
  57. return $this->deletedUsers;
  58. }
  59. /**
  60. * returns all LDAP users that are marked as deleted
  61. * @return OfflineUser[]
  62. */
  63. public function getUsers(): array {
  64. if (is_array($this->deletedUsers)) {
  65. return $this->deletedUsers;
  66. }
  67. return $this->fetchDeletedUsers();
  68. }
  69. /**
  70. * whether at least one user was detected as deleted
  71. */
  72. public function hasUsers(): bool {
  73. if (!is_array($this->deletedUsers)) {
  74. $this->fetchDeletedUsers();
  75. }
  76. return is_array($this->deletedUsers) && (count($this->deletedUsers) > 0);
  77. }
  78. /**
  79. * marks a user as deleted
  80. *
  81. * @throws \OCP\PreConditionNotMetException
  82. */
  83. public function markUser(string $ocName): void {
  84. if ($this->isUserMarked($ocName)) {
  85. // the user is already marked, do not write to DB again
  86. return;
  87. }
  88. $this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
  89. $this->config->setUserValue($ocName, 'user_ldap', 'foundDeleted', (string)time());
  90. $this->deletedUsers = null;
  91. }
  92. public function isUserMarked(string $ocName): bool {
  93. return ($this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0') === '1');
  94. }
  95. }