1
0
mirror of https://github.com/FatttSnake/Pinnacle-OA.git synced 2026-04-05 15:01:23 +08:00

Added UserManagement

This commit is contained in:
2023-05-17 15:39:28 +08:00
parent e1bdb21756
commit e5a3cb83be
13 changed files with 656 additions and 58 deletions

View File

@@ -1,14 +1,13 @@
package com.cfive.pinnacle.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cfive.pinnacle.entity.User;
import com.cfive.pinnacle.entity.common.ResponseCode;
import com.cfive.pinnacle.entity.common.ResponseResult;
import com.cfive.pinnacle.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -32,13 +31,54 @@ public class UserController {
@GetMapping
public ResponseResult getAllUser() {
List<User> users = userService.getBasicInfo();
return ResponseResult.build(ResponseCode.DATABASE_SELECT_OK, "success", users);
List<User> users = userService.getAllUser();
return ResponseResult.databaseSelectSuccess(users);
}
@GetMapping("/{id}")
public ResponseResult getUser(@PathVariable int id) {
User user = userService.getBasicInfo(id);
return ResponseResult.build(ResponseCode.DATABASE_SELECT_OK, "success", user);
public ResponseResult getUser(@PathVariable Long id) {
User user = userService.getUser(id);
return ResponseResult.databaseSelectSuccess(user);
}
@PostMapping
public ResponseResult addUser(@RequestBody User user) {
if (!StringUtils.hasText(user.getUsername())) {
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "Username cannot be empty", null);
}
if (!StringUtils.hasText(user.getPasswd())) {
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "Password cannot be empty", null);
}
if (userService.addUser(user)) {
return ResponseResult.databaseSaveSuccess(user);
} else {
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "error", null);
}
}
@DeleteMapping("/{id}")
public ResponseResult deleteRole(@PathVariable Long id) {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getId, id);
if (userService.remove(wrapper)) {
return ResponseResult.databaseDeleteSuccess();
} else {
return ResponseResult.build(ResponseCode.DATABASE_DELETE_ERROR, "error", null);
}
}
@PutMapping()
public ResponseResult modifyRole(@RequestBody User user) {
if (!StringUtils.hasText(user.getUsername())) {
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "Username cannot be empty", null);
}
if (!StringUtils.hasText(user.getPasswd())) {
user.setPasswd(null);
}
if (userService.modifyUser(user)) {
return ResponseResult.databaseUpdateSuccess(user);
} else {
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "error", null);
}
}
}

View File

@@ -2,12 +2,12 @@ package com.cfive.pinnacle.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
@@ -59,9 +59,14 @@ public class User implements Serializable {
@TableField("enable")
private Integer enable;
@TableField(exist = false)
private List<Role> roles;
@TableField(exist = false)
private List<Group> groups;
@TableField("deleted")
@TableLogic
private Integer deleted;
private Long deleted;
@TableField("version")
@Version

View File

@@ -1,6 +1,8 @@
package com.cfive.pinnacle.handler;
import com.cfive.pinnacle.entity.common.ResponseCode;
import com.cfive.pinnacle.entity.common.ResponseResult;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -8,6 +10,9 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
public class CustomExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ResponseResult exceptionHandler(Exception e) {
if (e instanceof DuplicateKeyException) {
return ResponseResult.build(ResponseCode.DATABASE_SAVE_ERROR, "无法添加重复数据", null);
}
return ResponseResult.fail(e.getClass().toString() + ": " + e.getMessage());
}
}

View File

@@ -3,6 +3,9 @@ package com.cfive.pinnacle.mapper;
import com.cfive.pinnacle.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* <p>
@@ -14,5 +17,7 @@ import org.apache.ibatis.annotations.Mapper;
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> getAll();
User getOneById(@Param("id") long id);
}

View File

@@ -14,7 +14,12 @@ import java.util.List;
* @since 2023-04-30
*/
public interface IUserService extends IService<User> {
List<User> getBasicInfo();
User getBasicInfo(int id);
List<User> getAllUser();
User getUser(long id);
boolean addUser(User user);
boolean modifyUser(User user);
}

