diff --git a/src/main/java/com/monkeyk/sos/domain/dto/UserFormDto.java b/src/main/java/com/monkeyk/sos/domain/dto/UserFormDto.java new file mode 100644 index 0000000..1fd4928 --- /dev/null +++ b/src/main/java/com/monkeyk/sos/domain/dto/UserFormDto.java @@ -0,0 +1,43 @@ +package com.monkeyk.sos.domain.dto; + +import com.monkeyk.sos.domain.user.Privilege; +import com.monkeyk.sos.domain.user.User; +import com.monkeyk.sos.infrastructure.PasswordHandler; + +/** + * 2016/3/25 + * + * @author Shengzhao Li + */ +public class UserFormDto extends UserDto { + private static final long serialVersionUID = 7959857016962260738L; + + + private String password; + + public UserFormDto() { + } + + + public Privilege[] getAllPrivileges() { + return new Privilege[]{Privilege.MOBILE, Privilege.UNITY}; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public User newUser() { + final User user = new User() + .username(getUsername()) + .phone(getPhone()) + .email(getEmail()) + .password(PasswordHandler.md5(getPassword())); + user.privileges().addAll(getPrivileges()); + return user; + } +} diff --git a/src/main/java/com/monkeyk/sos/infrastructure/PasswordHandler.java b/src/main/java/com/monkeyk/sos/infrastructure/PasswordHandler.java new file mode 100644 index 0000000..b1a8d88 --- /dev/null +++ b/src/main/java/com/monkeyk/sos/infrastructure/PasswordHandler.java @@ -0,0 +1,21 @@ +package com.monkeyk.sos.infrastructure; + +import org.springframework.security.authentication.encoding.Md5PasswordEncoder; + +/** + * 2016/3/25 + * + * @author Shengzhao Li + */ +public abstract class PasswordHandler { + + + private PasswordHandler() { + } + + + public static String md5(String password) { + Md5PasswordEncoder encoder = new Md5PasswordEncoder(); + return encoder.encodePassword(password, null); + } +} diff --git a/src/main/java/com/monkeyk/sos/infrastructure/jdbc/UserRepositoryJdbc.java b/src/main/java/com/monkeyk/sos/infrastructure/jdbc/UserRepositoryJdbc.java index e3f773e..c213cb4 100644 --- a/src/main/java/com/monkeyk/sos/infrastructure/jdbc/UserRepositoryJdbc.java +++ b/src/main/java/com/monkeyk/sos/infrastructure/jdbc/UserRepositoryJdbc.java @@ -78,6 +78,12 @@ public class UserRepositoryJdbc implements UserRepository { ps.setString(7, user.phone()); }); + + //get user id + + //insert privileges + + } @Override diff --git a/src/main/java/com/monkeyk/sos/service/UserService.java b/src/main/java/com/monkeyk/sos/service/UserService.java index e8719ff..51250e7 100644 --- a/src/main/java/com/monkeyk/sos/service/UserService.java +++ b/src/main/java/com/monkeyk/sos/service/UserService.java @@ -1,5 +1,6 @@ package com.monkeyk.sos.service; +import com.monkeyk.sos.domain.dto.UserFormDto; import com.monkeyk.sos.domain.dto.UserJsonDto; import com.monkeyk.sos.domain.dto.UserOverviewDto; import org.springframework.security.core.userdetails.UserDetailsService; @@ -12,4 +13,8 @@ public interface UserService extends UserDetailsService { UserJsonDto loadCurrentUserJsonDto(); UserOverviewDto loadUserOverviewDto(UserOverviewDto overviewDto); + + boolean isExistedUsername(String username); + + String saveUser(UserFormDto formDto); } \ No newline at end of file diff --git a/src/main/java/com/monkeyk/sos/service/impl/UserServiceImpl.java b/src/main/java/com/monkeyk/sos/service/impl/UserServiceImpl.java index e819406..71a1872 100644 --- a/src/main/java/com/monkeyk/sos/service/impl/UserServiceImpl.java +++ b/src/main/java/com/monkeyk/sos/service/impl/UserServiceImpl.java @@ -1,6 +1,7 @@ package com.monkeyk.sos.service.impl; import com.monkeyk.sos.domain.dto.UserDto; +import com.monkeyk.sos.domain.dto.UserFormDto; import com.monkeyk.sos.domain.dto.UserJsonDto; import com.monkeyk.sos.domain.dto.UserOverviewDto; import com.monkeyk.sos.domain.shared.security.WdcyUserDetails; @@ -59,6 +60,19 @@ public class UserServiceImpl implements UserService { return overviewDto; } + @Override + public boolean isExistedUsername(String username) { + final User user = userRepository.findByUsername(username); + return user != null; + } + + @Override + public String saveUser(UserFormDto formDto) { + User user = formDto.newUser(); + userRepository.saveUser(user); + return user.guid(); + } + private UserJsonDto loadOauthUserJsonDto(OAuth2Authentication oAuth2Authentication) { UserJsonDto userJsonDto = new UserJsonDto(); diff --git a/src/main/java/com/monkeyk/sos/web/controller/UserController.java b/src/main/java/com/monkeyk/sos/web/controller/UserController.java index 24f9e95..a864592 100644 --- a/src/main/java/com/monkeyk/sos/web/controller/UserController.java +++ b/src/main/java/com/monkeyk/sos/web/controller/UserController.java @@ -1,11 +1,15 @@ package com.monkeyk.sos.web.controller; +import com.monkeyk.sos.domain.dto.UserFormDto; import com.monkeyk.sos.domain.dto.UserOverviewDto; import com.monkeyk.sos.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; /** * @author Shengzhao Li @@ -18,9 +22,10 @@ public class UserController { @Autowired private UserService userService; + @Autowired + private UserFormDtoValidator validator; + /** - * Just forward to page - * * @return View page */ @RequestMapping("overview") @@ -31,4 +36,22 @@ public class UserController { } + @RequestMapping(value = "form/plus", method = RequestMethod.GET) + public String showForm(Model model) { + model.addAttribute("formDto", new UserFormDto()); + return "user_form"; + } + + + @RequestMapping(value = "form/plus", method = RequestMethod.POST) + public String submitRegisterClient(@ModelAttribute("formDto") UserFormDto formDto, BindingResult result) { + validator.validate(formDto, result); + if (result.hasErrors()) { + return "user_form"; + } + userService.saveUser(formDto); + return "redirect:../overview"; + } + + } \ No newline at end of file diff --git a/src/main/java/com/monkeyk/sos/web/controller/UserFormDtoValidator.java b/src/main/java/com/monkeyk/sos/web/controller/UserFormDtoValidator.java new file mode 100644 index 0000000..851add4 --- /dev/null +++ b/src/main/java/com/monkeyk/sos/web/controller/UserFormDtoValidator.java @@ -0,0 +1,67 @@ +package com.monkeyk.sos.web.controller; + +import com.monkeyk.sos.domain.dto.UserFormDto; +import com.monkeyk.sos.domain.user.Privilege; +import com.monkeyk.sos.service.UserService; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.validation.Errors; +import org.springframework.validation.Validator; + +import java.util.List; + +/** + * 2016/3/25 + * + * @author Shengzhao Li + */ +@Component +public class UserFormDtoValidator implements Validator { + + + @Autowired + private UserService userService; + + @Override + public boolean supports(Class clazz) { + return UserFormDto.class.equals(clazz); + } + + @Override + public void validate(Object target, Errors errors) { + UserFormDto formDto = (UserFormDto) target; + + validateUsername(errors, formDto); + validatePassword(errors, formDto); + validatePrivileges(errors, formDto); + } + + private void validatePrivileges(Errors errors, UserFormDto formDto) { + final List privileges = formDto.getPrivileges(); + if (privileges == null || privileges.isEmpty()) { + errors.rejectValue("privileges", null, "Privileges is required"); + } + } + + private void validatePassword(Errors errors, UserFormDto formDto) { + final String password = formDto.getPassword(); + if (StringUtils.isEmpty(password)) { + errors.rejectValue("password", null, "Password is required"); + } + } + + private void validateUsername(Errors errors, UserFormDto formDto) { + final String username = formDto.getUsername(); + if (StringUtils.isEmpty(username)) { + errors.rejectValue("username", null, "Username is required"); + return; + } + + boolean existed = userService.isExistedUsername(username); + if (existed) { + errors.rejectValue("username", null, "Username already existed"); + } + + } +} diff --git a/src/main/webapp/WEB-INF/jsp/user_form.jsp b/src/main/webapp/WEB-INF/jsp/user_form.jsp new file mode 100644 index 0000000..c2aa467 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/user_form.jsp @@ -0,0 +1,93 @@ +<%-- + * + * @author Shengzhao Li +--%> + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fun" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + + Add User + + +Home + +

Add User

+ + +
+ + +
+ + +

Username, unique.

+
+
+
+ + +
+ + +

Password, unique.

+
+
+
+ + +
+ + + +

Select Privilege(s).

+
+
+ +
+ + +
+ + +

User phone, optional.

+
+
+
+ + +
+ + +

User email, optional.

+
+
+ +
+
+
+ +
+
+ + +
+
+
+ + Cancel +
+
+
+ + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/user_overview.jsp b/src/main/webapp/WEB-INF/jsp/user_overview.jsp index 7591b18..47e2e1e 100644 --- a/src/main/webapp/WEB-INF/jsp/user_overview.jsp +++ b/src/main/webapp/WEB-INF/jsp/user_overview.jsp @@ -15,6 +15,9 @@

User Overview

+
+ Add User +