|
6 | 6 | import java.util.List;
|
7 | 7 | import java.util.regex.Pattern;
|
8 | 8 | import java.util.stream.Collectors;
|
| 9 | +import java.util.stream.Stream; |
9 | 10 | import javax.annotation.PostConstruct;
|
10 | 11 | import javax.naming.directory.SearchControls;
|
11 | 12 | import org.slf4j.Logger;
|
@@ -66,7 +67,7 @@ public List<AccessIdRepresentationModel> searchUsersAndGroups(final String name)
|
66 | 67 |
|
67 | 68 | List<AccessIdRepresentationModel> accessIds = new ArrayList<>();
|
68 | 69 | if (nameIsDn(name)) {
|
69 |
| - AccessIdRepresentationModel groupByDn = searchGroupByDn(name); |
| 70 | + AccessIdRepresentationModel groupByDn = searchAccessIdByDn(name); |
70 | 71 | if (groupByDn != null) {
|
71 | 72 | accessIds.add(groupByDn);
|
72 | 73 | }
|
@@ -101,16 +102,12 @@ public List<AccessIdRepresentationModel> searchUsersByNameOrAccessId(final Strin
|
101 | 102 | orFilter.or(new WhitespaceWildcardsFilter(getUserIdAttribute(), name));
|
102 | 103 | andFilter.and(orFilter);
|
103 | 104 |
|
104 |
| - String[] userAttributesToReturn = { |
105 |
| - getUserFirstnameAttribute(), getUserLastnameAttribute(), getUserIdAttribute() |
106 |
| - }; |
107 |
| - |
108 | 105 | final List<AccessIdRepresentationModel> accessIds =
|
109 | 106 | ldapTemplate.search(
|
110 | 107 | getUserSearchBase(),
|
111 | 108 | andFilter.encode(),
|
112 | 109 | SearchControls.SUBTREE_SCOPE,
|
113 |
| - userAttributesToReturn, |
| 110 | + getLookUpUserAttributesToReturn(), |
114 | 111 | new UserContextMapper());
|
115 | 112 | LOGGER.debug(
|
116 | 113 | "exit from searchUsersByNameOrAccessId. Retrieved the following users: {}.", accessIds);
|
@@ -166,19 +163,19 @@ public List<AccessIdRepresentationModel> searchGroupsByName(final String name)
|
166 | 163 | return accessIds;
|
167 | 164 | }
|
168 | 165 |
|
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); |
171 | 168 | isInitOrFail();
|
172 | 169 | // Obviously Spring LdapTemplate does have a inconsistency and always adds the base name to the
|
173 | 170 | // given DN.
|
174 | 171 | // https://stackoverflow.com/questions/55285743/spring-ldaptemplate-how-to-lookup-fully-qualified-dn-with-configured-base-dn
|
175 | 172 | // 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); |
177 | 174 | LOGGER.debug(
|
178 | 175 | "Removed baseDN {} from given DN. New DN to be used: {}", getBaseDn(), nameWithoutBaseDn);
|
179 | 176 | final AccessIdRepresentationModel accessId =
|
180 | 177 | ldapTemplate.lookup(
|
181 |
| - nameWithoutBaseDn, getLookUpGroupAttributesToReturn(), new GroupContextMapper()); |
| 178 | + nameWithoutBaseDn, getLookUpUserAndGroupAttributesToReturn(), new DnContextMapper()); |
182 | 179 | LOGGER.debug("Exit from searchGroupByDn. Retrieved the following group: {}", accessId);
|
183 | 180 | return accessId;
|
184 | 181 | }
|
@@ -332,6 +329,19 @@ String[] getLookUpGroupAttributesToReturn() {
|
332 | 329 | return new String[] {getGroupNameAttribute(), CN};
|
333 | 330 | }
|
334 | 331 |
|
| 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 | + |
335 | 345 | @PostConstruct
|
336 | 346 | void init() {
|
337 | 347 | LOGGER.debug("Entry to init()");
|
@@ -403,4 +413,25 @@ public AccessIdRepresentationModel doMapFromContext(final DirContextOperations c
|
403 | 413 | return accessId;
|
404 | 414 | }
|
405 | 415 | }
|
| 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 | + } |
406 | 437 | }
|
0 commit comments