Refactor user param

pull/146/head
johnniang 2019-04-28 15:17:11 +08:00
parent de70c1066e
commit 0ded87246a
10 changed files with 77 additions and 33 deletions

View File

@ -5,7 +5,9 @@ import run.halo.app.model.dto.UserDTO;
import run.halo.app.model.entity.User; import run.halo.app.model.entity.User;
import run.halo.app.model.params.PasswordParam; import run.halo.app.model.params.PasswordParam;
import run.halo.app.model.params.UserParam; import run.halo.app.model.params.UserParam;
import run.halo.app.model.support.UpdateCheck;
import run.halo.app.service.UserService; import run.halo.app.service.UserService;
import run.halo.app.utils.ValidationUtils;
import javax.validation.Valid; import javax.validation.Valid;
@ -25,13 +27,16 @@ public class UserController {
this.userService = userService; this.userService = userService;
} }
@GetMapping("profile") @GetMapping("profiles")
public UserDTO getProfile(User user) { public UserDTO getProfile(User user) {
return new UserDTO().convertFrom(user); return new UserDTO().convertFrom(user);
} }
@PutMapping("profile") @PutMapping("profiles")
public UserDTO updateProfile(@Valid @RequestBody UserParam userParam, User user) { public UserDTO updateProfile(@RequestBody UserParam userParam, User user) {
// Validate the user param
ValidationUtils.validate(userParam, UpdateCheck.class);
// Update properties // Update properties
userParam.update(user); userParam.update(user);
@ -39,8 +44,8 @@ public class UserController {
return new UserDTO().convertFrom(userService.update(user)); return new UserDTO().convertFrom(userService.update(user));
} }
@PutMapping("profile/password") @PutMapping("profiles/password")
public void updatePassword(@Valid @RequestBody PasswordParam passwordParam, User user) { public void updatePassword(@RequestBody @Valid PasswordParam passwordParam, User user) {
userService.updatePassword(passwordParam.getOldPassword(), passwordParam.getNewPassword(), user.getId()); userService.updatePassword(passwordParam.getOldPassword(), passwordParam.getNewPassword(), user.getId());
} }
} }

View File

@ -1,4 +1,4 @@
package run.halo.app.controller.admin.api; package run.halo.app.controller.content.api;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -147,7 +147,7 @@ public class InstallController {
} }
private User createDefaultUser(InstallParam installParam) { private User createDefaultUser(InstallParam installParam) {
return userService.createBy(installParam, installParam.getPassword()); return userService.createBy(installParam);
} }
private void initSettings(InstallParam installParam) { private void initSettings(InstallParam installParam) {

View File

@ -10,10 +10,12 @@ import run.halo.app.config.properties.HaloProperties;
import run.halo.app.model.entity.User; import run.halo.app.model.entity.User;
import run.halo.app.model.params.UserParam; import run.halo.app.model.params.UserParam;
import run.halo.app.model.properties.PrimaryProperties; import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.support.CreateCheck;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
import run.halo.app.service.UserService; import run.halo.app.service.UserService;
import run.halo.app.utils.FileUtils; import run.halo.app.utils.FileUtils;
import run.halo.app.utils.ValidationUtils;
import java.net.URI; import java.net.URI;
import java.nio.file.*; import java.nio.file.*;
@ -66,10 +68,14 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
userParam.setUsername("test"); userParam.setUsername("test");
userParam.setNickname("developer"); userParam.setNickname("developer");
userParam.setEmail("test@test.com"); userParam.setEmail("test@test.com");
userParam.setPassword("opentest");
log.debug("Initializing a test user: [{}]", userParam); log.debug("Initializing a test user: [{}]", userParam);
User testUser = userService.createBy(userParam, "opentest"); // Validate the user param
ValidationUtils.validate(userParam, CreateCheck.class);
User testUser = userService.createBy(userParam);
log.debug("Initialized a test user: [{}]", testUser); log.debug("Initialized a test user: [{}]", testUser);
} }

View File

@ -31,13 +31,6 @@ public class InstallParam extends UserParam {
/** /**
* Blog url. * Blog url.
*/ */
@NotBlank(message = "Blog url must not be blank")
private String url; private String url;
/**
* Password.
*/
@NotBlank(message = "Password must not be blank")
@Size(max = 100, message = "Length of password must not be more than {max}")
private String password;
} }

View File

