|
|
@@ -1,13 +1,5 @@
|
|
|
package com.goldenwater.system.service.impl;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
import com.goldenwater.common.annotation.DataScope;
|
|
|
import com.goldenwater.common.constant.UserConstants;
|
|
|
import com.goldenwater.common.core.domain.entity.SysRole;
|
|
|
@@ -23,15 +15,19 @@ import com.goldenwater.system.mapper.SysRoleMapper;
|
|
|
import com.goldenwater.system.mapper.SysRoleMenuMapper;
|
|
|
import com.goldenwater.system.mapper.SysUserRoleMapper;
|
|
|
import com.goldenwater.system.service.ISysRoleService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 角色 业务层处理
|
|
|
- *
|
|
|
+ *
|
|
|
* @author goldenwater
|
|
|
*/
|
|
|
@Service
|
|
|
-public class SysRoleServiceImpl implements ISysRoleService
|
|
|
-{
|
|
|
+public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
@Autowired
|
|
|
private SysRoleMapper roleMapper;
|
|
|
|
|
|
@@ -46,34 +42,29 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 根据条件分页查询角色数据
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色信息
|
|
|
* @return 角色数据集合信息
|
|
|
*/
|
|
|
@Override
|
|
|
@DataScope(deptAlias = "d")
|
|
|
- public List<SysRole> selectRoleList(SysRole role)
|
|
|
- {
|
|
|
+ public List<SysRole> selectRoleList(SysRole role) {
|
|
|
return roleMapper.selectRoleList(role);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据用户ID查询角色
|
|
|
- *
|
|
|
+ *
|
|
|
* @param userId 用户ID
|
|
|
* @return 角色列表
|
|
|
*/
|
|
|
@Override
|
|
|
- public List<SysRole> selectRolesByUserId(String userId)
|
|
|
- {
|
|
|
+ public List<SysRole> selectRolesByUserId(String userId) {
|
|
|
List<SysRole> userRoles = roleMapper.selectRolePermissionByUserId(userId);
|
|
|
List<SysRole> roles = selectRoleAll();
|
|
|
- for (SysRole role : roles)
|
|
|
- {
|
|
|
- for (SysRole userRole : userRoles)
|
|
|
- {
|
|
|
- if (role.getRoleId().longValue() == userRole.getRoleId().longValue())
|
|
|
- {
|
|
|
+ for (SysRole role : roles) {
|
|
|
+ for (SysRole userRole : userRoles) {
|
|
|
+ if (role.getRoleId().equals(userRole.getRoleId())) {
|
|
|
role.setFlag(true);
|
|
|
break;
|
|
|
}
|
|
|
@@ -84,19 +75,16 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 根据用户ID查询权限
|
|
|
- *
|
|
|
+ *
|
|
|
* @param userId 用户ID
|
|
|
* @return 权限列表
|
|
|
*/
|
|
|
@Override
|
|
|
- public Set<String> selectRolePermissionByUserId(String userId)
|
|
|
- {
|
|
|
+ public Set<String> selectRolePermissionByUserId(String userId) {
|
|
|
List<SysRole> perms = roleMapper.selectRolePermissionByUserId(userId);
|
|
|
Set<String> permsSet = new HashSet<>();
|
|
|
- for (SysRole perm : perms)
|
|
|
- {
|
|
|
- if (StringUtils.isNotNull(perm))
|
|
|
- {
|
|
|
+ for (SysRole perm : perms) {
|
|
|
+ if (StringUtils.isNotNull(perm)) {
|
|
|
permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
|
|
|
}
|
|
|
}
|
|
|
@@ -105,52 +93,47 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 查询所有角色
|
|
|
- *
|
|
|
+ *
|
|
|
* @return 角色列表
|
|
|
*/
|
|
|
@Override
|
|
|
- public List<SysRole> selectRoleAll()
|
|
|
- {
|
|
|
+ public List<SysRole> selectRoleAll() {
|
|
|
return SpringUtils.getAopProxy(this).selectRoleList(new SysRole());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据用户ID获取角色选择框列表
|
|
|
- *
|
|
|
+ *
|
|
|
* @param userId 用户ID
|
|
|
* @return 选中角色ID列表
|
|
|
*/
|
|
|
@Override
|
|
|
- public List<String> selectRoleListByUserId(String userId)
|
|
|
- {
|
|
|
+ public List<String> selectRoleListByUserId(String userId) {
|
|
|
return roleMapper.selectRoleListByUserId(userId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 通过角色ID查询角色
|
|
|
- *
|
|
|
+ *
|
|
|
* @param roleId 角色ID
|
|
|
* @return 角色对象信息
|
|
|
*/
|
|
|
@Override
|
|
|
- public SysRole selectRoleById(Long roleId)
|
|
|
- {
|
|
|
+ public SysRole selectRoleById(String roleId) {
|
|
|
return roleMapper.selectRoleById(roleId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 校验角色名称是否唯一
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色信息
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public boolean checkRoleNameUnique(SysRole role)
|
|
|
- {
|
|
|
- Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
|
|
|
+ public boolean checkRoleNameUnique(SysRole role) {
|
|
|
+ String roleId = StringUtils.isNull(role.getRoleId()) ? null : role.getRoleId();
|
|
|
SysRole info = roleMapper.checkRoleNameUnique(role.getRoleName());
|
|
|
- if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
|
|
|
- {
|
|
|
+ if (StringUtils.isNotNull(info) && !info.getRoleId().equals(roleId)) {
|
|
|
return UserConstants.NOT_UNIQUE;
|
|
|
}
|
|
|
return UserConstants.UNIQUE;
|
|
|
@@ -158,17 +141,15 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 校验角色权限是否唯一
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色信息
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public boolean checkRoleKeyUnique(SysRole role)
|
|
|
- {
|
|
|
- Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId();
|
|
|
+ public boolean checkRoleKeyUnique(SysRole role) {
|
|
|
+ String roleId = StringUtils.isNull(role.getRoleId()) ? null : role.getRoleId();
|
|
|
SysRole info = roleMapper.checkRoleKeyUnique(role.getRoleKey());
|
|
|
- if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue())
|
|
|
- {
|
|
|
+ if (StringUtils.isNotNull(info) && !info.getRoleId().equals(roleId)) {
|
|
|
return UserConstants.NOT_UNIQUE;
|
|
|
}
|
|
|
return UserConstants.UNIQUE;
|
|
|
@@ -176,35 +157,29 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 校验角色是否允许操作
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色信息
|
|
|
*/
|
|
|
@Override
|
|
|
- public void checkRoleAllowed(SysRole role)
|
|
|
- {
|
|
|
- if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin())
|
|
|
- {
|
|
|
+ public void checkRoleAllowed(SysRole role) {
|
|
|
+ if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin()) {
|
|
|
throw new ServiceException("不允许操作超级管理员角色");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 校验角色是否有数据权限
|
|
|
- *
|
|
|
+ *
|
|
|
* @param roleIds 角色id
|
|
|
*/
|
|
|
@Override
|
|
|
- public void checkRoleDataScope(Long... roleIds)
|
|
|
- {
|
|
|
- if (!SecurityUtils.isAdmin())
|
|
|
- {
|
|
|
- for (Long roleId : roleIds)
|
|
|
- {
|
|
|
+ public void checkRoleDataScope(String... roleIds) {
|
|
|
+ if (!SecurityUtils.isAdmin()) {
|
|
|
+ for (String roleId : roleIds) {
|
|
|
SysRole role = new SysRole();
|
|
|
role.setRoleId(roleId);
|
|
|
List<SysRole> roles = SpringUtils.getAopProxy(this).selectRoleList(role);
|
|
|
- if (StringUtils.isEmpty(roles))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(roles)) {
|
|
|
throw new ServiceException("没有权限访问角色数据!");
|
|
|
}
|
|
|
}
|
|
|
@@ -213,26 +188,24 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 通过角色ID查询角色使用数量
|
|
|
- *
|
|
|
+ *
|
|
|
* @param roleId 角色ID
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public int countUserRoleByRoleId(Long roleId)
|
|
|
- {
|
|
|
+ public int countUserRoleByRoleId(String roleId) {
|
|
|
return userRoleMapper.countUserRoleByRoleId(roleId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 新增保存角色信息
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色信息
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public int insertRole(SysRole role)
|
|
|
- {
|
|
|
+ public int insertRole(SysRole role) {
|
|
|
// 新增角色信息
|
|
|
roleMapper.insertRole(role);
|
|
|
return insertRoleMenu(role);
|
|
|
@@ -240,14 +213,13 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 修改保存角色信息
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色信息
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public int updateRole(SysRole role)
|
|
|
- {
|
|
|
+ public int updateRole(SysRole role) {
|
|
|
// 修改角色信息
|
|
|
roleMapper.updateRole(role);
|
|
|
// 删除角色与菜单关联
|
|
|
@@ -257,26 +229,24 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 修改角色状态
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色信息
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public int updateRoleStatus(SysRole role)
|
|
|
- {
|
|
|
+ public int updateRoleStatus(SysRole role) {
|
|
|
return roleMapper.updateRole(role);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改数据权限信息
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色信息
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public int authDataScope(SysRole role)
|
|
|
- {
|
|
|
+ public int authDataScope(SysRole role) {
|
|
|
// 修改角色信息
|
|
|
roleMapper.updateRole(role);
|
|
|
// 删除角色与部门关联
|
|
|
@@ -287,23 +257,20 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 新增角色菜单信息
|
|
|
- *
|
|
|
+ *
|
|
|
* @param role 角色对象
|
|
|
*/
|
|
|
- public int insertRoleMenu(SysRole role)
|
|
|
- {
|
|
|
+ public int insertRoleMenu(SysRole role) {
|
|
|
int rows = 1;
|
|
|
// 新增用户与角色管理
|
|
|
List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
|
|
|
- for (Long menuId : role.getMenuIds())
|
|
|
- {
|
|
|
+ for (Long menuId : role.getMenuIds()) {
|
|
|
SysRoleMenu rm = new SysRoleMenu();
|
|
|
rm.setRoleId(role.getRoleId());
|
|
|
rm.setMenuId(menuId);
|
|
|
list.add(rm);
|
|
|
}
|
|
|
- if (list.size() > 0)
|
|
|
- {
|
|
|
+ if (list.size() > 0) {
|
|
|
rows = roleMenuMapper.batchRoleMenu(list);
|
|
|
}
|
|
|
return rows;
|
|
|
@@ -314,20 +281,17 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
*
|
|
|
* @param role 角色对象
|
|
|
*/
|
|
|
- public int insertRoleDept(SysRole role)
|
|
|
- {
|
|
|
+ public int insertRoleDept(SysRole role) {
|
|
|
int rows = 1;
|
|
|
// 新增角色与部门(数据权限)管理
|
|
|
List<SysRoleDept> list = new ArrayList<SysRoleDept>();
|
|
|
- for (Long deptId : role.getDeptIds())
|
|
|
- {
|
|
|
+ for (String deptId : role.getDeptIds()) {
|
|
|
SysRoleDept rd = new SysRoleDept();
|
|
|
rd.setRoleId(role.getRoleId());
|
|
|
rd.setDeptId(deptId);
|
|
|
list.add(rd);
|
|
|
}
|
|
|
- if (list.size() > 0)
|
|
|
- {
|
|
|
+ if (list.size() > 0) {
|
|
|
rows = roleDeptMapper.batchRoleDept(list);
|
|
|
}
|
|
|
return rows;
|
|
|
@@ -335,14 +299,13 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 通过角色ID删除角色
|
|
|
- *
|
|
|
+ *
|
|
|
* @param roleId 角色ID
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public int deleteRoleById(Long roleId)
|
|
|
- {
|
|
|
+ public int deleteRoleById(String roleId) {
|
|
|
// 删除角色与菜单关联
|
|
|
roleMenuMapper.deleteRoleMenuByRoleId(roleId);
|
|
|
// 删除角色与部门关联
|
|
|
@@ -352,21 +315,18 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 批量删除角色信息
|
|
|
- *
|
|
|
+ *
|
|
|
* @param roleIds 需要删除的角色ID
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public int deleteRoleByIds(Long[] roleIds)
|
|
|
- {
|
|
|
- for (Long roleId : roleIds)
|
|
|
- {
|
|
|
+ public int deleteRoleByIds(String[] roleIds) {
|
|
|
+ for (String roleId : roleIds) {
|
|
|
checkRoleAllowed(new SysRole(roleId));
|
|
|
checkRoleDataScope(roleId);
|
|
|
SysRole role = selectRoleById(roleId);
|
|
|
- if (countUserRoleByRoleId(roleId) > 0)
|
|
|
- {
|
|
|
+ if (countUserRoleByRoleId(roleId) > 0) {
|
|
|
throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName()));
|
|
|
}
|
|
|
}
|
|
|
@@ -379,43 +339,39 @@ public class SysRoleServiceImpl implements ISysRoleService
|
|
|
|
|
|
/**
|
|
|
* 取消授权用户角色
|
|
|
- *
|
|
|
+ *
|
|
|
* @param userRole 用户和角色关联信息
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public int deleteAuthUser(SysUserRole userRole)
|
|
|
- {
|
|
|
+ public int deleteAuthUser(SysUserRole userRole) {
|
|
|
return userRoleMapper.deleteUserRoleInfo(userRole);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 批量取消授权用户角色
|
|
|
- *
|
|
|
- * @param roleId 角色ID
|
|
|
+ *
|
|
|
+ * @param roleId 角色ID
|
|
|
* @param userIds 需要取消授权的用户数据ID
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public int deleteAuthUsers(Long roleId, String[] userIds)
|
|
|
- {
|
|
|
+ public int deleteAuthUsers(String roleId, String[] userIds) {
|
|
|
return userRoleMapper.deleteUserRoleInfos(roleId, userIds);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 批量选择授权用户角色
|
|
|
- *
|
|
|
- * @param roleId 角色ID
|
|
|
+ *
|
|
|
+ * @param roleId 角色ID
|
|
|
* @param userIds 需要授权的用户数据ID
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public int insertAuthUsers(Long roleId, String[] userIds)
|
|
|
- {
|
|
|
+ public int insertAuthUsers(String roleId, String[] userIds) {
|
|
|
// 新增用户与角色管理
|
|
|
List<SysUserRole> list = new ArrayList<SysUserRole>();
|
|
|
- for (String userId : userIds)
|
|
|
- {
|
|
|
+ for (String userId : userIds) {
|
|
|
SysUserRole ur = new SysUserRole();
|
|
|
ur.setUserId(userId);
|
|
|
ur.setRoleId(roleId);
|