-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLDAPGroupManager.php
288 lines (256 loc) · 8.23 KB
/
LDAPGroupManager.php
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
* @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
*
* @author Alan Tygel <[email protected]>
* @author Vinicius Brand <[email protected]>
* @author Daniel Tygel <[email protected]>
* @author Arthur Schiwon <[email protected]>
*
* @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\LdapWriteSupport;
use Exception;
use OC\Group\Backend;
use OCA\LdapWriteSupport\AppInfo\Application;
use OCA\LdapWriteSupport\Service\Configuration;
use OCA\User_LDAP\Group_Proxy;
use OCA\User_LDAP\ILDAPGroupPlugin;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
use OCP\LDAP\ILDAPProvider;
use Psr\Log\LoggerInterface;
class LDAPGroupManager implements ILDAPGroupPlugin {
/** @var Configuration */
protected $configuration;
/** @var ILDAPProvider */
private $ldapProvider;
/** @var IUserSession */
private $userSession;
/** @var IGroupManager */
private $groupManager;
/** @var LDAPConnect */
private $ldapConnect;
/** @var LoggerInterface */
private $logger;
public function __construct(IGroupManager $groupManager, IUserSession $userSession, LDAPConnect $ldapConnect, LoggerInterface $logger, ILDAPProvider $ldapProvider, Configuration $configuration) {
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->ldapConnect = $ldapConnect;
$this->ldapProvider = $ldapProvider;
$this->configuration = $configuration;
$this->logger = $logger;
if($this->ldapConnect->groupsEnabled()) {
$this->makeLdapBackendFirst();
}
}
/**
* Check if plugin implements actions
*
* @param int $actions bitwise-or'ed actions
* @return boolean
*
* Returns the supported actions as int to be
* compared with OC_GROUP_BACKEND_CREATE_GROUP etc.
*/
public function respondToActions() {
if(!$this->ldapConnect->groupsEnabled()) {
return 0;
}
return Backend::CREATE_GROUP |
Backend::DELETE_GROUP |
Backend::ADD_TO_GROUP |
Backend::REMOVE_FROM_GROUP;
}
/**
* @param string $gid
* @return string|null
*/
public function createGroup($gid) {
$adminUser = $this->userSession->getUser();
$requireActorFromLDAP = $this->configuration->isLdapActorRequired();
if ($requireActorFromLDAP && !$adminUser instanceof IUser) {
throw new Exception('Acting user is not from LDAP');
}
try {
$connection = $this->ldapProvider->getLDAPConnection($adminUser->getUID());
// TODO: what about multiple bases?
$base = $this->ldapProvider->getLDAPBaseGroups($adminUser->getUID());
} catch (Exception $e) {
if ($requireActorFromLDAP) {
if ($this->configuration->isPreventFallback()) {
throw new \Exception('Acting admin is not from LDAP', 0, $e);
}
return false;
}
$connection = $this->ldapConnect->getLDAPConnection();
$base = $this->ldapConnect->getLDAPBaseGroups()[0];
}
list($newGroupDN, $newGroupEntry) = $this->buildNewEntry($gid, $base);
$newGroupDN = $this->ldapProvider->sanitizeDN([$newGroupDN])[0];
if ($ret = ldap_add($connection, $newGroupDN, $newGroupEntry)) {
$message = "Create LDAP group '$gid' ($newGroupDN)";
$this->logger->notice($message, ['app' => Application::APP_ID]);
} else {
$message = "Unable to create LDAP group '$gid' ($newGroupDN)";
$this->logger->error($message, ['app' => Application::APP_ID]);
}
return $ret ? $newGroupDN : null;
}
/**
* delete a group
*
* @param string $gid gid of the group to delete
* @return bool
* @throws Exception
*/
public function deleteGroup($gid) {
$connection = $this->ldapProvider->getGroupLDAPConnection($gid);
$groupDN = $this->ldapProvider->getGroupDN($gid);
if (!$ret = ldap_delete($connection, $groupDN)) {
$message = "Unable to delete LDAP Group: " . $gid;
$this->logger->error($message, ['app' => Application::APP_ID]);
} else {
$message = "Delete LDAP Group: " . $gid;
$this->logger->notice($message, ['app' => Application::APP_ID]);
}
return $ret;
}
/**
* Add a LDAP user to a LDAP group
*
* @param string $uid Name of the user to add to group
* @param string $gid Name of the group in which add the user
* @return bool
*
* Adds a LDAP user to a LDAP group.
* @throws Exception
*/
public function addToGroup($uid, $gid) {
$connection = $this->ldapProvider->getGroupLDAPConnection($gid);
$groupDN = $this->ldapProvider->getGroupDN($gid);
$entry = [];
switch ($this->ldapProvider->getLDAPGroupMemberAssoc($gid)) {
case 'memberUid':
$entry['memberuid'] = $uid;
break;
case 'uniqueMember':
$entry['uniquemember'] = $this->ldapProvider->getUserDN($uid);
break;
case 'member':
$entry['member'] = $this->ldapProvider->getUserDN($uid);
break;
case 'gidNumber':
throw new Exception('Cannot add to group when gidNumber is used as relation');
}
if (!$ret = ldap_mod_add($connection, $groupDN, $entry)) {
$message = "Unable to add user " . $uid . " to group " . $gid;
$this->logger->error($message, ['app' => Application::APP_ID]);
} else {
$message = "Add user: " . $uid . " to group: " . $gid;
$this->logger->notice($message, ['app' => Application::APP_ID]);
}
return $ret;
}
/**
* Removes a LDAP user from a LDAP group
*
* @param string $uid Name of the user to remove from group
* @param string $gid Name of the group from which remove the user
* @return bool
*
* removes the user from a group.
* @throws Exception
*/
public function removeFromGroup($uid, $gid) {
$connection = $this->ldapProvider->getGroupLDAPConnection($gid);
$groupDN = $this->ldapProvider->getGroupDN($gid);
$entry = [];
switch ($this->ldapProvider->getLDAPGroupMemberAssoc($gid)) {
case 'memberUid':
$entry['memberuid'] = $uid;
break;
case 'uniqueMember':
$entry['uniquemember'] = $this->ldapProvider->getUserDN($uid);
break;
case 'member':
$entry['member'] = $this->ldapProvider->getUserDN($uid);
break;
case 'gidNumber':
throw new Exception('Cannot remove from group when gidNumber is used as relation');
}
if (!$ret = ldap_mod_del($connection, $groupDN, $entry)) {
$message = "Unable to remove user: " . $uid . " from group: " . $gid;
$this->logger->error($message, ['app' => Application::APP_ID]);
} else {
$message = "Remove user: " . $uid . " from group: " . $gid;
$this->logger->notice($message, ['app' => Application::APP_ID]);
}
return $ret;
}
public function countUsersInGroup($gid, $search = '') {
return false;
}
public function getGroupDetails($gid) {
return false;
}
public function isLDAPGroup($gid): bool {
try {
return !empty($this->ldapProvider->getGroupDN($gid));
} catch (Exception $e) {
return false;
}
}
private function buildNewEntry($gid, $base): array {
$ldif = $this->configuration->getGroupTemplate();
$ldif = str_replace('{GID}', $gid, $ldif);
$ldif = str_replace('{BASE}', $base, $ldif);
$entry = [];
$lines = explode(PHP_EOL, $ldif);
foreach ($lines as $line) {
$split = explode(':', $line, 2);
$key = trim($split[0]);
$value = trim($split[1]);
if (!isset($entry[$key])) {
$entry[$key] = $value;
} else if (is_array($entry[$key])) {
$entry[$key][] = $value;
} else {
$entry[$key] = [$entry[$key], $value];
}
}
$dn = $entry['dn'];
unset($entry['dn']);
return [$dn, $entry];
}
public function makeLdapBackendFirst(): void {
$backends = $this->groupManager->getBackends();
$otherBackends = [];
$this->groupManager->clearBackends();
foreach ($backends as $backend) {
if ($backend instanceof Group_Proxy) {
$this->groupManager->addBackend($backend);
} else {
$otherBackends[] = $backend;
}
}
#insert other backends: database, etc
foreach ($otherBackends as $backend) {
$this->groupManager->addBackend($backend);
}
}
}