提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.permission; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
d9f9ba
|
4 |
import cn.hutool.core.util.ObjUtil; |
7da8f1
|
5 |
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
d9f9ba
|
6 |
import com.google.common.annotations.VisibleForTesting; |
H |
7 |
import com.google.common.collect.Lists; |
|
8 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
e7c126
|
9 |
import com.iailab.framework.common.util.object.BeanUtils; |
H |
10 |
import com.iailab.module.system.controller.admin.permission.vo.menu.MenuListReqVO; |
|
11 |
import com.iailab.module.system.controller.admin.permission.vo.menu.MenuSaveVO; |
818a01
|
12 |
import com.iailab.module.system.controller.admin.tenant.vo.packages.TenantPackageSaveReqVO; |
H |
13 |
import com.iailab.module.system.dal.dataobject.app.AppDO; |
e7c126
|
14 |
import com.iailab.module.system.dal.dataobject.permission.MenuDO; |
818a01
|
15 |
import com.iailab.module.system.dal.dataobject.permission.RoleDO; |
7da8f1
|
16 |
import com.iailab.module.system.dal.dataobject.permission.RoleMenuDO; |
818a01
|
17 |
import com.iailab.module.system.dal.dataobject.tenant.TenantDO; |
H |
18 |
import com.iailab.module.system.dal.dataobject.tenant.TenantPackageDO; |
e7c126
|
19 |
import com.iailab.module.system.dal.mysql.permission.MenuMapper; |
7da8f1
|
20 |
import com.iailab.module.system.dal.mysql.permission.RoleMenuMapper; |
e7c126
|
21 |
import com.iailab.module.system.dal.redis.RedisKeyConstants; |
H |
22 |
import com.iailab.module.system.enums.permission.MenuTypeEnum; |
818a01
|
23 |
import com.iailab.module.system.service.app.AppService; |
H |
24 |
import com.iailab.module.system.service.tenant.TenantPackageService; |
e7c126
|
25 |
import com.iailab.module.system.service.tenant.TenantService; |
H |
26 |
import lombok.extern.slf4j.Slf4j; |
|
27 |
import org.springframework.cache.annotation.CacheEvict; |
|
28 |
import org.springframework.cache.annotation.Cacheable; |
|
29 |
import org.springframework.context.annotation.Lazy; |
|
30 |
import org.springframework.stereotype.Service; |
|
31 |
import org.springframework.transaction.annotation.Transactional; |
|
32 |
|
|
33 |
import javax.annotation.Resource; |
d9f9ba
|
34 |
import java.util.*; |
7da8f1
|
35 |
import java.util.stream.Collectors; |
e7c126
|
36 |
|
H |
37 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
7da8f1
|
38 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
H |
39 |
import static com.iailab.framework.common.util.collection.CollectionUtils.*; |
|
40 |
import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
818a01
|
41 |
import static com.iailab.framework.tenant.core.context.TenantContextHolder.getTenantId; |
e7c126
|
42 |
import static com.iailab.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; |
H |
43 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
d9f9ba
|
44 |
|
e7c126
|
45 |
|
H |
46 |
/** |
|
47 |
* 菜单 Service 实现 |
|
48 |
* |
4d4165
|
49 |
* @author iailab |
e7c126
|
50 |
*/ |
H |
51 |
@Service |
|
52 |
@Slf4j |
|
53 |
public class MenuServiceImpl implements MenuService { |
|
54 |
|
|
55 |
@Resource |
|
56 |
private MenuMapper menuMapper; |
|
57 |
@Resource |
|
58 |
private PermissionService permissionService; |
|
59 |
@Resource |
|
60 |
@Lazy // 延迟,避免循环依赖报错 |
|
61 |
private TenantService tenantService; |
|
62 |
|
818a01
|
63 |
@Resource |
H |
64 |
private TenantPackageService tenantPackageService; |
|
65 |
|
|
66 |
@Resource |
|
67 |
private AppService appService; |
|
68 |
|
|
69 |
@Resource |
|
70 |
private RoleService roleService; |
7da8f1
|
71 |
|
H |
72 |
@Resource |
|
73 |
private RoleMenuMapper roleMenuMapper; |
818a01
|
74 |
|
e7c126
|
75 |
@Override |
H |
76 |
@CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#createReqVO.permission", |
|
77 |
condition = "#createReqVO.permission != null") |
818a01
|
78 |
@Transactional(rollbackFor = Exception.class) |
e7c126
|
79 |
public Long createMenu(MenuSaveVO createReqVO) { |
H |
80 |
// 校验父菜单存在 |
|
81 |
validateParentMenu(createReqVO.getParentId(), null); |
|
82 |
// 校验菜单(自己) |
|
83 |
validateMenu(createReqVO.getParentId(), createReqVO.getName(), null); |
|
84 |
|
|
85 |
// 插入数据库 |
|
86 |
MenuDO menu = BeanUtils.toBean(createReqVO, MenuDO.class); |
|
87 |
initMenuProperty(menu); |
818a01
|
88 |
|
H |
89 |
//菜单归属租户和应用 |
|
90 |
Long tenantId = getTenantId(); |
|
91 |
menu.setTenantId(tenantId); |
|
92 |
menu.setAppId(createReqVO.getAppId()); |
e7c126
|
93 |
menuMapper.insert(menu); |
818a01
|
94 |
if(tenantId != 1L) { |
H |
95 |
dealPermission(menu); |
|
96 |
} |
e7c126
|
97 |
// 返回 |
H |
98 |
return menu.getId(); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
@CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, |
|
103 |
allEntries = true) // allEntries 清空所有缓存,因为 permission 如果变更,涉及到新老两个 permission。直接清理,简单有效 |
818a01
|
104 |
@Transactional(rollbackFor = Exception.class) |
e7c126
|
105 |
public void updateMenu(MenuSaveVO updateReqVO) { |
H |
106 |
// 校验更新的菜单是否存在 |
|
107 |
if (menuMapper.selectById(updateReqVO.getId()) == null) { |
|
108 |
throw exception(MENU_NOT_EXISTS); |
|
109 |
} |
|
110 |
// 校验父菜单存在 |
|
111 |
validateParentMenu(updateReqVO.getParentId(), updateReqVO.getId()); |
|
112 |
// 校验菜单(自己) |
|
113 |
validateMenu(updateReqVO.getParentId(), updateReqVO.getName(), updateReqVO.getId()); |
|
114 |
|
|
115 |
// 更新到数据库 |
|
116 |
MenuDO updateObj = BeanUtils.toBean(updateReqVO, MenuDO.class); |
|
117 |
initMenuProperty(updateObj); |
818a01
|
118 |
//菜单归属租户和应用 |
H |
119 |
Long tenantId = getTenantId(); |
|
120 |
AppDO appDO = appService.getAppByTenantId(tenantId); |
|
121 |
updateObj.setTenantId(tenantId); |
|
122 |
updateObj.setAppId(appDO.getId()); |
e7c126
|
123 |
menuMapper.updateById(updateObj); |
H |
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
@Transactional(rollbackFor = Exception.class) |
|
128 |
@CacheEvict(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, |
|
129 |
allEntries = true) // allEntries 清空所有缓存,因为此时不知道 id 对应的 permission 是多少。直接清理,简单有效 |
|
130 |
public void deleteMenu(Long id) { |
|
131 |
// 校验是否还有子菜单 |
|
132 |
if (menuMapper.selectCountByParentId(id) > 0) { |
|
133 |
throw exception(MENU_EXISTS_CHILDREN); |
|
134 |
} |
|
135 |
// 校验删除的菜单是否存在 |
|
136 |
if (menuMapper.selectById(id) == null) { |
|
137 |
throw exception(MENU_NOT_EXISTS); |
|
138 |
} |
|
139 |
// 标记删除 |
|
140 |
menuMapper.deleteById(id); |
|
141 |
// 删除授予给角色的权限 |
|
142 |
permissionService.processMenuDeleted(id); |
|
143 |
} |
|
144 |
|
|
145 |
@Override |
|
146 |
public List<MenuDO> getMenuList() { |
|
147 |
return menuMapper.selectList(); |
|
148 |
} |
|
149 |
|
|
150 |
@Override |
|
151 |
public List<MenuDO> getMenuListByTenant(MenuListReqVO reqVO) { |
d9f9ba
|
152 |
// 查询所有菜单,并过滤掉关闭的节点 |
e7c126
|
153 |
List<MenuDO> menus = getMenuList(reqVO); |
818a01
|
154 |
// 开启多租户的情况下,需要过滤掉未开通的菜单 |
H |
155 |
tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); |
|
156 |
return menus; |
|
157 |
} |
|
158 |
|
|
159 |
@Override |
|
160 |
public List<MenuDO> getAppMenuListByTenant(MenuListReqVO reqVO) { |
ce910c
|
161 |
// 获取 tenantId |
H |
162 |
Long tenantId = getTenantId(); |
818a01
|
163 |
// 查询所有菜单,并过滤掉关闭的节点 |
ce910c
|
164 |
List<MenuDO> menus = getAppMenuList(tenantId, reqVO); |
e7c126
|
165 |
// 开启多租户的情况下,需要过滤掉未开通的菜单 |
H |
166 |
tenantService.handleTenantMenu(menuIds -> menus.removeIf(menu -> !CollUtil.contains(menuIds, menu.getId()))); |
|
167 |
return menus; |
|
168 |
} |
|
169 |
|
|
170 |
@Override |
d9f9ba
|
171 |
public List<MenuDO> filterDisableMenus(List<MenuDO> menuList) { |
H |
172 |
if (CollUtil.isEmpty(menuList)){ |
|
173 |
return Collections.emptyList(); |
|
174 |
} |
|
175 |
Map<Long, MenuDO> menuMap = convertMap(menuList, MenuDO::getId); |
|
176 |
|
|
177 |
// 遍历 menu 菜单,查找不是禁用的菜单,添加到 enabledMenus 结果 |
|
178 |
List<MenuDO> enabledMenus = new ArrayList<>(); |
|
179 |
Set<Long> disabledMenuCache = new HashSet<>(); // 存下递归搜索过被禁用的菜单,防止重复的搜索 |
|
180 |
for (MenuDO menu : menuList) { |
|
181 |
if (isMenuDisabled(menu, menuMap, disabledMenuCache)) { |
|
182 |
continue; |
|
183 |
} |
|
184 |
enabledMenus.add(menu); |
|
185 |
} |
|
186 |
return enabledMenus; |
|
187 |
} |
|
188 |
|
|
189 |
private boolean isMenuDisabled(MenuDO node, Map<Long, MenuDO> menuMap, Set<Long> disabledMenuCache) { |
|
190 |
// 如果已经判定是禁用的节点,直接结束 |
|
191 |
if (disabledMenuCache.contains(node.getId())) { |
|
192 |
return true; |
|
193 |
} |
|
194 |
|
|
195 |
// 1. 遍历到 parentId 为根节点,则无需判断 |
|
196 |
Long parentId = node.getParentId(); |
|
197 |
if (ObjUtil.equal(parentId, ID_ROOT)) { |
|
198 |
if (CommonStatusEnum.isDisable(node.getStatus())) { |
|
199 |
disabledMenuCache.add(node.getId()); |
|
200 |
return true; |
|
201 |
} |
|
202 |
return false; |
|
203 |
} |
|
204 |
|
|
205 |
// 2. 继续遍历 parent 节点 |
|
206 |
MenuDO parent = menuMap.get(parentId); |
|
207 |
if (parent == null || isMenuDisabled(parent, menuMap, disabledMenuCache)) { |
|
208 |
disabledMenuCache.add(node.getId()); |
|
209 |
return true; |
|
210 |
} |
|
211 |
return false; |
|
212 |
} |
|
213 |
|
|
214 |
@Override |
e7c126
|
215 |
public List<MenuDO> getMenuList(MenuListReqVO reqVO) { |
H |
216 |
return menuMapper.selectList(reqVO); |
818a01
|
217 |
} |
H |
218 |
|
|
219 |
@Override |
ce910c
|
220 |
public List<MenuDO> getAppMenuList(Long tenantId, MenuListReqVO reqVO) { |
7da8f1
|
221 |
List<MenuDO> menuDOS = menuMapper.selectAppMenuList(tenantId, reqVO); |
H |
222 |
Set<Long> menuDOIds = menuDOS.stream().map(MenuDO::getId).collect(Collectors.toSet()); |
|
223 |
// 获得角色列表 |
|
224 |
Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(getLoginUserId()); |
|
225 |
List<RoleDO> roles = roleService.getRoleList(roleIds); |
|
226 |
roles.removeIf(role -> !CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())); // 移除禁用的角色 |
|
227 |
if (roles.stream().noneMatch(role -> role.getCode().equals("tenant_admin"))) { |
|
228 |
// 获得菜单列表 |
|
229 |
Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(convertSet(roles, RoleDO::getId)); |
|
230 |
//取交集 |
|
231 |
menuIds.retainAll(menuDOIds); |
|
232 |
List<MenuDO> menuList = getMenuList(menuIds); |
|
233 |
menuList = filterDisableMenus(menuList); |
|
234 |
return menuList; |
|
235 |
} |
|
236 |
return menuDOS; |
e7c126
|
237 |
} |
H |
238 |
|
|
239 |
@Override |
|
240 |
@Cacheable(value = RedisKeyConstants.PERMISSION_MENU_ID_LIST, key = "#permission") |
|
241 |
public List<Long> getMenuIdListByPermissionFromCache(String permission) { |
|
242 |
List<MenuDO> menus = menuMapper.selectListByPermission(permission); |
|
243 |
return convertList(menus, MenuDO::getId); |
|
244 |
} |
|
245 |
|
|
246 |
@Override |
|
247 |
public MenuDO getMenu(Long id) { |
|
248 |
return menuMapper.selectById(id); |
|
249 |
} |
|
250 |
|
|
251 |
@Override |
7da8f1
|
252 |
public MenuDO getMenuByAppId(Long id) { |
H |
253 |
return menuMapper.selectOne(new LambdaQueryWrapper<MenuDO>().eq(MenuDO::getAppId, id).eq(MenuDO::getParentId, 0l)); |
|
254 |
} |
|
255 |
|
|
256 |
@Override |
e7c126
|
257 |
public List<MenuDO> getMenuList(Collection<Long> ids) { |
H |
258 |
// 当 ids 为空时,返回一个空的实例对象 |
|
259 |
if (CollUtil.isEmpty(ids)) { |
|
260 |
return Lists.newArrayList(); |
|
261 |
} |
|
262 |
return menuMapper.selectBatchIds(ids); |
7da8f1
|
263 |
} |
H |
264 |
|
|
265 |
@Override |
|
266 |
public List<MenuDO> selectListByParentId(Collection<Long> ids) { |
|
267 |
return menuMapper.selectListByParentId(ids); |
e7c126
|
268 |
} |
H |
269 |
|
|
270 |
/** |
|
271 |
* 校验父菜单是否合法 |
|
272 |
* <p> |
|
273 |
* 1. 不能设置自己为父菜单 |
|
274 |
* 2. 父菜单不存在 |
|
275 |
* 3. 父菜单必须是 {@link MenuTypeEnum#MENU} 菜单类型 |
|
276 |
* |
|
277 |
* @param parentId 父菜单编号 |
|
278 |
* @param childId 当前菜单编号 |
|
279 |
*/ |
|
280 |
@VisibleForTesting |
|
281 |
void validateParentMenu(Long parentId, Long childId) { |
|
282 |
if (parentId == null || ID_ROOT.equals(parentId)) { |
|
283 |
return; |
|
284 |
} |
|
285 |
// 不能设置自己为父菜单 |
|
286 |
if (parentId.equals(childId)) { |
|
287 |
throw exception(MENU_PARENT_ERROR); |
|
288 |
} |
|
289 |
MenuDO menu = menuMapper.selectById(parentId); |
|
290 |
// 父菜单不存在 |
|
291 |
if (menu == null) { |
|
292 |
throw exception(MENU_PARENT_NOT_EXISTS); |
|
293 |
} |
|
294 |
// 父菜单必须是目录或者菜单类型 |
|
295 |
if (!MenuTypeEnum.DIR.getType().equals(menu.getType()) |
|
296 |
&& !MenuTypeEnum.MENU.getType().equals(menu.getType())) { |
|
297 |
throw exception(MENU_PARENT_NOT_DIR_OR_MENU); |
|
298 |
} |
|
299 |
} |
|
300 |
|
|
301 |
/** |
|
302 |
* 校验菜单是否合法 |
|
303 |
* <p> |
|
304 |
* 1. 校验相同父菜单编号下,是否存在相同的菜单名 |
|
305 |
* |
|
306 |
* @param name 菜单名字 |
|
307 |
* @param parentId 父菜单编号 |
|
308 |
* @param id 菜单编号 |
|
309 |
*/ |
|
310 |
@VisibleForTesting |
|
311 |
void validateMenu(Long parentId, String name, Long id) { |
|
312 |
MenuDO menu = menuMapper.selectByParentIdAndName(parentId, name); |
|
313 |
if (menu == null) { |
|
314 |
return; |
|
315 |
} |
|
316 |
// 如果 id 为空,说明不用比较是否为相同 id 的菜单 |
|
317 |
if (id == null) { |
|
318 |
throw exception(MENU_NAME_DUPLICATE); |
|
319 |
} |
|
320 |
if (!menu.getId().equals(id)) { |
|
321 |
throw exception(MENU_NAME_DUPLICATE); |
|
322 |
} |
|
323 |
} |
|
324 |
|
|
325 |
/** |
|
326 |
* 初始化菜单的通用属性。 |
|
327 |
* <p> |
|
328 |
* 例如说,只有目录或者菜单类型的菜单,才设置 icon |
|
329 |
* |
|
330 |
* @param menu 菜单 |
|
331 |
*/ |
|
332 |
private void initMenuProperty(MenuDO menu) { |
|
333 |
// 菜单为按钮类型时,无需 component、icon、path 属性,进行置空 |
|
334 |
if (MenuTypeEnum.BUTTON.getType().equals(menu.getType())) { |
|
335 |
menu.setComponent(""); |
|
336 |
menu.setComponentName(""); |
|
337 |
menu.setIcon(""); |
|
338 |
menu.setPath(""); |
|
339 |
} |
|
340 |
} |
|
341 |
|
818a01
|
342 |
/** |
H |
343 |
* 新创建菜单赋权给租户管理员 |
|
344 |
*/ |
|
345 |
|
|
346 |
private void dealPermission(MenuDO menu) { |
|
347 |
Long tenantId = menu.getTenantId(); |
7da8f1
|
348 |
RoleDO tenantRole = roleService.getTenantAdminRole(tenantId); |
818a01
|
349 |
TenantDO tenant = tenantService.getTenant(tenantId); |
H |
350 |
TenantPackageDO tenantPackage = tenantPackageService.getTenantPackage(tenant.getPackageId()); |
|
351 |
Set<Long> menuIds = tenantPackage.getMenuIds(); |
|
352 |
menuIds.add(menu.getId()); |
|
353 |
tenantPackage.setMenuIds(menuIds); |
|
354 |
tenantPackageService.updateTenantPackage(BeanUtils.toBean(tenantPackage, TenantPackageSaveReqVO.class)); |
7da8f1
|
355 |
permissionService.assignRoleMenu(tenantRole.getId(), menuIds); |
H |
356 |
// 开发者自己创建的应用菜单默认赋权给创建者所拥有的角色 |
|
357 |
//查询当前用户所拥有的角色 |
|
358 |
Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(getLoginUserId()); |
|
359 |
List<RoleDO> roles = roleService.getRoleList(roleIds); |
|
360 |
roles.removeIf(role -> !CommonStatusEnum.ENABLE.getStatus().equals(role.getStatus())); // 移除禁用的角色 |
|
361 |
roles.removeIf(role -> tenantRole.getId().equals(role.getId())); // 移除租户管理员角色 |
|
362 |
if (!roles.isEmpty()) { |
|
363 |
roles.stream().forEach(roleDO -> { |
|
364 |
RoleMenuDO roleMenuDO = new RoleMenuDO(); |
|
365 |
roleMenuDO.setMenuId(menu.getId()); |
|
366 |
roleMenuDO.setRoleId(roleDO.getId()); |
|
367 |
roleMenuDO.setTenantId(tenant.getId()); |
|
368 |
roleMenuMapper.insert(roleMenuDO); |
|
369 |
}); |
|
370 |
} |
818a01
|
371 |
} |
H |
372 |
|
e7c126
|
373 |
} |