CleanUp.php 6.1 KB

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