Skip to content

Commit 946c6ff

Browse files
committed
Enable syncing of more LDAP groups
Resolves: #55
1 parent 41bd1b7 commit 946c6ff

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

config/config-sample.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ group_member_value = uid
138138
; interface
139139
admin_group_cn = ska-administrators
140140

141+
; Other LDAP groups that should have their memberships synced
142+
;sync_groups[] = ldap_group_name
143+
141144
[inventory]
142145
; SSH Key Authority will read the contents of the file /etc/uuid (if it
143146
; exists) when syncing with a server. If a value is found, it can be used as a

model/user.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function check_csrf_token($token) {
298298
* @throws UserNotFoundException if the user is not found in LDAP
299299
*/
300300
public function get_details_from_ldap() {
301-
global $config;
301+
global $config, $group_dir;
302302
$attributes = array();
303303
$attributes[] = 'dn';
304304
$attributes[] = $config['ldap']['user_id'];
@@ -327,8 +327,35 @@ public function get_details_from_ldap() {
327327
$this->admin = 0;
328328
$group_member = $ldapuser[strtolower($config['ldap']['group_member_value'])];
329329
$ldapgroups = $this->ldap->search($config['ldap']['dn_group'], LDAP::escape($config['ldap']['group_member']).'='.LDAP::escape($group_member), array('cn'));
330+
$memberships = array();
330331
foreach($ldapgroups as $ldapgroup) {
331-
if($ldapgroup['cn'] == $config['ldap']['admin_group_cn']) $this->admin = 1;
332+
$memberships[$ldapgroup['cn']] = true;
333+
}
334+
if(isset($config['ldap']['sync_groups']) && is_array($config['ldap']['sync_groups'])) {
335+
$syncgroups = $config['ldap']['sync_groups'];
336+
} else {
337+
$syncgroups = array();
338+
}
339+
$syncgroups[] = $config['ldap']['admin_group_cn'];
340+
foreach($syncgroups as $syncgroup) {
341+
try {
342+
$group = $group_dir->get_group_by_name($syncgroup);
343+
} catch(GroupNotFoundException $e) {
344+
$group = new Group;
345+
$group->name = $syncgroup;
346+
$group->system = 1;
347+
$group_dir->add_group($group);
348+
}
349+
if(isset($memberships[$syncgroup])) {
350+
if($syncgroup == $config['ldap']['admin_group_cn']) $this->admin = 1;
351+
if(!$this->member_of($group)) {
352+
$group->add_member($this);
353+
}
354+
} else {
355+
if($this->member_of($group)) {
356+
$group->delete_member($this);
357+
}
358+
}
332359
}
333360
} else {
334361
throw new UserNotFoundException('User does not exist.');

scripts/ldap_update.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@
3535
$user_dir->add_user($active_user);
3636
}
3737

38-
try {
39-
$sysgrp = $group_dir->get_group_by_name($config['ldap']['admin_group_cn']);
40-
} catch(GroupNotFoundException $e) {
41-
$sysgrp = new Group;
42-
$sysgrp->name = $config['ldap']['admin_group_cn'];
43-
$sysgrp->system = 1;
44-
$group_dir->add_group($sysgrp);
45-
}
4638
foreach($users as $user) {
4739
if($user->auth_realm == 'LDAP') {
4840
$active = $user->active;
@@ -88,12 +80,6 @@
8880
}
8981
}
9082
}
91-
if($user->admin && $user->active && !$user->member_of($sysgrp)) {
92-
$sysgrp->add_member($user);
93-
}
94-
if(!($user->admin && $user->active) && $user->member_of($sysgrp)) {
95-
$sysgrp->delete_member($user);
96-
}
9783
$user->update();
9884
}
9985
}

0 commit comments

Comments
 (0)