Skip to content

Commit 78ef03d

Browse files
committed
TSK-1321: Fixed user lookup with dn.
1 parent da9cf99 commit 78ef03d

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

rest/taskana-rest-spring/src/main/java/pro/taskana/common/rest/ldap/LdapClient.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77
import java.util.regex.Pattern;
88
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
910
import javax.annotation.PostConstruct;
1011
import javax.naming.directory.SearchControls;
1112
import org.slf4j.Logger;
@@ -66,7 +67,7 @@ public List<AccessIdRepresentationModel> searchUsersAndGroups(final String name)
6667

6768
List<AccessIdRepresentationModel> accessIds = new ArrayList<>();
6869
if (nameIsDn(name)) {
69-
AccessIdRepresentationModel groupByDn = searchGroupByDn(name);
70+
AccessIdRepresentationModel groupByDn = searchAccessIdByDn(name);
7071
if (groupByDn != null) {
7172
accessIds.add(groupByDn);
7273
}
@@ -101,16 +102,12 @@ public List<AccessIdRepresentationModel> searchUsersByNameOrAccessId(final Strin
101102
orFilter.or(new WhitespaceWildcardsFilter(getUserIdAttribute(), name));
102103
andFilter.and(orFilter);
103104

104-
String[] userAttributesToReturn = {
105-
getUserFirstnameAttribute(), getUserLastnameAttribute(), getUserIdAttribute()
106-
};
107-
108105
final List<AccessIdRepresentationModel> accessIds =
109106
ldapTemplate.search(
110107
getUserSearchBase(),
111108
andFilter.encode(),
112109
SearchControls.SUBTREE_SCOPE,
113-
userAttributesToReturn,
110+
getLookUpUserAttributesToReturn(),
114111
new UserContextMapper());
115112
LOGGER.debug(
116113
"exit from searchUsersByNameOrAccessId. Retrieved the following users: {}.", accessIds);
@@ -166,19 +163,19 @@ public List<AccessIdRepresentationModel> searchGroupsByName(final String name)
166163
return accessIds;
167164
}
168165

169-
public AccessIdRepresentationModel searchGroupByDn(final String name) {
170-
LOGGER.debug("entry to searchGroupByDn(name = {}).", name);
166+
public AccessIdRepresentationModel searchAccessIdByDn(final String dn) {
167+
LOGGER.debug("entry to searchGroupByDn(name = {}).", dn);
171168
isInitOrFail();
172169
// Obviously Spring LdapTemplate does have a inconsistency and always adds the base name to the
173170
// given DN.
174171
// https://stackoverflow.com/questions/55285743/spring-ldaptemplate-how-to-lookup-fully-qualified-dn-with-configured-base-dn
175172
// Therefore we have to remove the base name from the dn before performing the lookup
176-
String nameWithoutBaseDn = getNameWithoutBaseDn(name);
173+
String nameWithoutBaseDn = getNameWithoutBaseDn(dn);
177174
LOGGER.debug(
178175
"Removed baseDN {} from given DN. New DN to be used: {}", getBaseDn(), nameWithoutBaseDn);
179176
final AccessIdRepresentationModel accessId =
180177
ldapTemplate.lookup(
181-
nameWithoutBaseDn, getLookUpGroupAttributesToReturn(), new GroupContextMapper());
178+
nameWithoutBaseDn, getLookUpUserAndGroupAttributesToReturn(), new DnContextMapper());
182179
LOGGER.debug("Exit from searchGroupByDn. Retrieved the following group: {}", accessId);
183180
return accessId;
184181
}
@@ -332,6 +329,19 @@ String[] getLookUpGroupAttributesToReturn() {
332329
return new String[] {getGroupNameAttribute(), CN};
333330
}
334331

332+
String[] getLookUpUserAndGroupAttributesToReturn() {
333+
return Stream.concat(
334+
Arrays.stream(getLookUpUserAttributesToReturn()),
335+
Arrays.stream(getLookUpGroupAttributesToReturn()))
336+
.toArray(String[]::new);
337+
}
338+
339+
String[] getLookUpUserAttributesToReturn() {
340+
return new String[] {
341+
getUserFirstnameAttribute(), getUserLastnameAttribute(), getUserIdAttribute()
342+
};
343+
}
344+
335345
@PostConstruct
336346
void init() {
337347
LOGGER.debug("Entry to init()");
@@ -403,4 +413,25 @@ public AccessIdRepresentationModel doMapFromContext(final DirContextOperations c
403413
return accessId;
404414
}
405415
}
416+
417+
/** General Context Mapper for DNs, which can be both, user or groups. */
418+
class DnContextMapper extends AbstractContextMapper<AccessIdRepresentationModel> {
419+
420+
@Override
421+
public AccessIdRepresentationModel doMapFromContext(final DirContextOperations context) {
422+
final AccessIdRepresentationModel accessId = new AccessIdRepresentationModel();
423+
String userId = context.getStringAttribute(getUserIdAttribute());
424+
if (userId != null) {
425+
accessId.setAccessId(userId);
426+
String firstName = context.getStringAttribute(getUserFirstnameAttribute());
427+
String lastName = context.getStringAttribute(getUserLastnameAttribute());
428+
accessId.setName(String.format("%s, %s", lastName, firstName));
429+
} else {
430+
String dn = getDnWithBaseDn(context.getDn().toString());
431+
accessId.setAccessId(dn); // fully qualified dn
432+
accessId.setName(context.getStringAttribute(getGroupNameAttribute()));
433+
}
434+
return accessId;
435+
}
436+
}
406437
}

rest/taskana-rest-spring/src/test/java/pro/taskana/common/rest/AccessIdControllerIntTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ void testQueryGroupsByDn() {
4646
.containsExactly("cn=ksc-users,cn=groups,OU=Test,O=TASKANA");
4747
}
4848

49+
@Test
50+
void testQueryUserByDn() {
51+
ResponseEntity<AccessIdListResource> response =
52+
TEMPLATE.exchange(
53+
restHelper.toUrl(Mapping.URL_ACCESSID)
54+
+ "?search-for=uid=teamlead-1,cn=users,OU=Test,O=TASKANA",
55+
HttpMethod.GET,
56+
restHelper.defaultRequest(),
57+
ParameterizedTypeReference.forType(AccessIdListResource.class));
58+
assertThat(response.getBody())
59+
.isNotNull()
60+
.extracting(AccessIdRepresentationModel::getAccessId)
61+
.usingElementComparator(String.CASE_INSENSITIVE_ORDER)
62+
.containsExactly("teamlead-1");
63+
}
64+
4965
@Test
5066
void testQueryGroupsByCn() {
5167
ResponseEntity<AccessIdListResource> response =

rest/taskana-rest-spring/src/test/java/pro/taskana/common/rest/ldap/LdapClientTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ void testLdap_searchGroupByDn() {
4242
setUpEnvMock();
4343
cut.init();
4444

45-
cut.searchGroupByDn("cn=developersgroup,ou=groups,o=taskanatest");
45+
cut.searchAccessIdByDn("cn=developersgroup,ou=groups,o=taskanatest");
4646

4747
verify(ldapTemplate)
48-
.lookup(
49-
eq("cn=developersgroup,ou=groups"), any(), any(LdapClient.GroupContextMapper.class));
48+
.lookup(eq("cn=developersgroup,ou=groups"), any(), any(LdapClient.DnContextMapper.class));
5049
}
5150

5251
@Test

0 commit comments

Comments
 (0)