Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit a86f563

Browse files
committed
Made several LDAP queries, DN's and properties configurable
1 parent af1412b commit a86f563

File tree

5 files changed

+132
-48
lines changed

5 files changed

+132
-48
lines changed

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,30 @@ parameters:
125125
ldap_dn: ~
126126
ldap_pass: ~
127127

128-
# DN of where the users are located in LDAP
129-
users_dn: 'ou=Users,ou=SURFUni,dc=surfuni,dc=org'
128+
# DN of where the users are located in LDAP, can be multiple DN's seperated by a comma
129+
users_dn: ['ou=Users,ou=SURFUni,dc=surfuni,dc=org']
130+
user_query: 'cn=*'
130131
# DN of where groups are located in LDAP, can be multiple DN's seperated by a comma
131132
groups_dn: ['ou=Formalgroups,dc=surfuni,dc=org']
133+
group_query: 'cn=*'
132134
# Root group of where Grouphub groups will be stored
133135
grouphub_dn: 'ou=Grouphub,dc=surfuni,dc=org'
134136
# Subgroups located beneath the 'grouphub' DN where formal and adhoc groups will be stored
135137
formal_dn: 'ou=SemiFormal,ou=Grouphub,dc=surfuni,dc=org'
136138
adhoc_dn: 'ou=AdHoc,ou=Grouphub,dc=surfuni,dc=org'
139+
140+
# Mapping of GroupHub properties to LDAP properties
141+
ldap.mapping:
142+
user:
143+
firstName: givenname
144+
lastName: sn
145+
loginName: uid
146+
email: mail
147+
group:
148+
name: cn
149+
description: description
150+
objectClass: groupOfNames # this not a mapping, but a hardcoded value
151+
groupType: ~ # this not a mapping, but a hardcoded value which will not be added if empty
137152

138153
# Whether or not to sync admins to dedicated groups and, if so, to which DN
139154
# Note this DN should not be located beneath one of the groups mentioned earlier

app/config/parameters.yml.dist

+15-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,26 @@ parameters:
1919
ldap_dn: ~
2020
ldap_pass: ~
2121

22-
users_dn: 'ou=Users,ou=SURFUni,dc=surfuni,dc=org'
22+
users_dn: ['ou=Users,ou=SURFUni,dc=surfuni,dc=org']
23+
user_query: 'cn=*'
2324
groups_dn: ['ou=Formalgroups,dc=surfuni,dc=org']
25+
group_query: 'cn=*'
2426
grouphub_dn: 'ou=Grouphub,dc=surfuni,dc=org'
2527
formal_dn: 'ou=SemiFormal,ou=Grouphub,dc=surfuni,dc=org'
2628
adhoc_dn: 'ou=AdHoc,ou=Grouphub,dc=surfuni,dc=org'
2729

30+
ldap.mapping:
31+
user:
32+
firstName: givenname
33+
lastName: sn
34+
loginName: uid
35+
email: mail
36+
group:
37+
name: cn
38+
description: description
39+
objectClass: groupOfNames
40+
groupType: ~
41+
2842
admin_groups_sync: false
2943
admin_groups_dn: ~
3044

app/config/services.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ services:
99

1010
app.ldap_normalizer:
1111
class: AppBundle\Ldap\Normalizer
12+
arguments: ["%ldap.mapping%"]
1213
private: true
1314

1415
app.grouphub_ldap_client:
1516
class: AppBundle\Ldap\GrouphubClient
16-
arguments: ["@app.ldap_client", "@app.ldap_normalizer", "%users_dn%", "%groups_dn%", "%grouphub_dn%", "%formal_dn%", "%adhoc_dn%", "%admin_groups_dn%"]
17+
arguments: ["@app.ldap_client", "@app.ldap_normalizer", "%users_dn%", "%groups_dn%", "%grouphub_dn%", "%formal_dn%", "%adhoc_dn%", "%admin_groups_dn%", "%user_query%", "%group_query%"]
1718
private: true
1819

1920
app.api_normalizer:

src/AppBundle/Ldap/GrouphubClient.php

+53-27
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace AppBundle\Ldap;
44

55
use AppBundle\Model\Group;
6+
use AppBundle\Model\User;
67
use AppBundle\Sequence;
78
use AppBundle\SynchronizableSequence;
9+
use Doctrine\Common\Comparable;
810
use InvalidArgumentException;
911

