| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.google.common.annotations.VisibleForTesting; |
| | | import com.google.common.collect.Lists; |
| | | import com.iailab.framework.common.enums.CommonStatusEnum; |
| | |
| | | import com.iailab.module.system.dal.dataobject.app.AppDO; |
| | | import com.iailab.module.system.dal.dataobject.permission.MenuDO; |
| | | import com.iailab.module.system.dal.dataobject.permission.RoleDO; |
| | | import com.iailab.module.system.dal.dataobject.permission.RoleMenuDO; |
| | | import com.iailab.module.system.dal.dataobject.tenant.TenantDO; |
| | | import com.iailab.module.system.dal.dataobject.tenant.TenantPackageDO; |
| | | import com.iailab.module.system.dal.mysql.permission.MenuMapper; |
| | | import com.iailab.module.system.dal.mysql.permission.RoleMenuMapper; |
| | | import com.iailab.module.system.dal.redis.RedisKeyConstants; |
| | | import com.iailab.module.system.enums.permission.MenuTypeEnum; |
| | | import com.iailab.module.system.service.app.AppService; |
| | | import com.iailab.module.system.service.tenant.TenantPackageService; |
| | | import com.iailab.module.system.service.tenant.TenantService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.springframework.cache.annotation.CacheEvict; |
| | | import org.springframework.cache.annotation.Cacheable; |
| | | import org.springframework.context.annotation.Lazy; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static com.iailab.framework.common.util.collection.CollectionUtils.convertList; |
| | | import static com.iailab.framework.common.util.collection.CollectionUtils.convertMap; |
| | | import static com.iailab.framework.common.pojo.CommonResult.success; |
| | | import static com.iailab.framework.common.util.collection.CollectionUtils.*; |
| | | import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static com.iailab.framework.tenant.core.context.TenantContextHolder.getTenantId; |
| | | import static com.iailab.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; |
| | | import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
| | |
| | | |
| | | @Resource |
| | | private RoleService roleService; |
| | | |
| | | @Resource |
| | | private RoleMenuMapper roleMenuMapper; |
| | | |
| | | @Override |
| | | @CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#createReqVO.permission", |
| | |
| | | public List<MenuDO> getAppMenuList(MenuListReqVO reqVO) { |
| | | // 获取 tenantId |
| | | Long tenantId = getTenantId(); |
| | | return menuMapper.selectAppMenuList(tenantId, reqVO); |
| | | List<MenuDO> menuDOS = menuMapper.selectAppMenuList(tenantId, reqVO); |
| | | Set<Long> menuDOIds = menuDOS.stream().map(MenuDO::getId).collect(Collectors.toSet()); |
| | | // 获得角色列表 |
| | | Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(getLoginUserId()); |
| | | List<RoleDO> roles = roleService.getRoleList(roleIds); |
| | | roles.removeIf(role -> !CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())); // 移除禁用的角色 |
| | | if (roles.stream().noneMatch(role -> role.getCode().equals("tenant_admin"))) { |
| | | // 获得菜单列表 |
| | | Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); |
| | | //取交集 |
| | | menuIds.retainAll(menuDOIds); |
| | | List<MenuDO> menuList = getMenuList(menuIds); |
| | | menuList = filterDisableMenus(menuList); |
| | | return menuList; |
| | | } |
| | | return menuDOS; |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public MenuDO getMenuByAppId(Long id) { |
| | | return menuMapper.selectOne(new LambdaQueryWrapper<MenuDO>().eq(MenuDO::getAppId, id).eq(MenuDO::getParentId, 0l)); |
| | | } |
| | | |
| | | @Override |
| | | public List<MenuDO> getMenuList(Collection<Long> ids) { |
| | | // 当 ids 为空时,返回一个空的实例对象 |
| | | if (CollUtil.isEmpty(ids)) { |
| | | return Lists.newArrayList(); |
| | | } |
| | | return menuMapper.selectBatchIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | public List<MenuDO> selectListByParentId(Collection<Long> ids) { |
| | | return menuMapper.selectListByParentId(ids); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | private void dealPermission(MenuDO menu) { |
| | | Long tenantId = menu.getTenantId(); |
| | | RoleDO role = roleService.getTenantAdminRole(tenantId); |
| | | RoleDO tenantRole = roleService.getTenantAdminRole(tenantId); |
| | | TenantDO tenant = tenantService.getTenant(tenantId); |
| | | TenantPackageDO tenantPackage = tenantPackageService.getTenantPackage(tenant.getPackageId()); |
| | | Set<Long> menuIds = tenantPackage.getMenuIds(); |
| | | menuIds.add(menu.getId()); |
| | | tenantPackage.setMenuIds(menuIds); |
| | | tenantPackageService.updateTenantPackage(BeanUtils.toBean(tenantPackage, TenantPackageSaveReqVO.class)); |
| | | permissionService.assignRoleMenu(role.getId(), menuIds); |
| | | permissionService.assignRoleMenu(tenantRole.getId(), menuIds); |
| | | // 开发者自己创建的应用菜单默认赋权给创建者所拥有的角色 |
| | | //查询当前用户所拥有的角色 |
| | | Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(getLoginUserId()); |
| | | List<RoleDO> roles = roleService.getRoleList(roleIds); |
| | | roles.removeIf(role -> !CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())); // 移除禁用的角色 |
| | | roles.removeIf(role -> tenantRole.getId().equals(role.getId())); // 移除租户管理员角色 |
| | | if (!roles.isEmpty()) { |
| | | roles.stream().forEach(roleDO -> { |
| | | RoleMenuDO roleMenuDO = new RoleMenuDO(); |
| | | roleMenuDO.setMenuId(menu.getId()); |
| | | roleMenuDO.setRoleId(roleDO.getId()); |
| | | roleMenuDO.setTenantId(tenant.getId()); |
| | | roleMenuMapper.insert(roleMenuDO); |
| | | }); |
| | | } |
| | | } |
| | | |
| | | } |