Commit 3f753954 authored by suntao's avatar suntao

用户User add

用户role set改成list
parent 2264c603
...@@ -4,6 +4,7 @@ import cn.quantgroup.cashloanflowboss.api.role.entity.Role; ...@@ -4,6 +4,7 @@ import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserRank; import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserRank;
import lombok.Data; import lombok.Data;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
...@@ -31,7 +32,7 @@ public class Principal { ...@@ -31,7 +32,7 @@ public class Principal {
/** /**
* 角色列表 * 角色列表
*/ */
private Set<Role> roles; private List<Role> roles;
/** /**
* 是否是超级管理员 * 是否是超级管理员
......
...@@ -3,7 +3,7 @@ package cn.quantgroup.cashloanflowboss.api.login.service; ...@@ -3,7 +3,7 @@ package cn.quantgroup.cashloanflowboss.api.login.service;
import cn.quantgroup.cashloanflowboss.api.login.model.Principal; import cn.quantgroup.cashloanflowboss.api.login.model.Principal;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus; import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.api.user.entity.User; import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.api.user.service.UserService; import cn.quantgroup.cashloanflowboss.api.user.service.UserServiceImpl;
import cn.quantgroup.cashloanflowboss.core.asserts.Assert; import cn.quantgroup.cashloanflowboss.core.asserts.Assert;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationDictionary; import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationDictionary;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus; import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus;
...@@ -11,7 +11,6 @@ import cn.quantgroup.cashloanflowboss.utils.JSONTools; ...@@ -11,7 +11,6 @@ import cn.quantgroup.cashloanflowboss.utils.JSONTools;
import cn.quantgroup.cashloanflowboss.utils.MD5Tools; import cn.quantgroup.cashloanflowboss.utils.MD5Tools;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -25,7 +24,7 @@ import javax.servlet.http.HttpSession; ...@@ -25,7 +24,7 @@ import javax.servlet.http.HttpSession;
public class LogService { public class LogService {
@Autowired @Autowired
private UserService userService; private UserServiceImpl userService;
@Autowired @Autowired
private HttpServletRequest request; private HttpServletRequest request;
......
...@@ -4,7 +4,9 @@ import cn.quantgroup.cashloanflowboss.api.user.entity.User; ...@@ -4,7 +4,9 @@ import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.api.user.model.Pagination; import cn.quantgroup.cashloanflowboss.api.user.model.Pagination;
import cn.quantgroup.cashloanflowboss.api.user.model.RegisterUserFormModel; import cn.quantgroup.cashloanflowboss.api.user.model.RegisterUserFormModel;
import cn.quantgroup.cashloanflowboss.api.user.model.UserDetailInfo; import cn.quantgroup.cashloanflowboss.api.user.model.UserDetailInfo;
import cn.quantgroup.cashloanflowboss.api.user.service.UserService; import cn.quantgroup.cashloanflowboss.api.user.model.UserInfoModel;
import cn.quantgroup.cashloanflowboss.api.user.service.UserServiceImpl;
import cn.quantgroup.cashloanflowboss.component.security.annotiation.Security;
import cn.quantgroup.cashloanflowboss.component.validator.constraints.NotEmpty; import cn.quantgroup.cashloanflowboss.component.validator.constraints.NotEmpty;
import cn.quantgroup.cashloanflowboss.core.base.Result; import cn.quantgroup.cashloanflowboss.core.base.Result;
import cn.quantgroup.cashloanflowboss.spi.user.service.XyqbUserService; import cn.quantgroup.cashloanflowboss.spi.user.service.XyqbUserService;
...@@ -28,7 +30,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -28,7 +30,7 @@ import org.springframework.web.bind.annotation.RestController;
public class UserController { public class UserController {
@Autowired @Autowired
private UserService userService; private UserServiceImpl userService;
@Autowired @Autowired
private XyqbUserService xyqbUserService; private XyqbUserService xyqbUserService;
...@@ -43,6 +45,19 @@ public class UserController { ...@@ -43,6 +45,19 @@ public class UserController {
return this.userService.createUser(registerUserFormModel.getUsername(), registerUserFormModel.getPassword()); return this.userService.createUser(registerUserFormModel.getUsername(), registerUserFormModel.getPassword());
} }
/**
* 添加用户
*
* @param userInfoModel
* @return
*/
@Security(authorityId = "User.addUser")
@PostMapping("saveUserInfo")
public Result<User> saveUserInfo(@RequestBody UserInfoModel userInfoModel) {
User info = this.userService.saveUserInfo(userInfoModel);
return Result.buildSuccess(info);
}
/** /**
* 获取用户列表(分页) * 获取用户列表(分页)
* *
......
...@@ -8,6 +8,7 @@ import lombok.Data; ...@@ -8,6 +8,7 @@ import lombok.Data;
import javax.persistence.*; import javax.persistence.*;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
...@@ -24,6 +25,12 @@ public class User extends Primary { ...@@ -24,6 +25,12 @@ public class User extends Primary {
@Column(name = "username") @Column(name = "username")
private String username; private String username;
/**
* 昵称
*/
@Column(name = "nickname")
private String nickname;
/** /**
* 密码 * 密码
*/ */
...@@ -48,7 +55,7 @@ public class User extends Primary { ...@@ -48,7 +55,7 @@ public class User extends Primary {
*/ */
@OneToMany(fetch = FetchType.EAGER) @OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_role_mapping", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id")) @JoinTable(name = "user_role_mapping", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles; private List<Role> roles;
/** /**
* 用户状态 * 用户状态
......
...@@ -2,6 +2,7 @@ package cn.quantgroup.cashloanflowboss.api.user.model; ...@@ -2,6 +2,7 @@ package cn.quantgroup.cashloanflowboss.api.user.model;
import cn.quantgroup.cashloanflowboss.api.login.model.Principal; import cn.quantgroup.cashloanflowboss.api.login.model.Principal;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role; import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.core.asserts.Assert; import cn.quantgroup.cashloanflowboss.core.asserts.Assert;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus; import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
...@@ -33,15 +34,16 @@ public class UserDetailInfo { ...@@ -33,15 +34,16 @@ public class UserDetailInfo {
public static class UserInfo { public static class UserInfo {
private Long userId; private Long userId;
private String userName; private String userName;
private String nickname;
} }
public static UserDetailInfo valueOf(Principal principal) { public static UserDetailInfo valueOf(Principal principal, User user) {
Assert.isNull(principal, ApplicationStatus.INVALID_USER); Assert.isNull(principal, ApplicationStatus.INVALID_USER);
UserDetailInfo userDetailInfo = new UserDetailInfo(); UserDetailInfo userDetailInfo = new UserDetailInfo();
Long userId = principal.getUserId(); Long userId = principal.getUserId();
userDetailInfo.setRank(principal.getRank().name()); userDetailInfo.setRank(principal.getRank().name());
userDetailInfo.setChannelId(principal.getChannelId()); userDetailInfo.setChannelId(principal.getChannelId());
Set<Role> roles = principal.getRoles(); List<Role> roles = principal.getRoles();
if (null != roles && roles.size() > 0) { if (null != roles && roles.size() > 0) {
RoleInfo roleInfo = new RoleInfo(); RoleInfo roleInfo = new RoleInfo();
...@@ -58,6 +60,8 @@ public class UserDetailInfo { ...@@ -58,6 +60,8 @@ public class UserDetailInfo {
UserInfo userInfo = new UserInfo(); UserInfo userInfo = new UserInfo();
userInfo.setUserId(userId); userInfo.setUserId(userId);
userInfo.setNickname(user.getNickname());
userInfo.setUserName(user.getUsername());
userDetailInfo.setUserInfo(userInfo); userDetailInfo.setUserInfo(userInfo);
return userDetailInfo; return userDetailInfo;
......
package cn.quantgroup.cashloanflowboss.api.user.model;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserRank;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.core.persistence.Primary;
import lombok.Data;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Set;
/**
* Created by WeiWei on 2019/7/22.
*/
@Data
public class UserInfoModel {
private Long id;
/**
* 用户名
*/
private String username;
/**
* 昵称
*/
private String nickname;
/**
* 密码
*/
private String password;
/**
* 渠道ID
*/
private Long channelId;
/**
* 用户级别
*/
private UserRank rank;
/**
* 用户角色列表
*/
private List<Role> roles;
/**
* 用户状态
*/
private UserStatus status;
}
...@@ -22,6 +22,14 @@ public interface UserRepository extends PagingAndSortingRepository<User, String> ...@@ -22,6 +22,14 @@ public interface UserRepository extends PagingAndSortingRepository<User, String>
*/ */
User getUserByUsername(String username); User getUserByUsername(String username);
/**
* 获取用户
*
* @param id
* @return
*/
User findById(Long id);
/** /**
* 获取用户列表(分页) * 获取用户列表(分页)
* *
......
package cn.quantgroup.cashloanflowboss.api.user.service; package cn.quantgroup.cashloanflowboss.api.user.service;
import cn.quantgroup.cashloanflowboss.api.login.model.Principal;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.api.user.entity.User; import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.api.user.model.UserDetailInfo; import cn.quantgroup.cashloanflowboss.api.user.model.UserDetailInfo;
import cn.quantgroup.cashloanflowboss.api.user.repository.UserRepository; import cn.quantgroup.cashloanflowboss.api.user.model.UserInfoModel;
import cn.quantgroup.cashloanflowboss.core.Application;
import cn.quantgroup.cashloanflowboss.core.asserts.Assert;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus;
import cn.quantgroup.cashloanflowboss.utils.MD5Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.Objects;
/** /**
* Created by WeiWei on 2019/7/22. * function:
*/ * date: 2019/9/19
@Slf4j
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
/**
* 创建用户
* *
* @param username 用户名 * @author: suntao
* @param password 登录密码(明文)
* @return
*/ */
public boolean createUser(String username, String password) { public interface UserService {
boolean createUser(String username, String password);
User user = new User();
user.setUsername(username);
user.setPassword(MD5Tools.md5(password));
user.setStatus(UserStatus.ENABLED);
return Objects.nonNull(this.userRepository.save(user));
}
/**
* 获取用户
*
* @param username 用户名
* @return
*/
public User getUser(String username) {
return this.userRepository.getUserByUsername(username);
}
/**
* 获取用户列表(分页)
*
* @param pageNumber
* @param pageSize
* @return
*/
public Page<User> getUsers(Integer pageNumber, Integer pageSize) {
return this.userRepository.findAllBy(new PageRequest(pageNumber, pageSize));
}
/**
* 启用用户
*
* @param id 用户ID
* @return
*/
public boolean enableUser(String id) {
User user = this.userRepository.findOne(id);
// 检查用户是否存在 User getUser(String username);
Assert.isNull(user, ApplicationStatus.INVALID_USER);
user.setStatus(UserStatus.ENABLED); Page<User> getUsers(Integer pageNumber, Integer pageSize);
return Objects.nonNull(this.userRepository.save(user));
}
/**
* 禁用用户
*
* @param id 用户ID
* @return
*/
public boolean disableUser(String id) {
User user = this.userRepository.findOne(id);
// 检查用户是否存在
Assert.isNull(user, ApplicationStatus.INVALID_USER);
user.setStatus(UserStatus.DISABLED);
return Objects.nonNull(this.userRepository.save(user));
}
/**
* 更新用户
*
* @param user
* @return
*/
public User updateUser(User user) {
return this.userRepository.save(user);
}
/**
* 移除用户
*
* @param id 用户ID
* @return
*/
public Boolean removeUser(String id) {
try { boolean enableUser(String id);
this.userRepository.delete(id);
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return false; boolean disableUser(String id);
} User updateUser(User user);
public UserDetailInfo getUserDetailInfo() { Boolean removeUser(String id);
Principal principal = Application.getPrincipal();
Assert.isNull(principal, ApplicationStatus.INVALID_USER);
return UserDetailInfo.valueOf(principal); UserDetailInfo getUserDetailInfo();
}
User saveUserInfo(UserInfoModel user);
} }
package cn.quantgroup.cashloanflowboss.api.user.service;
import cn.quantgroup.cashloanflowboss.api.login.model.Principal;
import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.api.user.entity.User;
import cn.quantgroup.cashloanflowboss.api.user.model.UserDetailInfo;
import cn.quantgroup.cashloanflowboss.api.user.model.UserInfoModel;
import cn.quantgroup.cashloanflowboss.api.user.repository.UserRepository;
import cn.quantgroup.cashloanflowboss.core.Application;
import cn.quantgroup.cashloanflowboss.core.asserts.Assert;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus;
import cn.quantgroup.cashloanflowboss.utils.MD5Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Objects;
/**
* Created by WeiWei on 2019/7/22.
*/
@Slf4j
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserRepository userRepository;
/**
* 创建用户
*
* @param username 用户名
* @param password 登录密码(明文)
* @return
*/
@Override
public boolean createUser(String username, String password) {
User user = new User();
user.setUsername(username);
user.setPassword(MD5Tools.md5(password));
user.setStatus(UserStatus.ENABLED);
return Objects.nonNull(this.userRepository.save(user));
}
/**
* 获取用户
*
* @param username 用户名
* @return
*/
@Override
public User getUser(String username) {
return this.userRepository.getUserByUsername(username);
}
/**
* 获取用户列表(分页)
*
* @param pageNumber
* @param pageSize
* @return
*/
@Override
public Page<User> getUsers(Integer pageNumber, Integer pageSize) {
return this.userRepository.findAllBy(new PageRequest(pageNumber, pageSize));
}
/**
* 启用用户
*
* @param id 用户ID
* @return
*/
@Override
public boolean enableUser(String id) {
User user = this.userRepository.findOne(id);
// 检查用户是否存在
Assert.isNull(user, ApplicationStatus.INVALID_USER);
user.setStatus(UserStatus.ENABLED);
return Objects.nonNull(this.userRepository.save(user));
}
/**
* 禁用用户
*
* @param id 用户ID
* @return
*/
@Override
public boolean disableUser(String id) {
User user = this.userRepository.findOne(id);
// 检查用户是否存在
Assert.isNull(user, ApplicationStatus.INVALID_USER);
user.setStatus(UserStatus.DISABLED);
return Objects.nonNull(this.userRepository.save(user));
}
/**
* 更新用户
*
* @param user
* @return
*/
@Override
public User updateUser(User user) {
return this.userRepository.save(user);
}
/**
* 移除用户
*
* @param id 用户ID
* @return
*/
@Override
public Boolean removeUser(String id) {
try {
this.userRepository.delete(id);
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return false;
}
@Override
public UserDetailInfo getUserDetailInfo() {
Principal principal = Application.getPrincipal();
Assert.isNull(principal, ApplicationStatus.INVALID_USER);
User user = userRepository.findById(principal.getUserId());
return UserDetailInfo.valueOf(principal, user);
}
@Override
public User saveUserInfo(UserInfoModel userInfoModel) {
User user1 = new User();
BeanUtils.copyProperties(userInfoModel, user1);
user1.setRoles(userInfoModel.getRoles());
return userRepository.save(user1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment