Skip to content

Commit

Permalink
Merge pull request #10 from FINTLabs/using_spesification
Browse files Browse the repository at this point in the history
Using spesification
  • Loading branch information
erlingjahr authored Feb 29, 2024
2 parents b3bfc0c + 967c6fd commit 7db5347
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 49 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/CD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ jobs:
include:
- org: fintlabs-no
cluster: aks-beta-fint-2021-11-23
- org: ofk-no
cluster: aks-api-fint-2022-02-08
- org: afk-no
cluster: aks-api-fint-2022-02-08
- org: bfk-no
cluster: aks-api-fint-2022-02-08
# - org: ofk-no
# cluster: aks-api-fint-2022-02-08
# - org: afk-no
# cluster: aks-api-fint-2022-02-08
# - org: bfk-no
# cluster: aks-api-fint-2022-02-08

steps:
- name: Checkout repository
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/no/fintlabs/user/ResponseFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public ResponseEntity<Map<String,Object>> toResponseEntity(
int page,
int size
) {
List<SimpleUser> simpleUsers = userService.getSimpleUsers(principal,search,orgUnits,userType);
//List<SimpleUser> simpleUsers = userService.getSimpleUsers(principal,search,orgUnits,userType);
List<SimpleUser> simpleUsers = userService.getSimpleUsersUsingSpec(search,orgUnits,userType);
ResponseEntity<Map<String,Object>> entity = toResponseEntity(
toPage(simpleUsers,PageRequest.of(page, size)));

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/no/fintlabs/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ public ResponseEntity<Map<String,Object>> getSimpleUsers(@AuthenticationPrincipa
){

log.info("Finding users with search: " + search + " with orgUnitIDs: " + orgUnits + " with UserType: " + userType);
List<String> allAuthorizedOrgUnitIDsFromOPA = userService.getAllAutorizedOrgUnitIDs();


if (orgUnits == null){
List<String> allAuthorizedOrgUnitIDsFromOPA = userService.getAllAutorizedOrgUnitIDs();
log.info("No orgUnits spesified. Returning users from all authorized orgUnits. Authorized orgUnitIDs: " + allAuthorizedOrgUnitIDsFromOPA);
return responseFactory.toResponseEntity(FintJwtEndUserPrincipal.from(jwt),search,allAuthorizedOrgUnitIDsFromOPA,userType,page,size);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/no/fintlabs/user/UserRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.fintlabs.user;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

Expand All @@ -9,7 +10,7 @@
import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

List<User> findUsersByUserTypeEquals(String userType);

Expand Down
94 changes: 54 additions & 40 deletions src/main/java/no/fintlabs/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ private Runnable onSaveNewUser(User user) {
private Consumer<User> onSaveExistingUser(User user) {
return existingUser -> {
user.setId(existingUser.getId());
//log.info("Update user: " + user.getId());
log.debug("Update user: " + user.getId());
memberService.process(memberService.create(user));
User savedUser = userRepository.save(user);
//log.info("update kontrollUser: " + savedUser.getIdentityProviderUserObjectId());
log.debug("update kontrollUser: " + savedUser.getIdentityProviderUserObjectId());
userEntityProducerService.publish(savedUser);
};
}
Expand Down Expand Up @@ -85,52 +85,66 @@ public Optional<User> getUserById(Long id) {
}


public List<SimpleUser> getSimpleUsers(
FintJwtEndUserPrincipal principal,
// public List<SimpleUser> getSimpleUsers(
// FintJwtEndUserPrincipal principal,
// String search,
// List<String> orgUnits,
// String userType) {
//
// List<User> users;
//
//
// if ((orgUnits == null) && !(userType.equals("ALLTYPES"))) {
// users = userRepository.findUsersByNameType(search, userType);
// return users
// .stream()
// .filter(user -> user.getStatus().equals("ACTIV"))
// .map(User::toSimpleUser)
// .toList();
// }
//
// if ((orgUnits != null) && (userType.equals("ALLTYPES"))) {
// users = userRepository.findUsersByNameOrg(search, orgUnits);
// return users
// .stream()
// .filter(user -> user.getStatus().equals("ACTIV"))
// .map(User::toSimpleUser)
// .toList();
// }
//
// if ((orgUnits == null) && (userType.equals("ALLTYPES"))) {
// users = userRepository.findUsersByName(search);
// return users
// .stream()
// .filter(user -> user.getStatus().equals("ACTIV"))
// .map(User::toSimpleUser)
// .toList();
// }
//
//
// users = userRepository.findUsersByNameOrgType(search, orgUnits, userType);
// return users
// .stream()
// .filter(user -> user.getStatus().equals("ACTIV"))
// .map(User::toSimpleUser)
// .toList();
// }


public List<SimpleUser> getSimpleUsersUsingSpec(
String search,
List<String> orgUnits,
String userType) {
String userType
){
UserSpesificationBuilder userSpesification = new UserSpesificationBuilder(search,orgUnits,userType);
List<User> userList = userRepository.findAll(userSpesification.build());

List<User> users;


if ((orgUnits == null) && !(userType.equals("ALLTYPES"))) {
users = userRepository.findUsersByNameType(search, userType);
return users
.stream()
.filter(user -> user.getStatus().equals("ACTIV"))
.map(User::toSimpleUser)
.toList();
}

if ((orgUnits != null) && (userType.equals("ALLTYPES"))) {
users = userRepository.findUsersByNameOrg(search, orgUnits);
return users
.stream()
.filter(user -> user.getStatus().equals("ACTIV"))
.map(User::toSimpleUser)
.toList();
}

if ((orgUnits == null) && (userType.equals("ALLTYPES"))) {
users = userRepository.findUsersByName(search);
return users
.stream()
.filter(user -> user.getStatus().equals("ACTIV"))
.map(User::toSimpleUser)
.toList();
}


users = userRepository.findUsersByNameOrgType(search, orgUnits, userType);
return users
.stream()
return userList.stream()
.filter(user -> user.getStatus().equals("ACTIV"))
.map(User::toSimpleUser)
.toList();
}


public List<String> getAllAutorizedOrgUnitIDs() {

List<Scope> scope = authorizationClient.getUserScopesList();
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/no/fintlabs/user/UserSpesificationBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package no.fintlabs.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;

@Slf4j
public class UserSpesificationBuilder {
private final String search;
private final List<String> orgUnits; //lovlige orgunits
private final String userType;

public UserSpesificationBuilder(String search, List<String> orgUnits, String userType) {
this.search = search;
this.orgUnits = orgUnits;
this.userType = userType;
}

public Specification<User> build() {
Specification<User> userSpec = allAutorizedOrgUnits(orgUnits) ;

if (!search.isEmpty()){
userSpec = userSpec.and(usersNameLike(search));
}
if (!userType.equals("ALLTYPES")) {
userSpec = userSpec.and(userTypeEquals(userType.toLowerCase()));
}

return userSpec;
}

private Specification<User> allAutorizedOrgUnits(List<String> orgUnits) {

return (root, query, criteriaBuilder) -> criteriaBuilder
.in(root.get("mainOrganisationUnitId")).value(orgUnits);
}

private Specification<User> userTypeEquals(String userType) {
return (root, query, criteriaBuilder) -> criteriaBuilder
.equal(criteriaBuilder.lower(root.get("userType")),userType);
}

private Specification<User> usersNameLike(String search) {
return (root, query, criteriaBuilder) ->
criteriaBuilder.or(
criteriaBuilder.like(criteriaBuilder.lower(root.get("firstName")), "%" + search + "%"),
criteriaBuilder.like(criteriaBuilder.lower(root.get("lastName")), "%" + search + "%")
);

}


}

0 comments on commit 7db5347

Please sign in to comment.