You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2021/11/29 09:50:16 UTC

[syncope] branch master updated: [SYNCOPE-1654] Allowing to search realm by key or full path

This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/master by this push:
     new e6dc5e8  [SYNCOPE-1654] Allowing to search realm by key or full path
e6dc5e8 is described below

commit e6dc5e8d79b58d50a760c70f55f9c6e4ae0b7a8b
Author: Francesco Chicchiriccò <il...@apache.org>
AuthorDate: Mon Nov 29 10:38:45 2021 +0100

    [SYNCOPE-1654] Allowing to search realm by key or full path
---
 .../persistence/jpa/dao/PGJPAJSONAnySearchDAO.java     | 13 +++++++++++++
 .../core/persistence/jpa/dao/JPAAnySearchDAO.java      | 13 +++++++++++++
 .../core/persistence/jpa/inner/AnySearchTest.java      | 18 ++++++++++++++++++
 .../persistence/jpa/dao/ElasticsearchAnySearchDAO.java | 12 ++++++++++++
 .../java/org/apache/syncope/fit/core/SearchITCase.java | 12 ++++++++++++
 5 files changed, 68 insertions(+)

diff --git a/core/persistence-jpa-json/src/main/java/org/apache/syncope/core/persistence/jpa/dao/PGJPAJSONAnySearchDAO.java b/core/persistence-jpa-json/src/main/java/org/apache/syncope/core/persistence/jpa/dao/PGJPAJSONAnySearchDAO.java
index 4ca1803..514b436 100644
--- a/core/persistence-jpa-json/src/main/java/org/apache/syncope/core/persistence/jpa/dao/PGJPAJSONAnySearchDAO.java
+++ b/core/persistence-jpa-json/src/main/java/org/apache/syncope/core/persistence/jpa/dao/PGJPAJSONAnySearchDAO.java
@@ -28,8 +28,10 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.commons.lang3.tuple.Triple;
 import org.apache.syncope.common.lib.SyncopeClientException;
+import org.apache.syncope.common.lib.SyncopeConstants;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.AttrSchemaType;
+import org.apache.syncope.common.rest.api.service.JAXRSService;
 import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
 import org.apache.syncope.core.persistence.api.dao.DynRealmDAO;
 import org.apache.syncope.core.persistence.api.dao.GroupDAO;
