Skip to content

Commit

Permalink
UBO-263 FSU040THUL-405 Allow to search arbitrary realm in LocalService
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi authored and kkrebs committed Sep 4, 2023
1 parent aaa840b commit 8ade2e3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package org.mycore.ubo.ldap.picker;

import java.util.Optional;

import javax.naming.OperationNotSupportedException;

import org.mycore.common.config.MCRConfiguration2;
import org.mycore.ubo.local.LocalService;
import org.mycore.ubo.picker.PersonSearchResult;
import org.mycore.user2.MCRRealmFactory;

public class LDAPWithLocalService extends LDAPService {

public static final String LDAP_REALM = MCRConfiguration2.getString(
"MCR.user2.IdentityManagement.UserCreation.LDAP.Realm").orElse(null);

@Override
public PersonSearchResult searchPerson(String query) throws OperationNotSupportedException {
PersonSearchResult results = super.searchPerson(query);
LocalService localService = new LocalService();
PersonSearchResult personSearchResult = localService.searchPerson(query);
results.join(personSearchResult, 0);

if (LDAP_REALM != null) {
Optional.ofNullable(MCRRealmFactory.getRealm(LDAP_REALM))
.ifPresent(realm -> results.join(localService.searchPerson(query, realm), 0));
}

return results;
}
}
64 changes: 35 additions & 29 deletions ubo-common/src/main/java/org/mycore/ubo/local/LocalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.mycore.common.config.MCRConfiguration2;
import org.mycore.ubo.picker.IdentityService;
import org.mycore.ubo.picker.PersonSearchResult;
import org.mycore.user2.MCRRealm;
import org.mycore.user2.MCRRealmFactory;
import org.mycore.user2.MCRUser;
import org.mycore.user2.MCRUserAttribute;
import org.mycore.user2.MCRUserManager;
Expand Down Expand Up @@ -40,38 +42,42 @@ public Element searchPerson(Map<String, String> paramMap) {

@Override
public PersonSearchResult searchPerson(String query) throws OperationNotSupportedException {
return searchPerson(query, MCRRealmFactory.getLocalRealm());
}

public PersonSearchResult searchPerson(String query, MCRRealm realm) {
String displayName = "*" + query.trim() + "*";
final List<MCRUser> matchingUsers = MCRUserManager.listUsers(null, "local", displayName, null);
final List<MCRUser> matchingUsers = MCRUserManager.listUsers(null, realm.getID(), displayName, null);

List<PersonSearchResult.PersonResult> personResults = matchingUsers.stream().map(user -> {
PersonSearchResult.PersonResult personSearchResult = new PersonSearchResult.PersonResult(this);
personSearchResult.pid = user.getUserAttribute("id_" + LEAD_ID);
personSearchResult.displayName = user.getRealName().length() > 0 ? user.getRealName() : user.getUserName();

user.getAttributes().stream()
.filter(attr -> attr.getName().equals(USER_FIRST_NAME_ATTR))
.findFirst()
.map(MCRUserAttribute::getValue)
.ifPresent(attrVal -> {
personSearchResult.firstName = attrVal;
});

user.getAttributes().stream()
.filter(attr -> attr.getName().equals(USER_LAST_NAME_ATTR))
.findFirst()
.map(MCRUserAttribute::getValue)
.ifPresent(attrVal -> {
personSearchResult.lastName = attrVal;
});

if (user.getEMailAddress() != null) {
personSearchResult.information = new ArrayList<>();
personSearchResult.information.add(user.getEMailAddress());
}

return personSearchResult;
}).filter(psr -> (psr.pid != null && !psr.pid.isEmpty()) || !ENFORCE_LEAD_ID_PRESENT)
.collect(Collectors.toList());
PersonSearchResult.PersonResult personSearchResult = new PersonSearchResult.PersonResult(this);
personSearchResult.pid = user.getUserAttribute("id_" + LEAD_ID);
personSearchResult.displayName = user.getRealName().length() > 0 ? user.getRealName() : user.getUserName();

user.getAttributes().stream()
.filter(attr -> attr.getName().equals(USER_FIRST_NAME_ATTR))
.findFirst()
.map(MCRUserAttribute::getValue)
.ifPresent(attrVal -> {
personSearchResult.firstName = attrVal;
});

user.getAttributes().stream()
.filter(attr -> attr.getName().equals(USER_LAST_NAME_ATTR))
.findFirst()
.map(MCRUserAttribute::getValue)
.ifPresent(attrVal -> {
personSearchResult.lastName = attrVal;
});

if (user.getEMailAddress() != null) {
personSearchResult.information = new ArrayList<>();
personSearchResult.information.add(user.getEMailAddress());
}

return personSearchResult;
}).filter(psr -> (psr.pid != null && !psr.pid.isEmpty()) || !ENFORCE_LEAD_ID_PRESENT)
.collect(Collectors.toList());

PersonSearchResult personSearchResult = new PersonSearchResult();
personSearchResult.count = personResults.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.mycore.ubo.picker;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PersonSearchResult {

Expand All @@ -28,7 +30,7 @@ public class PersonSearchResult {

public static class PersonResult {

public PersonResult(IdentityService identityService){
public PersonResult(IdentityService identityService) {
this.service = identityService.getClass().getSimpleName();
}

Expand Down Expand Up @@ -70,7 +72,8 @@ public PersonResult(IdentityService identityService){

/**
* Joins another {@link PersonSearchResult} with the current {@link PersonSearchResult}. The count field is updated
* automatically.
* automatically. If an item in the given {@link PersonSearchResult} is already contained (test is based on
* {@link PersonResult#pid}) it will not be added again.
*
* @param other the {@link PersonSearchResult} to join.
* */
Expand All @@ -80,14 +83,21 @@ public void join(PersonSearchResult other) {

/**
* Joins another {@link PersonSearchResult} with the current {@link PersonSearchResult}. The count field is updated
* automatically.
* automatically. If an item in the given {@link PersonSearchResult} is already contained (test is based on
* {@link PersonResult#pid}) it will not be added again.
*
* @param other the {@link PersonSearchResult} to join.
* @param index index at which to insert the first element from the specified {@link PersonSearchResult}
* */
public void join(PersonSearchResult other, int index) {
this.personList.addAll(index, other.personList);
this.count += other.count;
Stream<PersonResult> currentPersonList = this.personList.stream();

List<PersonResult> toAdd = other.personList.stream()
.filter(pr -> pr.pid == null || !currentPersonList.anyMatch(alreadyInList -> alreadyInList.pid == pr.pid))
.collect(Collectors.toList());

this.personList.addAll(index, toAdd);
this.count += toAdd.size();
}

}
Expand Down

0 comments on commit 8ade2e3

Please sign in to comment.