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.

LoginListener.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\EventDispatcher\Event;
  28. use OCP\EventDispatcher\IEventDispatcher;
  29. use OCP\EventDispatcher\IEventListener;
  30. use OCP\Group\Events\UserAddedEvent;
  31. use OCP\Group\Events\UserRemovedEvent;
  32. use OCP\IGroupManager;
  33. use OCP\IUser;
  34. use OCP\User\Events\PostLoginEvent;
  35. use Psr\Log\LoggerInterface;
  36. /**
  37. * @template-implements IEventListener<PostLoginEvent>
  38. */
  39. class LoginListener implements IEventListener {
  40. public function __construct(
  41. private IEventDispatcher $dispatcher,
  42. private Group_Proxy $groupBackend,
  43. private IGroupManager $groupManager,
  44. private LoggerInterface $logger,
  45. private GroupMembershipMapper $groupMembershipMapper,
  46. ) {
  47. }
  48. public function handle(Event $event): void {
  49. if ($event instanceof PostLoginEvent) {
  50. $this->onPostLogin($event->getUser());
  51. }
  52. }
  53. public function onPostLogin(IUser $user): void {
  54. $this->logger->info(
  55. __CLASS__ . ' – {user} postLogin',
  56. [
  57. 'app' => 'user_ldap',
  58. 'user' => $user->getUID(),
  59. ]
  60. );
  61. $this->updateGroups($user);
  62. }
  63. private function updateGroups(IUser $userObject): void {
  64. $userId = $userObject->getUID();
  65. $groupMemberships = $this->groupMembershipMapper->findGroupMembershipsForUser($userId);
  66. $knownGroups = array_map(
  67. static fn (GroupMembership $groupMembership): string => $groupMembership->getGroupid(),
  68. $groupMemberships
  69. );
  70. $groupMemberships = array_combine($knownGroups, $groupMemberships);
  71. $actualGroups = $this->groupBackend->getUserGroups($userId);
  72. $newGroups = array_diff($actualGroups, $knownGroups);
  73. $oldGroups = array_diff($knownGroups, $actualGroups);
  74. foreach ($newGroups as $groupId) {
  75. $groupObject = $this->groupManager->get($groupId);
  76. if ($groupObject === null) {
  77. $this->logger->error(
  78. __CLASS__ . ' – group {group} could not be found (user {user})',
  79. [
  80. 'app' => 'user_ldap',
  81. 'user' => $userId,
  82. 'group' => $groupId
  83. ]
  84. );
  85. continue;
  86. }
  87. $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $groupId,'userid' => $userId]));
  88. $this->groupBackend->addRelationshipToCaches($userId, null, $groupId);
  89. $this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
  90. $this->logger->info(
  91. __CLASS__ . ' – {user} added to {group}',
  92. [
  93. 'app' => 'user_ldap',
  94. 'user' => $userId,
  95. 'group' => $groupId
  96. ]
  97. );
  98. }
  99. foreach ($oldGroups as $groupId) {
  100. $this->groupMembershipMapper->delete($groupMemberships[$groupId]);
  101. $groupObject = $this->groupManager->get($groupId);
  102. if ($groupObject === null) {
  103. $this->logger->error(
  104. __CLASS__ . ' – group {group} could not be found (user {user})',
  105. [
  106. 'app' => 'user_ldap',
  107. 'user' => $userId,
  108. 'group' => $groupId
  109. ]
  110. );
  111. continue;
  112. }
  113. $this->dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
  114. $this->logger->info(
  115. 'service "updateGroups" – {user} removed from {group}',
  116. [
  117. 'user' => $userId,
  118. 'group' => $groupId
  119. ]
  120. );
  121. }
  122. }
  123. }