UpdateGroupsService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bart Visscher <bartv@thisnet.nl>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Robin McCorkell <robin@mccorkell.me.uk>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\User_LDAP\Service;
  32. use OCA\User_LDAP\Db\GroupMembership;
  33. use OCA\User_LDAP\Db\GroupMembershipMapper;
  34. use OCA\User_LDAP\Group_Proxy;
  35. use OCP\DB\Exception;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. use OCP\Group\Events\UserAddedEvent;
  38. use OCP\Group\Events\UserRemovedEvent;
  39. use OCP\IGroup;
  40. use OCP\IGroupManager;
  41. use OCP\IUser;
  42. use OCP\IUserManager;
  43. use Psr\Log\LoggerInterface;
  44. class UpdateGroupsService {
  45. public function __construct(
  46. private Group_Proxy $groupBackend,
  47. private IEventDispatcher $dispatcher,
  48. private IGroupManager $groupManager,
  49. private IUserManager $userManager,
  50. private LoggerInterface $logger,
  51. private GroupMembershipMapper $groupMembershipMapper,
  52. ) {
  53. }
  54. /**
  55. * @throws Exception
  56. */
  57. public function updateGroups(): void {
  58. $knownGroups = $this->groupMembershipMapper->getKnownGroups();
  59. $actualGroups = $this->groupBackend->getGroups();
  60. if (empty($actualGroups) && empty($knownGroups)) {
  61. $this->logger->info(
  62. 'service "updateGroups" – groups do not seem to be configured properly, aborting.',
  63. );
  64. return;
  65. }
  66. $this->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
  67. $this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
  68. $this->handleRemovedGroups(array_diff($knownGroups, $actualGroups));
  69. $this->logger->debug('service "updateGroups" – Finished.');
  70. }
  71. /**
  72. * @param string[] $groups
  73. * @throws Exception
  74. */
  75. public function handleKnownGroups(array $groups): void {
  76. $this->logger->debug('service "updateGroups" – Dealing with known Groups.');
  77. foreach ($groups as $group) {
  78. $this->logger->debug('service "updateGroups" – Dealing with {group}.', ['group' => $group]);
  79. $groupMemberships = $this->groupMembershipMapper->findGroupMemberships($group);
  80. $knownUsers = array_map(
  81. static fn (GroupMembership $groupMembership): string => $groupMembership->getUserid(),
  82. $groupMemberships
  83. );
  84. $groupMemberships = array_combine($knownUsers, $groupMemberships);
  85. $actualUsers = $this->groupBackend->usersInGroup($group);
  86. $groupObject = $this->groupManager->get($group);
  87. if ($groupObject === null) {
  88. /* We are not expecting the group to not be found since it was returned by $this->groupBackend->getGroups() */
  89. $this->logger->error(
  90. 'service "updateGroups" – Failed to get group {group} for update',
  91. [
  92. 'group' => $group
  93. ]
  94. );
  95. continue;
  96. }
  97. foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
  98. $this->groupMembershipMapper->delete($groupMemberships[$removedUser]);
  99. $userObject = $this->userManager->get($removedUser);
  100. if ($userObject instanceof IUser) {
  101. $this->dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
  102. }
  103. $this->logger->info(
  104. 'service "updateGroups" – {user} removed from {group}',
  105. [
  106. 'user' => $removedUser,
  107. 'group' => $group
  108. ]
  109. );
  110. }
  111. foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
  112. $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $group,'userid' => $addedUser]));
  113. $userObject = $this->userManager->get($addedUser);
  114. if ($userObject instanceof IUser) {
  115. $this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
  116. }
  117. $this->logger->info(
  118. 'service "updateGroups" – {user} added to {group}',
  119. [
  120. 'user' => $addedUser,
  121. 'group' => $group
  122. ]
  123. );
  124. }
  125. }
  126. $this->logger->debug('service "updateGroups" – FINISHED dealing with known Groups.');
  127. }
  128. /**
  129. * @param string[] $createdGroups
  130. * @throws Exception
  131. */
  132. public function handleCreatedGroups(array $createdGroups): void {
  133. $this->logger->debug('service "updateGroups" – dealing with created Groups.');
  134. foreach ($createdGroups as $createdGroup) {
  135. $this->logger->info('service "updateGroups" – new group "' . $createdGroup . '" found.');
  136. $users = $this->groupBackend->usersInGroup($createdGroup);
  137. $groupObject = $this->groupManager->get($createdGroup);
  138. foreach ($users as $user) {
  139. $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $createdGroup,'userid' => $user]));
  140. if ($groupObject instanceof IGroup) {
  141. $userObject = $this->userManager->get($user);
  142. if ($userObject instanceof IUser) {
  143. $this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
  144. }
  145. }
  146. }
  147. }
  148. $this->logger->debug('service "updateGroups" – FINISHED dealing with created Groups.');
  149. }
  150. /**
  151. * @param string[] $removedGroups
  152. * @throws Exception
  153. */
  154. public function handleRemovedGroups(array $removedGroups): void {
  155. $this->logger->debug('service "updateGroups" – dealing with removed groups.');
  156. $this->groupMembershipMapper->deleteGroups($removedGroups);
  157. foreach ($removedGroups as $group) {
  158. $groupObject = $this->groupManager->get($group);
  159. if ($groupObject instanceof IGroup) {
  160. $groupMemberships = $this->groupMembershipMapper->findGroupMemberships($group);
  161. foreach ($groupMemberships as $groupMembership) {
  162. $userObject = $this->userManager->get($groupMembership->getUserid());
  163. if ($userObject instanceof IUser) {
  164. $this->dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
  165. }
  166. }
  167. }
  168. }
  169. $this->logger->info(
  170. 'service "updateGroups" – groups {removedGroups} were removed.',
  171. [
  172. 'removedGroups' => $removedGroups
  173. ]
  174. );
  175. }
  176. }