Skip to content

Commit 2c7c529

Browse files
committed
Restricting orderby clauses for Audit Events search (#1437)
1 parent e4cd05e commit 2c7c529

4 files changed

Lines changed: 52 additions & 12 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.syncope.core.persistence.common.dao;
20+
21+
import java.util.List;
22+
import java.util.stream.Stream;
23+
import org.apache.syncope.core.persistence.api.entity.AuditEvent;
24+
import org.springframework.data.domain.Sort;
25+
import org.springframework.util.ReflectionUtils;
26+
27+
public abstract class AbstractAuditEventDAO {
28+
29+
protected List<Sort.Order> filterOrderBy(
30+
final Stream<Sort.Order> orderByClauses,
31+
final Class<? extends AuditEvent> clazz) {
32+
33+
return orderByClauses.
34+
filter(clause -> ReflectionUtils.findField(clazz, clause.getProperty().trim()) != null).
35+
toList();
36+
}
37+
}

‎core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAuditEventDAO.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929
import org.apache.syncope.common.lib.types.OpEvent;
3030
import org.apache.syncope.core.persistence.api.dao.AuditEventDAO;
3131
import org.apache.syncope.core.persistence.api.entity.AuditEvent;
32+
import org.apache.syncope.core.persistence.common.dao.AbstractAuditEventDAO;
3233
import org.apache.syncope.core.persistence.jpa.entity.JPAAuditEvent;
3334
import org.springframework.data.domain.Pageable;
35+
import org.springframework.data.domain.Sort;
3436
import org.springframework.transaction.annotation.Transactional;
3537

36-
public class JPAAuditEventDAO implements AuditEventDAO {
38+
public class JPAAuditEventDAO extends AbstractAuditEventDAO implements AuditEventDAO {
3739

3840
protected static class AuditEventCriteriaBuilder {
3941

@@ -168,8 +170,10 @@ public List<AuditEventTO> search(
168170
before(before, parameters).
169171
after(after, parameters).
170172
build();
171-
if (!pageable.getSort().isEmpty()) {
172-
queryString += " ORDER BY " + pageable.getSort().stream().
173+
174+
List<Sort.Order> orderBy = filterOrderBy(pageable.getSort().stream(), JPAAuditEvent.class);
175+
if (!orderBy.isEmpty()) {
176+
queryString += " ORDER BY " + orderBy.stream().
173177
map(clause -> ("when".equals(clause.getProperty()) ? "event_date" : clause.getProperty())
174178
+ ' ' + clause.getDirection().name()).
175179
collect(Collectors.joining(","));

‎core/persistence-neo4j/src/main/java/org/apache/syncope/core/persistence/neo4j/dao/Neo4jAuditEventDAO.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,16 @@
2828
import org.apache.syncope.common.lib.types.OpEvent;
2929
import org.apache.syncope.core.persistence.api.dao.AuditEventDAO;
3030
import org.apache.syncope.core.persistence.api.entity.AuditEvent;
31+
import org.apache.syncope.core.persistence.common.dao.AbstractAuditEventDAO;
3132
import org.apache.syncope.core.persistence.neo4j.entity.Neo4jAuditEvent;
3233
import org.apache.syncope.core.persistence.neo4j.spring.NodeValidator;
33-
import org.slf4j.Logger;
34-
import org.slf4j.LoggerFactory;
3534
import org.springframework.data.domain.Pageable;
35+
import org.springframework.data.domain.Sort;
3636
import org.springframework.data.neo4j.core.Neo4jClient;
3737
import org.springframework.data.neo4j.core.Neo4jTemplate;
3838
import org.springframework.transaction.annotation.Transactional;
3939

40-
public class Neo4jAuditEventDAO implements AuditEventDAO {
41-
42-
protected static final Logger LOG = LoggerFactory.getLogger(AuditEventDAO.class);
40+
public class Neo4jAuditEventDAO extends AbstractAuditEventDAO implements AuditEventDAO {
4341

4442
protected static class AuditEventCriteriaBuilder {
4543

@@ -169,8 +167,9 @@ public List<AuditEventTO> search(
169167
build()
170168
+ " RETURN n.id");
171169

172-
if (!pageable.getSort().isEmpty()) {
173-
query.append(" ORDER BY ").append(pageable.getSort().stream().
170+
List<Sort.Order> orderBy = filterOrderBy(pageable.getSort().stream(), Neo4jAuditEvent.class);
171+
if (!orderBy.isEmpty()) {
172+
query.append(" ORDER BY ").append(orderBy.stream().
174173
map(clause -> "n." + clause.getProperty() + ' ' + clause.getDirection().name()).
175174
collect(Collectors.joining(",")));
176175
}

‎core/persistence-neo4j/src/main/java/org/apache/syncope/core/persistence/neo4j/dao/Neo4jTaskDAO.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public <T extends Task<T>> List<T> findToExec(final TaskType type, final Pageabl
291291
query.append("(p)-[:").append(execRelationship(type)).append("]-() ");
292292
}
293293

294-
query.append("RETURN p.id ").append(toOrderByStatement(pageable.getSort().get()));
294+
query.append("RETURN p.id ").append(toOrderByStatement(pageable.getSort().stream()));
295295

296296
if (pageable.isPaged()) {
297297
query.append(" SKIP ").append(pageable.getPageSize() * pageable.getPageNumber()).
@@ -463,7 +463,7 @@ public <T extends Task<T>> List<T> findAll(
463463

464464
query.append(" WITH n ");
465465

466-
query.append(toOrderByStatement(pageable.getSort().get()));
466+
query.append(toOrderByStatement(pageable.getSort().stream()));
467467

468468
if (pageable.isPaged()) {
469469
query.append(" SKIP ").append(pageable.getPageSize() * pageable.getPageNumber()).

0 commit comments

Comments
 (0)