summaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/lib/Service/UpdateGroupsService.php
diff options
context:
space:
mode:
authorCôme Chilliet <come.chilliet@nextcloud.com>2023-07-20 16:43:50 +0200
committerCôme Chilliet <come.chilliet@nextcloud.com>2023-08-10 10:57:33 +0200
commit2c19aac9e1452872400e09fadd5a0629c1681596 (patch)
tree25007616fe626f74af369c37c155bca7d3258209 /apps/user_ldap/lib/Service/UpdateGroupsService.php
parent34fa4138fb5c5f4e469d0b74b8773ce5d5f34609 (diff)
downloadnextcloud-server-2c19aac9e1452872400e09fadd5a0629c1681596.tar.gz
nextcloud-server-2c19aac9e1452872400e09fadd5a0629c1681596.zip
Move UpdateGroups methods to a service
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Diffstat (limited to 'apps/user_ldap/lib/Service/UpdateGroupsService.php')
-rw-r--r--apps/user_ldap/lib/Service/UpdateGroupsService.php179
1 files changed, 179 insertions, 0 deletions
diff --git a/apps/user_ldap/lib/Service/UpdateGroupsService.php b/apps/user_ldap/lib/Service/UpdateGroupsService.php
new file mode 100644
index 00000000000..aaa86435695
--- /dev/null
+++ b/apps/user_ldap/lib/Service/UpdateGroupsService.php
@@ -0,0 +1,179 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ *
+ * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
+ * @author Bart Visscher <bartv@thisnet.nl>
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ * @author Côme Chilliet <come.chilliet@nextcloud.com>
+ * @author Joas Schilling <coding@schilljs.com>
+ * @author Lukas Reschke <lukas@statuscode.ch>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Robin Appelman <robin@icewind.nl>
+ * @author Robin McCorkell <robin@mccorkell.me.uk>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\User_LDAP\Service;
+
+use OCA\User_LDAP\Db\GroupMembership;
+use OCA\User_LDAP\Db\GroupMembershipMapper;
+use OCA\User_LDAP\Group_Proxy;
+use OCP\DB\Exception;
+use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Group\Events\UserAddedEvent;
+use OCP\Group\Events\UserRemovedEvent;
+use OCP\IGroupManager;
+use OCP\IUser;
+use OCP\IUserManager;
+use Psr\Log\LoggerInterface;
+
+class UpdateGroupsService {
+ public function __construct(
+ private Group_Proxy $groupBackend,
+ private IEventDispatcher $dispatcher,
+ private IGroupManager $groupManager,
+ private IUserManager $userManager,
+ private LoggerInterface $logger,
+ private GroupMembershipMapper $groupMembershipMapper,
+ ) {
+ }
+
+ /**
+ * @throws Exception
+ */
+ public function updateGroups(): void {
+ $knownGroups = $this->groupMembershipMapper->getKnownGroups();
+ $actualGroups = $this->groupBackend->getGroups();
+
+ if (empty($actualGroups) && empty($knownGroups)) {
+ $this->logger->info(
+ 'service "updateGroups" – groups do not seem to be configured properly, aborting.',
+ );
+ return;
+ }
+
+ $this->service->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
+ $this->service->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
+ $this->service->handleRemovedGroups(array_diff($knownGroups, $actualGroups));
+
+ $this->logger->debug('service "updateGroups" – Finished.');
+ }
+
+ /**
+ * @param string[] $groups
+ * @throws Exception
+ */
+ public function handleKnownGroups(array $groups): void {
+ $this->logger->debug('service "updateGroups" – Dealing with known Groups.');
+
+ foreach ($groups as $group) {
+ $groupMemberships = $this->groupMembershipMapper->findGroupMemberships($group);
+ $knownUsers = array_map(
+ fn (GroupMembership $groupMembership): string => $groupMembership->getUserid(),
+ $groupMemberships
+ );
+ $groupMemberships = array_combine($knownUsers, $groupMemberships);
+ $actualUsers = $this->groupBackend->usersInGroup($group);
+
+ $groupObject = $this->groupManager->get($group);
+ if ($groupObject === null) {
+ /* We are not expecting the group to not be found since it was returned by $this->groupBackend->getGroups() */
+ $this->logger->error(
+ 'service "updateGroups" – Failed to get group {group} for update',
+ [
+ 'group' => $group
+ ]
+ );
+ continue;
+ }
+ foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
+ $this->groupMembershipMapper->delete($groupMemberships[$removedUser]);
+ $userObject = $this->userManager->get($removedUser);
+ if ($userObject instanceof IUser) {
+ $this->dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
+ }
+ $this->logger->info(
+ 'service "updateGroups" – {user} removed from {group}',
+ [
+ 'user' => $removedUser,
+ 'group' => $group
+ ]
+ );
+ }
+ foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
+ $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $group,'userid' => $addedUser]));
+ $userObject = $this->userManager->get($addedUser);
+ if ($userObject instanceof IUser) {
+ $this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
+ }
+ $this->logger->info(
+ 'service "updateGroups" – {user} added to {group}',
+ [
+ 'user' => $addedUser,
+ 'group' => $group
+ ]
+ );
+ }
+ }
+ $this->logger->debug('service "updateGroups" – FINISHED dealing with known Groups.');
+ }
+
+ /**
+ * @param string[] $createdGroups
+ * @throws Exception
+ */
+ public function handleCreatedGroups(array $createdGroups): void {
+ $this->logger->debug('service "updateGroups" – dealing with created Groups.');
+
+ foreach ($createdGroups as $createdGroup) {
+ $this->logger->info('service "updateGroups" – new group "' . $createdGroup . '" found.');
+
+ $users = $this->groupBackend->usersInGroup($createdGroup);
+ foreach ($users as $user) {
+ $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $createdGroup,'userid' => $user]));
+ }
+ $groupObject = $this->groupManager->get($group);
+ if ($groupObject instanceof IGroup) {
+ $this->dispatcher->dispatchTyped(new GroupCreatedEvent($groupObject));
+ }
+ }
+ $this->logger->debug('service "updateGroups" – FINISHED dealing with created Groups.');
+ }
+
+ /**
+ * @param string[] $removedGroups
+ * @throws Exception
+ */
+ public function handleRemovedGroups(array $removedGroups): void {
+ $this->logger->debug('service "updateGroups" – dealing with removed groups.');
+
+ $this->groupMembershipMapper->deleteGroups($removedGroups);
+
+ //TODO find a way to dispatch GroupDeletedEvent
+
+ $this->logger->info(
+ 'service "updateGroups" – groups {removedGroups} were removed.',
+ [
+ 'removedGroups' => $removedGroups
+ ]
+ );
+ }
+}