diff --git a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtController.java b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtController.java index 152348cc..06eb0e02 100644 --- a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtController.java +++ b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtController.java @@ -39,10 +39,16 @@ public HttpResponse> search( @RequestParam(value = "size", required = false, defaultValue = PagingHelper.DEFAULT_SIZE_STR) int size, @RequestParam(value = "filterInternalUser", required = false, defaultValue = "false") - Boolean filterInternalUser) { + Boolean filterInternalUser, + @RequestParam(value = "state", required = false) String state, + @RequestParam(value = "role", required = false) String role) { return HttpResponse.ok( userService.search( - q, UnifiedAssetService.getSearchPageRequest(page, size), filterInternalUser)); + q, + UnifiedAssetService.getSearchPageRequest(page, size), + filterInternalUser, + state, + role)); } @Operation(summary = "add a new user") diff --git a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/repo/UserRepository.java b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/repo/UserRepository.java index ee47259a..ec82d758 100644 --- a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/repo/UserRepository.java +++ b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/repo/UserRepository.java @@ -1,6 +1,7 @@ package com.consoleconnect.kraken.operator.auth.repo; import com.consoleconnect.kraken.operator.auth.entity.UserEntity; +import com.consoleconnect.kraken.operator.auth.enums.UserStateEnum; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -19,7 +20,10 @@ public interface UserRepository value = "select e from #{#entityName} e " + " where ( (:q) is null or LOWER(e.name) like %:q% or LOWER(e.email) like %:q% )" - + " and ( (:filterRoles) is null or e.role not in :filterRoles )") + + " and ( (:filterRoles) is null or e.role not in :filterRoles )" + + " and ((:state) is null or e.state = :state)" + + " and ((:role) is null or e.role = :role)") @Transactional(readOnly = true) - Page search(String q, Pageable pageable, List filterRoles); + Page search( + String q, Pageable pageable, List filterRoles, UserStateEnum state, String role); } diff --git a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/service/UserService.java b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/service/UserService.java index bad61e55..e04545ea 100644 --- a/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/service/UserService.java +++ b/kraken-java-sdk/kraken-java-sdk-auth/src/main/java/com/consoleconnect/kraken/operator/auth/service/UserService.java @@ -19,6 +19,7 @@ import java.util.*; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -77,7 +78,8 @@ public void initialize() { public void initSystemUpgradeUser() { Paging userPaging = - this.search(UserContext.SYSTEM_UPGRADE.toLowerCase(), PageRequest.of(0, 1), false); + this.search( + UserContext.SYSTEM_UPGRADE.toLowerCase(), PageRequest.of(0, 1), false, null, null); if (userPaging.getTotal() == 0) { CreateUserRequest request = new CreateUserRequest(); request.setEmail(UserContext.SYSTEM_UPGRADE); @@ -122,6 +124,7 @@ public User create(CreateUserRequest request, String createdBy) { UserEntity userEntity = UserMapper.INSTANCE.toEntity(request); userEntity.setPassword(passwordEncoder.encode(request.getPassword())); userEntity.setState(UserStateEnum.ENABLED); + userEntity.setRole(request.getRole()); userEntity.setCreatedAt(DateTime.nowInUTC()); userEntity.setCreatedBy(createdBy); userEntity = userRepository.save(userEntity); @@ -129,11 +132,18 @@ public User create(CreateUserRequest request, String createdBy) { return UserMapper.INSTANCE.toUser(userEntity); } - public Paging search(String q, PageRequest pageRequest, boolean filterInternalUser) { + public Paging search( + String q, PageRequest pageRequest, boolean filterInternalUser, String state, String role) { log.info("Searching users, q:{}, pageRequest:{}", q, pageRequest); + UserStateEnum userStateEnum = + (StringUtils.isBlank(state) ? null : UserStateEnum.valueOf(state)); Page userEntityPage = userRepository.search( - q, pageRequest, filterInternalUser ? List.of(UserRoleEnum.INTERNAL_USER.name()) : null); + q, + pageRequest, + filterInternalUser ? List.of(UserRoleEnum.INTERNAL_USER.name()) : null, + userStateEnum, + role); log.info("Users found:{}", userEntityPage.getTotalElements()); return PagingHelper.toPaging(userEntityPage, UserMapper.INSTANCE::toUser); } diff --git a/kraken-java-sdk/kraken-java-sdk-auth/src/test/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtControllerTest.java b/kraken-java-sdk/kraken-java-sdk-auth/src/test/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtControllerTest.java index dff99503..a25b871f 100644 --- a/kraken-java-sdk/kraken-java-sdk-auth/src/test/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtControllerTest.java +++ b/kraken-java-sdk/kraken-java-sdk-auth/src/test/java/com/consoleconnect/kraken/operator/auth/controller/UserMgmtControllerTest.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import java.util.*; import java.util.function.Consumer; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.SpyBean; @@ -31,6 +32,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.reactive.server.WebTestClient; +@Slf4j @ActiveProfiles("test-login-enabled") @MockIntegrationTest @ContextConfiguration(classes = {TestApplication.class}) @@ -429,6 +431,25 @@ void givenUserCreated_whenSearch_thenReturnOk() { assertThat(bodyStr, hasJsonPath("$.data.data", hasSize(greaterThanOrEqualTo(1)))); assertThat(bodyStr, hasJsonPath("$.data.data[0].email", equalTo("abc@test.com"))); }); + + // search by role and state + testClientHelper.requestAndVerify( + HttpMethod.GET, + (uriBuilder -> + uriBuilder + .path("/users") + .queryParam("role", "USER") + .queryParam("state", "ENABLED") + .build()), + headers, + null, + HttpStatus.OK.value(), + bodyStr -> { + log.info("bodyStr:{}", bodyStr); + Assertions.assertNotNull(bodyStr); + assertThat(bodyStr, hasJsonPath("$.data.data", notNullValue())); + assertThat(bodyStr, hasJsonPath("$.data.data", hasSize(greaterThanOrEqualTo(3)))); + }); } @Test diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/api/BuyerMgmtController.java b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/api/BuyerMgmtController.java index 0b630018..4cd04a8d 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/api/BuyerMgmtController.java +++ b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/api/BuyerMgmtController.java @@ -79,7 +79,7 @@ public HttpResponse> search( envId, buyerId, status, - null, + orderBy, PageRequest.of(page, size, direction, FIELD_CREATE_AT_ORIGINAL))); } diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/BuyerService.java b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/BuyerService.java index ce70c169..b49f1097 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/BuyerService.java +++ b/kraken-java-sdk/kraken-java-sdk-controller/src/main/java/com/consoleconnect/kraken/operator/controller/service/BuyerService.java @@ -10,6 +10,7 @@ import com.consoleconnect.kraken.operator.controller.mapper.BuyerAssetDtoMapper; import com.consoleconnect.kraken.operator.controller.model.Environment; import com.consoleconnect.kraken.operator.controller.model.MgmtProperty; +import com.consoleconnect.kraken.operator.core.dto.Tuple2; import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto; import com.consoleconnect.kraken.operator.core.entity.UnifiedAssetEntity; import com.consoleconnect.kraken.operator.core.enums.AssetStatusEnum; @@ -25,10 +26,7 @@ import java.time.Instant; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; +import java.util.*; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -36,6 +34,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -94,6 +93,44 @@ public BuyerAssetDto create(String productId, CreateBuyerRequest buyerOnboard, S buyerCreated, buyerOnboard.getBuyerId(), buyerOnboard.getTokenExpiredInSeconds()); } + @Transactional(readOnly = true) + public Paging search( + String parentId, + String envId, + String buyerId, + String status, + String orderBy, + PageRequest pageRequest) { + if (parentId != null) { + parentId = unifiedAssetService.findOneByIdOrKey(parentId).getId().toString(); + } + List eqConditions = new ArrayList<>(); + eqConditions.add(Tuple2.of("kind", PRODUCT_BUYER.getKind())); + if (StringUtils.isNotBlank(parentId)) { + eqConditions.add(Tuple2.of("parentId", parentId)); + } + + if (StringUtils.isNotBlank(status) + && (AssetStatusEnum.ACTIVATED.getKind().equals(status) + || AssetStatusEnum.DEACTIVATED.getKind().equals(status))) { + eqConditions.add(Tuple2.of("status", status)); + } + List labelConditions = new ArrayList<>(); + if (StringUtils.isNotBlank(envId)) { + labelConditions.add(Tuple2.of(LABEL_ENV_ID, envId)); + } + if (StringUtils.isNotBlank(buyerId)) { + labelConditions.add(Tuple2.of(LABEL_BUYER_ID, buyerId)); + } + if (StringUtils.isNotBlank(orderBy)) { + pageRequest = + PageRequest.of( + pageRequest.getPageNumber(), pageRequest.getPageSize(), Sort.Direction.DESC, orderBy); + } + return unifiedAssetService.findBySpecification( + eqConditions, labelConditions, null, pageRequest, null); + } + @Transactional(readOnly = true) public Paging search( String parentId, @@ -185,6 +222,7 @@ private UnifiedAsset createBuyer(String buyerId, String envId, String companyNam unifiedAsset.getMetadata().setDescription(BUYER_DESC); unifiedAsset.getMetadata().getLabels().put(LABEL_ENV_ID, envId); unifiedAsset.getMetadata().getLabels().put(LABEL_BUYER_ID, buyerId); + unifiedAsset .getMetadata() .getLabels() diff --git a/kraken-java-sdk/kraken-java-sdk-controller/src/test/java/com/consoleconnect/kraken/operator/controller/BuyerControllerTest.java b/kraken-java-sdk/kraken-java-sdk-controller/src/test/java/com/consoleconnect/kraken/operator/controller/BuyerControllerTest.java index 966fff98..9c6f9e43 100644 --- a/kraken-java-sdk/kraken-java-sdk-controller/src/test/java/com/consoleconnect/kraken/operator/controller/BuyerControllerTest.java +++ b/kraken-java-sdk/kraken-java-sdk-controller/src/test/java/com/consoleconnect/kraken/operator/controller/BuyerControllerTest.java @@ -12,6 +12,7 @@ import com.consoleconnect.kraken.operator.core.dto.Tuple2; import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto; import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum; +import com.consoleconnect.kraken.operator.core.enums.AssetStatusEnum; import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService; import com.consoleconnect.kraken.operator.core.toolkit.AssetsConstants; import com.consoleconnect.kraken.operator.core.toolkit.LabelConstants; @@ -74,7 +75,12 @@ void givenBuyer_whenCreate_thenOK() { void givenBuyer_whenSearch_thenOK() { webTestClient.requestAndVerify( HttpMethod.GET, - uriBuilder -> uriBuilder.path(BUYER_BASE_URL).build(), + uriBuilder -> + uriBuilder + .path(BUYER_BASE_URL) + .queryParam("status", AssetStatusEnum.ACTIVATED.getKind()) + .queryParam("buyerId", "testing-company") + .build(), HttpStatus.OK.value(), null, bodyStr -> { @@ -83,6 +89,25 @@ void givenBuyer_whenSearch_thenOK() { }); } + @Test + @Order(3) + void givenBuyer_whenSearchInactive_thenReturnEmptyData() { + webTestClient.requestAndVerify( + HttpMethod.GET, + uriBuilder -> + uriBuilder + .path(BUYER_BASE_URL) + .queryParam("status", AssetStatusEnum.DEACTIVATED.getKind()) + .build(), + HttpStatus.OK.value(), + null, + bodyStr -> { + log.info(bodyStr); + assertThat(bodyStr, Matchers.notNullValue()); + assertThat(bodyStr, hasJsonPath("$.data.data", hasSize(0))); + }); + } + @Test @Order(4) void givenDuplicatedBuyer_whenCreate_thenNot200() {