1012
/**
@@ -25,7 +27,7 @@ class GrouphubClient
2527
private $normalizer;
2628

2729
/**
28-
* @var string
30+
* @var string[]
2931
*/
3032
private $usersDn;
3133

@@ -54,25 +56,39 @@ class GrouphubClient
5456
*/
5557
private $adminGroupsDn;
5658

59+
/**
60+
* @var string
61+
*/
62+
private $userQuery;
63+
64+
/**
65+
* @var string
66+
*/
67+
private $groupQuery;
68+
5769
/**
5870
* @param LdapClient $ldap
5971
* @param Normalizer $normalizer
60-
* @param string $usersDn
72+
* @param string[] $usersDn
6173
* @param string[] $groupsDn
6274
* @param string $grouphubDn
6375
* @param string $formalDn
6476
* @param string $adhocDn
6577
* @param string $adminGroupsDn
78+
* @param string $userQuery
79+
* @param string $groupQuery
6680
*/
6781
public function __construct(
6882
LdapClient $ldap,
6983
$normalizer,
70-
$usersDn,
84+
array $usersDn,
7185
array $groupsDn,
7286
$grouphubDn,
7387
$formalDn,
7488
$adhocDn,
75-
$adminGroupsDn = ''
89+
$adminGroupsDn = '',
90+
$userQuery = 'cn=*',
91+
$groupQuery = 'cn=*'
7692
) {
7793
$this->ldap = $ldap;
7894
$this->normalizer = $normalizer;
@@ -83,63 +99,73 @@ public function __construct(
8399
$this->formalDn = $formalDn;
84100
$this->adhocDn = $adhocDn;
85101
$this->adminGroupsDn = $adminGroupsDn;
102+
$this->userQuery = $userQuery;
103+
$this->groupQuery = $groupQuery;
86104
}
87105

88106
/**
89107
* @param int $offset
90108
* @param int $limit
91109
*
92-
* @return Sequence
110+
* @return Sequence|User[]
93111
*/
94112
public function findUsers($offset, $limit)
95113
{
96-
$data = $this->ldap->find($this->usersDn, 'cn=*', '*', '', $offset, $limit);
97-
98-
if (empty($data)) {
99-
return new Sequence([]);
100-
}
101-
102-
$users = $this->normalizer->denormalizeUsers($data);
103-
104-
// @todo: use actual offset/limit
105-
$users = array_slice($users, $offset, $limit);
106-
107-
return new Sequence($users);
114+
return $this->findEntities($this->usersDn, $this->userQuery, ['*'], $offset, $limit, function ($data) {
115+
return $this->normalizer->denormalizeUsers($data);
116+
});
108117
}
109118

110119
/**
111120
* @param int $offset
112121
* @param int $limit
113122
*
114-
* @return Sequence
123+
* @return Sequence|Group[]
115124
*/
116125
public function findGroups($offset, $limit)
117126
{
118-
$groups = [];
127+
return $this->findEntities($this->groupsDn, $this->groupQuery, ['*'], $offset, $limit, function ($data) {
128+
return $this->normalizer->denormalizeGroups($data);
129+
});
130+
}
131+
132+
/**
133+
* @param array $dns
134+
* @param string $query
135+
* @param array $filter
136+
* @param int $offset
137+
* @param int $limit
138+
* @param \Closure $normalizer
139+
*
140+
* @return Sequence
141+
*/
142+
private function findEntities(array $dns, $query, $filter, $offset, $limit, \Closure $normalizer)
143+
{
144+
$entities = [];
119145

120-
foreach ($this->groupsDn as $dn) {
121-
$data = $this->ldap->find($dn, 'cn=*', ['cn', 'description'], '');
146+
foreach ($dns as $dn) {
147+
$data = $this->ldap->find($dn, $query, $filter, '');
122148

123149
if (empty($data)) {
124150
continue;
125151
}
126152

127-
$groups = array_merge($groups, $this->normalizer->denormalizeGroups($data));
153+
$entities = array_merge($entities, $normalizer($data));
128154
}
129155

130-
if (count($this->groupsDn) > 1) {
156+
if (count($dns) > 1) {
131157
usort(
132-
$groups,
133-
function (Group $a, Group $b) {
158+
$entities,
159+
function (Comparable $a, Comparable $b) {
134160
return $a->compareTo($b);
135161
}
136162
);
137163
}
138164

139165
// @todo: use actual offset/limit
140-
$groups = array_slice($groups, $offset, $limit);
166+
$entities = array_slice($entities, $offset, $limit);
141167

142-
return new Sequence($groups);
168+
return new Sequence($entities);
143169
}
144170

145171
/**

src/AppBundle/Ldap/Normalizer.php

+45-17
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,44 @@
1010
*/
1111
class Normalizer
1212
{
13+
/**
14+
* @var array
15+
*/
16+
private $mapping;
17+
18+
/**
19+
* @param array $mapping
20+
*/
21+
public function __construct(array $mapping)
22+
{
23+
$this->mapping = $mapping;
24+
}
25+
1326
/**
1427
* @param array $users
1528
*
1629
* @return User[]
1730
*/
1831
public function denormalizeUsers(array $users)
1932
{
33+
$mapping = $this->mapping['user'];
34+
2035
$result = [];
2136
for ($i = 0; $i < $users['count']; $i++) {
2237
$user = $users[$i];
2338

2439
$annotations = [];
2540

26-
if (isset($user['mail'][0])) {
27-
$annotations['email'] = $user['mail'][0];
41+
if (isset($user[$mapping['email']][0])) {
42+
$annotations['email'] = $user[$mapping['email']][0];
2843
}
2944

3045
$result[] = new User(
3146
null,
3247
$user['dn'],
33-
$user['givenname'][0],
34-
$user['sn'][0],
35-
$user['uid'][0],
48+
$user[$mapping['firstName']][0],
49+
$user[$mapping['lastName']][0],
50+
$user[$mapping['loginName']][0],
3651
$annotations
3752
);
3853
}
@@ -47,15 +62,17 @@ public function denormalizeUsers(array $users)
4762
*/
4863
public function denormalizeGroups(array $groups)
4964
{
65+
$mapping = $this->mapping['group'];
66+
5067
$result = [];
5168
for ($i = 0; $i < $groups['count']; $i++) {
5269
$group = $groups[$i];
5370

5471
$result[] = new Group(
5572
null,
5673
$group['dn'],
57-
$group['cn'][0],
58-
isset($group['description'][0]) ? $group['description'][0] : '',
74+
$group[$mapping['name']][0],
75+
isset($group[$mapping['description']][0]) ? $group[$mapping['description']][0] : '',
5976
'ldap',
6077
new User(1)
6178
);
@@ -97,15 +114,17 @@ public function denormalizeGroupUsers(array $groups)
97114
*/
98115
public function denormalizeGrouphubGroups(array $groups)
99116
{
117+
$mapping = $this->mapping['group'];
118+
100119
$result = [];
101120
for ($i = 0; $i < $groups['count']; $i++) {
102121
$group = $groups[$i];
103122

104123
$result[] = new Group(
105124
null,
106125
$group['dn'],
107-
$group['cn'][0],
108-
isset($group['description'][0]) ? $group['description'][0] : ''
126+
$group[$mapping['name']][0],
127+
isset($group[$mapping['description']][0]) ? $group[$mapping['description']][0] : ''
109128
);
110129
}
111130

@@ -119,11 +138,16 @@ public function denormalizeGrouphubGroups(array $groups)
119138
*/
120139
public function normalizeGroup(Group $group)
121140
{
122-
$data = array_filter([
123-
'cn' => $group->getName(),
124-
'description' => $group->getDescription(),
125-
'objectClass' => 'groupOfNames',
126-
]);
141+
$mapping = $this->mapping['group'];
142+
143+
$data = array_filter(
144+
[
145+
$mapping['name'] => $group->getName(),
146+
$mapping['description'] => $group->getDescription(),
147+
'objectClass' => $mapping['objectClass'],
148+
'groupType' => $mapping['groupType'],
149+
]
150+
);
127151

128152
$data['member'] = '';
129153

@@ -137,8 +161,12 @@ public function normalizeGroup(Group $group)
137161
*/
138162
public function normalizeGroupForUpdate(Group $group)
139163
{
140-
return array_filter([
141-
'description' => $group->getDescription(),
142-
]);
164+
$mapping = $this->mapping['group'];
165+
166+
return array_filter(
167+
[
168+
$mapping['description'] => $group->getDescription(),
169+
]
170+
);
143171
}
144172
}

0 commit comments

Comments
 (0)