| | |
| | | package com.ruoyi.system.controller;
|
| | |
|
| | | import java.io.IOException;
|
| | | import java.util.Date;
|
| | | import java.util.List;
|
| | | import java.util.Set;
|
| | | import java.util.stream.Collectors;
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | | import jakarta.servlet.http.HttpServletResponse;
|
| | | import org.apache.commons.lang3.ArrayUtils;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | | import org.springframework.validation.annotation.Validated;
|
| | |
| | | import org.springframework.web.bind.annotation.RequestMapping;
|
| | | import org.springframework.web.bind.annotation.RestController;
|
| | | import org.springframework.web.multipart.MultipartFile;
|
| | | import com.ruoyi.common.core.constant.UserConstants;
|
| | | import com.ruoyi.common.core.domain.R;
|
| | | import com.ruoyi.common.core.text.Convert;
|
| | | import com.ruoyi.common.core.utils.DateUtils;
|
| | | import com.ruoyi.common.core.utils.StringUtils;
|
| | | import com.ruoyi.common.core.utils.poi.ExcelUtil;
|
| | | import com.ruoyi.common.core.web.controller.BaseController;
|
| | |
| | | import com.ruoyi.common.log.enums.BusinessType;
|
| | | import com.ruoyi.common.security.annotation.InnerAuth;
|
| | | import com.ruoyi.common.security.annotation.RequiresPermissions;
|
| | | import com.ruoyi.common.security.service.TokenService;
|
| | | import com.ruoyi.common.security.utils.SecurityUtils;
|
| | | import com.ruoyi.system.api.domain.SysDept;
|
| | | import com.ruoyi.system.api.domain.SysRole;
|
| | | import com.ruoyi.system.api.domain.SysUser;
|
| | | import com.ruoyi.system.api.model.LoginUser;
|
| | | import com.ruoyi.system.service.ISysConfigService;
|
| | | import com.ruoyi.system.service.ISysDeptService;
|
| | | import com.ruoyi.system.service.ISysPermissionService;
|
| | | import com.ruoyi.system.service.ISysPostService;
|
| | | import com.ruoyi.system.service.ISysRoleService;
|
| | |
| | | private ISysRoleService roleService;
|
| | |
|
| | | @Autowired
|
| | | private ISysDeptService deptService;
|
| | |
|
| | | @Autowired
|
| | | private ISysPostService postService;
|
| | |
|
| | | @Autowired
|
| | |
| | |
|
| | | @Autowired
|
| | | private ISysConfigService configService;
|
| | |
|
| | | @Autowired
|
| | | private TokenService tokenService;
|
| | |
|
| | | /**
|
| | | * 获取用户列表
|
| | |
| | | List<SysUser> userList = util.importExcel(file.getInputStream());
|
| | | String operName = SecurityUtils.getUsername();
|
| | | String message = userService.importUser(userList, updateSupport, operName);
|
| | | return AjaxResult.success(message);
|
| | | return success(message);
|
| | | }
|
| | |
|
| | | @PostMapping("/importTemplate")
|
| | |
| | | return R.fail("用户名或密码错误");
|
| | | }
|
| | | // 角色集合
|
| | | Set<String> roles = permissionService.getRolePermission(sysUser.getUserId());
|
| | | Set<String> roles = permissionService.getRolePermission(sysUser);
|
| | | // 权限集合
|
| | | Set<String> permissions = permissionService.getMenuPermission(sysUser.getUserId());
|
| | | Set<String> permissions = permissionService.getMenuPermission(sysUser);
|
| | | LoginUser sysUserVo = new LoginUser();
|
| | | sysUserVo.setSysUser(sysUser);
|
| | | sysUserVo.setRoles(roles);
|
| | |
| | | {
|
| | | return R.fail("当前系统没有开启注册功能!");
|
| | | }
|
| | | if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(username)))
|
| | | if (!userService.checkUserNameUnique(sysUser))
|
| | | {
|
| | | return R.fail("保存用户'" + username + "'失败,注册账号已存在");
|
| | | }
|
| | | return R.ok(userService.registerUser(sysUser));
|
| | | }
|
| | |
|
| | | /**
|
| | | *记录用户登录IP地址和登录时间
|
| | | */
|
| | | @InnerAuth
|
| | | @PutMapping("/recordlogin")
|
| | | public R<Boolean> recordlogin(@RequestBody SysUser sysUser)
|
| | | {
|
| | | return R.ok(userService.updateLoginInfo(sysUser));
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | @GetMapping("getInfo")
|
| | | public AjaxResult getInfo()
|
| | | {
|
| | | Long userId = SecurityUtils.getUserId();
|
| | | LoginUser loginUser = SecurityUtils.getLoginUser();
|
| | | SysUser user = loginUser.getSysUser();
|
| | | // 角色集合
|
| | | Set<String> roles = permissionService.getRolePermission(userId);
|
| | | Set<String> roles = permissionService.getRolePermission(user);
|
| | | // 权限集合
|
| | | Set<String> permissions = permissionService.getMenuPermission(userId);
|
| | | Set<String> permissions = permissionService.getMenuPermission(user);
|
| | | if (!loginUser.getPermissions().equals(permissions))
|
| | | {
|
| | | loginUser.setPermissions(permissions);
|
| | | tokenService.refreshToken(loginUser);
|
| | | }
|
| | | AjaxResult ajax = AjaxResult.success();
|
| | | ajax.put("user", userService.selectUserById(userId));
|
| | | ajax.put("user", user);
|
| | | ajax.put("roles", roles);
|
| | | ajax.put("permissions", permissions);
|
| | | ajax.put("pwdChrtype", getSysAccountChrtype());
|
| | | ajax.put("isDefaultModifyPwd", initPasswordIsModify(user.getPwdUpdateDate()));
|
| | | ajax.put("isPasswordExpired", passwordIsExpiration(user.getPwdUpdateDate()));
|
| | | return ajax;
|
| | | }
|
| | |
|
| | | // 获取用户密码自定义配置规则
|
| | | public String getSysAccountChrtype()
|
| | | {
|
| | | return Convert.toStr(configService.selectConfigByKey("sys.account.chrtype"), "0");
|
| | | }
|
| | |
|
| | | // 检查初始密码是否提醒修改
|
| | | public boolean initPasswordIsModify(Date pwdUpdateDate)
|
| | | {
|
| | | Integer initPasswordModify = Convert.toInt(configService.selectConfigByKey("sys.account.initPasswordModify"));
|
| | | return initPasswordModify != null && initPasswordModify == 1 && pwdUpdateDate == null;
|
| | | }
|
| | |
|
| | | // 检查密码是否过期
|
| | | public boolean passwordIsExpiration(Date pwdUpdateDate)
|
| | | {
|
| | | Integer passwordValidateDays = Convert.toInt(configService.selectConfigByKey("sys.account.passwordValidateDays"));
|
| | | if (passwordValidateDays != null && passwordValidateDays > 0)
|
| | | {
|
| | | if (StringUtils.isNull(pwdUpdateDate))
|
| | | {
|
| | | // 如果从未修改过初始密码,直接提醒过期
|
| | | return true;
|
| | | }
|
| | | Date nowDate = DateUtils.getNowDate();
|
| | | return DateUtils.differentDaysByMillisecond(nowDate, pwdUpdateDate) > passwordValidateDays;
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | /**
|
| | |
| | | @GetMapping(value = { "/", "/{userId}" })
|
| | | public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
|
| | | {
|
| | | userService.checkUserDataScope(userId);
|
| | | AjaxResult ajax = AjaxResult.success();
|
| | | List<SysRole> roles = roleService.selectRoleAll();
|
| | | ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
|
| | | ajax.put("posts", postService.selectPostAll());
|
| | | if (StringUtils.isNotNull(userId))
|
| | | {
|
| | | ajax.put(AjaxResult.DATA_TAG, userService.selectUserById(userId));
|
| | | userService.checkUserDataScope(userId);
|
| | | SysUser sysUser = userService.selectUserById(userId);
|
| | | ajax.put(AjaxResult.DATA_TAG, sysUser);
|
| | | ajax.put("postIds", postService.selectPostListByUserId(userId));
|
| | | ajax.put("roleIds", roleService.selectRoleListByUserId(userId));
|
| | | ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
|
| | | }
|
| | | List<SysRole> roles = roleService.selectRoleAll();
|
| | | ajax.put("roles", SecurityUtils.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
|
| | | ajax.put("posts", postService.selectPostAll());
|
| | | return ajax;
|
| | | }
|
| | |
|
| | |
| | | @PostMapping
|
| | | public AjaxResult add(@Validated @RequestBody SysUser user)
|
| | | {
|
| | | if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName())))
|
| | | deptService.checkDeptDataScope(user.getDeptId());
|
| | | roleService.checkRoleDataScope(user.getRoleIds());
|
| | | if (!userService.checkUserNameUnique(user))
|
| | | {
|
| | | return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,登录账号已存在");
|
| | | return error("新增用户'" + user.getUserName() + "'失败,登录账号已存在");
|
| | | }
|
| | | else if (StringUtils.isNotEmpty(user.getPhonenumber())
|
| | | && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
|
| | | else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
|
| | | {
|
| | | return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,手机号码已存在");
|
| | | return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在");
|
| | | }
|
| | | else if (StringUtils.isNotEmpty(user.getEmail())
|
| | | && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
|
| | | else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
|
| | | {
|
| | | return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在");
|
| | | return error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在");
|
| | | }
|
| | | user.setCreateBy(SecurityUtils.getUsername());
|
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
|
| | |
| | | public AjaxResult edit(@Validated @RequestBody SysUser user)
|
| | | {
|
| | | userService.checkUserAllowed(user);
|
| | | if (StringUtils.isNotEmpty(user.getPhonenumber())
|
| | | && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
|
| | | userService.checkUserDataScope(user.getUserId());
|
| | | deptService.checkDeptDataScope(user.getDeptId());
|
| | | roleService.checkRoleDataScope(user.getRoleIds());
|
| | | if (!userService.checkUserNameUnique(user))
|
| | | {
|
| | | return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
|
| | | return error("修改用户'" + user.getUserName() + "'失败,登录账号已存在");
|
| | | }
|
| | | else if (StringUtils.isNotEmpty(user.getEmail())
|
| | | && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
|
| | | else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
|
| | | {
|
| | | return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
|
| | | return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
|
| | | }
|
| | | else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
|
| | | {
|
| | | return error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
|
| | | }
|
| | | user.setUpdateBy(SecurityUtils.getUsername());
|
| | | return toAjax(userService.updateUser(user));
|
| | |
| | | {
|
| | | if (ArrayUtils.contains(userIds, SecurityUtils.getUserId()))
|
| | | {
|
| | | return AjaxResult.error("当前用户不能删除");
|
| | | return error("当前用户不能删除");
|
| | | }
|
| | | return toAjax(userService.deleteUserByIds(userIds));
|
| | | }
|
| | |
| | | public AjaxResult resetPwd(@RequestBody SysUser user)
|
| | | {
|
| | | userService.checkUserAllowed(user);
|
| | | userService.checkUserDataScope(user.getUserId());
|
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
|
| | | user.setUpdateBy(SecurityUtils.getUsername());
|
| | | return toAjax(userService.resetPwd(user));
|
| | |
| | | public AjaxResult changeStatus(@RequestBody SysUser user)
|
| | | {
|
| | | userService.checkUserAllowed(user);
|
| | | userService.checkUserDataScope(user.getUserId());
|
| | | user.setUpdateBy(SecurityUtils.getUsername());
|
| | | return toAjax(userService.updateUserStatus(user));
|
| | | }
|
| | |
| | | SysUser user = userService.selectUserById(userId);
|
| | | List<SysRole> roles = roleService.selectRolesByUserId(userId);
|
| | | ajax.put("user", user);
|
| | | ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
|
| | | ajax.put("roles", SecurityUtils.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
|
| | | return ajax;
|
| | | }
|
| | |
|
| | |
| | | @PutMapping("/authRole")
|
| | | public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
|
| | | {
|
| | | userService.checkUserDataScope(userId);
|
| | | roleService.checkRoleDataScope(roleIds);
|
| | | userService.insertUserAuth(userId, roleIds);
|
| | | return success();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取部门树列表
|
| | | */
|
| | | @RequiresPermissions("system:user:list")
|
| | | @GetMapping("/deptTree")
|
| | | public AjaxResult deptTree(SysDept dept)
|
| | | {
|
| | | return success(deptService.selectDeptTreeList(dept));
|
| | | }
|
| | | }
|