Commit 458ce8cda426b617a8066a4d2d0edcbd3d0a54f8
Merge remote-tracking branch 'origin/master-jdk17' into master-jdk17
Showing
4 changed files
with
65 additions
and
5 deletions
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java
| @@ -303,7 +303,7 @@ public class PermissionServiceImpl implements PermissionService { | @@ -303,7 +303,7 @@ public class PermissionServiceImpl implements PermissionService { | ||
| 303 | CollUtil.addAll(result.getDeptIds(), role.getDataScopeDeptIds()); | 303 | CollUtil.addAll(result.getDeptIds(), role.getDataScopeDeptIds()); |
| 304 | // 自定义可见部门时,保证可以看到自己所在的部门。否则,一些场景下可能会有问题。 | 304 | // 自定义可见部门时,保证可以看到自己所在的部门。否则,一些场景下可能会有问题。 |
| 305 | // 例如说,登录时,基于 t_user 的 username 查询会可能被 dept_id 过滤掉 | 305 | // 例如说,登录时,基于 t_user 的 username 查询会可能被 dept_id 过滤掉 |
| 306 | - CollUtil.addAll(result.getDeptIds(), userDeptId.get()); | 306 | + CollectionUtils.addIfNotNull(result.getDeptIds(), userDeptId.get()); |
| 307 | continue; | 307 | continue; |
| 308 | } | 308 | } |
| 309 | // 情况三,DEPT_ONLY | 309 | // 情况三,DEPT_ONLY |
| @@ -313,9 +313,14 @@ public class PermissionServiceImpl implements PermissionService { | @@ -313,9 +313,14 @@ public class PermissionServiceImpl implements PermissionService { | ||
| 313 | } | 313 | } |
| 314 | // 情况四,DEPT_DEPT_AND_CHILD | 314 | // 情况四,DEPT_DEPT_AND_CHILD |
| 315 | if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_AND_CHILD.getScope())) { | 315 | if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_AND_CHILD.getScope())) { |
| 316 | - CollUtil.addAll(result.getDeptIds(), deptService.getChildDeptIdListFromCache(userDeptId.get())); | 316 | + Long deptId = userDeptId.get(); |
| 317 | + // 用户未设置部门,直接跳过;否则 getChildDeptIdListFromCache 走缓存注解会因 null key 报错 | ||
| 318 | + if (deptId == null) { | ||
| 319 | + continue; | ||
| 320 | + } | ||
| 321 | + CollUtil.addAll(result.getDeptIds(), deptService.getChildDeptIdListFromCache(deptId)); | ||
| 317 | // 添加本身部门编号 | 322 | // 添加本身部门编号 |
| 318 | - CollUtil.addAll(result.getDeptIds(), userDeptId.get()); | 323 | + result.getDeptIds().add(deptId); |
| 319 | continue; | 324 | continue; |
| 320 | } | 325 | } |
| 321 | // 情况五,SELF | 326 | // 情况五,SELF |
yudao-module-system/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java
| @@ -441,6 +441,34 @@ public class PermissionServiceTest extends BaseDbUnitTest { | @@ -441,6 +441,34 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||
| 441 | } | 441 | } |
| 442 | 442 | ||
| 443 | @Test | 443 | @Test |
| 444 | + public void testGetDeptDataPermission_DeptCustom_userDeptIdNull() { | ||
| 445 | + try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||
| 446 | + springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||
| 447 | + .thenReturn(permissionService); | ||
| 448 | + | ||
| 449 | + // 准备参数 | ||
| 450 | + Long userId = 1L; | ||
| 451 | + // mock 用户的角色编号 | ||
| 452 | + userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L)); | ||
| 453 | + // mock 获得用户的角色 | ||
| 454 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope()) | ||
| 455 | + .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||
| 456 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO)); | ||
| 457 | + // mock 部门的返回:用户未设置部门 | ||
| 458 | + when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO()); // deptId 为 null | ||
| 459 | + | ||
| 460 | + // 调用 | ||
| 461 | + DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||
| 462 | + // 断言:角色配置的可见部门仍正常加入,但 null 不进集合 | ||
| 463 | + assertFalse(result.getAll()); | ||
| 464 | + assertFalse(result.getSelf()); | ||
| 465 | + assertEquals(roleDO.getDataScopeDeptIds().size(), result.getDeptIds().size()); | ||
| 466 | + assertTrue(CollUtil.containsAll(result.getDeptIds(), roleDO.getDataScopeDeptIds())); | ||
| 467 | + assertFalse(result.getDeptIds().contains(null)); | ||
| 468 | + } | ||
| 469 | + } | ||
| 470 | + | ||
| 471 | + @Test | ||
| 444 | public void testGetDeptDataPermission_DeptOnly() { | 472 | public void testGetDeptDataPermission_DeptOnly() { |
| 445 | try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | 473 | try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { |
| 446 | springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | 474 | springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) |
| @@ -501,6 +529,33 @@ public class PermissionServiceTest extends BaseDbUnitTest { | @@ -501,6 +529,33 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||
| 501 | } | 529 | } |
| 502 | 530 | ||
| 503 | @Test | 531 | @Test |
| 532 | + public void testGetDeptDataPermission_DeptAndChild_userDeptIdNull() { | ||
| 533 | + try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||
| 534 | + springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||
| 535 | + .thenReturn(permissionService); | ||
| 536 | + | ||
| 537 | + // 准备参数 | ||
| 538 | + Long userId = 1L; | ||
| 539 | + // mock 用户的角色编号 | ||
| 540 | + userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L)); | ||
| 541 | + // mock 获得用户的角色 | ||
| 542 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope()) | ||
| 543 | + .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||
| 544 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO)); | ||
| 545 | + // mock 部门的返回:用户未设置部门 | ||
| 546 | + when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO()); // deptId 为 null | ||
| 547 | + | ||
| 548 | + // 调用 | ||
| 549 | + DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||
| 550 | + // 断言:deptId 为 null,整段跳过;deptIds 为空,子部门查询不被触发 | ||
| 551 | + assertFalse(result.getAll()); | ||
| 552 | + assertFalse(result.getSelf()); | ||
| 553 | + assertTrue(CollUtil.isEmpty(result.getDeptIds())); | ||
| 554 | + verify(deptService, never()).getChildDeptIdListFromCache(any()); | ||
| 555 | + } | ||
| 556 | + } | ||
| 557 | + | ||
| 558 | + @Test | ||
| 504 | public void testGetDeptDataPermission_Self() { | 559 | public void testGetDeptDataPermission_Self() { |
| 505 | try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | 560 | try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { |
| 506 | springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | 561 | springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) |
yudao-server/src/main/resources/application-dev.yaml
| @@ -117,7 +117,7 @@ spring: | @@ -117,7 +117,7 @@ spring: | ||
| 117 | # Lock4j 配置项 | 117 | # Lock4j 配置项 |
| 118 | lock4j: | 118 | lock4j: |
| 119 | acquire-timeout: 3000 # 获取分布式锁超时时间,默认为 3000 毫秒 | 119 | acquire-timeout: 3000 # 获取分布式锁超时时间,默认为 3000 毫秒 |
| 120 | - expire: 30000 # 分布式锁的超时时间,默认为 30 毫秒 | 120 | + expire: 30000 # 分布式锁的超时时间,默认为 30000 毫秒 |
| 121 | 121 | ||
| 122 | --- #################### 监控相关配置 #################### | 122 | --- #################### 监控相关配置 #################### |
| 123 | 123 |
yudao-server/src/main/resources/application-local.yaml
| @@ -138,7 +138,7 @@ spring: | @@ -138,7 +138,7 @@ spring: | ||
| 138 | # Lock4j 配置项 | 138 | # Lock4j 配置项 |
| 139 | lock4j: | 139 | lock4j: |
| 140 | acquire-timeout: 3000 # 获取分布式锁超时时间,默认为 3000 毫秒 | 140 | acquire-timeout: 3000 # 获取分布式锁超时时间,默认为 3000 毫秒 |
| 141 | - expire: 30000 # 分布式锁的超时时间,默认为 30 毫秒 | 141 | + expire: 30000 # 分布式锁的超时时间,默认为 30000 毫秒 |
| 142 | 142 | ||
| 143 | --- #################### 监控相关配置 #################### | 143 | --- #################### 监控相关配置 #################### |
| 144 | 144 |