package com.qianwen.smartman.modules.system.service.impl; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import javax.validation.constraints.NotEmpty; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.qianwen.core.log.exception.ServiceException; import com.qianwen.core.secure.utils.AuthUtil; import com.qianwen.core.tool.constant.BladeConstant; import com.qianwen.core.tool.node.ForestNodeMerger; import com.qianwen.core.tool.utils.CollectionUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.smartman.common.constant.CommonConstant; import com.qianwen.smartman.modules.system.entity.Role; import com.qianwen.smartman.modules.system.entity.RoleMenu; import com.qianwen.smartman.modules.system.entity.RoleScope; import com.qianwen.smartman.modules.system.mapper.RoleMapper; import com.qianwen.smartman.modules.system.service.IRoleMenuService; import com.qianwen.smartman.modules.system.service.IRoleScopeService; import com.qianwen.smartman.modules.system.service.IRoleService; import com.qianwen.smartman.modules.system.vo.RoleVO; import com.qianwen.smartman.modules.system.wrapper.RoleWrapper; @Service @Validated /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/system/service/impl/RoleServiceImpl.class */ public class RoleServiceImpl extends ServiceImpl implements IRoleService { private final IRoleMenuService roleMenuService; private final IRoleScopeService roleScopeService; public RoleServiceImpl(final IRoleMenuService roleMenuService, final IRoleScopeService roleScopeService) { this.roleMenuService = roleMenuService; this.roleScopeService = roleScopeService; } @Override // org.springblade.modules.system.service.IRoleService public IPage selectRolePage(IPage page, RoleVO role) { return page.setRecords(((RoleMapper) this.baseMapper).selectRolePage(page, role)); } @Override // org.springblade.modules.system.service.IRoleService public List tree(String tenantId) { String userRole = AuthUtil.getUserRole(); String excludeRole = null; if (!CollectionUtil.contains(Func.toStrArray(userRole), "admin") && !CollectionUtil.contains(Func.toStrArray(userRole), "administrator")) { excludeRole = "admin"; } return ForestNodeMerger.merge(((RoleMapper) this.baseMapper).tree(tenantId, excludeRole)); } @Override // org.springblade.modules.system.service.IRoleService @Transactional(rollbackFor = {Exception.class}) public boolean grant(@NotEmpty List roleIds, List menuIds, List dataScopeIds, List apiScopeIds) { grantRoleMenu(roleIds, menuIds); grantDataScope(roleIds, dataScopeIds); grantApiScope(roleIds, apiScopeIds); return true; } private boolean grantRoleMenu(List roleIds, List menuIds) { Long administratorCount = this.baseMapper.selectCount(Wrappers.query().lambda().eq(Role::getRoleAlias, "administrator").in(Role::getId, roleIds)); /* Long administratorCount = ((RoleMapper) this.baseMapper).selectCount((Wrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> { return v0.getRoleAlias(); }, "administrator")).in((v0) -> { return v0.getId(); }, roleIds));*/ if (!AuthUtil.isAdministrator() && administratorCount.longValue() > 0) { throw new ServiceException("无权配置超管角色!"); } Long adminCount = this.baseMapper.selectCount(Wrappers.query().lambda().eq(Role::getRoleAlias, "admin").in(Role::getId, roleIds)); /* Long adminCount = ((RoleMapper) this.baseMapper).selectCount((Wrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> { return v0.getRoleAlias(); }, "admin")).in((v0) -> { return v0.getId(); }, roleIds));*/ if (!AuthUtil.isAdmin() && adminCount.longValue() > 0) { throw new ServiceException("无权配置管理员角色!"); } this.roleMenuService.remove(Wrappers.update().lambda().in(RoleMenu::getRoleId, roleIds)); /* this.roleMenuService.remove((Wrapper) Wrappers.update().lambda().in((v0) -> { return v0.getRoleId(); }, roleIds));*/ List roleMenus = new ArrayList<>(); roleIds.forEach(roleId -> { menuIds.forEach(menuId -> { RoleMenu roleMenu = new RoleMenu(); roleMenu.setRoleId(roleId); roleMenu.setMenuId(menuId); roleMenus.add(roleMenu); }); }); this.roleMenuService.saveBatch(roleMenus); recursionRoleMenu(roleIds, menuIds); return true; } private void recursionRoleMenu(List roleIds, List menuIds) { //Role::getParentId RoleMenu::getRoleId RoleMenu::getMenuId roleIds.forEach(roleId -> { this.baseMapper.selectList( Wrappers.query().lambda().eq(Role::getParentId, roleId)).forEach(role -> { List roleMenuList = this.roleMenuService.list(Wrappers.query().lambda().eq(RoleMenu::getRoleId, role.getId())); List collectRoleMenuIds = roleMenuList.stream().map((v0) -> { return v0.getMenuId(); }).filter(menuId -> { return !menuIds.contains(menuId); }).collect(Collectors.toList()); if (collectRoleMenuIds.size() > 0) { this.roleMenuService.remove(Wrappers.update().lambda().eq(RoleMenu::getRoleId, role.getId()).in(RoleMenu::getMenuId, collectRoleMenuIds)); recursionRoleMenu(Collections.singletonList(role.getId()), menuIds); } }); }); } private boolean grantDataScope(List roleIds, List dataScopeIds) { this.roleScopeService.remove(Wrappers.update().lambda() .eq(RoleScope::getScopeCategory, CommonConstant.DATA_SCOPE_CATEGORY).in(RoleScope::getRoleId, roleIds)); /* this.roleScopeService.remove((Wrapper) ((LambdaUpdateWrapper) Wrappers.update().lambda().eq((v0) -> { return v0.getScopeCategory(); }, CommonConstant.DATA_SCOPE_CATEGORY)).in((v0) -> { return v0.getRoleId(); }, roleIds));*/ List roleDataScopes = new ArrayList<>(); roleIds.forEach(roleId -> { dataScopeIds.forEach(scopeId -> { RoleScope roleScope = new RoleScope(); roleScope.setScopeCategory(CommonConstant.DATA_SCOPE_CATEGORY); roleScope.setRoleId(roleId); roleScope.setScopeId(scopeId); roleDataScopes.add(roleScope); }); }); this.roleScopeService.saveBatch(roleDataScopes); return true; } private boolean grantApiScope(List roleIds, List apiScopeIds) { this.roleScopeService.remove(Wrappers.update().lambda() .eq(RoleScope::getScopeCategory, CommonConstant.API_SCOPE_CATEGORY).in(RoleScope::getRoleId, roleIds)); /* this.roleScopeService.remove((Wrapper) ((LambdaUpdateWrapper) Wrappers.update().lambda().eq((v0) -> { return v0.getScopeCategory(); }, CommonConstant.API_SCOPE_CATEGORY)).in((v0) -> { return v0.getRoleId(); }, roleIds));*/ List roleApiScopes = new ArrayList<>(); roleIds.forEach(roleId -> { apiScopeIds.forEach(scopeId -> { RoleScope roleScope = new RoleScope(); roleScope.setScopeCategory(CommonConstant.API_SCOPE_CATEGORY); roleScope.setScopeId(scopeId); roleScope.setRoleId(roleId); roleApiScopes.add(roleScope); }); }); this.roleScopeService.saveBatch(roleApiScopes); return true; } @Override // org.springblade.modules.system.service.IRoleService public String getRoleIds(String tenantId, String roleNames) { List roleList = this.baseMapper.selectList(Wrappers.query().lambda().eq(Role::getTenantId, tenantId).in(Role::getRoleName, Func.toStrList(roleNames))); /* List roleList = ((RoleMapper) this.baseMapper).selectList((Wrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> { return v0.getTenantId(); }, tenantId)).in((v0) -> { return v0.getRoleName(); }, Func.toStrList(roleNames)));*/ if (roleList != null && roleList.size() > 0) { return (String) roleList.stream().map(role -> { return Func.toStr(role.getId()); }).distinct().collect(Collectors.joining(",")); } return null; } @Override // org.springblade.modules.system.service.IRoleService public List getRoleNames(String roleIds) { return ((RoleMapper) this.baseMapper).getRoleNames(Func.toLongArray(roleIds)); } @Override // org.springblade.modules.system.service.IRoleService public List getRoleAliases(String roleIds) { return ((RoleMapper) this.baseMapper).getRoleAliases(Func.toLongArray(roleIds)); } @Override // org.springblade.modules.system.service.IRoleService public Role submit(Role role) { if (!AuthUtil.isAdministrator() && Func.toStr(role.getRoleAlias()).equals("administrator")) { throw new ServiceException("无权限创建超管角色!"); } if (Func.isEmpty(role.getParentId())) { role.setTenantId(AuthUtil.getTenantId()); role.setParentId(BladeConstant.TOP_PARENT_ID); } if (role.getParentId().longValue() > 0) { Role parent = (Role) getById(role.getParentId()); if (Func.toLong(role.getParentId()) == Func.toLong(role.getId())) { throw new ServiceException("父节点不可选择自身!"); } role.setTenantId(parent.getTenantId()); } role.setIsDeleted(0); saveOrUpdate(role); return role; } @Override // org.springblade.modules.system.service.IRoleService public List search(String roleName, Long parentId) { LambdaQueryWrapper lambda = Wrappers.query().lambda(); //Wrapper lambda = Wrappers.query().lambda(); if (Func.isNotEmpty(roleName)) { lambda.like((v0) -> { return v0.getRoleName(); }, roleName); } if (Func.isNotEmpty(parentId) && parentId.longValue() > BladeConstant.TOP_PARENT_ID.longValue()) { lambda.eq((v0) -> { return v0.getParentId(); }, parentId); } List roleList = ((RoleMapper) this.baseMapper).selectList(lambda); return RoleWrapper.build().listNodeVO(roleList); } }