From 93102d15e64189fab30259da84489584936e96a8 Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Wed, 3 Apr 2019 11:39:00 +0200
Subject: [PATCH] OO-4008: add search by primary key for admins. in user
 management

---
 .../admin/user/UsermanagerUserSearchForm.java |  9 ++++++++
 .../user/_i18n/LocalStrings_de.properties     |  2 +-
 .../user/_i18n/LocalStrings_en.properties     |  1 +
 .../basesecurity/SearchIdentityParams.java    |  9 ++++++++
 .../IdentityPowerSearchQueriesImpl.java       | 23 ++++++++++++++++++-
 5 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/olat/admin/user/UsermanagerUserSearchForm.java b/src/main/java/org/olat/admin/user/UsermanagerUserSearchForm.java
index 2cd283e4133..ee96926ca6e 100644
--- a/src/main/java/org/olat/admin/user/UsermanagerUserSearchForm.java
+++ b/src/main/java/org/olat/admin/user/UsermanagerUserSearchForm.java
@@ -52,6 +52,7 @@ import org.olat.core.gui.control.Event;
 import org.olat.core.gui.control.WindowControl;
 import org.olat.core.id.Identity;
 import org.olat.core.id.Organisation;
+import org.olat.core.id.Roles;
 import org.olat.core.id.context.StateMapped;
 import org.olat.core.util.StringHelper;
 import org.olat.login.LoginModule;
