LoginListener.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023, Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\User_LDAP;
  25. use OCA\User_LDAP\Db\GroupMembership;
  26. use OCA\User_LDAP\Db\GroupMembershipMapper;
  27. use OCP\DB\Exception;
  28. use OCP\EventDispatcher\Event;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\EventDispatcher\IEventListener;
  31. use OCP\Group\Events\UserAddedEvent;
  32. use OCP\Group\Events\UserRemovedEvent;
  33. use OCP\IGroupManager;
  34. use OCP\IUser;
  35. use OCP\User\Events\PostLoginEvent;
  36. use Psr\Log\LoggerInterface;
  37. /**
  38. * @template-implements IEventListener<PostLoginEvent>
  39. */
  40. class LoginListener implements IEventListener {
  41. public function __construct(
  42. private IEventDispatcher $dispatcher,
  43. private Group_Proxy $groupBackend,
  44. private IGroupManager $groupManager,
  45. private LoggerInterface $logger,
  46. private GroupMembershipMapper $groupMembershipMapper,
  47. ) {
  48. }
  49. public function handle(Event $event): void {
  50. if ($event instanceof PostLoginEvent) {
  51. $this->onPostLogin($event->getUser());
  52. }
  53. }
  54. public function onPostLogin(IUser $user): void {
  55. $this->logger->info(
  56. __CLASS__ . ' – {user} postLogin',
  57. [
  58. 'app' => 'user_ldap',
  59. 'user' => $user->getUID(),
  60. ]
  61. );
  62. $this->updateGroups($user);
  63. }
  64. private function updateGroups(IUser $userObject): void {
  65. $userId = $userObject->getUID();
  66. $groupMemberships = $this->groupMembershipMapper->findGroupMembershipsForUser($userId);
  67. $knownGroups = array_map(
  68. static fn (GroupMembership $groupMembership): string => $groupMembership->getGroupid(),
  69. $groupMemberships
  70. );
  71. $groupMemberships = array_combine($knownGroups, $groupMemberships);
  72. $actualGroups = $this->groupBackend->getUserGroups($userId);
  73. $newGroups = array_diff($actualGroups, $knownGroups);
  74. $oldGroups = array_diff($knownGroups, $actualGroups);
  75. foreach ($newGroups as $groupId) {
  76. $groupObject = $this->groupManager->get($groupId);
  77. if ($groupObject === null) {
  78. $this->logger->error(
  79. __CLASS__ . ' – group {group} could not be found (user {user})',
  80. [
  81. 'app' => 'user_ldap',
  82. 'user' => $userId,
  83. 'group' => $groupId
  84. ]
  85. );
  86. continue;
  87. }
  88. try {
  89. $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $groupId,'userid' => $userId]));
  90. } catch (Exception $e) {
  91. if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
  92. $this->logger->error(
  93. __CLASS__ . ' – group {group} membership failed to be added (user {user})',
  94. [
  95. 'app' => 'user_ldap',
  96. 'user' => $userId,
  97. 'group' => $groupId,
  98. 'exception' => $e,
  99. ]
  100. );
  101. }
  102. /* We failed to insert the groupmembership so we do not want to advertise it */
  103. continue;
  104. }
  105. $this->groupBackend->addRelationshipToCaches($userId, null, $groupId);
  106. $this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
  107. $this->logger->info(
  108. __CLASS__ . ' – {user} added to {group}',
  109. [
  110. 'app' => 'user_ldap',
  111. 'user' => $userId,
  112. 'group' => $groupId
  113. ]
  114. );
  115. }
  116. foreach ($oldGroups as $groupId) {
  117. try {
  118. $this->groupMembershipMapper->delete($groupMemberships[$groupId]);
  119. } catch (Exception $e) {
  120. if ($e->getReason() !== Exception::REASON_DATABASE_OBJECT_NOT_FOUND) {
  121. $this->logger->error(
  122. __CLASS__ . ' – group {group} membership failed to be removed (user {user})',
  123. [
  124. 'app' => 'user_ldap',
  125. 'user' => $userId,
  126. 'group' => $groupId,
  127. 'exception' => $e,
  128. ]
  129. );
  130. }
  131. /* We failed to delete the groupmembership so we do not want to advertise it */
  132. continue;
  133. }
  134. $groupObject = $this->groupManager->get($groupId);
  135. if ($groupObject === null) {
  136. $this->logger->error(
  137. __CLASS__ . ' – group {group} could not be found (user {user})',
  138. [
  139. 'app' => 'user_ldap',
  140. 'user' => $userId,
  141. 'group' => $groupId
  142. ]
  143. );
  144. continue;
  145. }
  146. $this->dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
  147. $this->logger->info(
  148. 'service "updateGroups" – {user} removed from {group}',
  149. [
  150. 'user' => $userId,
  151. 'group' => $groupId
  152. ]
  153. );
  154. }
  155. }
  156. }