|
@@ -6,11 +6,12 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.qmth.boot.api.exception.ApiException;
|
|
import com.qmth.boot.api.exception.ApiException;
|
|
|
|
+import com.qmth.teachcloud.common.bean.dto.DataPermissionInfo;
|
|
|
|
+import com.qmth.teachcloud.common.bean.params.SysRolePrivilegeParams;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.entity.*;
|
|
import com.qmth.teachcloud.common.entity.*;
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
import com.qmth.teachcloud.common.enums.FieldUniqueEnum;
|
|
import com.qmth.teachcloud.common.enums.FieldUniqueEnum;
|
|
-import com.qmth.teachcloud.common.enums.RoleSourceEnum;
|
|
|
|
import com.qmth.teachcloud.common.enums.RoleTypeEnum;
|
|
import com.qmth.teachcloud.common.enums.RoleTypeEnum;
|
|
import com.qmth.teachcloud.common.mapper.SysRoleMapper;
|
|
import com.qmth.teachcloud.common.mapper.SysRoleMapper;
|
|
import com.qmth.teachcloud.common.service.*;
|
|
import com.qmth.teachcloud.common.service.*;
|
|
@@ -21,6 +22,7 @@ import org.slf4j.LoggerFactory;
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
@@ -60,6 +62,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
@Resource
|
|
@Resource
|
|
SysRoleGroupMemberService sysRoleGroupMemberService;
|
|
SysRoleGroupMemberService sysRoleGroupMemberService;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private BasicRoleDataPermissionService basicRoleDataPermissionService;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public IPage<SysRole> list(String name, Boolean enable, Integer pageNumber, Integer pageSize) {
|
|
public IPage<SysRole> list(String name, Boolean enable, Integer pageNumber, Integer pageSize) {
|
|
Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
|
|
Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
|
|
@@ -69,73 +74,60 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
/**
|
|
/**
|
|
* 新增角色
|
|
* 新增角色
|
|
*
|
|
*
|
|
- * @param role
|
|
|
|
|
|
+ * @param sysRolePrivilegeParams
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
@Transactional
|
|
@Transactional
|
|
@Override
|
|
@Override
|
|
- public boolean saveRoleNew(SysRole role) {
|
|
|
|
|
|
+ public boolean saveRoleNew(SysRolePrivilegeParams sysRolePrivilegeParams) {
|
|
try {
|
|
try {
|
|
SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
|
|
SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
|
|
Long schoolId = Objects.nonNull(ServletUtil.getRequestHeaderSchoolIdByNotVaild()) ? Long.valueOf(ServletUtil.getRequestHeaderSchoolIdByNotVaild().toString()) : null;
|
|
Long schoolId = Objects.nonNull(ServletUtil.getRequestHeaderSchoolIdByNotVaild()) ? Long.valueOf(ServletUtil.getRequestHeaderSchoolIdByNotVaild().toString()) : null;
|
|
- if (Objects.isNull(schoolId) && Objects.isNull(role.getSchoolId())) {
|
|
|
|
- role.setSchoolId(sysUser.getSchoolId());
|
|
|
|
- } else if (Objects.nonNull(schoolId)) {
|
|
|
|
- role.setSchoolId(schoolId);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ // 组装角色
|
|
|
|
+ SysRole role = new SysRole();
|
|
|
|
+ role.setId(sysRolePrivilegeParams.getId());
|
|
|
|
+ role.setSchoolId(schoolId);
|
|
|
|
+ role.setName(sysRolePrivilegeParams.getName());
|
|
|
|
+ role.setInterpret(sysRolePrivilegeParams.getInterpret());
|
|
|
|
+ role.setPrivilegeIds(sysRolePrivilegeParams.getPrivilegeIds().stream().map(SystemConstant::convertIdToLong).toArray(Long[]::new));
|
|
|
|
+ role.setDefaultRole(sysRolePrivilegeParams.getDefaultRole() == null ? false : sysRolePrivilegeParams.getDefaultRole());
|
|
|
|
+
|
|
|
|
+ List<SysRole> sysRoleList = this.list();
|
|
// 内置角色检验
|
|
// 内置角色检验
|
|
- if(role.getDefaultRole()){
|
|
|
|
- List<SysRole> roleList = this.list(new QueryWrapper<SysRole>().lambda()
|
|
|
|
- .eq(SysRole::getDefaultRole, true)
|
|
|
|
- .eq(SysRole::getEnable, true))
|
|
|
|
- .stream()
|
|
|
|
- .filter(m->m.getName().equals(role.getName()) && !m.getId().equals(role.getId()))
|
|
|
|
- .distinct()
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- String roleName = role.getName();
|
|
|
|
- if (!roleList.isEmpty()) {
|
|
|
|
- throw ExceptionResultEnum.ERROR.exception("已存在相同的角色名称");
|
|
|
|
|
|
+ if (role.getDefaultRole()) {
|
|
|
|
+ // 校验在内置角色中是否存在相同名称角色
|
|
|
|
+ List<SysRole> defaultRoleList = sysRoleList.stream().filter(m -> m.getSchoolId() == null && m.getName().equals(role.getName()) && !m.getId().equals(role.getId())).distinct().collect(Collectors.toList());
|
|
|
|
+ if (!CollectionUtils.isEmpty(defaultRoleList)) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("内置角色中已存在名称[" + role.getName() + "]的角色");
|
|
|
|
+ }
|
|
|
|
+ // 校验在学校角色中是否存在相同名称角色
|
|
|
|
+ List<SysRole> schoolRoleList = sysRoleList.stream().filter(m -> m.getSchoolId() != null && m.getName().equals(role.getName())).distinct().collect(Collectors.toList());
|
|
|
|
+ if (!CollectionUtils.isEmpty(schoolRoleList)) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("学校角色中已存在名称[" + role.getName() + "]的角色");
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- // 系统默认内置角色(不允许和系统默认角色重名)
|
|
|
|
- List<String> defaultRoleNames = this.list(new QueryWrapper<SysRole>().lambda()
|
|
|
|
- .eq(SysRole::getDefaultRole, true)
|
|
|
|
- .eq(SysRole::getEnable, true))
|
|
|
|
- .stream()
|
|
|
|
- .map(SysRole::getName)
|
|
|
|
- .distinct()
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- String roleName = role.getName();
|
|
|
|
- if (defaultRoleNames.contains(roleName)) {
|
|
|
|
- throw ExceptionResultEnum.ERROR.exception("系统角色已存在相同的角色名称");
|
|
|
|
|
|
+ // 校验在内置角色中是否存在相同名称角色
|
|
|
|
+ List<SysRole> defaultRoleList = sysRoleList.stream().filter(m -> m.getSchoolId() == null && m.getName().equals(role.getName())).distinct().collect(Collectors.toList());
|
|
|
|
+ if (!CollectionUtils.isEmpty(defaultRoleList)) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("内置角色中已存在名称[" + role.getName() + "]的角色");
|
|
}
|
|
}
|
|
- }
|
|
|
|
- if (Objects.nonNull(role.getId())) {//编辑
|
|
|
|
- List<SysRolePrivilege> sysRolePrivilegeList = commonCacheService.rolePrivilegeCache(role.getId());
|
|
|
|
- QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
|
|
|
|
- sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, role.getPrivilegeIds());
|
|
|
|
- List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
|
|
|
|
- Set<String> relatedSet = sysPrivilegeList.stream().filter(s -> Objects.nonNull(s.getRelated())).map(s -> s.getRelated()).collect(Collectors.toSet());
|
|
|
|
- Set<Long> finalRelatedSet = new HashSet<>(Arrays.asList(role.getPrivilegeIds()));
|
|
|
|
- for (String s : relatedSet) {
|
|
|
|
- if (s.contains(",")) {
|
|
|
|
- String[] arrays = s.split(",");
|
|
|
|
- for (int i = 0; i < arrays.length; i++) {
|
|
|
|
- finalRelatedSet.add(Long.parseLong(arrays[i].trim()));
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- finalRelatedSet.add(Long.parseLong(s));
|
|
|
|
- }
|
|
|
|
|
|
+ // 校验在学校角色中是否存在相同名称角色
|
|
|
|
+ List<SysRole> schoolRoleList = sysRoleList.stream().filter(m -> m.getSchoolId().equals(schoolId) && m.getName().equals(role.getName()) && !m.getId().equals(role.getId())).distinct().collect(Collectors.toList());
|
|
|
|
+ if (!CollectionUtils.isEmpty(schoolRoleList)) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("学校角色中已存在名称[" + role.getName() + "]的角色");
|
|
}
|
|
}
|
|
- List<Long> relatedList = new ArrayList<>(finalRelatedSet);
|
|
|
|
- int count = (int) sysRolePrivilegeList.stream().filter(s -> relatedList.contains(s.getPrivilegeId())).count();
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //编辑
|
|
|
|
+ if (Objects.nonNull(role.getId())) {
|
|
role.updateInfo(sysUser.getId());
|
|
role.updateInfo(sysUser.getId());
|
|
sysRoleService.updateById(role);
|
|
sysRoleService.updateById(role);
|
|
commonCacheService.updateRoleCache(role.getId());
|
|
commonCacheService.updateRoleCache(role.getId());
|
|
|
|
|
|
- if (count != sysRolePrivilegeList.size() || count != finalRelatedSet.size()) {
|
|
|
|
- sysRolePrivilegeService.removeByRoleId(role.getId());
|
|
|
|
- sysRolePrivilegeService.saveBatch(role);//角色权限
|
|
|
|
|
|
+ boolean isChange = sysRolePrivilegeService.saveBatch(role);//角色权限
|
|
|
|
+
|
|
|
|
+ if (isChange) {
|
|
commonCacheService.updateRolePrivilegeCache(role.getId());
|
|
commonCacheService.updateRolePrivilegeCache(role.getId());
|
|
//绑定该角色的用户都需要清除鉴权缓存
|
|
//绑定该角色的用户都需要清除鉴权缓存
|
|
List<SysUserRole> sysUserRoleList = sysUserRoleService.listByRoleId(role.getId());
|
|
List<SysUserRole> sysUserRoleList = sysUserRoleService.listByRoleId(role.getId());
|
|
@@ -147,6 +139,12 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
sysRolePrivilegeService.saveBatch(role);//角色权限
|
|
sysRolePrivilegeService.saveBatch(role);//角色权限
|
|
commonCacheService.updateCustomRoleCache();
|
|
commonCacheService.updateCustomRoleCache();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 组装角色权限
|
|
|
|
+ Long roleId = role.getId();
|
|
|
|
+ // 数据权限集合
|
|
|
|
+ List<DataPermissionInfo> dataPermissionInfoList = sysRolePrivilegeParams.getDataPermissionInfo();
|
|
|
|
+ basicRoleDataPermissionService.saveRoleDataPermission(roleId, dataPermissionInfoList);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
if (e instanceof DuplicateKeyException) {
|
|
if (e instanceof DuplicateKeyException) {
|
|
String errorColumn = e.getCause().toString();
|
|
String errorColumn = e.getCause().toString();
|
|
@@ -353,7 +351,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
public List<SysRole> listToAdmin() {
|
|
public List<SysRole> listToAdmin() {
|
|
QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
|
|
QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
|
|
queryWrapper.lambda().isNull(SysRole::getSchoolId)
|
|
queryWrapper.lambda().isNull(SysRole::getSchoolId)
|
|
- .and(s->s.ne(SysRole::getType, RoleTypeEnum.ADMIN).or().isNull(SysRole::getType))
|
|
|
|
|
|
+ .and(s -> s.ne(SysRole::getType, RoleTypeEnum.ADMIN).or().isNull(SysRole::getType))
|
|
.eq(SysRole::getEnable, true)
|
|
.eq(SysRole::getEnable, true)
|
|
.orderByAsc(SysRole::getId);
|
|
.orderByAsc(SysRole::getId);
|
|
return this.list(queryWrapper);
|
|
return this.list(queryWrapper);
|
|
@@ -370,7 +368,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|
sysRoleQueryWrapper.lambda().eq(SysRole::getDefaultRole, true)
|
|
sysRoleQueryWrapper.lambda().eq(SysRole::getDefaultRole, true)
|
|
.eq(SysRole::getEnable, true)
|
|
.eq(SysRole::getEnable, true)
|
|
.isNull(SysRole::getSchoolId)
|
|
.isNull(SysRole::getSchoolId)
|
|
- .and(l->l.ne(SysRole::getType, SystemConstant.ADMIN_CODE.toUpperCase()).or().isNull(SysRole::getType))
|
|
|
|
|
|
+ .and(l -> l.ne(SysRole::getType, SystemConstant.ADMIN_CODE.toUpperCase()).or().isNull(SysRole::getType))
|
|
.orderByAsc(SysRole::getId);
|
|
.orderByAsc(SysRole::getId);
|
|
return sysRoleService.list(sysRoleQueryWrapper);
|
|
return sysRoleService.list(sysRoleQueryWrapper);
|
|
}
|
|
}
|