Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AdminUserDTO and the PublicUserResource #216

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ npm install -g generator-jhipster
2. Then install this in-development blueprint by

```
git clone https://github.com/jeremyg484/generator-jhipster-micronaut.git
git clone https://github.com/jhipster/generator-jhipster-micronaut.git
cd generator-jhipster-micronaut
git checkout micronaut-3
npm link generator-jhipster
Expand Down
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 @@ -44,7 +44,7 @@ public class User implements Serializable {
<%_ if (databaseType === 'couchbase') { _%>
@GeneratedValue(strategy = UNIQUE, delimiter = ID_DELIMITER)
<%_ } _%>
private <% if (authenticationType === 'oauth2') { %>String<% } else { %>Long<% } %> id;
private <%= user.primaryKey.type %> id;

@NotNull
@Pattern(regexp = Constants.LOGIN_REGEX)
Expand Down Expand Up @@ -120,11 +120,11 @@ public class User implements Serializable {
@Column(name = "last_modified_date")
private Instant lastModifiedDate;

public <% if (databaseType === 'sql' && authenticationType !== 'oauth2') { %>Long<% } else { %>String<% } %> getId() {
public <%= user.primaryKey.type %> getId() {
return id;
}

public void setId(<% if (databaseType === 'sql' && authenticationType !== 'oauth2') { %>Long<% } else { %>String<% } %> id) {
public void setId(<%= user.primaryKey.type %> id) {
this.id = id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.time.Instant;
* Micronaut Data JPA repository for the {@link User} entity.
*/
@Repository
public interface UserRepository extends JpaRepository<User, <% if (authenticationType !== 'oauth2') { %>Long<% } else { %>String<% } %>> {
public interface UserRepository extends JpaRepository<User, <%= user.primaryKey.type %>> {

<%_ if (usesCache) { _%>
public static String USERS_CACHE = "usersByLogin";
Expand All @@ -39,7 +39,7 @@ public interface UserRepository extends JpaRepository<User, <% if (authenticatio
public Optional<User> findOneByEmailIgnoreCase(String email);

@EntityGraph(attributePaths = "authorities")
public Optional<User> findOneById(<% if (authenticationType !== 'oauth2') { %>Long<% } else { %>String<% } %> id);
public Optional<User> findOneById(<%= user.primaryKey.type %> id);

@EntityGraph(attributePaths = "authorities")
<%_ if (usesCache) { _%>
Expand All @@ -61,5 +61,5 @@ public interface UserRepository extends JpaRepository<User, <% if (authenticatio

public Page<User> findByLoginNotEqual(String login, Pageable pageable);

public void update(@Id <% if (authenticationType !== 'oauth2') { %>Long<% } else { %>String<% } %> id, Instant createdDate);
public void update(@Id <%= user.primaryKey.type %> id, Instant createdDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import <%=packageName%>.repository.AuthorityRepository;
import <%=packageName%>.repository.UserRepository;
import <%=packageName%>.security.AuthoritiesConstants;
import <%=packageName%>.security.SecurityUtils;
import <%=packageName%>.service.dto.UserDTO;
import <%=packageName%>.service.dto.<%= asDto('AdminUser') %>;
import <%=packageName%>.service.dto.<%= asDto('User') %>;
import <%=packageName%>.service.util.RandomUtil;
import <%=packageName%>.web.rest.errors.*;
<%_ if (authenticationType !== 'oauth2') { _%>
Expand Down Expand Up @@ -142,7 +143,7 @@ public class UserService {
});
}

public User registerUser(UserDTO userDTO, String password) {
public User registerUser(<%= asDto('AdminUser') %> 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(<%= asDto('AdminUser') %> 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<<%= asDto('AdminUser') %>> updateUser(<%= asDto('AdminUser') %> 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(<%= asDto('AdminUser') %>::new);
}

public void deleteUser(String login) {
Expand Down Expand Up @@ -320,9 +321,15 @@ public class UserService {
}

@ReadOnly
public Page<UserDTO> getAllManagedUsers(Pageable pageable) {
public Page<<%= asDto('AdminUser') %>> getAllManagedUsers(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());
return Page.of(userPage.getContent().stream().map(<%= asDto('AdminUser') %>::new).collect(Collectors.toList()), pageable, userPage.getTotalSize());
}

@ReadOnly
public Page<<%= asDto('User') %>> getAllPublicUsers(Pageable pageable) {
Page<User> userPage = userRepository.findByLoginNotEqual(Constants.ANONYMOUS_USER, pageable);
return Page.of(userPage.getContent().stream().map(<%= asDto('User') %>::new).collect(Collectors.toList()), pageable, userPage.getTotalSize());
}

@ReadOnly
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 <%= user.primaryKey.type %> 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 <%= user.primaryKey.type %> getId() {
return id;
}

public void setId(<%= user.primaryKey.type %> 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
Loading