View File

@@ -1,13 +1,19 @@
package com.cfive.pinnacle.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cfive.pinnacle.entity.User;
import com.cfive.pinnacle.entity.*;
import com.cfive.pinnacle.mapper.UserGroupMapper;
import com.cfive.pinnacle.mapper.UserMapper;
import com.cfive.pinnacle.mapper.UserRoleMapper;
import com.cfive.pinnacle.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.HashSet;
import java.util.List;
/**
@@ -21,28 +27,120 @@ import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
private UserMapper userMapper;
private UserRoleMapper userRoleMapper;
private UserGroupMapper userGroupMapper;
private PasswordEncoder passwordEncoder;
@Autowired
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Autowired
public void setUserRoleMapper(UserRoleMapper userRoleMapper) {
this.userRoleMapper = userRoleMapper;
}
@Autowired
public void setUserGroupMapper(UserGroupMapper userGroupMapper) {
this.userGroupMapper = userGroupMapper;
}
@Autowired
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
this.passwordEncoder = passwordEncoder;
}
@Override
public List<User> getBasicInfo() {
List<User> users = userMapper.selectList(null);
users.forEach(user -> {
user.setPasswd("");
public List<User> getAllUser() {
return userMapper.getAll();
}
@Override
public User getUser(long id) {
return userMapper.getOneById(id);
}
@Override
@Transactional
public boolean addUser(User user) {
String encryptedPassword = passwordEncoder.encode(user.getPasswd());
user.setPasswd(encryptedPassword);
if (userMapper.insert(user) == 1) {
user.getRoles().forEach(role -> {
UserRole userRole = new UserRole();
userRole.setUserId(user.getId());
userRole.setRoleId(role.getId());
if (userRoleMapper.insert(userRole) != 1) {
throw new RuntimeException("Add user_role failure");
}
});
user.getGroups().forEach(group -> {
UserGroup userGroup = new UserGroup();
userGroup.setUserId(user.getId());
userGroup.setGroupId(group.getId());
if (userGroupMapper.insert(userGroup) != 1) {
throw new RuntimeException("Add user_group failure");
}
});
return true;
} else {
throw new RuntimeException("Add group failure");
}
}
@Override
@Transactional
public boolean modifyUser(User user) {
if (StringUtils.hasText(user.getPasswd())) {
String encryptedPassword = passwordEncoder.encode(user.getPasswd());
user.setPasswd(encryptedPassword);
}
userMapper.updateById(user);
User originalUser = getUser(user.getId());
HashSet<Long> newRoleIds = new HashSet<>();
HashSet<Long> newGroupIds = new HashSet<>();
user.getRoles().forEach(role -> newRoleIds.add(role.getId()));
user.getGroups().forEach(group -> newGroupIds.add(group.getId()));
HashSet<Long> addRoleIds = new HashSet<>(newRoleIds);
HashSet<Long> addGroupIds = new HashSet<>(newGroupIds);
if (originalUser != null) {
HashSet<Long> originalRoleIds = new HashSet<>();
HashSet<Long> originalGroupIds = new HashSet<>();
originalUser.getRoles().forEach(role -> originalRoleIds.add(role.getId()));
originalUser.getGroups().forEach(group -> originalGroupIds.add(group.getId()));
HashSet<Long> deleteRoleIds = new HashSet<>(originalRoleIds);
HashSet<Long> deleteGroupIds = new HashSet<>(originalGroupIds);
deleteRoleIds.removeAll(newRoleIds);
deleteGroupIds.removeAll(newGroupIds);
addRoleIds.removeAll(originalRoleIds);
addGroupIds.removeAll(originalGroupIds);
deleteRoleIds.forEach(deleteRoleId -> {
LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserRole::getUserId, user.getId())
.eq(UserRole::getRoleId, deleteRoleId);
userRoleMapper.delete(wrapper);
});
deleteGroupIds.forEach(deleteGroupId -> {
LambdaQueryWrapper<UserGroup> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserGroup::getUserId, user.getId())
.eq(UserGroup::getGroupId, deleteGroupId);
userGroupMapper.delete(wrapper);
});
}
addRoleIds.forEach(addRoleId -> {
UserRole userRole = new UserRole();
userRole.setUserId(user.getId());
userRole.setRoleId(addRoleId);
userRoleMapper.insert(userRole);
});
addGroupIds.forEach(addGroupId -> {
UserGroup userGroup = new UserGroup();
userGroup.setUserId(user.getId());
userGroup.setGroupId(addGroupId);
userGroupMapper.insert(userGroup);
});
return users;
}
@Override
public User getBasicInfo(int id) {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getId, id);
User user = userMapper.selectOne(wrapper);
user.setPasswd("");
return user;
return true;
}
}

View File

@@ -5,6 +5,7 @@ import com.cfive.pinnacle.entity.User;
import com.cfive.pinnacle.entity.permission.LoginUser;
import com.cfive.pinnacle.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -16,6 +17,7 @@ import java.util.Objects;
public class UserDetailsServiceImpl implements UserDetailsService {
private IUserService userService;
@Lazy
@Autowired
public void setUserService(IUserService userService) {
this.userService = userService;

View File

@@ -14,6 +14,6 @@ mybatis-plus:
db-config:
logic-delete-field: deleted
logic-not-delete-value: 0
logic-delete-value: 1
logic-delete-value: id
id-type: assign_id
type-aliases-package: com.cfive.pinnacle.entity

View File

@@ -2,4 +2,70 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cfive.pinnacle.mapper.UserMapper">
<select id="getAll" resultMap="userMap">
select t_user.id as user_id,
t_user.username as user_username,
t_user.department_id as user_department,
t_user.enable as user_enable,
t_user.deleted as user_deleted,
t_user.version as user_version,
tr.id as role_id,
tr.name as role_name,
tr.deleted as role_deleted,
tr.version as role_version,
tg.id as group_id,
tg.name as group_name,
tg.deleted as group_deleted,
tg.version as group_version
from t_user
left join (select * from t_user_role where deleted = 0) as tur on t_user.id = tur.user_id
left join (select * from t_role where deleted = 0) as tr on tr.id = tur.role_id
left join (select * from t_user_group where deleted = 0) as tug on t_user.id = tug.user_id
left join (select * from t_group where deleted = 0) as tg on tg.id = tug.group_id
where t_user.deleted = 0;
</select>
<select id="getOneById" resultMap="userMap">
select t_user.id as user_id,
t_user.username as user_username,
t_user.department_id as user_department,
t_user.enable as user_enable,
t_user.deleted as user_deleted,
t_user.version as user_version,
tr.id as role_id,
tr.name as role_name,
tr.deleted as role_deleted,
tr.version as role_version,
tg.id as group_id,
tg.name as group_name,
tg.deleted as group_deleted,
tg.version as group_version
from t_user
left join (select * from t_user_role where deleted = 0) as tur on t_user.id = tur.user_id
left join (select * from t_role where deleted = 0) as tr on tr.id = tur.role_id
left join (select * from t_user_group where deleted = 0) as tug on t_user.id = tug.user_id
left join (select * from t_group where deleted = 0) as tg on tg.id = tug.group_id
where t_user.deleted = 0
and t_user.id = #{id};
</select>
<resultMap id="userMap" type="user">
<id property="id" column="user_id"/>
<result property="username" column="user_username"/>
<result property="departmentId" column="user_department"/>
<result property="enable" column="user_enable"/>
<result property="deleted" column="user_deleted"/>
<result property="version" column="user_version"/>
<collection property="roles" ofType="role">
<id property="id" column="role_id"/>
<result property="name" column="role_name"/>
<result property="deleted" column="role_deleted"/>
<result property="version" column="role_version"/>
</collection>
<collection property="groups" ofType="group">
<id property="id" column="group_id"/>
<result property="name" column="group_name"/>
<result property="deleted" column="group_deleted"/>
<result property="version" column="group_version"/>
</collection>
</resultMap>
</mapper>