DeletedUsersIndex.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Share\IManager;
  27. /**
  28. * Class DeletedUsersIndex
  29. * @package OCA\User_LDAP
  30. */
  31. class DeletedUsersIndex {
  32. /**
  33. * @var \OCP\IConfig $config
  34. */
  35. protected $config;
  36. /**
  37. * @var \OCA\User_LDAP\Mapping\UserMapping $mapping
  38. */
  39. protected $mapping;
  40. /**
  41. * @var array $deletedUsers
  42. */
  43. protected $deletedUsers;
  44. /** @var IManager */
  45. private $shareManager;
  46. public function __construct(\OCP\IConfig $config, UserMapping $mapping, IManager $shareManager) {
  47. $this->config = $config;
  48. $this->mapping = $mapping;
  49. $this->shareManager = $shareManager;
  50. }
  51. /**
  52. * reads LDAP users marked as deleted from the database
  53. * @return \OCA\User_LDAP\User\OfflineUser[]
  54. */
  55. private function fetchDeletedUsers() {
  56. $deletedUsers = $this->config->getUsersForUserValue(
  57. 'user_ldap', 'isDeleted', '1');
  58. $userObjects = [];
  59. foreach ($deletedUsers as $user) {
  60. $userObjects[] = new OfflineUser($user, $this->config, $this->mapping, $this->shareManager);
  61. }
  62. $this->deletedUsers = $userObjects;
  63. return $this->deletedUsers;
  64. }
  65. /**
  66. * returns all LDAP users that are marked as deleted
  67. * @return \OCA\User_LDAP\User\OfflineUser[]
  68. */
  69. public function getUsers() {
  70. if (is_array($this->deletedUsers)) {
  71. return $this->deletedUsers;
  72. }
  73. return $this->fetchDeletedUsers();
  74. }
  75. /**
  76. * whether at least one user was detected as deleted
  77. * @return bool
  78. */
  79. public function hasUsers() {
  80. if (!is_array($this->deletedUsers)) {
  81. $this->fetchDeletedUsers();
  82. }
  83. return is_array($this->deletedUsers) && (count($this->deletedUsers) > 0);
  84. }
  85. /**
  86. * marks a user as deleted
  87. *
  88. * @param string $ocName
  89. * @throws \OCP\PreConditionNotMetException
  90. */
  91. public function markUser($ocName) {
  92. $curValue = $this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0');
  93. if ($curValue === '1') {
  94. // the user is already marked, do not write to DB again
  95. return;
  96. }
  97. $this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
  98. $this->config->setUserValue($ocName, 'user_ldap', 'foundDeleted', (string)time());
  99. $this->deletedUsers = null;
  100. }
  101. }