检测获取当前登录用户权限最大的角色

This commit is contained in:
xiaofajia 2024-12-15 16:53:41 +08:00
parent 7cc778776e
commit 2b3f5b49e0
3 changed files with 151 additions and 1 deletions

View File

@ -0,0 +1,77 @@
package cn.iocoder.yudao.common;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 检测相关和角色和其对应的权重
*
* 权重分配如下
* 检测业务管理员
* 检测站老板
* 检测官方
* 检测商户
* 经销商
* 合作商
* 引车员
* 环检员
* 外检员
* 外检底检
* 一审
* 环检预审
* 检测用户
* @author 小李
* @date 15:39 2024/12/15
**/
@AllArgsConstructor
@Getter
public enum InspectionRoleCommon {
/** 检测业务管理员 */
JIANCE("jiance", 1),
/** 检测站老板 */
JCBOSS("jcboss", 1),
/** 检测官方 */
JCGF("jcgf", 2),
/** 检测商户 */
JCSHOP("jcshop", 2),
/** 经销商 */
DEALERS("dealers", 2),
/** 合作商 */
PARTNERS("partners", 2),
/** 引车员 */
JCYCY("jcycy", 3),
/** 环检员 */
JCHJY("jchjy", 4),
/** 外检员 */
JCWJY("jcwjy", 4),
/** 外检底检 */
JCWJDJ("jcwjdj", 4),
/** 一审 */
JCYSZZ("jcyszz", 4),
/** 环检预审 */
JCHJYS("jchjys", 4),
/** 检测用户 */
JCYH("jcyh", 4),
/** 检测员工 */
JCWORKER("jcworker", 4);
/** 角色Code */
private final String code;
/** 角色权重 */
private final Integer weight;
}

View File

@ -0,0 +1,74 @@
package cn.iocoder.yudao.module.inspection.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.common.InspectionRoleCommon;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.api.permission.RoleApi;
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
/**
* 检测用的工具Controller
*
* @author 小李
* @date 15:36 2024/12/15
**/
@RestController
@RequestMapping("/admin-api/inspection/util")
public class InspectionUtilController {
@Resource
private PermissionApi permissionApi;
@Resource
private RoleApi roleApi;
/**
* 根据用户取出当前用户的角色只针对检测
*
* @author 小李
* @date 15:15 2024/12/15
**/
@GetMapping("/getRoleName")
public CommonResult<?> getRoleName(){
String name = "未知岗位";
List<Long> roleIdsByUserId = permissionApi.getRoleIdsByUserId(SecurityFrameworkUtils.getLoginUserId());
if (CollUtil.isEmpty(roleIdsByUserId)){
return success(name);
}
List<RoleReqDTO> roleList = roleApi.getRoleList();
List<RoleReqDTO> roles = roleList.stream().filter(item -> roleIdsByUserId.contains(item.getId())).collect(Collectors.toList());
List<InspectionRoleCommon> roleCommons = Arrays.asList(InspectionRoleCommon.values());
List<String> codes = roleCommons.stream().map(InspectionRoleCommon::getCode).collect(Collectors.toList());
List<RoleReqDTO> inspectionRoles = roles.stream().filter(item -> codes.contains(item.getCode())).collect(Collectors.toList());
if (CollUtil.isEmpty(inspectionRoles)){
return success(name);
}
Integer index = Integer.MAX_VALUE;
for (RoleReqDTO inspectionRole : inspectionRoles) {
InspectionRoleCommon inspectionRoleCommon = InspectionRoleCommon.valueOf(inspectionRole.getCode().toUpperCase());
if (ObjectUtil.isEmpty(inspectionRoleCommon)){
continue;
}
Integer weight = inspectionRoleCommon.getWeight();
if (weight < index){
index = weight;
name = inspectionRole.getName();
}
}
return success(name);
}
}

View File

@ -193,7 +193,6 @@ public class SysLoginController {
return success(loginService.login(authLoginReqVO));
}
/**
* 汽修小程序登录方法
*