update:修改sysrole实现

V0.5.x
荭琪枫 2023-05-30 16:32:51 +08:00
parent e335d709fc
commit a485fb9a5a
13 changed files with 320 additions and 51 deletions

View File

@ -0,0 +1,29 @@
package cc.iotkit.model.system;
import cc.iotkit.model.BaseModel;
import cc.iotkit.model.Id;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* author:
* date: 2023-05-30 16:16
* description:
**/
@EqualsAndHashCode(callSuper = true)
@Data
public class SysRoleDept extends BaseModel implements Id<Long>, Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Long roleId;
/**
* ID
*/
private Long deptId;
}

View File

@ -0,0 +1,33 @@
package cc.iotkit.model.system;
import cc.iotkit.model.BaseModel;
import cc.iotkit.model.Id;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* author:
* date: 2023-05-30 10:57
* description:
**/
@EqualsAndHashCode(callSuper = true)
@Data
public class SysRoleMenu extends BaseModel implements Id<Long>, Serializable {
private static final long serialVersionUID = 1L;
private Long id;
/**
* ID
*/
private Long roleId;
/**
* ID
*/
private Long menuId;
}

View File

@ -43,4 +43,5 @@ public interface ISysMenuData extends ICommonData<SysMenu, Long> {
boolean checkMenuNameUnique(SysMenu menu);
}

View File

@ -19,4 +19,45 @@ public interface ISysRoleData extends ICommonData<SysRole, Long> {
* @return
*/
List<Long> selectMenuListByRoleId(Long roleId, boolean menuCheckStrictly);
/**
* ID
*
* @param userId ID
* @return
*/
List<SysRole> selectRolePermissionByUserId(Long userId);
/**
* ID
*
* @param userId ID
* @return ID
*/
List<Long> selectRoleListByUserId(Long userId);
/**
*
*
* @param role
* @return
*/
boolean checkRoleNameUnique(SysRole role);
/**
*
*
* @param role
* @return
*/
boolean checkRoleKeyUnique(SysRole role);
/**
*
*
* @param role
* @return
*/
int updateById(SysRole role);
List<SysRole> selectRoleList(SysRole role);
}

View File

