用户访问控制时校验数据权限,防止越权

master
RuoYi 2022-01-27 11:13:59 +08:00
parent e9ebf86ac8
commit ed1e7e69a8
5 changed files with 16 additions and 4 deletions

View File

@ -109,16 +109,17 @@ public class SysDeptController extends BaseController
@ResponseBody @ResponseBody
public AjaxResult editSave(@Validated SysDept dept) public AjaxResult editSave(@Validated SysDept dept)
{ {
Long deptId = dept.getDeptId();
deptService.checkDeptDataScope(deptId);
if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
{ {
return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在"); return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
} }
else if (dept.getParentId().equals(dept.getDeptId())) else if (dept.getParentId().equals(deptId))
{ {
return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己"); return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
} }
else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0)
&& deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0)
{ {
return AjaxResult.error("该部门包含未停用的子部门!"); return AjaxResult.error("该部门包含未停用的子部门!");
} }
@ -143,6 +144,7 @@ public class SysDeptController extends BaseController
{ {
return AjaxResult.warn("部门存在用户,不允许删除"); return AjaxResult.warn("部门存在用户,不允许删除");
} }
deptService.checkDeptDataScope(deptId);
return toAjax(deptService.deleteDeptById(deptId)); return toAjax(deptService.deleteDeptById(deptId));
} }

View File

@ -124,6 +124,7 @@ public class SysRoleController extends BaseController
public AjaxResult editSave(@Validated SysRole role) public AjaxResult editSave(@Validated SysRole role)
{ {
roleService.checkRoleAllowed(role); roleService.checkRoleAllowed(role);
roleService.checkRoleDataScope(role.getRoleId());
if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) if (UserConstants.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
{ {
return error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在"); return error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
@ -157,6 +158,7 @@ public class SysRoleController extends BaseController
public AjaxResult authDataScopeSave(SysRole role) public AjaxResult authDataScopeSave(SysRole role)
{ {
roleService.checkRoleAllowed(role); roleService.checkRoleAllowed(role);
roleService.checkRoleDataScope(role.getRoleId());
role.setUpdateBy(getLoginName()); role.setUpdateBy(getLoginName());
if (roleService.authDataScope(role) > 0) if (roleService.authDataScope(role) > 0)
{ {
@ -214,6 +216,7 @@ public class SysRoleController extends BaseController
public AjaxResult changeStatus(SysRole role) public AjaxResult changeStatus(SysRole role)
{ {
roleService.checkRoleAllowed(role); roleService.checkRoleAllowed(role);
roleService.checkRoleDataScope(role.getRoleId());
return toAjax(roleService.changeStatus(role)); return toAjax(roleService.changeStatus(role));
} }
@ -297,6 +300,7 @@ public class SysRoleController extends BaseController
@ResponseBody @ResponseBody
public AjaxResult selectAuthUserAll(Long roleId, String userIds) public AjaxResult selectAuthUserAll(Long roleId, String userIds)
{ {
roleService.checkRoleDataScope(roleId);
return toAjax(roleService.insertAuthUsers(roleId, userIds)); return toAjax(roleService.insertAuthUsers(roleId, userIds));
} }
} }

View File

@ -169,6 +169,7 @@ public class SysUserController extends BaseController
public AjaxResult editSave(@Validated SysUser user) public AjaxResult editSave(@Validated SysUser user)
{ {
userService.checkUserAllowed(user); userService.checkUserAllowed(user);
userService.checkUserDataScope(user.getUserId());
if (StringUtils.isNotEmpty(user.getPhonenumber()) if (StringUtils.isNotEmpty(user.getPhonenumber())
&& UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) && UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
{ {
@ -199,6 +200,7 @@ public class SysUserController extends BaseController
public AjaxResult resetPwdSave(SysUser user) public AjaxResult resetPwdSave(SysUser user)
{ {
userService.checkUserAllowed(user); userService.checkUserAllowed(user);
userService.checkUserDataScope(user.getUserId());
user.setSalt(ShiroUtils.randomSalt()); user.setSalt(ShiroUtils.randomSalt());
user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt())); user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
if (userService.resetUserPwd(user) > 0) if (userService.resetUserPwd(user) > 0)
@ -235,6 +237,7 @@ public class SysUserController extends BaseController
@ResponseBody @ResponseBody
public AjaxResult insertAuthRole(Long userId, Long[] roleIds) public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
{ {
userService.checkUserDataScope(userId);
userService.insertUserAuth(userId, roleIds); userService.insertUserAuth(userId, roleIds);
AuthorizationUtils.clearAllCachedAuthorizationInfo(); AuthorizationUtils.clearAllCachedAuthorizationInfo();
return success(); return success();
@ -293,6 +296,7 @@ public class SysUserController extends BaseController
public AjaxResult changeStatus(SysUser user) public AjaxResult changeStatus(SysUser user)
{ {
userService.checkUserAllowed(user); userService.checkUserAllowed(user);
userService.checkUserDataScope(user.getUserId());
return toAjax(userService.changeStatus(user)); return toAjax(userService.changeStatus(user));
} }
} }

View File

@ -159,6 +159,7 @@ public class SysRoleServiceImpl implements ISysRoleService
for (Long roleId : roleIds) for (Long roleId : roleIds)
{ {
checkRoleAllowed(new SysRole(roleId)); checkRoleAllowed(new SysRole(roleId));
checkRoleDataScope(roleId);
SysRole role = selectRoleById(roleId); SysRole role = selectRoleById(roleId);
if (countUserRoleByRoleId(roleId) > 0) if (countUserRoleByRoleId(roleId) > 0)
{ {

View File

@ -47,7 +47,7 @@ public class SysUserServiceImpl implements ISysUserService
@Autowired @Autowired
private SysRoleMapper roleMapper; private SysRoleMapper roleMapper;
@Autowired @Autowired
private SysPostMapper postMapper; private SysPostMapper postMapper;
@ -193,6 +193,7 @@ public class SysUserServiceImpl implements ISysUserService
for (Long userId : userIds) for (Long userId : userIds)
{ {
checkUserAllowed(new SysUser(userId)); checkUserAllowed(new SysUser(userId));
checkUserDataScope(userId);
} }
// 删除用户与角色关联 // 删除用户与角色关联
userRoleMapper.deleteUserRole(userIds); userRoleMapper.deleteUserRole(userIds);