refactor(权限): 多个“获取授权对象拥有资源”SQL查询语句合并

This commit is contained in:
zhangnf 2023-08-24 23:30:12 +08:00
parent e41946124b
commit e257556b5c
4 changed files with 59 additions and 131 deletions

View File

@ -2,6 +2,7 @@ package io.dataease.auth.service.impl;
import io.dataease.auth.entity.AuthItem;
import io.dataease.auth.service.ExtAuthService;
import io.dataease.commons.constants.SysAuthConstants;
import io.dataease.plugins.common.base.domain.SysAuth;
import io.dataease.ext.ExtAuthMapper;
import io.dataease.commons.constants.AuthConstants;
@ -66,59 +67,95 @@ public class ExtAuthServiceImpl implements ExtAuthService {
@Cacheable(value = AuthConstants.USER_LINK_NAME, key = "'user' + #userId")
@Override
public List<AuthItem> dataSourceIdByUser(Long userId) {
return extAuthMapper.dataSourceIdByUser(userId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_USER,
userId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_DATASOURCE
);
}
@Cacheable(value = AuthConstants.USER_DATASET_NAME, key = "'user' + #userId")
@Override
public List<AuthItem> dataSetIdByUser(Long userId) {
return extAuthMapper.dataSetIdByUser(userId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_USER,
userId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_DATASET
);
}
@Cacheable(value = AuthConstants.USER_PANEL_NAME, key = "'user' + #userId")
@Override
public List<AuthItem> panelIdByUser(Long userId) {
return extAuthMapper.panelIdByUser(userId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_USER,
userId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_PANEL
);
}
@Cacheable(value = AuthConstants.ROLE_LINK_NAME, key = "'role' + #roleId")
@Override
public List<AuthItem> dataSourceIdByRole(Long roleId) {
return extAuthMapper.dataSourceIdByRole(roleId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_ROLE,
roleId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_DATASOURCE
);
}
@Cacheable(value = AuthConstants.ROLE_DATASET_NAME, key = "'role' + #roleId")
@Override
public List<AuthItem> dataSetIdByRole(Long roleId) {
return extAuthMapper.dataSetIdByRole(roleId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_ROLE,
roleId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_DATASET
);
}
@Cacheable(value = AuthConstants.ROLE_PANEL_NAME, key = "'role' + #roleId")
@Override
public List<AuthItem> panelIdByRole(Long roleId) {
return extAuthMapper.panelIdByRole(roleId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_ROLE,
roleId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_PANEL
);
}
@Cacheable(value = AuthConstants.DEPT_LINK_NAME, key = "'dept' + #deptId")
@Override
public List<AuthItem> dataSourceIdByDept(Long deptId) {
if (ObjectUtils.isEmpty(deptId)) return emptyResult;
return extAuthMapper.dataSourceIdByDept(deptId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_DEPT,
deptId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_DATASOURCE
);
}
@Cacheable(value = AuthConstants.DEPT_DATASET_NAME, key = "'dept' + #deptId")
@Override
public List<AuthItem> dataSetIdByDept(Long deptId) {
if (ObjectUtils.isEmpty(deptId)) return emptyResult;
return extAuthMapper.dataSetIdByDept(deptId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_DEPT,
deptId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_DATASET
);
}
@Cacheable(value = AuthConstants.DEPT_PANEL_NAME, key = "'dept' + #deptId")
@Override
public List<AuthItem> panelIdByDept(Long deptId) {
if (ObjectUtils.isEmpty(deptId)) return emptyResult;
return extAuthMapper.panelIdByDept(deptId.toString());
return extAuthMapper.queryAuthItems(
SysAuthConstants.AUTH_TARGET_TYPE_DEPT,
deptId.toString(),
SysAuthConstants.AUTH_SOURCE_TYPE_PANEL
);
}
@Caching(evict = {

View File

@ -7,6 +7,12 @@ package io.dataease.commons.constants;
*/
public class SysAuthConstants {
public final static String AUTH_TARGET_TYPE_USER = "user";
public final static String AUTH_TARGET_TYPE_ROLE = "role";
public final static String AUTH_TARGET_TYPE_DEPT = "dept";
public final static String AUTH_SOURCE_TYPE_PANEL = "panel";
public final static String AUTH_SOURCE_TYPE_DATASET = "dataset";

View File

@ -16,17 +16,9 @@ public interface ExtAuthMapper {
List<SysAuth> queryByResource(@Param("resourceId") String resourceId);
List<AuthItem> dataSourceIdByUser(String userId);
List<AuthItem> dataSetIdByUser(String userId);
List<AuthItem> panelIdByUser(String userId);
List<AuthItem> dataSourceIdByRole(String roleId);
List<AuthItem> dataSetIdByRole(String roleId);
List<AuthItem> panelIdByRole(String roleId);
List<AuthItem> dataSourceIdByDept(String deptId);
List<AuthItem> dataSetIdByDept(String deptId);
List<AuthItem> panelIdByDept(String deptId);
List<AuthItem> queryAuthItems(@Param("authTargetType") String authTargetType,
@Param("authTarget") String authTarget,
@Param("sourceType") String sourceType);
String parentResource(@Param("resourceId") String resourceId, @Param("type") String type);
}

View File

@ -33,121 +33,16 @@
where a.auth_source = #{resourceId} and b.privilege_value = 1
</select>
<select id="dataSourceIdByUser" resultMap="AuthItemMap">
<select id="queryAuthItems" resultMap="AuthItemMap">
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'link'
AND auth_target_type = 'user'
AND auth_target = #{userId}
AND d.privilege_value = 1
GROUP BY a.id
</select>
<select id="dataSetIdByUser" resultMap="AuthItemMap" >
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'dataset'
AND auth_target_type = 'user'
AND auth_target = #{userId}
AND d.privilege_value = 1
GROUP BY a.id
</select>
<select id="panelIdByUser" resultMap="AuthItemMap">
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'panel'
AND auth_target_type = 'user'
AND auth_target = #{userId}
AND d.privilege_value = 1
GROUP BY a.id
</select>
<select id="dataSourceIdByRole" resultMap="AuthItemMap" >
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'link'
AND auth_target_type = 'role'
AND auth_target = #{roleId}
AND d.privilege_value = 1
GROUP BY a.id
</select>
<select id="dataSetIdByRole" resultMap="AuthItemMap" >
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'dataset'
AND auth_target_type = 'role'
AND auth_target = #{roleId}
AND d.privilege_value = 1
GROUP BY a.id
</select>
<select id="panelIdByRole" resultMap="AuthItemMap" >
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'panel'
AND auth_target_type = 'role'
AND auth_target = #{roleId}
AND d.privilege_value = 1
GROUP BY a.id
</select>
<select id="dataSourceIdByDept" resultMap="AuthItemMap" >
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'link'
AND auth_target_type = 'dept'
AND auth_target = #{deptId}
AND d.privilege_value = 1
GROUP BY a.id
</select>
<select id="dataSetIdByDept" resultMap="AuthItemMap" >
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'dataset'
AND auth_target_type = 'dept'
AND auth_target = #{deptId}
AND d.privilege_value = 1
GROUP BY a.id
</select>
<select id="panelIdByDept" resultMap="AuthItemMap" >
SELECT
auth_source, MAX(d.privilege_type) as level
FROM
sys_auth a
LEFT JOIN sys_auth_detail d on d.auth_id = a.id
WHERE
auth_source_type = 'panel'
AND auth_target_type = 'dept'
AND auth_target = #{deptId}
auth_source_type = #{sourceType}
AND auth_target_type = #{authTargetType}
AND auth_target = #{authTarget}
AND d.privilege_value = 1
GROUP BY a.id
</select>
@ -156,6 +51,4 @@
select GET_V_AUTH_MODEL_WITH_PARENT(#{resourceId}, #{type})
</select>
</mapper>