CleanUp.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\User_LDAP\Jobs;
  27. use OC\BackgroundJob\TimedJob;
  28. use OCA\User_LDAP\Helper;
  29. use OCA\User_LDAP\Mapping\UserMapping;
  30. use OCA\User_LDAP\User\DeletedUsersIndex;
  31. use OCA\User_LDAP\User_LDAP;
  32. use OCA\User_LDAP\User_Proxy;
  33. /**
  34. * Class CleanUp
  35. *
  36. * a Background job to clean up deleted users
  37. *
  38. * @package OCA\User_LDAP\Jobs;
  39. */
  40. class CleanUp extends TimedJob {
  41. /** @var ?int $limit amount of users that should be checked per run */
  42. protected $limit;
  43. /** @var int $defaultIntervalMin default interval in minutes */
  44. protected $defaultIntervalMin = 51;
  45. /** @var User_LDAP|User_Proxy $userBackend */
  46. protected $userBackend;
  47. /** @var \OCP\IConfig $ocConfig */
  48. protected $ocConfig;
  49. /** @var \OCP\IDBConnection $db */
  50. protected $db;
  51. /** @var Helper $ldapHelper */
  52. protected $ldapHelper;
  53. /** @var UserMapping */
  54. protected $mapping;
  55. /** @var DeletedUsersIndex */
  56. protected $dui;
  57. public function __construct(User_Proxy $userBackend, DeletedUsersIndex $dui) {
  58. $minutes = \OC::$server->getConfig()->getSystemValue(
  59. 'ldapUserCleanupInterval', (string)$this->defaultIntervalMin);
  60. $this->setInterval((int)$minutes * 60);
  61. $this->userBackend = $userBackend;
  62. $this->dui = $dui;
  63. }
  64. /**
  65. * assigns the instances passed to run() to the class properties
  66. * @param array $arguments
  67. */
  68. public function setArguments($arguments): void {
  69. //Dependency Injection is not possible, because the constructor will
  70. //only get values that are serialized to JSON. I.e. whatever we would
  71. //pass in app.php we do add here, except something else is passed e.g.
  72. //in tests.
  73. if (isset($arguments['helper'])) {
  74. $this->ldapHelper = $arguments['helper'];
  75. } else {
  76. $this->ldapHelper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
  77. }
  78. if (isset($arguments['ocConfig'])) {
  79. $this->ocConfig = $arguments['ocConfig'];
  80. } else {
  81. $this->ocConfig = \OC::$server->getConfig();
  82. }
  83. if (isset($arguments['userBackend'])) {
  84. $this->userBackend = $arguments['userBackend'];
  85. }
  86. if (isset($arguments['db'])) {
  87. $this->db = $arguments['db'];
  88. } else {
  89. $this->db = \OC::$server->getDatabaseConnection();
  90. }
  91. if (isset($arguments['mapping'])) {
  92. $this->mapping = $arguments['mapping'];
  93. } else {
  94. $this->mapping = \OCP\Server::get(UserMapping::class);
  95. }
  96. if (isset($arguments['deletedUsersIndex'])) {
  97. $this->dui = $arguments['deletedUsersIndex'];
  98. }
  99. }
  100. /**
  101. * makes the background job do its work
  102. * @param array $argument
  103. */
  104. public function run($argument): void {
  105. $this->setArguments($argument);
  106. if (!$this->isCleanUpAllowed()) {
  107. return;
  108. }
  109. $users = $this->mapping->getList($this->getOffset(), $this->getChunkSize());
  110. $resetOffset = $this->isOffsetResetNecessary(count($users));
  111. $this->checkUsers($users);
  112. $this->setOffset($resetOffset);
  113. }
  114. /**
  115. * checks whether next run should start at 0 again
  116. */
  117. public function isOffsetResetNecessary(int $resultCount): bool {
  118. return $resultCount < $this->getChunkSize();
  119. }
  120. /**
  121. * checks whether cleaning up LDAP users is allowed
  122. */
  123. public function isCleanUpAllowed(): bool {
  124. try {
  125. if ($this->ldapHelper->haveDisabledConfigurations()) {
  126. return false;
  127. }
  128. } catch (\Exception $e) {
  129. return false;
  130. }
  131. return $this->isCleanUpEnabled();
  132. }
  133. /**
  134. * checks whether clean up is enabled by configuration
  135. */
  136. private function isCleanUpEnabled(): bool {
  137. return (bool)$this->ocConfig->getSystemValue(
  138. 'ldapUserCleanupInterval', (string)$this->defaultIntervalMin);
  139. }
  140. /**
  141. * checks users whether they are still existing
  142. * @param array $users result from getMappedUsers()
  143. */
  144. private function checkUsers(array $users): void {
  145. foreach ($users as $user) {
  146. $this->checkUser($user);
  147. }
  148. }
  149. /**
  150. * checks whether a user is still existing in LDAP
  151. * @param string[] $user
  152. */
  153. private function checkUser(array $user): void {
  154. if ($this->userBackend->userExistsOnLDAP($user['name'])) {
  155. //still available, all good
  156. return;
  157. }
  158. $this->dui->markUser($user['name']);
  159. }
  160. /**
  161. * gets the offset to fetch users from the mappings table
  162. */
  163. private function getOffset(): int {
  164. return (int)$this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', '0');
  165. }
  166. /**
  167. * sets the new offset for the next run
  168. * @param bool $reset whether the offset should be set to 0
  169. */
  170. public function setOffset(bool $reset = false): void {
  171. $newOffset = $reset ? 0 :
  172. $this->getOffset() + $this->getChunkSize();
  173. $this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', (string)$newOffset);
  174. }
  175. /**
  176. * returns the chunk size (limit in DB speak)
  177. */
  178. public function getChunkSize(): int {
  179. if ($this->limit === null) {
  180. $this->limit = (int)$this->ocConfig->getAppValue('user_ldap', 'cleanUpJobChunkSize', '50');
  181. }
  182. return $this->limit;
  183. }
  184. }