|
@@ -1,7 +1,27 @@
|
|
|
package com.persagy.dmp.auth.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.persagy.dmp.auth.client.EmsSaasWebClient;
|
|
|
+import com.persagy.dmp.auth.domain.Group;
|
|
|
+import com.persagy.dmp.auth.domain.Project;
|
|
|
+import com.persagy.dmp.auth.domain.ResultReturn;
|
|
|
+import com.persagy.dmp.common.constant.CommonConstant;
|
|
|
+import com.persagy.dmp.common.constant.ResponseCode;
|
|
|
+import com.persagy.dmp.common.context.AppContext;
|
|
|
+import com.persagy.dmp.common.exception.BusinessException;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 运维平台2.0 鉴权实现类
|
|
@@ -10,11 +30,68 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
* @date 2021-11-02
|
|
|
*/
|
|
|
public class EmsAuthServiceImpl extends AbstractAuthServiceImpl {
|
|
|
+ @Autowired
|
|
|
+ private EmsSaasWebClient emsSaasWebClient;
|
|
|
|
|
|
@Override
|
|
|
public void loginSuccess(HttpServletRequest request, HttpServletResponse response) {
|
|
|
super.loginSuccess(request, response);
|
|
|
loadContextByRequest(request);
|
|
|
- // TODO 验证集团是否有效、验证项目是否匹配集团、没有集团编码时根据项目匹配集团
|
|
|
+ if (StrUtil.isBlank(AppContext.getContext().getGroupCode())
|
|
|
+ && StrUtil.isBlank(AppContext.getContext().getProjectId())){
|
|
|
+ // 两者都为空时不做校验,例如def/class的接口,dic的接口
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONObject param = new JSONObject();
|
|
|
+ if (StrUtil.isNotBlank(AppContext.getContext().getGroupCode())){
|
|
|
+ param.put(CommonConstant.QUERY_GROUPCODE,AppContext.getContext().getGroupCode());
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(AppContext.getContext().getProjectId())){
|
|
|
+ param.put(CommonConstant.QUERY_PROJECTIDS, CollUtil.newArrayList(AppContext.getContext().getProjectId()));
|
|
|
+ }
|
|
|
+ ResultReturn<Group> queryResult = emsSaasWebClient.queryGroupProjectList(param);
|
|
|
+ if (!CommonConstant.QUERY_SUCCESS.equals(queryResult.getResult())){
|
|
|
+ throw new BusinessException(queryResult.getResultMsg());
|
|
|
+ }
|
|
|
+ List<Group> content = queryResult.getContent();
|
|
|
+ if (CollUtil.isEmpty(content)){
|
|
|
+ throw new BusinessException(ResponseCode.A0402.getCode(),ResponseCode.A0402.getDesc());
|
|
|
+ }
|
|
|
+ Map<String, List<Project>> groupMap = content.stream()
|
|
|
+ .collect(Collectors.toMap(Group::getGroupCode, Group::getProjects, (k1, k2) -> k1));
|
|
|
+ // 1.没有集团编码时根据项目匹配集团
|
|
|
+ if (StrUtil.isBlank(AppContext.getContext().getGroupCode())){
|
|
|
+ // 如果集团编码为空则通过项目id查找
|
|
|
+ Set<Map.Entry<String, List<Project>>> entries = groupMap.entrySet();
|
|
|
+ flag:
|
|
|
+ for (Map.Entry<String, List<Project>> entry : entries) {
|
|
|
+ if (CollUtil.isEmpty(entry.getValue())){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (Project project : entry.getValue()) {
|
|
|
+ if (AppContext.getContext().getProjectId().equals(project.getProjectId())){
|
|
|
+ AppContext.getContext().setGroupCode(entry.getKey());
|
|
|
+ break flag;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 2.验证集团是否有效
|
|
|
+ if (StrUtil.isBlank(AppContext.getContext().getGroupCode())
|
|
|
+ || !groupMap.containsKey(AppContext.getContext().getGroupCode())){
|
|
|
+ throw new BusinessException(ResponseCode.A0402.getCode(),ResponseCode.A0402.getDesc());
|
|
|
+ }
|
|
|
+ // 3.验证项目是否匹配集团
|
|
|
+ if (StrUtil.isBlank(AppContext.getContext().getProjectId())){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Project> projects = groupMap
|
|
|
+ .getOrDefault(AppContext.getContext().getGroupCode(), new ArrayList<>());
|
|
|
+ if (!projects.stream()
|
|
|
+ .map(Project::getProjectId)
|
|
|
+ .collect(Collectors.toSet())
|
|
|
+ .contains(AppContext.getContext().getProjectId())){
|
|
|
+ throw new BusinessException(ResponseCode.A0402.getCode(),ResponseCode.A0402.getDesc());
|
|
|
+ }
|
|
|
}
|
|
|
}
|