@@ -605,6 +607,17 @@ public class PGJPAJSONAnySearchDAO extends JPAAnySearchDAO {
             final List<Object> parameters,
             final SearchSupport svs) {
 
+        if (JAXRSService.PARAM_REALM.equals(cond.getSchema())
+                && !SyncopeConstants.UUID_PATTERN.matcher(cond.getExpression()).matches()) {
+
+            Realm realm = realmDAO.findByFullPath(cond.getExpression());
+            if (realm == null) {
+                LOG.warn("Invalid Realm full path: {}", cond.getExpression());
+                return EMPTY_QUERY;
+            }
+            cond.setExpression(realm.getKey());
+        }
+
         Triple<PlainSchema, PlainAttrValue, AnyCond> checked;
         try {
             checked = check(cond, svs.anyTypeKind);
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
index 2ae39ae..7b04807 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
@@ -32,9 +32,11 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.commons.lang3.tuple.Triple;
 import org.apache.syncope.common.lib.SyncopeClientException;
+import org.apache.syncope.common.lib.SyncopeConstants;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.AttrSchemaType;
 import org.apache.syncope.common.lib.types.ClientExceptionType;
+import org.apache.syncope.common.rest.api.service.JAXRSService;
 import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
 import org.apache.syncope.core.persistence.api.dao.DynRealmDAO;
 import org.apache.syncope.core.persistence.api.dao.GroupDAO;
@@ -1111,6 +1113,17 @@ public class JPAAnySearchDAO extends AbstractAnySearchDAO {
             final List<Object> parameters,
             final SearchSupport svs) {
 
+        if (JAXRSService.PARAM_REALM.equals(cond.getSchema())
+                && !SyncopeConstants.UUID_PATTERN.matcher(cond.getExpression()).matches()) {
+
+            Realm realm = realmDAO.findByFullPath(cond.getExpression());
+            if (realm == null) {
+                LOG.warn("Invalid Realm full path: {}", cond.getExpression());
+                return EMPTY_QUERY;
+            }
+            cond.setExpression(realm.getKey());
+        }
+
         Triple<PlainSchema, PlainAttrValue, AnyCond> checked;
         try {
             checked = check(cond, svs.anyTypeKind);
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AnySearchTest.java b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AnySearchTest.java
index a067afe..963177c 100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AnySearchTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AnySearchTest.java
@@ -235,6 +235,24 @@ public class AnySearchTest extends AbstractTest {
     }
 
     @Test
+    public void searchByRealm() {
+        AnyCond anyCond = new AnyCond(AttrCond.Type.EQ);
+        anyCond.setSchema("realm");
+        anyCond.setExpression("c5b75db1-fce7-470f-b780-3b9934d82a9d");
+
+        List<User> users = searchDAO.search(SearchCond.getLeaf(anyCond), AnyTypeKind.USER);
+        assertNotNull(users);
+        assertEquals(1, users.size());
+        assertEquals("rossini", users.get(0).getUsername());
+
+        anyCond.setExpression("/even");
+        users = searchDAO.search(SearchCond.getLeaf(anyCond), AnyTypeKind.USER);
+        assertNotNull(users);
+        assertEquals(1, users.size());
+        assertEquals("rossini", users.get(0).getUsername());
+    }
+
+    @Test
     public void searchByPageAndSize() {
         AttrCond fullnameLeafCond = new AttrCond(AttrCond.Type.LIKE);
         fullnameLeafCond.setSchema("fullname");
diff --git a/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java b/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java
index 7b33775..c2aaa9b 100644
--- a/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java
+++ b/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java
@@ -35,6 +35,7 @@ import org.apache.syncope.common.lib.SyncopeConstants;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
 import org.apache.syncope.common.lib.types.AttrSchemaType;
 import org.apache.syncope.common.lib.types.ClientExceptionType;
+import org.apache.syncope.common.rest.api.service.JAXRSService;
 import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
 import org.apache.syncope.core.persistence.api.dao.DynRealmDAO;
 import org.apache.syncope.core.persistence.api.dao.GroupDAO;
@@ -547,6 +548,17 @@ public class ElasticsearchAnySearchDAO extends AbstractAnySearchDAO {
     }
 
     protected QueryBuilder getQueryBuilder(final AnyCond cond, final AnyTypeKind kind) {
+        if (JAXRSService.PARAM_REALM.equals(cond.getSchema())
+                && SyncopeConstants.UUID_PATTERN.matcher(cond.getExpression()).matches()) {
+
+            Realm realm = realmDAO.find(cond.getExpression());
+            if (realm == null) {
+                LOG.warn("Invalid Realm key: {}", cond.getExpression());
+                return MATCH_NONE_QUERY_BUILDER;
+            }
+            cond.setExpression(realm.getFullPath());
+        }
+
         Triple<PlainSchema, PlainAttrValue, AnyCond> checked;
         try {
             checked = check(cond, kind);
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java
index 073104f..c9f0e79 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/SearchITCase.java
@@ -287,6 +287,18 @@ public class SearchITCase extends AbstractITCase {
     }
 
     @Test
+    public void searchByRealm() {
+        PagedResult<UserTO> users = userService.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).
+                fiql(SyncopeClient.getUserSearchConditionBuilder().is("realm").
+                        equalTo("c5b75db1-fce7-470f-b780-3b9934d82a9d").query()).build());
+        assertTrue(users.getResult().stream().anyMatch(user -> "rossini".equals(user.getUsername())));
+
+        users = userService.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).
+                fiql(SyncopeClient.getUserSearchConditionBuilder().is("realm").equalTo("/even").query()).build());
+        assertTrue(users.getResult().stream().anyMatch(user -> "rossini".equals(user.getUsername())));
+    }
+
+    @Test
     public void searchByBooleanAnyCond() {
         PagedResult<GroupTO> groups = groupService.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).
                 fiql(SyncopeClient.getGroupSearchConditionBuilder().is("show").equalTo("true").query()).build());