aboutsummaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/lib/Service/UpdateGroupsService.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/user_ldap/lib/Service/UpdateGroupsService.php')
-rw-r--r--apps/user_ldap/lib/Service/UpdateGroupsService.php111
1 files changed, 69 insertions, 42 deletions
diff --git a/apps/user_ldap/lib/Service/UpdateGroupsService.php b/apps/user_ldap/lib/Service/UpdateGroupsService.php
index faee092a372..94f2a7fd4a1 100644
--- a/apps/user_ldap/lib/Service/UpdateGroupsService.php
+++ b/apps/user_ldap/lib/Service/UpdateGroupsService.php
@@ -3,32 +3,9 @@
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/>
- *
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\User_LDAP\Service;
@@ -66,7 +43,7 @@ class UpdateGroupsService {
if (empty($actualGroups) && empty($knownGroups)) {
$this->logger->info(
- 'service "updateGroups" – groups do not seem to be configured properly, aborting.',
+ 'service "updateGroups" - groups do not seem to be configured properly, aborting.',
);
return;
}
@@ -75,7 +52,7 @@ class UpdateGroupsService {
$this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
$this->handleRemovedGroups(array_diff($knownGroups, $actualGroups));
- $this->logger->debug('service "updateGroups" – Finished.');
+ $this->logger->debug('service "updateGroups" - Finished.');
}
/**
@@ -83,10 +60,10 @@ class UpdateGroupsService {
* @throws Exception
*/
public function handleKnownGroups(array $groups): void {
- $this->logger->debug('service "updateGroups" – Dealing with known Groups.');
+ $this->logger->debug('service "updateGroups" - Dealing with known Groups.');
foreach ($groups as $group) {
- $this->logger->debug('service "updateGroups" – Dealing with {group}.', ['group' => $group]);
+ $this->logger->debug('service "updateGroups" - Dealing with {group}.', ['group' => $group]);
$groupMemberships = $this->groupMembershipMapper->findGroupMemberships($group);
$knownUsers = array_map(
static fn (GroupMembership $groupMembership): string => $groupMembership->getUserid(),
@@ -99,7 +76,7 @@ class UpdateGroupsService {
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',
+ 'service "updateGroups" - Failed to get group {group} for update',
[
'group' => $group
]
@@ -107,13 +84,30 @@ class UpdateGroupsService {
continue;
}
foreach (array_diff($knownUsers, $actualUsers) as $removedUser) {
- $this->groupMembershipMapper->delete($groupMemberships[$removedUser]);
+ try {
+ $this->groupMembershipMapper->delete($groupMemberships[$removedUser]);
+ } catch (Exception $e) {
+ if ($e->getReason() !== Exception::REASON_DATABASE_OBJECT_NOT_FOUND) {
+ /* If reason is not found something else removed the membership, that’s fine */
+ $this->logger->error(
+ self::class . ' - group {group} membership failed to be removed (user {user})',
+ [
+ 'app' => 'user_ldap',
+ 'user' => $removedUser,
+ 'group' => $group,
+ 'exception' => $e,
+ ]
+ );
+ }
+ /* We failed to delete the groupmembership so we do not want to advertise it */
+ continue;
+ }
$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}',
+ 'service "updateGroups" - {user} removed from {group}',
[
'user' => $removedUser,
'group' => $group
@@ -121,13 +115,30 @@ class UpdateGroupsService {
);
}
foreach (array_diff($actualUsers, $knownUsers) as $addedUser) {
- $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $group,'userid' => $addedUser]));
+ try {
+ $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $group,'userid' => $addedUser]));
+ } catch (Exception $e) {
+ if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
+ /* If reason is unique constraint something else added the membership, that’s fine */
+ $this->logger->error(
+ self::class . ' - group {group} membership failed to be added (user {user})',
+ [
+ 'app' => 'user_ldap',
+ 'user' => $addedUser,
+ 'group' => $group,
+ 'exception' => $e,
+ ]
+ );
+ }
+ /* We failed to insert the groupmembership so we do not want to advertise it */
+ continue;
+ }
$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}',
+ 'service "updateGroups" - {user} added to {group}',
[
'user' => $addedUser,
'group' => $group
@@ -135,7 +146,7 @@ class UpdateGroupsService {
);
}
}
- $this->logger->debug('service "updateGroups" – FINISHED dealing with known Groups.');
+ $this->logger->debug('service "updateGroups" - FINISHED dealing with known Groups.');
}
/**
@@ -143,15 +154,31 @@ class UpdateGroupsService {
* @throws Exception
*/
public function handleCreatedGroups(array $createdGroups): void {
- $this->logger->debug('service "updateGroups" – dealing with created Groups.');
+ $this->logger->debug('service "updateGroups" - dealing with created Groups.');
foreach ($createdGroups as $createdGroup) {
- $this->logger->info('service "updateGroups" – new group "' . $createdGroup . '" found.');
+ $this->logger->info('service "updateGroups" - new group "' . $createdGroup . '" found.');
$users = $this->groupBackend->usersInGroup($createdGroup);
$groupObject = $this->groupManager->get($createdGroup);
foreach ($users as $user) {
- $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $createdGroup,'userid' => $user]));
+ try {
+ $this->groupMembershipMapper->insert(GroupMembership::fromParams(['groupid' => $createdGroup,'userid' => $user]));
+ } catch (Exception $e) {
+ if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
+ $this->logger->error(
+ self::class . ' - group {group} membership failed to be added (user {user})',
+ [
+ 'app' => 'user_ldap',
+ 'user' => $user,
+ 'group' => $createdGroup,
+ 'exception' => $e,
+ ]
+ );
+ }
+ /* We failed to insert the groupmembership so we do not want to advertise it */
+ continue;
+ }
if ($groupObject instanceof IGroup) {
$userObject = $this->userManager->get($user);
if ($userObject instanceof IUser) {
@@ -160,7 +187,7 @@ class UpdateGroupsService {
}
}
}
- $this->logger->debug('service "updateGroups" – FINISHED dealing with created Groups.');
+ $this->logger->debug('service "updateGroups" - FINISHED dealing with created Groups.');
}
/**
@@ -168,7 +195,7 @@ class UpdateGroupsService {
* @throws Exception
*/
public function handleRemovedGroups(array $removedGroups): void {
- $this->logger->debug('service "updateGroups" – dealing with removed groups.');
+ $this->logger->debug('service "updateGroups" - dealing with removed groups.');
$this->groupMembershipMapper->deleteGroups($removedGroups);
foreach ($removedGroups as $group) {
@@ -185,7 +212,7 @@ class UpdateGroupsService {
}
$this->logger->info(
- 'service "updateGroups" – groups {removedGroups} were removed.',
+ 'service "updateGroups" - groups {removedGroups} were removed.',
[
'removedGroups' => $removedGroups
]