@ -1,9 +1,15 @@
package cc.iotkit.data.system;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.system.SysRoleDept;
/**
*
*
* @author sjg
*/
public interface ISysRoleDeptData {
public interface ISysRoleDeptData extends ICommonData<SysRoleDept, Long> {
void delete(Long roleId);
}

View File

@ -1,9 +1,13 @@
package cc.iotkit.data.system;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.system.SysRoleMenu;
/**
*
*
* @author sjg
*/
public interface ISysRoleMenuData {
public interface ISysRoleMenuData extends ICommonData<SysRoleMenu, Long> {
boolean checkMenuExistRole(Long menuId);
}

View File

@ -1,20 +1,30 @@
package cc.iotkit.data.service;
import cc.iotkit.common.constant.UserConstants;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.dao.SysRoleRepository;
import cc.iotkit.data.model.QTbSysRole;
import cc.iotkit.data.model.TbSysRole;
import cc.iotkit.data.system.ISysRoleData;
import cc.iotkit.data.util.PredicateBuilder;
import cc.iotkit.model.system.SysRole;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import static cc.iotkit.data.model.QTbSysDept.tbSysDept;
import static cc.iotkit.data.model.QTbSysMenu.tbSysMenu;
import static cc.iotkit.data.model.QTbSysRole.tbSysRole;
import static cc.iotkit.data.model.QTbSysRoleMenu.tbSysRoleMenu;
import static cc.iotkit.data.model.QTbSysUser.tbSysUser;
import static cc.iotkit.data.model.QTbSysUserRole.tbSysUserRole;
/**
* author:
@ -59,4 +69,76 @@ public class SysRoleDataImpl implements ISysRoleData {
.where(predicateBuilder.build())
.orderBy(tbSysMenu.parentId.asc(), tbSysMenu.orderNum.asc()).fetch();
}
@Override
public List<SysRole> selectRolePermissionByUserId(Long userId) {
return buildQueryTitle(PredicateBuilder.instance()
.and(tbSysRole.delFlag.eq("0"))
.and(tbSysUserRole.userId.eq(userId))
.build());
}
@Override
public List<Long> selectRoleListByUserId(Long userId) {
return jpaQueryFactory.select(tbSysRole.roleId).from(tbSysRole)
.leftJoin(tbSysUserRole).on(tbSysUserRole.roleId.eq(tbSysRole.roleId))
.leftJoin(tbSysUser).on(tbSysUser.userId.eq(tbSysUserRole.userId))
.where(PredicateBuilder.instance().and(tbSysUser.userId.eq(userId)).build()).fetch();
}
@Override
public boolean checkRoleNameUnique(SysRole role) {
final TbSysRole tbSysRoleRes = jpaQueryFactory.select(tbSysRole).from(tbSysRole)
.where(PredicateBuilder.instance()
.and(tbSysRole.roleName.eq(role.getRoleName()))
.and(Objects.nonNull(role.getId()), () -> tbSysRole.roleId.eq(role.getId()))
.build()).fetchOne();
return Objects.isNull(tbSysRoleRes);
}
@Override
public boolean checkRoleKeyUnique(SysRole role) {
final TbSysRole tbSysRoleRes = jpaQueryFactory.select(tbSysRole).from(tbSysRole)
.where(PredicateBuilder.instance()
.and(tbSysRole.roleKey.eq(role.getRoleKey()))
.and(Objects.nonNull(role.getId()), () -> tbSysRole.roleId.eq(role.getId()))
.build()).fetchOne();
return Objects.isNull(tbSysRoleRes);
}
@Override
public int updateById(SysRole role) {
long execute = jpaQueryFactory.update(tbSysRole)
.where(PredicateBuilder.instance().and(tbSysRole.roleId.eq(role.getId())).build()).execute();
return Integer.parseInt(execute + "");
}
@Override
public List<SysRole> selectRoleList(SysRole role) {
return buildQueryTitle(buildQueryWrapper(role));
}
private List<SysRole> buildQueryTitle(Predicate predicate) {
return jpaQueryFactory.select(Projections.bean(SysRole.class, tbSysRole.roleId.countDistinct(), tbSysRole.roleName,
tbSysRole.roleKey, tbSysRole.roleSort, tbSysRole.menuCheckStrictly, tbSysRole.deptCheckStrictly,
tbSysRole.status, tbSysRole.delFlag, tbSysRole.createTime, tbSysRole.remark))
.from(tbSysRole)
.leftJoin(tbSysUserRole).on(tbSysUserRole.roleId.eq(tbSysRole.roleId))
.leftJoin(tbSysUser).on(tbSysUser.userId.eq(tbSysUserRole.userId))
.leftJoin(tbSysDept).on(tbSysUser.deptId.eq(tbSysDept.deptId))
.where(predicate)
.orderBy(tbSysRole.roleSort.asc(), tbSysRole.createTime.asc()).fetch();
}
private Predicate buildQueryWrapper(SysRole role) {
return PredicateBuilder.instance()
.and(tbSysRole.delFlag.eq(UserConstants.ROLE_NORMAL))
.and(Objects.nonNull(role.getId()), () -> tbSysRole.roleId.eq(role.getId()))
.and(StringUtils.isNotBlank(role.getRoleName()), () -> tbSysRole.roleName.like(role.getRoleName()))
.and(StringUtils.isNotBlank(role.getStatus()), () -> tbSysRole.roleName.eq(role.getStatus()))
.and(StringUtils.isNotBlank(role.getRoleKey()), () -> tbSysRole.roleKey.like(role.getRoleKey()))
.build();
}
}

View File

@ -0,0 +1,28 @@
package cc.iotkit.data.service;
import cc.iotkit.data.system.ISysRoleDeptData;
import cc.iotkit.data.util.PredicateBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import static cc.iotkit.data.model.QTbSysRoleDept.tbSysRoleDept;
/**
* author:
* date: 2023-05-30 16:20
* description:
**/
@Primary
@Service
@RequiredArgsConstructor
public class SysRoleDeptDataImpl implements ISysRoleDeptData {
private final JPAQueryFactory jpaQueryFactory;
@Override
public void delete(Long roleId) {
jpaQueryFactory.delete(tbSysRoleDept).where(PredicateBuilder.instance().and(tbSysRoleDept.roleId.eq(roleId)).build());
}
}

View File

@ -0,0 +1,36 @@
package cc.iotkit.data.service;
import cc.iotkit.data.model.QTbSysRoleMenu;
import cc.iotkit.data.model.TbSysRoleMenu;
import cc.iotkit.data.system.ISysRoleMenuData;
import cc.iotkit.data.util.PredicateBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import java.util.Objects;
/**
* author:
* date: 2023-05-30 11:00
* description:
**/
@Primary
@Service
@RequiredArgsConstructor
public class SysRoleMenuDataImpl implements ISysRoleMenuData {
private final JPAQueryFactory jpaQueryFactory;
@Override
public boolean checkMenuExistRole(Long menuId) {
TbSysRoleMenu tbSysRoleMenu = jpaQueryFactory
.select(QTbSysRoleMenu.tbSysRoleMenu)
.from(QTbSysRoleMenu.tbSysRoleMenu)
.where(PredicateBuilder.instance()
.and(QTbSysRoleMenu.tbSysRoleMenu.menuId.eq(menuId))
.build()).fetchOne();
return Objects.nonNull(tbSysRoleMenu);
}
}

View File

@ -108,22 +108,6 @@ public class SysMenuController extends BaseController {
return selectVo;
}
/**
*
*
* @param packageId ID
*/
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:menu:query")
@GetMapping(value = "/tenantPackageMenuTreeselect/{packageId}")
public MenuTreeSelectVo tenantPackageMenuTreeselect(@PathVariable("packageId") Long packageId) {
List<SysMenuVo> menus = menuService.selectMenuList(LoginHelper.getUserId());
MenuTreeSelectVo selectVo = new MenuTreeSelectVo();
selectVo.setCheckedKeys(menuService.selectMenuListByPackageId(packageId));
selectVo.setMenus(menuService.buildMenuTreeSelect(menus));
return selectVo;
}
/**
*
*/

View File

@ -65,14 +65,6 @@ public interface ISysMenuService {
*/
List<Long> selectMenuListByRoleId(Long roleId);
/**
* ID
*
* @param packageId ID
* @return
*/
List<Long> selectMenuListByPackageId(Long packageId);
/**
*
*

View File

@ -9,6 +9,7 @@ import cc.iotkit.common.utils.StringUtils;
import cc.iotkit.common.utils.TreeBuildUtils;
import cc.iotkit.data.system.ISysMenuData;
import cc.iotkit.data.system.ISysRoleData;
import cc.iotkit.data.system.ISysRoleMenuData;
import cc.iotkit.model.system.SysMenu;
import cc.iotkit.model.system.SysRole;
import cc.iotkit.system.dto.bo.SysMenuBo;
@ -34,6 +35,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
private final ISysMenuData sysMenuData;
private final ISysRoleData sysRoleData;
private final ISysRoleMenuData iSysRoleMenuData;
/**
*
@ -126,17 +128,6 @@ public class SysMenuServiceImpl implements ISysMenuService {
return sysRoleData.selectMenuListByRoleId(roleId, role.getMenuCheckStrictly());
}
/**
* ID
*
* @param packageId ID
* @return
*/
@Override
public List<Long> selectMenuListByPackageId(Long packageId) {
return new ArrayList<>();
}
/**
*
*
@ -324,7 +315,7 @@ public class SysMenuServiceImpl implements ISysMenuService {
*/
@Override
public boolean checkMenuExistRole(Long menuId) {
return false;
return iSysRoleMenuData.checkMenuExistRole(menuId);
}
/**

View File

@ -4,7 +4,12 @@ import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.common.utils.StringUtils;
import cc.iotkit.data.system.ISysRoleData;
import cc.iotkit.data.system.ISysRoleDeptData;
import cc.iotkit.data.system.ISysRoleMenuData;
import cc.iotkit.data.system.ISysUserRoleData;
import cc.iotkit.model.system.SysRole;
import cc.iotkit.system.dto.SysUserRole;
import cc.iotkit.system.dto.bo.SysRoleBo;
@ -16,7 +21,6 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -30,7 +34,10 @@ import java.util.Set;
@Service
public class SysRoleServiceImpl implements ISysRoleService {
private final ISysRoleData sysRoleData;
private final ISysRoleData iSysRoleData;
private final ISysRoleMenuData iSysRoleMenuData;
private final ISysUserRoleData iSysUserRoleData;
private final ISysRoleDeptData iSysRoleDeptData;
@Override
public Paging<SysRoleVo> selectPageRoleList(SysRoleBo role, PageRequest<?> query) {
@ -45,9 +52,15 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public List<SysRoleVo> selectRoleList(SysRoleBo role) {
return new ArrayList<>();
List<SysRole> sysRoles = getSysRoles(role);
return MapstructUtils.convert(sysRoles, SysRoleVo.class);
}
private List<SysRole> getSysRoles(SysRoleBo role) {
return iSysRoleData.selectRoleList(MapstructUtils.convert(role, SysRole.class));
}
/**
* ID
*
@ -56,7 +69,17 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public List<SysRoleVo> selectRolesByUserId(Long userId) {
return new ArrayList<>();
List<SysRole> sysRoles = iSysRoleData.selectRolePermissionByUserId(userId);
List<SysRole> roles = getSysRoles(new SysRoleBo());
for (SysRole role : roles) {
for (SysRole sysRole : sysRoles) {
if (role.getId().longValue() == sysRole.getId().longValue()) {
role.setFlag(true);
break;
}
}
}
return MapstructUtils.convert(roles, SysRoleVo.class);
}
/**
@ -67,7 +90,14 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public Set<String> selectRolePermissionByUserId(Long userId) {
return new HashSet<>();
List<SysRole> perms = iSysRoleData.selectRolePermissionByUserId(userId);
Set<String> permsSet = new HashSet<>();
for (SysRole perm : perms) {
if (ObjectUtil.isNotNull(perm)) {
permsSet.addAll(StringUtils.splitList(perm.getRoleKey().trim()));
}
}
return permsSet;
}
/**
@ -88,7 +118,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public List<Long> selectRoleListByUserId(Long userId) {
return new ArrayList<>();
return iSysRoleData.selectRoleListByUserId(userId);
}
/**
@ -99,7 +129,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public SysRoleVo selectRoleById(Long roleId) {
return sysRoleData.findById(roleId).to(SysRoleVo.class);
return iSysRoleData.findById(roleId).to(SysRoleVo.class);
}
/**
@ -110,7 +140,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public boolean checkRoleNameUnique(SysRoleBo role) {
return false;
return iSysRoleData.checkRoleNameUnique(MapstructUtils.convert(role, SysRole.class));
}
/**
@ -121,7 +151,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public boolean checkRoleKeyUnique(SysRoleBo role) {
return false;
return iSysRoleData.checkRoleKeyUnique(MapstructUtils.convert(role, SysRole.class));
}
/**
@ -176,7 +206,7 @@ public class SysRoleServiceImpl implements ISysRoleService {
@Override
@Transactional(rollbackFor = Exception.class)
public void insertRole(SysRoleBo bo) {
SysRole role = sysRoleData.save(bo.to(SysRole.class));
SysRole role = iSysRoleData.save(bo.to(SysRole.class));
bo.setRoleId(role.getId());
insertRoleMenu(bo);
}
@ -190,8 +220,8 @@ public class SysRoleServiceImpl implements ISysRoleService {
@Override
@Transactional(rollbackFor = Exception.class)
public int updateRole(SysRoleBo bo) {
SysRole role = sysRoleData.save(bo.to(SysRole.class));
if(ObjectUtil.isNull(role)){
SysRole role = iSysRoleData.save(bo.to(SysRole.class));
if (ObjectUtil.isNull(role)) {
return 0;
}
return 1;
@ -206,6 +236,11 @@ public class SysRoleServiceImpl implements ISysRoleService {
*/
@Override
public void updateRoleStatus(Long roleId, String status) {
SysRole sysRole = new SysRole();
sysRole.setId(roleId);
sysRole.setStatus(status);
iSysRoleData.updateById(sysRole);
}
/**
@ -217,6 +252,13 @@ public class SysRoleServiceImpl implements ISysRoleService {
@Override
@Transactional(rollbackFor = Exception.class)
public void authDataScope(SysRoleBo bo) {
// 修改角色信息
iSysRoleData.updateById(MapstructUtils.convert(bo, SysRole.class));
// 删除角色与部门关联
iSysRoleDeptData.delete(bo.getRoleId());
// 新增角色和部门信息(数据权限)
insertRoleDept(bo);
}
/**