You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CleanUp.php 6.0KB

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