RoleScopeGuardServiceImpl.java
1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.diligrp.rider.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diligrp.rider.common.exception.BizException;
import com.diligrp.rider.entity.SysRole;
import com.diligrp.rider.mapper.SysRoleMapper;
import com.diligrp.rider.service.RoleScopeGuardService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class RoleScopeGuardServiceImpl implements RoleScopeGuardService {
private final SysRoleMapper sysRoleMapper;
@Override
public SysRole requireRole(Long roleId, String requiredScope) {
return requireRole(roleId, requiredScope, null);
}
@Override
public SysRole requireRole(Long roleId, String requiredScope, Long cityId) {
if (roleId == null || roleId < 1) {
throw new BizException("角色不能为空");
}
SysRole role = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>()
.eq(SysRole::getId, roleId)
.eq(SysRole::getStatus, 1)
.last("LIMIT 1"));
if (role == null) {
throw new BizException("角色不存在或已禁用");
}
if (!requiredScope.equals(role.getRoleScope())) {
throw new BizException("角色归属不匹配");
}
// cityId != null 时,要求角色属于该租户或是全局角色(city_id=0)
if (cityId != null) {
boolean isGlobal = role.getCityId() == null || role.getCityId() == 0L;
boolean isOwned = cityId.equals(role.getCityId());
if (!isGlobal && !isOwned) {
throw new BizException("角色不属于当前租户");
}
}
return role;
}
}