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
This commit is contained in:
Orlando M Guerreiro 2025-06-04 12:22:49 +01:00
parent 4890950952
commit 2aa75f0e13
8 changed files with 174 additions and 82 deletions

View file

@ -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<User, Long> {
Optional<User> findOneWithAuthoritiesByEmailIgnoreCase(String email);
Page<User> 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<User> findActiveUsersNotInOrganization(Pageable pageable);
}

View file

@ -274,6 +274,11 @@ public class UserService {
return userRepository.findAllByIdNotNullAndActivatedIsTrue(pageable).map(UserDTO::new);
}
@Transactional(readOnly = true)
public Page<UserDTO> getAllPublicUsersForOrganization(Pageable pageable) {
return userRepository.findActiveUsersNotInOrganization(pageable).map(UserDTO::new);
}
@Transactional(readOnly = true)
public Optional<User> getUserWithAuthoritiesByLogin(String login) {
return userRepository.findOneWithAuthoritiesByLogin(login);

View file

@ -1,9 +1,10 @@
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.
*/
@ -15,14 +16,16 @@ public class UserDTO implements Serializable {
private String login;
private String email;
public UserDTO() {
// Empty constructor needed for Jackson.
}
public UserDTO(User user) {
this.id = user.getId();
// Customize it here if you need, or not, firstName/lastName/etc
this.login = user.getLogin();
this.email = user.getEmail();
}
public Long getId() {
@ -41,6 +44,14 @@ public class UserDTO implements Serializable {
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) {
@ -61,9 +72,12 @@ public class UserDTO implements Serializable {
// prettier-ignore
@Override
public String toString() {
return "UserDTO{" +
"id='" + id + '\'' +
", login='" + login + '\'' +
"}";
//@formatter:off
return "UserDTO{"
+ "id='" + id + '\''
+ ", login='" + login + '\''
+ ", email='" + email + '\''
+ "}";
//@formatter:on
}
}

View file

@ -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,8 +12,14 @@ 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
@ -21,8 +27,7 @@ import tech.jhipster.web.util.PaginationUtil;
public class PublicUserResource {
private static final List<String> ALLOWED_ORDERED_PROPERTIES = Collections.unmodifiableList(
Arrays.asList("id", "login", "firstName", "lastName", "email", "activated", "langKey")
);
Arrays.asList("id", "login", "firstName", "lastName", "email", "activated", "langKey"));
private final Logger log = LoggerFactory.getLogger(PublicUserResource.class);
@ -39,14 +44,36 @@ public class PublicUserResource {
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body all users.
*/
@GetMapping("/users")
public ResponseEntity<List<UserDTO>> getAllPublicUsers(@org.springdoc.core.annotations.ParameterObject Pageable pageable) {
public ResponseEntity<List<UserDTO>> 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<UserDTO> page = userService.getAllPublicUsers(pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
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<List<UserDTO>> 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();
}
final Page<UserDTO> page = userService.getAllPublicUsersForOrganization(pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(),
page);
return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}

View file

@ -186,12 +186,36 @@
<!-- User property applies only to OrganizationNature.PERSON -->
<div class="mb-3" *ngIf="editForm.get('organizationType')?.value?.nature === organizationNature.PERSON">
<label class="form-label" for="field_user" jhiTranslate="resilientApp.organization.user">User</label>
<select class="form-control" id="field_user" data-cy="user" name="user" formControlName="user" [compareWith]="compareUser">
<!--
<select
class="form-control"
id="field_user"
data-cy="user"
name="user"
formControlName="user"
[compareWith]="compareUser">
<option [ngValue]="null"></option>
@for (userOption of usersSharedCollection; track $index) {
<option [ngValue]="userOption">{{ userOption.login }}</option>
}
</select>
-->
<ng-select
class="form-control drop-down-panel"
formControlName="user"
[items]="usersSharedCollection"
[compareWith]="compareUser"
[clearable]="true"
[searchFn]="userSearchFn"
>
<ng-template ng-label-tmp let-item="item">
{{ item.login }} ( {{ item.email }} )
</ng-template>
<ng-template ng-option-tmp let-item="item">
{{ item.login }} ( {{ item.email }} )
</ng-template>
</ng-select>
</div>
</div>

View file

@ -6,6 +6,7 @@ import { finalize, map } from 'rxjs/operators';
import SharedModule from 'app/shared/shared.module';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NgSelectModule } from '@ng-select/ng-select';
import { AlertError } from 'app/shared/alert/alert-error.model';
import { EventManager, EventWithContent } from 'app/core/util/event-manager.service';
@ -23,7 +24,7 @@ import { OrganizationNature } from 'app/entities/enumerations/organization-natur
standalone: true,
selector: 'jhi-organization-update',
templateUrl: './organization-update.component.html',
imports: [SharedModule, FormsModule, ReactiveFormsModule],
imports: [SharedModule, FormsModule, ReactiveFormsModule, NgSelectModule],
})
export class OrganizationUpdateComponent implements OnInit {
// Properties with defaults by router
@ -181,7 +182,7 @@ export class OrganizationUpdateComponent implements OnInit {
protected loadRelationshipsOptionsUser(): void {
this.userService
.query()
.queryForOrganization()
.pipe(map((res: HttpResponse<IUser[]>) => res.body ?? []))
.pipe(map((users: IUser[]) => this.userService.addUserToCollectionIfMissing<IUser>(users, this.organization?.user)))
.subscribe((users: IUser[]) => (this.usersSharedCollection = users));
@ -227,4 +228,9 @@ export class OrganizationUpdateComponent implements OnInit {
}
});
}
userSearchFn = (term: string, item: any) => {
term = term.toLowerCase();
return (item.login + "( " + item.email + " )").toLowerCase().includes(term);
};
}

View file

@ -26,6 +26,11 @@ export class UserService {
return this.http.get<IUser[]>(this.resourceUrl, { params: options, observe: 'response' });
}
queryForOrganization(req?: any): Observable<EntityArrayResponseType> {
const options = createRequestOption(req);
return this.http.get<IUser[]>(`${this.resourceUrl}/for/organization`, { params: options, observe: 'response' });
}
getUserIdentifier(user: Pick<IUser, 'id'>): number {
return user.id;
}

View file

@ -1,4 +1,5 @@
export interface IUser {
id: number;
login?: string | null;
email?: string | null;
}