DeletedUsersIndex.php 3.0 KB

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