@ -1,11 +1,15 @@
package run.halo.app.model.params; package run.halo.app.model.params;
import lombok.Data;
import run.halo.app.model.dto.base.InputConverter; import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.User; import run.halo.app.model.entity.User;
import lombok.Data; import run.halo.app.model.support.AllCheck;
import run.halo.app.model.support.CreateCheck;
import run.halo.app.model.support.UpdateCheck;
import javax.validation.constraints.Email; import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
/** /**
@ -17,22 +21,27 @@ import javax.validation.constraints.Size;
@Data @Data
public class UserParam implements InputConverter<User> { public class UserParam implements InputConverter<User> {
@NotBlank(message = "Username must not be blank") @NotBlank(message = "Username must not be blank", groups = {AllCheck.class})
@Size(max = 50, message = "Length of username must not be more than {max}") @Size(max = 50, message = "Length of username must not be more than {max}", groups = {AllCheck.class})
private String username; private String username;
@NotBlank(message = "Nickname must not be blank") @NotBlank(message = "Nickname must not be blank", groups = {AllCheck.class})
@Size(max = 255, message = "Length of nickname must not be more than {max}") @Size(max = 255, message = "Length of nickname must not be more than {max}", groups = {AllCheck.class})
private String nickname; private String nickname;
@Email(message = "The email format is incorrect") @Email(message = "The email format is incorrect", groups = {AllCheck.class})
@NotBlank(message = "Email must not be blank") @NotBlank(message = "Email must not be blank", groups = {AllCheck.class})
@Size(max = 127, message = "Length of email must not be more than {max}") @Size(max = 127, message = "Length of email must not be more than {max}", groups = {AllCheck.class})
private String email; private String email;
@Size(max = 1023, message = "Length of avatar link must not be more than {max}") @Null(groups = UpdateCheck.class)
@Size(min = 8, max = 100, message = "Length of password must be between {min} and {max}", groups = {CreateCheck.class})
private String password;
@Size(max = 1023, message = "Length of avatar link must not be more than {max}", groups = {AllCheck.class})
private String avatar; private String avatar;
@Size(max = 1023, message = "Length of description must not be more than {max}") @Size(max = 1023, message = "Length of description must not be more than {max}", groups = {AllCheck.class})
private String description; private String description;
} }

View File

@ -0,0 +1,13 @@
package run.halo.app.model.support;
import javax.validation.GroupSequence;
/**
* All check for hibernate validation.
*
* @author johnniang
* @date 19-4-28
*/
@GroupSequence({CreateCheck.class, UpdateCheck.class})
public interface AllCheck {
}

View File

@ -0,0 +1,10 @@
package run.halo.app.model.support;
/**
* Create check for hibernate validation
*
* @author johnniang
* @date 19-4-28
*/
public interface CreateCheck {
}

View File

@ -0,0 +1,10 @@
package run.halo.app.model.support;
/**
* Update check for hibernate validation
*
* @author johnniang
* @date 19-4-28
*/
public interface UpdateCheck {
}

View File

@ -103,9 +103,8 @@ public interface UserService extends CrudService<User, Integer> {
* Creates an user. * Creates an user.
* *
* @param userParam user param must not be null. * @param userParam user param must not be null.
* @param password password must not be blank
* @return created user * @return created user
*/ */
@NonNull @NonNull
User createBy(@NonNull UserParam userParam, @NonNull String password); User createBy(@NonNull UserParam userParam);
} }

View File

@ -180,7 +180,7 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
} }
// Set new password // Set new password
setPassword(newPassword, user); setPassword(user, newPassword);
// Update this user // Update this user
User updatedUser = update(user); User updatedUser = update(user);
@ -192,13 +192,12 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
} }
@Override @Override
public User createBy(UserParam userParam, String password) { public User createBy(UserParam userParam) {
Assert.notNull(userParam, "User param must not be null"); Assert.notNull(userParam, "User param must not be null");
Assert.hasText(password, "Password must not be blank");
User user = userParam.convertTo(); User user = userParam.convertTo();
setPassword(password, user); setPassword(user, userParam.getPassword());
return create(user); return create(user);
} }
@ -213,9 +212,9 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
return updatedUser; return updatedUser;
} }
private void setPassword(@NonNull String plainPassword, @NonNull User user) { private void setPassword(@NonNull User user, @NonNull String plainPassword) {
Assert.hasText(plainPassword, "Plain password must not be blank");
Assert.notNull(user, "User must not be null"); Assert.notNull(user, "User must not be null");
Assert.hasText(plainPassword, "Plain password must not be blank");
user.setPassword(BCrypt.hashpw(plainPassword, BCrypt.gensalt())); user.setPassword(BCrypt.hashpw(plainPassword, BCrypt.gensalt()));
} }