I'm trying to upgrade a JHipster v5 app to v7.9.3. With JHipster 5, I was able to secure Elasticsearch endpoints by adding a filter by the user's login.
public ResponseEntity<List<BloodPressure>> searchBloodPressures(@RequestParam String query, Pageable pageable) {
log.debug("REST request to search for a page of BloodPressures for query {}", query);
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(queryStringQuery(query));
if (SecurityUtils.isAuthenticated() && !SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
queryBuilder = queryBuilder.filter(matchQuery("user.login",
SecurityUtils.getCurrentUserLogin().orElse("")));
}
Page<BloodPressure> page = bloodPressureSearchRepository.search(queryBuilder, pageable);
HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/blood-pressures");
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
With JHipster 7, here's what the method looks like before I try to lock it down.
public ResponseEntity<List<BloodPressure>> searchBloodPressures(
@RequestParam String query,
@org.springdoc.api.annotations.ParameterObject Pageable pageable
) {
log.debug("REST request to search for a page of BloodPressures for query {}", query);
Page<BloodPressure> page = bloodPressureSearchRepository.search(query, pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
JHipster 5 allowed me to pass the queryBuilder
directly into the search()
method. However, with JHipster 7, it expects a String. Is it possible to get the query from the queryBuilder
to make it all work?
Here's what I have so far:
public ResponseEntity<List<BloodPressure>> searchBloodPressures(
@RequestParam String query,
@org.springdoc.api.annotations.ParameterObject Pageable pageable
) {
log.debug("REST request to search for a page of BloodPressures for query {}", query);
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(queryStringQuery(query));
if (SecurityUtils.isAuthenticated() && !SecurityUtils.hasCurrentUserThisAuthority(AuthoritiesConstants.ADMIN)) {
queryBuilder = queryBuilder.filter(matchQuery("user.login", SecurityUtils.getCurrentUserLogin().orElse("")));
}
// todo: figure out how to use queryBuilder in search()
// SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// sourceBuilder.query(queryBuilder);
Page<BloodPressure> page = bloodPressureSearchRepository.search(query, pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
The code for the BloodPressureSearchRepository
is as follows:
package org.jhipster.health.repository.search;
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
import java.util.List;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.elasticsearch.search.sort.SortBuilder;
import org.jhipster.health.domain.BloodPressure;
import org.jhipster.health.repository.BloodPressureRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* Spring Data Elasticsearch repository for the {@link BloodPressure} entity.
*/
public interface BloodPressureSearchRepository
extends ElasticsearchRepository<BloodPressure, Long>, BloodPressureSearchRepositoryInternal {}
interface BloodPressureSearchRepositoryInternal {
Page<BloodPressure> search(String query, Pageable pageable);
Page<BloodPressure> search(Query query);
void index(BloodPressure entity);
}
class BloodPressureSearchRepositoryInternalImpl implements BloodPressureSearchRepositoryInternal {
private final ElasticsearchRestTemplate elasticsearchTemplate;
private final BloodPressureRepository repository;
BloodPressureSearchRepositoryInternalImpl(ElasticsearchRestTemplate elasticsearchTemplate, BloodPressureRepository repository) {
this.elasticsearchTemplate = elasticsearchTemplate;
this.repository = repository;
}
@Override
public Page<BloodPressure> search(String query, Pageable pageable) {
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryStringQuery(query));
return search(nativeSearchQuery.setPageable(pageable));
}
@Override
public Page<BloodPressure> search(Query query) {
SearchHits<BloodPressure> searchHits = elasticsearchTemplate.search(query, BloodPressure.class);
List<BloodPressure> hits = searchHits.map(SearchHit::getContent).stream().collect(Collectors.toList());
return new PageImpl<>(hits, query.getPageable(), searchHits.getTotalHits());
}
@Override
public void index(BloodPressure entity) {
repository.findOneWithEagerRelationships(entity.getId()).ifPresent(elasticsearchTemplate::save);
}
}