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
commit cb72af53771e8fd909e8adfb14fc9b415916bc27 Author: Francesco Chicchiriccò <[email protected]> AuthorDate: Fri Jan 31 16:58:29 2020 +0100 [SYNCOPE-1538] Introducing realm search --- .../syncope/client/console/pages/Realms.java | 1 + .../client/console/rest/RealmRestClient.java | 3 +- .../client/enduser/rest/RealmRestClient.java | 4 +- .../syncope/common/rest/api/beans/RealmQuery.java | 80 ++++++++++++++++++++++ .../common/rest/api/service/RealmService.java | 10 ++- .../org/apache/syncope/core/logic/RealmLogic.java | 18 ++++- .../core/rest/cxf/service/RealmServiceImpl.java | 6 +- .../syncope/core/persistence/api/dao/RealmDAO.java | 2 + .../core/persistence/jpa/dao/JPARealmDAO.java | 28 +++++--- .../java/data/RealmDataBinderImpl.java | 4 +- .../syncope/fit/core/MultitenancyITCase.java | 7 +- .../org/apache/syncope/fit/core/RealmITCase.java | 11 ++- .../org/apache/syncope/fit/core/UserITCase.java | 3 +- .../apache/syncope/fit/core/UserIssuesITCase.java | 7 +- 14 files changed, 154 insertions(+), 30 deletions(-) diff --git a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/pages/Realms.java b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/pages/Realms.java index 48de67d..034b9a2 100644 --- a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/pages/Realms.java +++ b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/pages/Realms.java @@ -72,6 +72,7 @@ public class Realms extends BasePage { @Override protected Serializable onApplyInternal( final TemplatableTO targetObject, final String type, final AnyTO anyTO) { + targetObject.getTemplates().put(type, anyTO); RealmRestClient.update(RealmTO.class.cast(targetObject)); return targetObject; diff --git a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/rest/RealmRestClient.java b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/rest/RealmRestClient.java index 54ae734..486d0ba 100644 --- a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/rest/RealmRestClient.java +++ b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/rest/RealmRestClient.java @@ -21,6 +21,7 @@ package org.apache.syncope.client.console.rest; import java.util.List; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.Response; +import org.apache.syncope.common.lib.SyncopeConstants; import org.apache.syncope.common.lib.to.DynRealmTO; import org.apache.syncope.common.lib.to.ProvisioningResult; import org.apache.syncope.common.lib.to.RealmTO; @@ -35,7 +36,7 @@ public class RealmRestClient extends BaseRestClient { private static final long serialVersionUID = -8549081557283519638L; public static List<RealmTO> list() { - return getService(RealmService.class).list(); + return getService(RealmService.class).list(SyncopeConstants.ROOT_REALM); } public static List<DynRealmTO> listDynReams() { diff --git a/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/rest/RealmRestClient.java b/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/rest/RealmRestClient.java index d7b5729..def9fd0 100644 --- a/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/rest/RealmRestClient.java +++ b/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/rest/RealmRestClient.java @@ -19,6 +19,7 @@ package org.apache.syncope.client.enduser.rest; import java.util.List; +import org.apache.syncope.common.lib.SyncopeConstants; import org.apache.syncope.common.lib.to.RealmTO; import org.apache.syncope.common.rest.api.service.RealmService; @@ -30,7 +31,6 @@ public class RealmRestClient extends BaseRestClient { private static final long serialVersionUID = -8549081557283519638L; public static List<RealmTO> list() { - return getService(RealmService.class).list(); + return getService(RealmService.class).list(SyncopeConstants.ROOT_REALM); } - } diff --git a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/beans/RealmQuery.java b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/beans/RealmQuery.java new file mode 100644 index 0000000..f32d6d7 --- /dev/null +++ b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/beans/RealmQuery.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.common.rest.api.beans; + +import java.io.Serializable; +import javax.ws.rs.QueryParam; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; + +public class RealmQuery implements Serializable { + + private static final long serialVersionUID = -2278419397595186866L; + + public static class Builder { + + private final RealmQuery instance = new RealmQuery(); + + public Builder keyword(final String keyword) { + instance.setKeyword(keyword); + return this; + } + + public RealmQuery build() { + return instance; + } + } + + private String keyword; + + public String getKeyword() { + return keyword; + } + + @QueryParam("keyword") + public void setKeyword(final String keyword) { + this.keyword = keyword; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + RealmQuery other = (RealmQuery) obj; + return new EqualsBuilder(). + appendSuper(super.equals(obj)). + append(keyword, other.keyword). + build(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(). + appendSuper(super.hashCode()). + append(keyword). + build(); + } +} diff --git a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/RealmService.java b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/RealmService.java index c7617a9..0790b7c 100644 --- a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/RealmService.java +++ b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/RealmService.java @@ -30,6 +30,7 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirements; import io.swagger.v3.oas.annotations.tags.Tag; import java.util.List; import javax.validation.constraints.NotNull; +import javax.ws.rs.BeanParam; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -44,6 +45,7 @@ import javax.ws.rs.core.Response; import org.apache.syncope.common.lib.to.ProvisioningResult; import org.apache.syncope.common.lib.to.RealmTO; import org.apache.syncope.common.rest.api.RESTHeaders; +import org.apache.syncope.common.rest.api.beans.RealmQuery; /** * REST operations for realms. @@ -56,13 +58,15 @@ import org.apache.syncope.common.rest.api.RESTHeaders; public interface RealmService extends JAXRSService { /** - * Returns a list of all realms. + * Returns a paged list of existing realms matching the given query. * - * @return list of all realms. + * @param query query conditions + * @return paged list of existing realms matching the given query */ @GET + @Path("search") @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - List<RealmTO> list(); + List<RealmTO> search(@BeanParam RealmQuery query); /** * Returns realms rooted at the given path. diff --git a/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java b/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java index 4a55cde..a1fd376 100644 --- a/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java +++ b/core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java @@ -19,6 +19,7 @@ package org.apache.syncope.core.logic; import java.lang.reflect.Method; +import java.util.Comparator; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -69,6 +70,20 @@ public class RealmLogic extends AbstractTransactionalLogic<RealmTO> { @Autowired private PropagationTaskExecutor taskExecutor; + @PreAuthorize("hasRole('" + IdRepoEntitlement.REALM_LIST + "')") + @Transactional(readOnly = true) + public List<RealmTO> search(final String keyword) { + Set<String> bases = AuthContextUtils.getAuthorizations().get(IdRepoEntitlement.REALM_LIST); + + return realmDAO.findMatching(keyword).stream(). + filter(realm -> bases.stream().anyMatch(base -> realm.getFullPath().startsWith(base))). + flatMap(realm -> realmDAO.findDescendants(realm).stream()). + distinct(). + map(realm -> binder.getRealmTO(realm, true)). + sorted(Comparator.comparing(RealmTO::getFullPath)). + collect(Collectors.toList()); + } + @PreAuthorize("isAuthenticated()") @Transactional(readOnly = true) public List<RealmTO> list(final String fullPath) { @@ -79,7 +94,7 @@ public class RealmLogic extends AbstractTransactionalLogic<RealmTO> { throw new NotFoundException(fullPath); } - final boolean admin = AuthContextUtils.getAuthorizations().keySet().contains(IdRepoEntitlement.REALM_LIST); + boolean admin = AuthContextUtils.getAuthorizations().keySet().contains(IdRepoEntitlement.REALM_LIST); return realmDAO.findDescendants(realm).stream(). map(descendant -> binder.getRealmTO(descendant, admin)).collect(Collectors.toList()); } @@ -222,5 +237,4 @@ public class RealmLogic extends AbstractTransactionalLogic<RealmTO> { throw new UnresolvedReferenceException(); } - } diff --git a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/RealmServiceImpl.java b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/RealmServiceImpl.java index e937278..5767ad2 100644 --- a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/RealmServiceImpl.java +++ b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/RealmServiceImpl.java @@ -26,6 +26,7 @@ import org.apache.syncope.common.lib.SyncopeConstants; import org.apache.syncope.common.lib.to.ProvisioningResult; import org.apache.syncope.common.lib.to.RealmTO; import org.apache.syncope.common.rest.api.RESTHeaders; +import org.apache.syncope.common.rest.api.beans.RealmQuery; import org.apache.syncope.common.rest.api.service.RealmService; import org.apache.syncope.core.logic.RealmLogic; import org.springframework.beans.factory.annotation.Autowired; @@ -38,8 +39,9 @@ public class RealmServiceImpl extends AbstractServiceImpl implements RealmServic private RealmLogic logic; @Override - public List<RealmTO> list() { - return list(StringUtils.EMPTY); + public List<RealmTO> search(final RealmQuery query) { + String keyword = query.getKeyword() == null ? null : query.getKeyword().replace('*', '%'); + return logic.search(keyword); } @Override diff --git a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/RealmDAO.java b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/RealmDAO.java index 800b493..9daabb1 100644 --- a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/RealmDAO.java +++ b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/dao/RealmDAO.java @@ -41,6 +41,8 @@ public interface RealmDAO extends DAO<Realm> { List<Realm> findByResource(ExternalResource resource); + List<Realm> findMatching(String keyword); + <T extends Policy> List<Realm> findByPolicy(T policy); List<Realm> findByLogicActions(Implementation logicActions); diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java index 6860f96..f8a0020 100644 --- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java +++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPARealmDAO.java @@ -102,16 +102,15 @@ public class JPARealmDAO extends AbstractDAO<Realm> implements RealmDAO { private <T extends Policy> List<Realm> findSamePolicyChildren(final Realm realm, final T policy) { List<Realm> result = new ArrayList<>(); - for (Realm child : findChildren(realm)) { - if ((policy instanceof AccountPolicy - && child.getAccountPolicy() == null || policy.equals(child.getAccountPolicy())) - || (policy instanceof PasswordPolicy - && child.getPasswordPolicy() == null || policy.equals(child.getPasswordPolicy()))) { - - result.add(child); - result.addAll(findSamePolicyChildren(child, policy)); - } - } + findChildren(realm).stream(). + filter(child -> (policy instanceof AccountPolicy + && child.getAccountPolicy() == null || policy.equals(child.getAccountPolicy())) + || (policy instanceof PasswordPolicy + && child.getPasswordPolicy() == null || policy.equals(child.getPasswordPolicy()))). + forEach(child -> { + result.add(child); + result.addAll(findSamePolicyChildren(child, policy)); + }); return result; } @@ -135,6 +134,15 @@ public class JPARealmDAO extends AbstractDAO<Realm> implements RealmDAO { } @Override + public List<Realm> findMatching(final String keyword) { + TypedQuery<Realm> query = entityManager().createQuery("SELECT e FROM " + JPARealm.class.getSimpleName() + " e " + + "WHERE e.name LIKE :keyword", Realm.class); + query.setParameter("keyword", keyword); + + return query.getResultList(); + } + + @Override public <T extends Policy> List<Realm> findByPolicy(final T policy) { if (ProvisioningPolicy.class.isAssignableFrom(policy.getClass())) { return Collections.<Realm>emptyList(); diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java index 583bc13..236027c 100644 --- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java +++ b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java @@ -231,8 +231,8 @@ public class RealmDataBinderImpl implements RealmDataBinder { realm.getActions().forEach(action -> realmTO.getActions().add(action.getKey())); - realm.getTemplates(). - forEach(template -> realmTO.getTemplates().put(template.getAnyType().getKey(), template.get())); + realm.getTemplates().forEach( + template -> realmTO.getTemplates().put(template.getAnyType().getKey(), template.get())); realm.getResources().forEach(resource -> realmTO.getResources().add(resource.getKey())); } diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/MultitenancyITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/MultitenancyITCase.java index 9181c1b..f5f16e0 100644 --- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/MultitenancyITCase.java +++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/MultitenancyITCase.java @@ -58,6 +58,7 @@ import org.apache.syncope.common.lib.types.PullMode; import org.apache.syncope.common.lib.types.SchemaType; import org.apache.syncope.common.lib.types.TaskType; import org.apache.syncope.common.rest.api.beans.AnyQuery; +import org.apache.syncope.common.rest.api.beans.RealmQuery; import org.apache.syncope.common.rest.api.beans.ReconQuery; import org.apache.syncope.common.rest.api.beans.SchemaQuery; import org.apache.syncope.common.rest.api.beans.TaskQuery; @@ -113,14 +114,16 @@ public class MultitenancyITCase extends AbstractITCase { @Test public void readRealm() { - List<RealmTO> realms = adminClient.getService(RealmService.class).list(); + List<RealmTO> realms = adminClient.getService(RealmService.class). + search(new RealmQuery.Builder().keyword("*").build()); assertEquals(1, realms.size()); assertEquals(SyncopeConstants.ROOT_REALM, realms.get(0).getName()); } @Test public void createUser() { - assertNull(adminClient.getService(RealmService.class).list().get(0).getPasswordPolicy()); + assertNull(adminClient.getService(RealmService.class). + search(new RealmQuery.Builder().keyword("*").build()).get(0).getPasswordPolicy()); UserCR userCR = new UserCR(); userCR.setRealm(SyncopeConstants.ROOT_REALM); diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java index 134e0bb..bc86a1b 100644 --- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java +++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java @@ -43,6 +43,7 @@ import org.apache.syncope.common.lib.types.PolicyType; import org.apache.syncope.common.lib.types.ExecStatus; import org.apache.syncope.common.lib.types.IdRepoImplementationType; import org.apache.syncope.common.rest.api.RESTHeaders; +import org.apache.syncope.common.rest.api.beans.RealmQuery; import org.apache.syncope.common.rest.api.service.RealmService; import org.apache.syncope.core.provisioning.api.serialization.POJOHelper; import org.apache.syncope.fit.AbstractITCase; @@ -56,8 +57,14 @@ public class RealmITCase extends AbstractITCase { } @Test + public void search() { + List<RealmTO> match = realmService.search(new RealmQuery.Builder().keyword("*o*").build()); + assertTrue(match.stream().allMatch(realm -> realm.getName().contains("o"))); + } + + @Test public void list() { - List<RealmTO> realms = realmService.list(); + List<RealmTO> realms = realmService.list(SyncopeConstants.ROOT_REALM); assertNotNull(realms); assertFalse(realms.isEmpty()); realms.forEach(Assertions::assertNotNull); @@ -105,7 +112,7 @@ public class RealmITCase extends AbstractITCase { assertNotNull(actual); assertEquals("/odd/last", actual.getFullPath()); - assertEquals(1, realmService.list().stream(). + assertEquals(1, realmService.list(SyncopeConstants.ROOT_REALM).stream(). filter(object -> realm.getName().equals(object.getName())).count()); // 4. create under invalid path diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserITCase.java index f0a2e61..f93a363 100644 --- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserITCase.java +++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserITCase.java @@ -85,6 +85,7 @@ import org.apache.syncope.common.lib.types.TaskType; import org.apache.syncope.common.rest.api.RESTHeaders; import org.apache.syncope.common.rest.api.batch.BatchResponseItem; import org.apache.syncope.common.rest.api.beans.AnyQuery; +import org.apache.syncope.common.rest.api.beans.RealmQuery; import org.apache.syncope.common.rest.api.beans.TaskQuery; import org.apache.syncope.common.rest.api.service.ResourceService; import org.apache.syncope.common.rest.api.service.UserSelfService; @@ -962,7 +963,7 @@ public class UserITCase extends AbstractITCase { passwordPolicy = createPolicy(PolicyType.PASSWORD, passwordPolicy); assertNotNull(passwordPolicy); - RealmTO realm = realmService.list("/even/two").get(0); + RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).get(0); String oldAccountPolicy = realm.getAccountPolicy(); realm.setAccountPolicy(accountPolicy.getKey()); String oldPasswordPolicy = realm.getPasswordPolicy(); diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserIssuesITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserIssuesITCase.java index 07125d8..a24107c 100644 --- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserIssuesITCase.java +++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/UserIssuesITCase.java @@ -75,6 +75,7 @@ import org.apache.syncope.common.lib.types.ExecStatus; import org.apache.syncope.common.lib.types.IdMImplementationType; import org.apache.syncope.common.lib.types.IdRepoImplementationType; import org.apache.syncope.common.rest.api.RESTHeaders; +import org.apache.syncope.common.rest.api.beans.RealmQuery; import org.apache.syncope.core.provisioning.api.serialization.POJOHelper; import org.apache.syncope.core.provisioning.java.propagation.DBPasswordPropagationActions; import org.apache.syncope.core.provisioning.java.propagation.LDAPPasswordPropagationActions; @@ -792,7 +793,7 @@ public class UserIssuesITCase extends AbstractITCase { } assertNotNull(logicActions); - RealmTO realm = realmService.list("/even/two").get(0); + RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).get(0); assertNotNull(realm); realm.getActions().add(logicActions.getKey()); realmService.update(realm); @@ -1222,7 +1223,7 @@ public class UserIssuesITCase extends AbstractITCase { passwordPolicy = createPolicy(PolicyType.PASSWORD, passwordPolicy); assertNotNull(passwordPolicy); - RealmTO realm = realmService.list("/even/two").get(0); + RealmTO realm = realmService.search(new RealmQuery.Builder().keyword("two").build()).get(0); String oldPasswordPolicy = realm.getPasswordPolicy(); realm.setPasswordPolicy(passwordPolicy.getKey()); realmService.update(realm); @@ -1548,6 +1549,6 @@ public class UserIssuesITCase extends AbstractITCase { UserTO userTO = userService.read("1417acbe-cbf6-4277-9372-e75e04f97000"); assertFalse(userTO.getResources().contains(RESOURCE_NAME_TESTDB), "Should not contain removed resources"); assertFalse(userTO.getAuxClasses().contains("csv"), "Should not contain removed auxiliary classes"); - assertFalse(userTO.getRoles().contains("Other"),"Should not contain removed roles"); + assertFalse(userTO.getRoles().contains("Other"), "Should not contain removed roles"); } }
