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.

UpdateGroups.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Roger Szabo <roger.szabo@web.de>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\User_LDAP\Jobs;
  34. use OC\BackgroundJob\TimedJob;
  35. use OCA\User_LDAP\Group_Proxy;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. use OCP\Group\Events\UserAddedEvent;
  38. use OCP\Group\Events\UserRemovedEvent;
  39. use OCP\ILogger;
  40. class UpdateGroups extends TimedJob {
  41. private $groupsFromDB;
  42. /** @var Group_Proxy */
  43. private $groupBackend;
  44. public function __construct(Group_Proxy $groupBackend) {
  45. $this->interval = $this->getRefreshInterval();
  46. $this->groupBackend = $groupBackend;
  47. }
  48. /**
  49. * @param mixed $argument
  50. */
  51. public function run($argument) {
  52. $this->updateGroups();
  53. }
  54. public function updateGroups() {
  55. \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', ILogger::DEBUG);
  56. $knownGroups = array_keys($this->getKnownGroups());
  57. $actualGroups = $this->groupBackend->getGroups();
  58. if (empty($actualGroups) && empty($knownGroups)) {
  59. \OCP\Util::writeLog('user_ldap',
  60. 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.',
  61. ILogger::INFO);
  62. return;
  63. }
  64. $this->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
  65. $this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
  66. $this->handleRemovedGroups(array_diff($knownGroups, $actualGroups));
  67. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', ILogger::DEBUG);
  68. }
  69. /**
  70. * @return int
  71. */
  72. private function getRefreshInterval() {
  73. //defaults to every hour
  74. return \OC::$server->getConfig()->getAppValue('user_ldap', 'bgjRefreshInterval', 3600);
  75. }
  76. /**
  77. * @param string[] $groups
  78. */
  79. private function handleKnownGroups($groups) {
  80. /** @var IEventDispatcher $dispatcher */
  81. $dispatcher = \OC::$server->query(IEventDispatcher::class);
  82. $groupManager = \OC::$server->getGroupManager();
  83. $userManager = \OC::$server->getUserManager();
  84. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', ILogger::DEBUG);
  85. $query = \OC_DB::prepare('
  86. UPDATE `*PREFIX*ldap_group_members`
  87. SET `owncloudusers` = ?
  88. WHERE `owncloudname` = ?
  89. ');
  90. if (!is_array($this->groupsFromDB)) {
  91. $this->getKnownGroups();
  92. }
  93. foreach ($groups as $group) {
  94. $knownUsers = unserialize($this->groupsFromDB[$group]['owncloudusers']);
  95. $actualUsers = $this->groupBackend->usersInGroup($group);
  96. $hasChanged = false;
  97. $groupObject = $groupManager->get($group);
  98. foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
  99. $userObject = $userManager->get($removedUser);
  100. $dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
  101. \OCP\Util::writeLog('user_ldap',
  102. 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".',
  103. ILogger::INFO);
  104. $hasChanged = true;
  105. }
  106. foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
  107. $userObject = $userManager->get($addedUser);
  108. $dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
  109. \OCP\Util::writeLog('user_ldap',
  110. 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".',
  111. ILogger::INFO);
  112. $hasChanged = true;
  113. }
  114. if ($hasChanged) {
  115. $query->execute([serialize($actualUsers), $group]);
  116. }
  117. }
  118. \OCP\Util::writeLog('user_ldap',
  119. 'bgJ "updateGroups" – FINISHED dealing with known Groups.',
  120. ILogger::DEBUG);
  121. }
  122. /**
  123. * @param string[] $createdGroups
  124. */
  125. private function handleCreatedGroups($createdGroups) {
  126. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', ILogger::DEBUG);
  127. $query = \OC_DB::prepare('
  128. INSERT
  129. INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`)
  130. VALUES (?, ?)
  131. ');
  132. foreach ($createdGroups as $createdGroup) {
  133. \OCP\Util::writeLog('user_ldap',
  134. 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.',
  135. ILogger::INFO);
  136. $users = serialize($this->groupBackend->usersInGroup($createdGroup));
  137. $query->execute([$createdGroup, $users]);
  138. }
  139. \OCP\Util::writeLog('user_ldap',
  140. 'bgJ "updateGroups" – FINISHED dealing with created Groups.',
  141. ILogger::DEBUG);
  142. }
  143. /**
  144. * @param string[] $removedGroups
  145. */
  146. private function handleRemovedGroups($removedGroups) {
  147. \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', ILogger::DEBUG);
  148. $query = \OC_DB::prepare('
  149. DELETE
  150. FROM `*PREFIX*ldap_group_members`
  151. WHERE `owncloudname` = ?
  152. ');
  153. foreach ($removedGroups as $removedGroup) {
  154. \OCP\Util::writeLog('user_ldap',
  155. 'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.',
  156. ILogger::INFO);
  157. $query->execute([$removedGroup]);
  158. }
  159. \OCP\Util::writeLog('user_ldap',
  160. 'bgJ "updateGroups" – FINISHED dealing with removed groups.',
  161. ILogger::DEBUG);
  162. }
  163. /**
  164. * @return array
  165. */
  166. private function getKnownGroups() {
  167. if (is_array($this->groupsFromDB)) {
  168. $this->groupsFromDB;
  169. }
  170. $query = \OC_DB::prepare('
  171. SELECT `owncloudname`, `owncloudusers`
  172. FROM `*PREFIX*ldap_group_members`
  173. ');
  174. $result = $query->execute()->fetchAll();
  175. $this->groupsFromDB = [];
  176. foreach ($result as $dataset) {
  177. $this->groupsFromDB[$dataset['owncloudname']] = $dataset;
  178. }
  179. return $this->groupsFromDB;
  180. }
  181. }