From ba3e3ef2c4f95886aaa3999ff061b370f49de879 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 25 Apr 2014 14:35:44 -0400
Subject: [PATCH] Updated TPS search filters.

The token, certificate, and activity database have been updated
to search against some predetermined attributes.

Ticket #920
---
 .../server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java | 6 +++---
 .../src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java        | 8 +++++++-
 .../src/org/dogtagpki/server/tps/dbs/TPSCertDatabase.java         | 8 +++++++-
 .../src/org/dogtagpki/server/tps/dbs/TokenDatabase.java           | 8 +++++++-
 4 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java b/base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java
index 4c3ac8dfb4f15bb57fbf63856886f46936f1ed83..650a65ea9a72bddbe50cbab734bad03b39873a19 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/dbs/LDAPDatabase.java
@@ -103,9 +103,9 @@ public abstract class LDAPDatabase<E extends IDBObj> extends Database<E> {
 
         try (IDBSSession session = dbSubsystem.createSession()) {
             Collection<E> list = new ArrayList<E>();
-            filter = createFilter(filter);
-            CMS.debug("LDAPDatabase: searching " + baseDN + " with filter " + filter);
-            IDBSearchResults results = session.search(baseDN, filter);
+            String ldapFilter = createFilter(filter);
+            CMS.debug("LDAPDatabase: searching " + baseDN + " with filter " + ldapFilter);
+            IDBSearchResults results = session.search(baseDN, ldapFilter);
 
             while (results.hasMoreElements()) {
                 @SuppressWarnings("unchecked")
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java
index 0222f3f00843719771c4aad1b2cc065e873d7d65..da884f0e94354f821e0807f9daed3980d6bc1558 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java
@@ -53,6 +53,12 @@ public class ActivityDatabase extends LDAPDatabase<ActivityRecord> {
 
     @Override
     public String createFilter(String filter) {
-        return StringUtils.isEmpty(filter) ? "(id=*)" : "(id=*"+LDAPUtil.escapeFilter(filter)+"*)";
+
+        if (StringUtils.isEmpty(filter)) {
+            return "(id=*)";
+        }
+
+        filter = LDAPUtil.escapeFilter(filter);
+        return "(|(tokenID=*" + filter + "*)(userID=*" + filter + "*))";
     }
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/TPSCertDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/TPSCertDatabase.java
index e12a41b69bbe24a521a279922a47ef5e08896309..7450b06654d32095eb55c8b6c7e255dc7492ee8f 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/TPSCertDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/TPSCertDatabase.java
@@ -60,6 +60,12 @@ public class TPSCertDatabase extends LDAPDatabase<TPSCertRecord> {
 
     @Override
     public String createFilter(String filter) {
-        return StringUtils.isEmpty(filter) ? "(id=*)" : "(id=*"+LDAPUtil.escapeFilter(filter)+"*)";
+
+        if (StringUtils.isEmpty(filter)) {
+            return "(id=*)";
+        }
+
+        filter = LDAPUtil.escapeFilter(filter);
+        return "(|(id=*" + filter + "*)(tokenID=*" + filter + "*)(userID=*" + filter + "*))";
     }
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/TokenDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/TokenDatabase.java
index f95e10bf7813dcd2878adc0ce5feb6946cec93fa..f86c6e203badb119549290ab3db8e8d9079fa944 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/TokenDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/TokenDatabase.java
@@ -57,6 +57,12 @@ public class TokenDatabase extends LDAPDatabase<TokenRecord> {
 
     @Override
     public String createFilter(String filter) {
-        return StringUtils.isEmpty(filter) ? "(id=*)" : "(id=*"+LDAPUtil.escapeFilter(filter)+"*)";
+
+        if (StringUtils.isEmpty(filter)) {
+            return "(id=*)";
+        }
+
+        filter = LDAPUtil.escapeFilter(filter);
+        return "(|(id=*" + filter + "*)(userID=*" + filter + "*))";
     }
 }
-- 
1.8.4.2

