I have an issue with Spring JPA Data. I’m using a Specification array to searching in multiple fields.
I wonder how can I apply the Specfication array into @Query annotation using Spring Security.
<code>/// UserService.class
List<Specification<User>> specifications = new ArrayList<>();
///…….
if ((StringUtils.isNotEmpty(fieldName) && StringUtils.isNotEmpty(query)) && fieldName.equals("firstName"))
specifications.add(UserSpecification.byFirstName(query));
if ((StringUtils.isNotEmpty(fieldName) && StringUtils.isNotEmpty(query)) && fieldName.equals("lastName"))
specifications.add(UserSpecification.byLastName(query));
Page<User> pageSlice = userRepository.findAll(
Specification.anyOf(specifications),
PageRequest.of(page, pageSize, Sort.by(Sort.Direction.fromString(direction), sortBy))
);
/// UserRepository.class
/// Always return all entries even if firstName's value is "tototototo"
@Query(
" FROM #{#entityName} entity " +
" LEFT OUTER JOIN entity.authorities authority " +
" LEFT OUTER JOIN entity.company company " +
" WHERE entity.username <> ?#{principal?.username}"+
" AND (1 = ?#{security.hasRole('SUPER') ? 1 : 0} " +
" OR 1 = ?#{security.hasRole('ADMIN') ? 1 : 0}" +
" OR (1 = ?#{security.hasRole('STAFF') ? 1 : 0} AND authority NOT IN ('ROLE_ADMIN','ROLE_SUPER')" +
" OR (1 = ?#{security.hasRole('PARTNER_ADMIN') ? 1 : 0} " +
" AND entity.uid IN ( " +
" SELECT e.uid " +
" FROM #{#entityName} e " +
" JOIN e.company.employees employee " +
" WHERE employee.username = ?#{principal?.username}" +
" )" +
" )" +
" OR (1 = ?#{security.hasRole('PARTNER') ? 1 : 0} " +
" AND entity.uid IN ( " +
" SELECT e.uid " +
" FROM #{#entityName} e " +
" JOIN e.company.employees employee " +
" WHERE employee.username = ?#{principal?.username}" +
" AND authority <> 'ROLE_PARTNER_ADMIN'" +
" )" +
" )" +
" )" +
" )"
)
Page<User> findAll(Specification<User> specification, Pageable pageable);
</code>
<code>/// UserService.class
List<Specification<User>> specifications = new ArrayList<>();
///…….
if ((StringUtils.isNotEmpty(fieldName) && StringUtils.isNotEmpty(query)) && fieldName.equals("firstName"))
specifications.add(UserSpecification.byFirstName(query));
if ((StringUtils.isNotEmpty(fieldName) && StringUtils.isNotEmpty(query)) && fieldName.equals("lastName"))
specifications.add(UserSpecification.byLastName(query));
Page<User> pageSlice = userRepository.findAll(
Specification.anyOf(specifications),
PageRequest.of(page, pageSize, Sort.by(Sort.Direction.fromString(direction), sortBy))
);
/// UserRepository.class
/// Always return all entries even if firstName's value is "tototototo"
@Query(
" FROM #{#entityName} entity " +
" LEFT OUTER JOIN entity.authorities authority " +
" LEFT OUTER JOIN entity.company company " +
" WHERE entity.username <> ?#{principal?.username}"+
" AND (1 = ?#{security.hasRole('SUPER') ? 1 : 0} " +
" OR 1 = ?#{security.hasRole('ADMIN') ? 1 : 0}" +
" OR (1 = ?#{security.hasRole('STAFF') ? 1 : 0} AND authority NOT IN ('ROLE_ADMIN','ROLE_SUPER')" +
" OR (1 = ?#{security.hasRole('PARTNER_ADMIN') ? 1 : 0} " +
" AND entity.uid IN ( " +
" SELECT e.uid " +
" FROM #{#entityName} e " +
" JOIN e.company.employees employee " +
" WHERE employee.username = ?#{principal?.username}" +
" )" +
" )" +
" OR (1 = ?#{security.hasRole('PARTNER') ? 1 : 0} " +
" AND entity.uid IN ( " +
" SELECT e.uid " +
" FROM #{#entityName} e " +
" JOIN e.company.employees employee " +
" WHERE employee.username = ?#{principal?.username}" +
" AND authority <> 'ROLE_PARTNER_ADMIN'" +
" )" +
" )" +
" )" +
" )"
)
Page<User> findAll(Specification<User> specification, Pageable pageable);
</code>
/// UserService.class
List<Specification<User>> specifications = new ArrayList<>();
///…….
if ((StringUtils.isNotEmpty(fieldName) && StringUtils.isNotEmpty(query)) && fieldName.equals("firstName"))
specifications.add(UserSpecification.byFirstName(query));
if ((StringUtils.isNotEmpty(fieldName) && StringUtils.isNotEmpty(query)) && fieldName.equals("lastName"))
specifications.add(UserSpecification.byLastName(query));
Page<User> pageSlice = userRepository.findAll(
Specification.anyOf(specifications),
PageRequest.of(page, pageSize, Sort.by(Sort.Direction.fromString(direction), sortBy))
);
/// UserRepository.class
/// Always return all entries even if firstName's value is "tototototo"
@Query(
" FROM #{#entityName} entity " +
" LEFT OUTER JOIN entity.authorities authority " +
" LEFT OUTER JOIN entity.company company " +
" WHERE entity.username <> ?#{principal?.username}"+
" AND (1 = ?#{security.hasRole('SUPER') ? 1 : 0} " +
" OR 1 = ?#{security.hasRole('ADMIN') ? 1 : 0}" +
" OR (1 = ?#{security.hasRole('STAFF') ? 1 : 0} AND authority NOT IN ('ROLE_ADMIN','ROLE_SUPER')" +
" OR (1 = ?#{security.hasRole('PARTNER_ADMIN') ? 1 : 0} " +
" AND entity.uid IN ( " +
" SELECT e.uid " +
" FROM #{#entityName} e " +
" JOIN e.company.employees employee " +
" WHERE employee.username = ?#{principal?.username}" +
" )" +
" )" +
" OR (1 = ?#{security.hasRole('PARTNER') ? 1 : 0} " +
" AND entity.uid IN ( " +
" SELECT e.uid " +
" FROM #{#entityName} e " +
" JOIN e.company.employees employee " +
" WHERE employee.username = ?#{principal?.username}" +
" AND authority <> 'ROLE_PARTNER_ADMIN'" +
" )" +
" )" +
" )" +
" )"
)
Page<User> findAll(Specification<User> specification, Pageable pageable);
Thanks