【8.0.1】【system】非管理员用户只能删除自己创建的角色

pull/57/head
fengshuonan 2023-10-09 10:58:43 +08:00
parent d794054060
commit b5fd51b697
2 changed files with 40 additions and 17 deletions

View File

@ -26,7 +26,12 @@ public enum SysRoleExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
SYSTEM_ROLE_CANT_DELETE(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + "10003", "系统角色不能被删除");
SYSTEM_ROLE_CANT_DELETE(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + "10003", "系统角色不能被删除"),
/**
*
*/
DEL_PERMISSION_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + "10004", "非管理员用户,不能删除其他角色");
/**
*

View File

@ -65,6 +65,13 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
throw new ServiceException(SysRoleExceptionEnum.SYSTEM_ROLE_CANT_DELETE);
}
// 非管理员,只能删除自己的角色
if (!LoginContext.me().getSuperAdminFlag()) {
if (!sysRole.getCreateUser().equals(LoginContext.me().getLoginUser().getUserId())) {
throw new ServiceException(SysRoleExceptionEnum.DEL_PERMISSION_ERROR);
}
}
// 删除角色
this.baseDelete(CollectionUtil.set(false, sysRole.getRoleId()));
}
@ -82,6 +89,17 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
throw new ServiceException(SysRoleExceptionEnum.SYSTEM_ROLE_CANT_DELETE);
}
// 如果当前用户是非管理员,则只能删除自己创建的角色
if (!LoginContext.me().getSuperAdminFlag()) {
LambdaQueryWrapper<SysRole> tempWrapper = new LambdaQueryWrapper<>();
tempWrapper.in(SysRole::getRoleId, sysRoleRequest.getRoleIdList());
tempWrapper.ne(BaseEntity::getCreateUser, LoginContext.me().getLoginUser().getUserId());
long notMeCreateCount = this.count(tempWrapper);
if (notMeCreateCount > 0) {
throw new ServiceException(SysRoleExceptionEnum.DEL_PERMISSION_ERROR);
}
}
// 执行删除角色
this.baseDelete(sysRoleRequest.getRoleIdList());
}
@ -188,22 +206,6 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
return this.list(wrapper);
}
/**
*
* <p>
*
*
* @author fengshuonan
* @since 2023/10/9 10:44
*/
private void filterRolePermission(LambdaQueryWrapper<SysRole> wrapper) {
if (!LoginContext.me().getSuperAdminFlag()) {
Long userId = LoginContext.me().getLoginUser().getUserId();
wrapper.eq(SysRole::getCreateUser, userId);
wrapper.in(SysRole::getRoleId, sysUserRoleServiceApi.getUserRoleIdList(userId));
}
}
@Override
public Long getDefaultRoleId() {
@ -294,4 +296,20 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
// 删除角色
this.removeBatchByIds(roleIdList);
}
/**
*
* <p>
*
*
* @author fengshuonan
* @since 2023/10/9 10:44
*/
private void filterRolePermission(LambdaQueryWrapper<SysRole> wrapper) {
if (!LoginContext.me().getSuperAdminFlag()) {
Long userId = LoginContext.me().getLoginUser().getUserId();
wrapper.eq(SysRole::getCreateUser, userId).or().in(SysRole::getRoleId, sysUserRoleServiceApi.getUserRoleIdList(userId));
}
}
}