1
0

UpdateGroupsService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\User_LDAP\Service;
  9. use OCA\User_LDAP\Db\GroupMembership;
  10. use OCA\User_LDAP\Db\GroupMembershipMapper;
  11. use OCA\User_LDAP\Group_Proxy;
  12. use OCP\DB\Exception;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\Group\Events\UserAddedEvent;
  15. use OCP\Group\Events\UserRemovedEvent;
  16. use OCP\IGroup;
  17. use OCP\IGroupManager;
  18. use OCP\IUser;
  19. use OCP\IUserManager;
  20. use Psr\Log\LoggerInterface;
  21. class UpdateGroupsService {
  22. public function __construct(
  23. private Group_Proxy $groupBackend,
  24. private IEventDispatcher $dispatcher,
  25. private IGroupManager $groupManager,
  26. private IUserManager $userManager,
  27. private LoggerInterface $logger,
  28. private GroupMembershipMapper $groupMembershipMapper,
  29. ) {
  30. }
  31. /**
  32. * @throws Exception
  33. */
  34. public function updateGroups(): void {
  35. $knownGroups = $this->groupMembershipMapper->getKnownGroups();
  36. $actualGroups = $this->groupBackend->getGroups();
  37. if (empty($actualGroups) && empty($knownGroups)) {
  38. $this->logger->info(
  39. 'service "updateGroups" - groups do not seem to be configured properly, aborting.',
  40. );
  41. return;
  42. }
  43. $this->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
  44. $this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
  45. $this->handleRemovedGroups(array_diff($knownGroups, $actualGroups));
  46. $this->logger->debug('service "updateGroups" - Finished.');
  47. }
  48. /**
  49. * @param string[] $groups
  50. * @throws Exception
  51. */
  52. public function handleKnownGroups(array $groups): void {
  53. $this->logger->debug('service "updateGroups" - Dealing with known Groups.');
  54. foreach ($groups as $group) {
  55. $this->logger->debug('service "updateGroups" - Dealing with {group}.', ['group' => $group]);
  56. $groupMemberships = $this->groupMembershipMapper->findGroupMemberships($group);
  57. $knownUsers = array_map(
  58. static fn (GroupMembership $groupMembership): string => $groupMembership->getUserid(),
  59. $groupMemberships
  60. );
  61. $groupMemberships = array_combine($knownUsers, $groupMemberships);
  62. $actualUsers = $this->groupBackend->usersInGroup($group);
  63. $groupObject = $this->groupManager->get($group);
  64. if ($groupObject === null) {
  65. /* We are not expecting the group to not be found since it was returned by $this->groupBackend->getGroups() */
  66. $this->logger->error(
  67. 'service "updateGroups" - Failed to get group {group} for update',
  68. [
  69. 'group' => $group
  70. ]
  71. );
  72. continue;
  73. }
  74. foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
  75. try {
  76. $this->groupMembershipMapper->delete($groupMemberships[$removedUser]);
  77. } catch (Exception $e) {
  78. if ($e->getReason() !== Exception::REASON_DATABASE_OBJECT_NOT_FOUND) {
  79. /* If reason is not found something else removed the membership, that’s fine */
  80. $this->logger->error(
  81. self::class . ' - group {group} membership failed to be removed (user {user})',
  82. [
  83. 'app' => 'user_ldap',
  84. 'user' => $removedUser,
  85. 'group' => $group,
  86. 'exception' => $e,
  87. ]
  88. );
  89. }
  90. /* We failed to delete the groupmembership so we do not want to advertise it */
  91. continue;
  92. }
  93. $userObject = $this->userManager->get($removedUser);
  94. if ($userObject instanceof IUser) {
  95. $this->dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
  96. }
  97. $this->logger->info(
  98. 'service "updateGroups" - {user} removed from {group}',
  99. [
  100. 'user' => $removedUser,
  101. 'group' => $group
  102. ]
  103. );
  104. }
  105. foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
  106. try {
  107. $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $group,'userid' => $addedUser]));
  108. } catch (Exception $e) {
  109. if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
  110. /* If reason is unique constraint something else added the membership, that’s fine */
  111. $this->logger->error(
  112. self::class . ' - group {group} membership failed to be added (user {user})',
  113. [
  114. 'app' => 'user_ldap',
  115. 'user' => $addedUser,
  116. 'group' => $group,
  117. 'exception' => $e,
  118. ]
  119. );
  120. }
  121. /* We failed to insert the groupmembership so we do not want to advertise it */
  122. continue;
  123. }
  124. $userObject = $this->userManager->get($addedUser);
  125. if ($userObject instanceof IUser) {
  126. $this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
  127. }
  128. $this->logger->info(
  129. 'service "updateGroups" - {user} added to {group}',
  130. [
  131. 'user' => $addedUser,
  132. 'group' => $group
  133. ]
  134. );
  135. }
  136. }
  137. $this->logger->debug('service "updateGroups" - FINISHED dealing with known Groups.');
  138. }
  139. /**
  140. * @param string[] $createdGroups
  141. * @throws Exception
  142. */
  143. public function handleCreatedGroups(array $createdGroups): void {
  144. $this->logger->debug('service "updateGroups" - dealing with created Groups.');
  145. foreach ($createdGroups as $createdGroup) {
  146. $this->logger->info('service "updateGroups" - new group "' . $createdGroup . '" found.');
  147. $users = $this->groupBackend->usersInGroup($createdGroup);
  148. $groupObject = $this->groupManager->get($createdGroup);
  149. foreach ($users as $user) {
  150. try {
  151. $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $createdGroup,'userid' => $user]));
  152. } catch (Exception $e) {
  153. if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
  154. $this->logger->error(
  155. self::class . ' - group {group} membership failed to be added (user {user})',
  156. [
  157. 'app' => 'user_ldap',
  158. 'user' => $user,
  159. 'group' => $createdGroup,
  160. 'exception' => $e,
  161. ]
  162. );
  163. }
  164. /* We failed to insert the groupmembership so we do not want to advertise it */
  165. continue;
  166. }
  167. if ($groupObject instanceof IGroup) {
  168. $userObject = $this->userManager->get($user);
  169. if ($userObject instanceof IUser) {
  170. $this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
  171. }
  172. }
  173. }
  174. }
  175. $this->logger->debug('service "updateGroups" - FINISHED dealing with created Groups.');
  176. }
  177. /**
  178. * @param string[] $removedGroups
  179. * @throws Exception
  180. */
  181. public function handleRemovedGroups(array $removedGroups): void {
  182. $this->logger->debug('service "updateGroups" - dealing with removed groups.');
  183. $this->groupMembershipMapper->deleteGroups($removedGroups);
  184. foreach ($removedGroups as $group) {
  185. $groupObject = $this->groupManager->get($group);
  186. if ($groupObject instanceof IGroup) {
  187. $groupMemberships = $this->groupMembershipMapper->findGroupMemberships($group);
  188. foreach ($groupMemberships as $groupMembership) {
  189. $userObject = $this->userManager->get($groupMembership->getUserid());
  190. if ($userObject instanceof IUser) {
  191. $this->dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
  192. }
  193. }
  194. }
  195. }
  196. $this->logger->info(
  197. 'service "updateGroups" - groups {removedGroups} were removed.',
  198. [
  199. 'removedGroups' => $removedGroups
  200. ]
  201. );
  202. }
  203. }