Skip to content

Commit

Permalink
Add AdminUserDTO and the PublicUserResource - to separate the publicl…
Browse files Browse the repository at this point in the history
…y accessible user information

from the private one.
 Porting jhipster/generator-jhipster#12374
  • Loading branch information
gzsombor committed Aug 17, 2023
1 parent 8c22d01 commit 1781b12
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 211 deletions.
6 changes: 6 additions & 0 deletions generators/app/__snapshots__/generator.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ Object {
"src/main/java/com/mycompany/myapp/service/UserService.java": Object {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/service/dto/AdminUserDTO.java": Object {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/service/dto/PasswordChangeDTO.java": Object {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -245,6 +248,9 @@ Object {
"src/main/java/com/mycompany/myapp/web/rest/ClientForwardController.java": Object {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/web/rest/PublicUserResource.java": Object {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/web/rest/SwaggerResource.java": Object {
"stateCleared": "modified",
},
Expand Down
6 changes: 6 additions & 0 deletions generators/server/__snapshots__/generator.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ Object {
"src/main/java/com/mycompany/myapp/service/UserService.java": Object {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/service/dto/AdminUserDTO.java": Object {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/service/dto/PasswordChangeDTO.java": Object {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -221,6 +224,9 @@ Object {
"src/main/java/com/mycompany/myapp/web/rest/ClientForwardController.java": Object {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/web/rest/PublicUserResource.java": Object {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/web/rest/SwaggerResource.java": Object {
"stateCleared": "modified",
},
Expand Down
13 changes: 12 additions & 1 deletion generators/server/files.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,15 @@ const serverFiles = {
condition: generator => !generator.skipUserManagement || generator.authenticationType === 'oauth2',
path: SERVER_MAIN_SRC_DIR,
templates: [
{
file: 'package/service/dto/AdminUserDTO.java',
renameTo: generator => `${generator.javaDir}service/dto/${generator.asDto('AdminUser')}.java`,
useBluePrint: true,
},

{
file: 'package/service/dto/UserDTO.java',
renameTo: generator => `${generator.javaDir}service/dto/UserDTO.java`,
renameTo: generator => `${generator.javaDir}service/dto/${generator.asDto('User')}.java`,
useBluePrint: true,
},
{
Expand Down Expand Up @@ -763,6 +769,11 @@ const serverFiles = {
renameTo: generator => `${generator.javaDir}web/rest/vm/ManagedUserVM.java`,
useBluePrint: true,
},
{
file: 'package/web/rest/PublicUserResource.java',
renameTo: generator => `${generator.javaDir}web/rest/PublicUserResource.java`,
useBluePrint: true,
},
// Base rest pkg
{
file: 'package/web/rest/ClientForwardController.java',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import <%=packageName%>.repository.AuthorityRepository;
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.security.AuthoritiesConstants;
import <%=packageName%>.security.SecurityUtils;
import <%=packageName%>.service.dto.AdminUserDTO;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.service.util.RandomUtil;
import <%=packageName%>.web.rest.errors.*;
Expand Down Expand Up @@ -142,7 +143,7 @@ public class UserService {
});
}
public User registerUser(UserDTO userDTO, String password) {
public User registerUser(AdminUserDTO userDTO, String password) {
userRepository.findOneByLogin(userDTO.getLogin().toLowerCase()).ifPresent(existingUser -> {
boolean removed = removeNonActivatedUser(existingUser);
if (!removed) {
Expand Down Expand Up @@ -192,7 +193,7 @@ public class UserService {
return true;
}
public User createUser(UserDTO userDTO) {
public User createUser(AdminUserDTO userDTO) {
User user = new User();
user.setLogin(userDTO.getLogin().toLowerCase());
user.setFirstName(userDTO.getFirstName());
Expand Down Expand Up @@ -231,7 +232,7 @@ public class UserService {
* @param userDTO user to update.
* @return updated user.
*/
public Optional<UserDTO> updateUser(UserDTO userDTO) {
public Optional<AdminUserDTO> updateUser(AdminUserDTO userDTO) {
return Optional.of(userRepository
.findById(userDTO.getId()))
.filter(Optional::isPresent)
Expand Down Expand Up @@ -261,7 +262,7 @@ public class UserService {
log.debug("Changed Information for User: {}", user);
return user;
})
.map(UserDTO::new);
.map(AdminUserDTO::new);
}
public void deleteUser(String login) {
Expand Down Expand Up @@ -320,7 +321,13 @@ public class UserService {
}
@ReadOnly
public Page<UserDTO> getAllManagedUsers(Pageable pageable) {
public Page<AdminUserDTO> getAllManagedUsers(Pageable pageable) {
Page<User> userPage = userRepository.findByLoginNotEqual(Constants.ANONYMOUS_USER, pageable);
return Page.of(userPage.getContent().stream().map(AdminUserDTO::new).collect(Collectors.toList()), pageable, userPage.getTotalSize());
}
@ReadOnly
public Page<UserDTO> getAllPublicUsers(Pageable pageable) {
Page<User> userPage = userRepository.findByLoginNotEqual(Constants.ANONYMOUS_USER, pageable);
return Page.of(userPage.getContent().stream().map(UserDTO::new).collect(Collectors.toList()), pageable, userPage.getTotalSize());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package <%=packageName%>.service.dto;

import <%=packageName%>.config.Constants;

import <%=packageName%>.domain.Authority;
import <%=packageName%>.domain.User;
import io.micronaut.core.annotation.Introspected;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

import javax.validation.constraints.*;
import java.time.Instant;
import java.util.Set;
import java.util.stream.Collectors;

/**
* A DTO representing a user, with their authorities.
*/
@Introspected
public class <%= asDto('AdminUser') %> {

private <%= userPkType %> id;

@NotBlank
@Pattern(regexp = Constants.LOGIN_REGEX)
@Size(min = 1, max = 50)
private String login;

@Size(max = 50)
private String firstName;

@Size(max = 50)
private String lastName;

@Email
@Size(min = 5, max = 254)
private String email;

@Size(max = 256)
private String imageUrl;

private boolean activated = false;

@Size(min = 2, max = 6)
private String langKey;

private String createdBy;

private Instant createdDate;

private String lastModifiedBy;

private Instant lastModifiedDate;

private Set<String> authorities;

public <%= asDto('AdminUser') %>() {
// Empty constructor needed for Jackson.
}

public <%= asDto('AdminUser') %>(User user) {
this.id = user.getId();
this.login = user.getLogin();
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
this.email = user.getEmail();
this.activated = user.getActivated();
this.imageUrl = user.getImageUrl();
this.langKey = user.getLangKey();
this.createdDate = user.getCreatedDate();
this.lastModifiedDate = user.getLastModifiedDate();
this.authorities = user.getAuthorities().stream()
.map(Authority::getName)
.collect(Collectors.toSet());
}

public <%= userPkType %> getId() {
return id;
}

public void setId(<%= userPkType %> id) {
this.id = id;
}

public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getImageUrl() {
return imageUrl;
}

public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

public boolean isActivated() {
return activated;
}

public void setActivated(boolean activated) {
this.activated = activated;
}

public String getLangKey() {
return langKey;
}

public void setLangKey(String langKey) {
this.langKey = langKey;
}

public Instant getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Instant createdDate) {
this.createdDate = createdDate;
}

public Instant getLastModifiedDate() {
return lastModifiedDate;
}

public void setLastModifiedDate(Instant lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}

public Set<String> getAuthorities() {
return authorities;
}

public void setAuthorities(Set<String> authorities) {
this.authorities = authorities;
}

@Override
public String toString() {
return "<%= asDto('AdminUser') %>{" +
"login='" + login + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", imageUrl='" + imageUrl + '\'' +
", activated=" + activated +
", langKey='" + langKey + '\'' +
", createdBy=" + createdBy +
", createdDate=" + createdDate +
", lastModifiedBy='" + lastModifiedBy + '\'' +
", lastModifiedDate=" + lastModifiedDate +
", authorities=" + authorities +
"}";
}
}
Loading

0 comments on commit 1781b12

Please sign in to comment.