From 2aa75f0e131631d875f34295e71038326b49fadd Mon Sep 17 00:00:00 2001 From: Orlando M Guerreiro Date: Wed, 4 Jun 2025 12:22:49 +0100 Subject: [PATCH] root/resilient#2 Change the User selection control to a 'on type' combo. Also restricted the user list to only users that have NO association to org yet --- .../resilient/repository/UserRepository.java | 10 ++ .../resilient/service/UserService.java | 5 + .../resilient/service/dto/UserDTO.java | 114 ++++++++++-------- .../web/rest/PublicUserResource.java | 85 ++++++++----- .../update/organization-update.component.html | 26 +++- .../update/organization-update.component.ts | 10 +- .../app/entities/user/service/user.service.ts | 5 + .../webapp/app/entities/user/user.model.ts | 1 + 8 files changed, 174 insertions(+), 82 deletions(-) diff --git a/src/main/java/com/oguerreiro/resilient/repository/UserRepository.java b/src/main/java/com/oguerreiro/resilient/repository/UserRepository.java index f6d338b..235ebcb 100644 --- a/src/main/java/com/oguerreiro/resilient/repository/UserRepository.java +++ b/src/main/java/com/oguerreiro/resilient/repository/UserRepository.java @@ -9,6 +9,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import com.oguerreiro.resilient.domain.User; @@ -43,4 +44,13 @@ public interface UserRepository extends JpaRepository { Optional findOneWithAuthoritiesByEmailIgnoreCase(String email); Page findAllByIdNotNullAndActivatedIsTrue(Pageable pageable); + + @Query(""" + SELECT u + FROM User u + WHERE u.id IS NOT NULL + AND u.activated = TRUE + AND u.id NOT IN (SELECT o.user.id FROM Organization o WHERE o.user IS NOT NULL) + """) + Page findActiveUsersNotInOrganization(Pageable pageable); } diff --git a/src/main/java/com/oguerreiro/resilient/service/UserService.java b/src/main/java/com/oguerreiro/resilient/service/UserService.java index aa6a340..bd8fda4 100644 --- a/src/main/java/com/oguerreiro/resilient/service/UserService.java +++ b/src/main/java/com/oguerreiro/resilient/service/UserService.java @@ -274,6 +274,11 @@ public class UserService { return userRepository.findAllByIdNotNullAndActivatedIsTrue(pageable).map(UserDTO::new); } + @Transactional(readOnly = true) + public Page getAllPublicUsersForOrganization(Pageable pageable) { + return userRepository.findActiveUsersNotInOrganization(pageable).map(UserDTO::new); + } + @Transactional(readOnly = true) public Optional getUserWithAuthoritiesByLogin(String login) { return userRepository.findOneWithAuthoritiesByLogin(login); diff --git a/src/main/java/com/oguerreiro/resilient/service/dto/UserDTO.java b/src/main/java/com/oguerreiro/resilient/service/dto/UserDTO.java index 20710b6..1f35350 100644 --- a/src/main/java/com/oguerreiro/resilient/service/dto/UserDTO.java +++ b/src/main/java/com/oguerreiro/resilient/service/dto/UserDTO.java @@ -1,69 +1,83 @@ package com.oguerreiro.resilient.service.dto; -import com.oguerreiro.resilient.domain.User; import java.io.Serializable; import java.util.Objects; +import com.oguerreiro.resilient.domain.User; + /** * A DTO representing a user, with only the public attributes. */ public class UserDTO implements Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - private Long id; + private Long id; - private String login; + private String login; - public UserDTO() { - // Empty constructor needed for Jackson. + private String email; + + public UserDTO() { + // Empty constructor needed for Jackson. + } + + public UserDTO(User user) { + this.id = user.getId(); + this.login = user.getLogin(); + this.email = user.getEmail(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; } - public UserDTO(User user) { - this.id = user.getId(); - // Customize it here if you need, or not, firstName/lastName/etc - this.login = user.getLogin(); + UserDTO userDTO = (UserDTO) o; + if (userDTO.getId() == null || getId() == null) { + return false; } - public Long getId() { - return id; - } + return Objects.equals(getId(), userDTO.getId()) && Objects.equals(getLogin(), userDTO.getLogin()); + } - public void setId(Long id) { - this.id = id; - } - - public String getLogin() { - return login; - } - - public void setLogin(String login) { - this.login = login; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - UserDTO userDTO = (UserDTO) o; - if (userDTO.getId() == null || getId() == null) { - return false; - } - - return Objects.equals(getId(), userDTO.getId()) && Objects.equals(getLogin(), userDTO.getLogin()); - } - - // prettier-ignore - @Override - public String toString() { - return "UserDTO{" + - "id='" + id + '\'' + - ", login='" + login + '\'' + - "}"; - } + // prettier-ignore + @Override + public String toString() { + //@formatter:off + return "UserDTO{" + + "id='" + id + '\'' + + ", login='" + login + '\'' + + ", email='" + email + '\'' + + "}"; + //@formatter:on + } } diff --git a/src/main/java/com/oguerreiro/resilient/web/rest/PublicUserResource.java b/src/main/java/com/oguerreiro/resilient/web/rest/PublicUserResource.java index d44d67a..9eece6d 100644 --- a/src/main/java/com/oguerreiro/resilient/web/rest/PublicUserResource.java +++ b/src/main/java/com/oguerreiro/resilient/web/rest/PublicUserResource.java @@ -1,9 +1,9 @@ package com.oguerreiro.resilient.web.rest; -import com.oguerreiro.resilient.service.UserService; -import com.oguerreiro.resilient.service.dto.UserDTO; -import java.util.*; +import java.util.Arrays; import java.util.Collections; +import java.util.List; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; @@ -12,45 +12,72 @@ import org.springframework.data.domain.Sort; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import com.oguerreiro.resilient.service.UserService; +import com.oguerreiro.resilient.service.dto.UserDTO; + import tech.jhipster.web.util.PaginationUtil; @RestController @RequestMapping("/api") public class PublicUserResource { - private static final List ALLOWED_ORDERED_PROPERTIES = Collections.unmodifiableList( - Arrays.asList("id", "login", "firstName", "lastName", "email", "activated", "langKey") - ); + private static final List ALLOWED_ORDERED_PROPERTIES = Collections.unmodifiableList( + Arrays.asList("id", "login", "firstName", "lastName", "email", "activated", "langKey")); - private final Logger log = LoggerFactory.getLogger(PublicUserResource.class); + private final Logger log = LoggerFactory.getLogger(PublicUserResource.class); - private final UserService userService; + private final UserService userService; - public PublicUserResource(UserService userService) { - this.userService = userService; + public PublicUserResource(UserService userService) { + this.userService = userService; + } + + /** + * {@code GET /users} : get all users with only public information - calling this method is allowed for anyone. + * + * @param pageable the pagination information. + * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body all users. + */ + @GetMapping("/users") + public ResponseEntity> getAllPublicUsers( + @org.springdoc.core.annotations.ParameterObject Pageable pageable) { + log.debug("REST request to get all public User names"); + if (!onlyContainsAllowedProperties(pageable)) { + return ResponseEntity.badRequest().build(); } - /** - * {@code GET /users} : get all users with only public information - calling this method is allowed for anyone. - * - * @param pageable the pagination information. - * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body all users. - */ - @GetMapping("/users") - public ResponseEntity> getAllPublicUsers(@org.springdoc.core.annotations.ParameterObject Pageable pageable) { - log.debug("REST request to get all public User names"); - if (!onlyContainsAllowedProperties(pageable)) { - return ResponseEntity.badRequest().build(); - } + final Page page = userService.getAllPublicUsers(pageable); + HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), + page); + return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK); + } - final Page page = userService.getAllPublicUsers(pageable); - HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page); - return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK); + /** + * {@code GET /users} : get all users with only public information - calling this method is allowed for anyone. + * + * @param pageable the pagination information. + * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body all users. + */ + @GetMapping("/users/for/organization") + public ResponseEntity> getAllPublicUsersForOrganization( + @org.springdoc.core.annotations.ParameterObject Pageable pageable) { + log.debug("REST request to get all public User names, restricted to users not yet associated in the organization"); + if (!onlyContainsAllowedProperties(pageable)) { + return ResponseEntity.badRequest().build(); } - private boolean onlyContainsAllowedProperties(Pageable pageable) { - return pageable.getSort().stream().map(Sort.Order::getProperty).allMatch(ALLOWED_ORDERED_PROPERTIES::contains); - } + final Page page = userService.getAllPublicUsersForOrganization(pageable); + HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), + page); + return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK); + } + + private boolean onlyContainsAllowedProperties(Pageable pageable) { + return pageable.getSort().stream().map(Sort.Order::getProperty).allMatch(ALLOWED_ORDERED_PROPERTIES::contains); + } } diff --git a/src/main/webapp/app/entities/organization/update/organization-update.component.html b/src/main/webapp/app/entities/organization/update/organization-update.component.html index 6ecfca3..f284083 100644 --- a/src/main/webapp/app/entities/organization/update/organization-update.component.html +++ b/src/main/webapp/app/entities/organization/update/organization-update.component.html @@ -186,12 +186,36 @@
-