You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
271 lines
11 KiB
Java
271 lines
11 KiB
Java
package com.glxp.api.controller.auth;
|
|
|
|
|
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import com.github.pagehelper.PageInfo;
|
|
import com.glxp.api.annotation.Log;
|
|
import com.glxp.api.constant.BusinessType;
|
|
import com.glxp.api.controller.BaseController;
|
|
import com.glxp.api.entity.auth.*;
|
|
import com.glxp.api.req.auth.FilterAuthUserRequest;
|
|
import com.glxp.api.req.auth.AuthUserSaveRequest;
|
|
import com.glxp.api.res.PageSimpleResponse;
|
|
import com.glxp.api.res.auth.AuthAdminResponse;
|
|
import com.glxp.api.res.auth.DeptUserResponse;
|
|
import com.glxp.api.service.auth.AuthAdminService;
|
|
import com.glxp.api.service.auth.CustomerInfoService;
|
|
import com.glxp.api.service.auth.DeptUserService;
|
|
import com.glxp.api.service.auth.ISysRoleService;
|
|
import com.glxp.api.util.PasswordUtils;
|
|
import com.glxp.api.util.StreamUtils;
|
|
import com.glxp.api.common.enums.ResultEnum;
|
|
import com.glxp.api.common.res.BaseResponse;
|
|
import com.glxp.api.common.util.ResultVOUtils;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.validation.BindingResult;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.validation.Valid;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* 用户信息
|
|
*
|
|
* @author Lion Li
|
|
*/
|
|
@Validated
|
|
@RequiredArgsConstructor
|
|
@RestController
|
|
@RequestMapping("/system/user")
|
|
public class SysUserController extends BaseController {
|
|
|
|
private final AuthAdminService userService;
|
|
private final ISysRoleService roleService;
|
|
@Resource
|
|
ISysRoleService sysRoleService;
|
|
|
|
@Resource
|
|
private CustomerInfoService customerInfoService;
|
|
|
|
@Resource
|
|
private AuthAdminService authAdminService;
|
|
|
|
@GetMapping("/list")
|
|
public BaseResponse list(FilterAuthUserRequest filterAuthUserRequest, BindingResult bindingResult) {
|
|
|
|
if (bindingResult.hasErrors()) {
|
|
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage());
|
|
}
|
|
List<AuthAdmin> authAdminList = userService.listAdminPage(filterAuthUserRequest);
|
|
//查询对应角色和部门
|
|
List<AuthAdminResponse> authAdminResponseList = authAdminList.stream().map(item -> {
|
|
AuthAdminResponse authAdminResponse = new AuthAdminResponse();
|
|
BeanUtils.copyProperties(item, authAdminResponse);
|
|
List<Long> roles = sysRoleService.selectRoleListByUserId(authAdminResponse.getId());
|
|
List<DeptUserResponse> deptUserResponses = deptUserService.selectByUserId(authAdminResponse.getId());
|
|
if (CollUtil.isNotEmpty(deptUserResponses)) {
|
|
List<Long> depts = new ArrayList<>();
|
|
String deptName = "";
|
|
for (DeptUserResponse deptUserResponse : deptUserResponses) {
|
|
depts.add(deptUserResponse.getDeptId());
|
|
deptName = deptName + "," + deptUserResponse.getDeptName();
|
|
}
|
|
authAdminResponse.setDeptName(deptName.substring(1));
|
|
authAdminResponse.setDepts(depts);
|
|
}
|
|
authAdminResponse.setRoles(roles);
|
|
return authAdminResponse;
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
PageInfo<AuthAdmin> authAdminPageInfo = new PageInfo<>(authAdminList);
|
|
PageSimpleResponse<AuthAdminResponse> authAdminPageSimpleResponse = new PageSimpleResponse<>();
|
|
authAdminPageSimpleResponse.setTotal(authAdminPageInfo.getTotal());
|
|
authAdminPageSimpleResponse.setList(authAdminResponseList);
|
|
|
|
return ResultVOUtils.success(authAdminPageSimpleResponse);
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据用户编号获取详细信息
|
|
*
|
|
* @param userId 用户ID
|
|
*/
|
|
@SaCheckPermission("system:user:query")
|
|
@GetMapping(value = {"/", "/{userId}"})
|
|
public BaseResponse getInfo(@PathVariable(value = "userId", required = false) Long userId) {
|
|
Map<String, Object> ajax = new HashMap<>();
|
|
List<SysRole> roles = roleService.selectRoleAll();
|
|
ajax.put("roles", isAdmin(userId) ? roles : StreamUtils.filter(roles, r -> !r.isAdmin()));
|
|
if (ObjectUtil.isNotNull(userId)) {
|
|
AuthAdmin sysUser = userService.findById(userId);
|
|
ajax.put("user", sysUser);
|
|
ajax.put("roleIds", StreamUtils.toList(sysUser.getRoles(), SysRole::getRoleId));
|
|
}
|
|
return ResultVOUtils.success(ajax);
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据用户编号获取授权角色
|
|
*
|
|
* @param userId 用户ID
|
|
*/
|
|
@SaCheckPermission("system:user:query")
|
|
@GetMapping("/authRole/{userId}")
|
|
public BaseResponse authRole(@PathVariable Long userId) {
|
|
AuthAdmin user = userService.findById(userId);
|
|
List<SysRole> roles = roleService.selectRolesByUserId(userId);
|
|
Map<String, Object> ajax = new HashMap<>();
|
|
ajax.put("user", user);
|
|
ajax.put("roles", isAdmin(userId) ? roles : StreamUtils.filter(roles, r -> !r.isAdmin()));
|
|
return ResultVOUtils.success(ajax);
|
|
}
|
|
|
|
/**
|
|
* 用户授权角色
|
|
*
|
|
* @param userId 用户Id
|
|
* @param roleIds 角色ID串
|
|
*/
|
|
@Log(title = "用户管理", businessType = BusinessType.GRANT)
|
|
@SaCheckPermission("system:user:edit")
|
|
@PutMapping("/authRole")
|
|
public BaseResponse inseinsertUserAuthrtAuthRole(Long userId, Long[] roleIds) {
|
|
userService.insertUserAuth(userId, CollUtil.toList(roleIds));
|
|
return ResultVOUtils.success("授权成功!");
|
|
}
|
|
|
|
@Log(title = "用户管理", businessType = BusinessType.INSERT)
|
|
@PostMapping("/save")
|
|
public BaseResponse save(@RequestBody @Valid AuthUserSaveRequest authUserSaveRequest,
|
|
BindingResult bindingResult) {
|
|
|
|
if (bindingResult.hasErrors()) {
|
|
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage());
|
|
}
|
|
|
|
AuthAdmin curUser = getUser();
|
|
//先获取账号最大用户量
|
|
CustomerInfoEntity customerInfoEntity = customerInfoService.selectById(curUser.getCustomerId());
|
|
//获取此账号下存在的用户量
|
|
List<AuthAdmin> byCustomerld = authAdminService.findByCustomerld(curUser.getCustomerId());
|
|
if (customerInfoEntity != null && customerInfoEntity.getUserMax() <= byCustomerld.size()) {
|
|
return ResultVOUtils.error(500, "已到达最大用户量");
|
|
}
|
|
|
|
// 检查是否存在相同名称的管理员
|
|
AuthAdmin byUserName = userService.findByUserName(authUserSaveRequest.getUserName());
|
|
if (byUserName != null) {
|
|
return ResultVOUtils.error(ResultEnum.DATA_REPEAT, "当前管理员已存在");
|
|
}
|
|
AuthAdmin authAdmin = new AuthAdmin();
|
|
BeanUtils.copyProperties(authUserSaveRequest, authAdmin);
|
|
if (authAdmin.getPassWord() != null) {
|
|
authAdmin.setPassWord(PasswordUtils.authAdminPwd(authAdmin.getPassWord()));
|
|
}
|
|
authAdmin.setCustomerId(curUser.getCustomerId());
|
|
authAdmin.setLastModifyTime(new Date());
|
|
boolean b = userService.insertAuthAdmin(authAdmin);
|
|
authAdmin = userService.findByUserName(authAdmin.getUserName());
|
|
if (!b) {
|
|
return ResultVOUtils.error(ResultEnum.NOT_NETWORK);
|
|
}
|
|
|
|
// 插入角色
|
|
if (authUserSaveRequest.getRoles().size() != 0) {
|
|
userService.insertUserAuth(authAdmin.getId(), authUserSaveRequest.getRoles());
|
|
}
|
|
//先获取角色id
|
|
SysUserRole sysUserRole1 = userService.selectByAuthId(curUser.getId());
|
|
SysUserRole sysUserRole = new SysUserRole();
|
|
sysUserRole.setUserId(authAdmin.getId());
|
|
sysUserRole.setRoleId(sysUserRole1.getRoleId());
|
|
userService.insertUserAuth1(sysUserRole);
|
|
|
|
|
|
//插入部门
|
|
if (CollUtil.isNotEmpty(authUserSaveRequest.getDepts())) {
|
|
List<DeptUserEntity> list = new ArrayList<>();
|
|
for (int i = 0; i < authUserSaveRequest.getDepts().size(); i++) {
|
|
DeptUserEntity deptUserEntity = new DeptUserEntity();
|
|
deptUserEntity.setUserId(authAdmin.getId());
|
|
deptUserEntity.setDeptId(authUserSaveRequest.getDepts().get(i));
|
|
list.add(deptUserEntity);
|
|
}
|
|
deptUserService.insertBatch(list);
|
|
} else {
|
|
|
|
}
|
|
|
|
Map<String, Long> res = new HashMap<>();
|
|
res.put("id", authAdmin.getId());
|
|
return ResultVOUtils.success(res);
|
|
}
|
|
|
|
|
|
@Resource
|
|
DeptUserService deptUserService;
|
|
|
|
/**
|
|
* 修改用户
|
|
*/
|
|
@Log(title = "用户管理", businessType = BusinessType.UPDATE)
|
|
@PostMapping("/edit")
|
|
public BaseResponse edit(@RequestBody @Valid AuthUserSaveRequest authUserSaveRequest,
|
|
BindingResult bindingResult) {
|
|
|
|
if (bindingResult.hasErrors()) {
|
|
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage());
|
|
}
|
|
if (authUserSaveRequest.getId() == null) {
|
|
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, "参数错误!");
|
|
}
|
|
// 检查是否存在除了当前管理员的其它名称的管理员
|
|
AuthAdmin byUserName = userService.findByUserName(authUserSaveRequest.getUserName());
|
|
if (byUserName != null && !authUserSaveRequest.getId().equals(byUserName.getId())) {
|
|
return ResultVOUtils.error(ResultEnum.DATA_REPEAT, "当前管理员已存在");
|
|
}
|
|
|
|
AuthAdmin authAdmin = new AuthAdmin();
|
|
BeanUtils.copyProperties(authUserSaveRequest, authAdmin);
|
|
if (authAdmin.getPassWord() != null) {
|
|
authAdmin.setPassWord(PasswordUtils.authAdminPwd(authAdmin.getPassWord()));
|
|
}
|
|
authAdmin.setLastModifyTime(new Date());
|
|
boolean b = userService.updateAuthAdmin(authAdmin);
|
|
|
|
if (!b) {
|
|
return ResultVOUtils.error(ResultEnum.NOT_NETWORK);
|
|
}
|
|
// 修改角色
|
|
// userService.insertUserAuth(authAdmin.getId(), authUserSaveRequest.getRoles());
|
|
|
|
userService.updateAuthAdmin(authAdmin);
|
|
|
|
|
|
//修改所属部门
|
|
deptUserService.deleteByUser(authUserSaveRequest.getId());
|
|
if (CollUtil.isNotEmpty(authUserSaveRequest.getDepts())) {
|
|
List<DeptUserEntity> list = new ArrayList<>();
|
|
for (int i = 0; i < authUserSaveRequest.getDepts().size(); i++) {
|
|
DeptUserEntity deptUserEntity = new DeptUserEntity();
|
|
deptUserEntity.setUserId(authUserSaveRequest.getId());
|
|
deptUserEntity.setDeptId(authUserSaveRequest.getDepts().get(i));
|
|
list.add(deptUserEntity);
|
|
}
|
|
deptUserService.insertBatch(list);
|
|
}
|
|
return ResultVOUtils.success();
|
|
}
|
|
|
|
|
|
}
|