1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
<?php
declare(strict_types=1);
/**
* 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;
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\IGroup;
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->handleKnownGroups(array_intersect($actualGroups, $knownGroups));
$this->handleCreatedGroups(array_diff($actualGroups, $knownGroups));
$this->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) {
$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(),
$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) {
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(
__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}',
[
'user' => $removedUser,
'group' => $group
]
);
}
foreach (array_diff($actualUsers, $knownUsers) as $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(
__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}',
[
'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);
$groupObject = $this->groupManager->get($createdGroup);
foreach ($users as $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(
__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) {
$this->dispatcher->dispatchTyped(new UserAddedEvent($groupObject, $userObject));
}
}
}
}
$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);
foreach ($removedGroups as $group) {
$groupObject = $this->groupManager->get($group);
if ($groupObject instanceof IGroup) {
$groupMemberships = $this->groupMembershipMapper->findGroupMemberships($group);
foreach ($groupMemberships as $groupMembership) {
$userObject = $this->userManager->get($groupMembership->getUserid());
if ($userObject instanceof IUser) {
$this->dispatcher->dispatchTyped(new UserRemovedEvent($groupObject, $userObject));
}
}
}
}
$this->logger->info(
'service "updateGroups" - groups {removedGroups} were removed.',
[
'removedGroups' => $removedGroups
]
);
}
}
|