@@ -185,6 +186,7 @@ public class UsermanagerUserSearchForm extends FormBasicController {
 	 */
 	public SearchIdentityParams getSearchIdentityParams() {
 		// get user attributes from form
+		String idVal = getStringValue("id");
 		String loginVal = getStringValue("login");
 		// when searching for deleted users, add wildcard to match with backup prefix
 		List<Integer> statusList = getStatus();
@@ -223,6 +225,7 @@ public class UsermanagerUserSearchForm extends FormBasicController {
 				getUserLoginAfter(), getUserLoginBefore(), null);
 		params.setOrganisations(getOrganisations());
 		params.setExactStatusList(statusList);
+		params.setIdAndExternalIds(idVal);
 		return params;
 	}
 
@@ -343,6 +346,12 @@ public class UsermanagerUserSearchForm extends FormBasicController {
 	@Override
 	protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
 		formLayout.setElementCssClass("o_sel_user_search_form");
+		
+		Roles uroles = ureq.getUserSession().getRoles();
+		TextElement id = uifactory.addTextElement("id", "search.form.id", 128, "", formLayout);
+		id.setVisible(isAdministrativeUser || uroles.isAdministrator() || uroles.isSystemAdmin());
+		id.setElementCssClass("o_sel_user_search_id");
+		items.put("id", id);
 	
 		TextElement login = uifactory.addTextElement("login", "search.form.login", 128, "", formLayout);
 		login.setVisible(isAdministrativeUser);
diff --git a/src/main/java/org/olat/admin/user/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/admin/user/_i18n/LocalStrings_de.properties
index 9ab66c57edb..f415c16cd52 100644
--- a/src/main/java/org/olat/admin/user/_i18n/LocalStrings_de.properties
+++ b/src/main/java/org/olat/admin/user/_i18n/LocalStrings_de.properties
@@ -152,7 +152,7 @@ search.form.constraint.sysadmin=$\:role.sysadmin
 search.form.constraint.user=$\:role.user
 search.form.constraint.usermanager=$\:role.usermanager
 
-
+search.form.id=ID
 search.form.login=Benutzername
 search.form.title.authentications=Einschr\u00E4nkung auf Authentifizierung
 search.form.title.date=Einschr\u00E4nkung auf Erstellungsdatum
diff --git a/src/main/java/org/olat/admin/user/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/admin/user/_i18n/LocalStrings_en.properties
index ab17ef46aca..95b5b663ea4 100644
--- a/src/main/java/org/olat/admin/user/_i18n/LocalStrings_en.properties
+++ b/src/main/java/org/olat/admin/user/_i18n/LocalStrings_en.properties
@@ -150,6 +150,7 @@ search.form.constraint.rolesmanager=$\:role.rolesmanager
 search.form.constraint.sysadmin=$\:role.sysadmin
 search.form.constraint.user=$\:role.user
 search.form.constraint.usermanager=User manager
+search.form.id=ID
 search.form.login=User name
 search.form.title.authentications=Authentication restrictions
 search.form.title.date=Creation date restrictions
diff --git a/src/main/java/org/olat/basesecurity/SearchIdentityParams.java b/src/main/java/org/olat/basesecurity/SearchIdentityParams.java
index 87c805c35fc..3cd188b46a8 100644
--- a/src/main/java/org/olat/basesecurity/SearchIdentityParams.java
+++ b/src/main/java/org/olat/basesecurity/SearchIdentityParams.java
@@ -35,6 +35,7 @@ import org.olat.core.id.OrganisationRef;
  * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
  */
 public class SearchIdentityParams {
+	private String idAndExternalIds;
 	private String login;
 	private Map<String, String> userProperties;
 	private boolean userPropertiesAsIntersectionSearch;
@@ -301,6 +302,14 @@ public class SearchIdentityParams {
 		this.status = status;
 	}
 	
+	public String getIdAndExternalIds() {
+		return idAndExternalIds;
+	}
+
+	public void setIdAndExternalIds(String idAndExternalIds) {
+		this.idAndExternalIds = idAndExternalIds;
+	}
+
 	public List<Integer> getExactStatusList() {
 		return exactStatusList;
 	}
diff --git a/src/main/java/org/olat/basesecurity/manager/IdentityPowerSearchQueriesImpl.java b/src/main/java/org/olat/basesecurity/manager/IdentityPowerSearchQueriesImpl.java
index 1c495a32335..7db640ad02f 100644
--- a/src/main/java/org/olat/basesecurity/manager/IdentityPowerSearchQueriesImpl.java
+++ b/src/main/java/org/olat/basesecurity/manager/IdentityPowerSearchQueriesImpl.java
@@ -41,6 +41,7 @@ import org.olat.core.commons.persistence.DB;
 import org.olat.core.commons.persistence.SortKey;
 import org.olat.core.id.Identity;
 import org.olat.core.id.OrganisationRef;
+import org.olat.core.util.StringHelper;
 import org.olat.user.propertyhandlers.UserPropertyHandler;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -161,7 +162,8 @@ public class IdentityPowerSearchQueriesImpl implements IdentityPowerSearchQuerie
 				|| params.getStatus() != null || (params.getExactStatusList() != null && !params.getExactStatusList().isEmpty())
 				|| params.hasRoles() || params.hasExcludedRoles() || params.isAuthorAndCoAuthor()
 				|| params.getRepositoryEntryRole() != null || params.getBusinessGroupRole() != null
-				|| params.hasOrganisations() || params.hasOrganisationParents();
+				|| params.hasOrganisations() || params.hasOrganisationParents()
+				|| StringHelper.containsNonWhitespace(params.getIdAndExternalIds());
 	}
 	
 	private boolean createQueryPart(SearchIdentityParams params, StringBuilder sb, boolean needsAnd) {	
@@ -243,6 +245,15 @@ public class IdentityPowerSearchQueriesImpl implements IdentityPowerSearchQuerie
 			sb.append("ident.key in (:identityKeys)");
 		}
 		
+		if(params.getIdAndExternalIds() != null) {
+			needsAnd = checkAnd(sb, needsAnd);
+			sb.append("(");
+			if(StringHelper.isLong(params.getIdAndExternalIds())) {
+				sb.append("ident.key=:idKey or user.key=:idKey or ");
+			}
+			sb.append("ident.externalId=:idAndRefs)");
+		}
+		
 		if(params.getManaged() != null) {
 			needsAnd = checkAnd(sb, needsAnd);
 			if(params.getManaged().booleanValue()) {
@@ -531,6 +542,16 @@ public class IdentityPowerSearchQueriesImpl implements IdentityPowerSearchQuerie
 		} else if (params.getStatus() != null) {
 			dbq.setParameter("status", params.getStatus());
 		}
+		
+		if(params.getIdAndExternalIds() != null) {
+		}
+		
+		if(params.getIdAndExternalIds() != null) {
+			if(StringHelper.isLong(params.getIdAndExternalIds())) {
+				dbq.setParameter("idKey", Long.valueOf(params.getIdAndExternalIds()));
+			}
+			dbq.setParameter("idAndRefs", params.getIdAndExternalIds());
+		}
 	}
 	
 	private boolean checkAnd(StringBuilder sb, boolean needsAnd) {
-- 
GitLab