(139) - User Overview/ user add/archive

mongodb
Li Shengzhao 2016-03-25 17:50:48 +08:00
parent c4e12d10b5
commit 96e5b95232
10 changed files with 297 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -78,6 +78,12 @@ public class UserRepositoryJdbc implements UserRepository {
ps.setString(7, user.phone());
});
//get user id
//insert privileges
}
@Override

View File

@ -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);
}

View File

@ -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();

View File

@ -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";
}
}

View File

@ -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<Privilege> 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");
}
}
}

View File

@ -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" %>
<!DOCTYPE HTML>
<html>
<head>
<title>Add User</title>
</head>
<body>
<a href="${contextPath}/">Home</a>
<h2>Add User</h2>
<form:form commandName="formDto" cssClass="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">Username<em class="text-danger">*</em></label>
<div class="col-sm-10">
<form:input path="username" cssClass="form-control" placeholder="Type username"
required="required"/>
<p class="help-block">Username, unique.</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Password<em class="text-danger">*</em></label>
<div class="col-sm-10">
<form:password path="password" cssClass="form-control" placeholder="Type password"
required="required"/>
<p class="help-block">Password, unique.</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Privileges<em class="text-danger">*</em></label>
<div class="col-sm-10">
<label class="checkbox-inline">
<form:checkbox path="privileges" value="MOBILE"/> MOBILE
</label>
<label class="checkbox-inline">
<form:checkbox path="privileges" value="UNITY"/> UNITY
</label>
<p class="help-block">Select Privilege(s).</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<form:input path="phone" cssClass="form-control" placeholder="Type phone"/>
<p class="help-block">User phone, optional.</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<form:input path="email" cssClass="form-control" placeholder="Type email"/>
<p class="help-block">User email, optional.</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-2"></div>
<div class="col-sm-10">
<form:errors path="*" cssClass="label label-warning"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-2"></div>
<div class="col-sm-10">
<button type="submit" class="btn btn-success">Save</button>
<a href="../overview">Cancel</a>
</div>
</div>
</form:form>
</body>
</html>

View File

@ -15,6 +15,9 @@
<h2>User Overview</h2>
<div class="pull-right">
<a href="form/plus" class="btn btn-success btn-sm">Add User</a>
</div>
<form action="" class="form-inline">
<div class="form-group">
<input type="text" class="form-control" placeholder="Type username" name="username"

View File

@ -0,0 +1,20 @@
package com.monkeyk.sos.infrastructure;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/*
* @author Shengzhao Li
*/
public class PasswordHandlerTest {
@Test
public void testMd5() throws Exception {
final String md5 = PasswordHandler.md5("123456");
assertNotNull(md5);
System.out.println(md5);
}
}