You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
5.5 KiB
125 lines
5.5 KiB
3 years ago
|
package com.fr.plugin.xx.gxkg.handler;
|
||
|
|
||
|
import com.banboocloud.Codec.BamboocloudFacade;
|
||
|
import com.fr.decision.authority.AuthorityContext;
|
||
|
import com.fr.decision.authority.base.constant.type.operation.ManualOperationType;
|
||
|
import com.fr.decision.authority.data.Department;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.decision.fun.impl.BaseHttpHandler;
|
||
|
import com.fr.decision.webservice.bean.user.DepartmentPostBean;
|
||
|
import com.fr.decision.webservice.v10.user.PositionService;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.xx.gxkg.bean.ResponseEntity;
|
||
|
import com.fr.plugin.xx.gxkg.conf.GxkgSsoConfig;
|
||
|
import com.fr.plugin.xx.gxkg.utils.BamboocloudUtils;
|
||
|
import com.fr.plugin.xx.gxkg.utils.LogUtils;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* @Author xx
|
||
|
* @Date 2022/7/7
|
||
|
* @Description
|
||
|
**/
|
||
|
public abstract class AbstractSyncHandler extends BaseHttpHandler {
|
||
|
|
||
|
private String requestId;
|
||
|
|
||
|
@Override
|
||
|
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception {
|
||
|
GxkgSsoConfig config = GxkgSsoConfig.getInstance();
|
||
|
if (StringUtils.isBlank(config.getSyncUser()) || StringUtils.isBlank(config.getSyncPass())) {
|
||
|
setResult(res, JSONObject.mapFrom(ResponseEntity.error("配置为空")));
|
||
|
return;
|
||
|
}
|
||
|
String bodyparam = BamboocloudUtils.getRequestBody(req);
|
||
|
if (StringUtils.isBlank(bodyparam)) {
|
||
|
setResult(res, JSONObject.mapFrom(ResponseEntity.error("报文为空")));
|
||
|
return;
|
||
|
}
|
||
|
if(StringUtils.isNotBlank(config.getDecodePwd())){
|
||
|
bodyparam = BamboocloudUtils.getPlaintext(bodyparam, config.getDecodePwd(), "AES");
|
||
|
}
|
||
|
JSONObject body = new JSONObject(bodyparam);
|
||
|
/*if (!BamboocloudUtils.verify(body.getMap(), "MD5").booleanValue()) {
|
||
|
setResult(res, JSONObject.mapFrom(ResponseEntity.error("报文签名认证失败")));
|
||
|
return;
|
||
|
}*/
|
||
|
requestId = body.getString("bimRequestId");
|
||
|
String username = body.getString("bimRemoteUser");
|
||
|
String password = body.getString("bimRemotePwd");
|
||
|
if (!BamboocloudUtils.checkUsernamePassword(username, password)) {
|
||
|
setResult(res, JSONObject.mapFrom(ResponseEntity.error("同步用户认证失败")));
|
||
|
return;
|
||
|
}
|
||
|
try {
|
||
|
LogUtils.debug4plugin("current url is {}, body is {}", req.getRequestURL(), body);
|
||
|
setResult(res, syncHandle(body));
|
||
|
} catch (Exception e) {
|
||
|
setResult(res, JSONObject.mapFrom(ResponseEntity.error("同步失败:" + e.getMessage())));
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected void setResult(HttpServletResponse res, JSONObject result) {
|
||
|
try {
|
||
|
result.put("bimRequestId",requestId);
|
||
|
String body = result.toString();
|
||
|
if (StringUtils.isNotBlank(GxkgSsoConfig.getInstance().getDecodePwd())) {
|
||
|
body = BamboocloudFacade.encrypt(result.toString(), GxkgSsoConfig.getInstance().getDecodePwd(), "AES");
|
||
|
}
|
||
|
WebUtils.printAsString(res, body);
|
||
|
} catch (Exception e) {
|
||
|
LogUtils.error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected abstract JSONObject syncHandle(JSONObject body) throws Exception;
|
||
|
|
||
|
|
||
|
protected User cover2User(JSONObject body) {
|
||
|
return (new User()).id(body.getString("bimUid")).password(body.getString("password")).userName(body.getString("oaAccount"))
|
||
|
.realName(body.getString("fullName")).creationType(ManualOperationType.KEY)
|
||
|
.lastOperationType(ManualOperationType.KEY).enable(body.getBoolean("__ENABLE__"));
|
||
|
}
|
||
|
|
||
|
protected String generateDefaultPosition(String depID) throws Exception {
|
||
|
String positionName = GxkgSsoConfig.getInstance().getDefaultPost();
|
||
|
String adminId = UserService.getInstance().getAdminUserIdList().get(0);
|
||
|
List<DepartmentPostBean> postBeanList = PositionService.getInstance().getPositionsUnderParentDepartment(adminId, depID, null);
|
||
|
boolean hasDefaultPosition = false;
|
||
|
String positionId = "";
|
||
|
for (DepartmentPostBean postBean : postBeanList) {
|
||
|
if (StringUtils.equals(positionName, postBean.getText())) {
|
||
|
return postBean.getId();
|
||
|
}
|
||
|
}
|
||
|
List<DepartmentPostBean> postBeans = PositionService.getInstance().getPositions(adminId, null);
|
||
|
for (DepartmentPostBean postBean : postBeans) {
|
||
|
if (StringUtils.equals(positionName, postBean.getText())) {
|
||
|
hasDefaultPosition = true;
|
||
|
positionId = postBean.getId();
|
||
|
}
|
||
|
}
|
||
|
if (!hasDefaultPosition) {
|
||
|
positionId = PositionService.getInstance().addPosition(positionName).getId();
|
||
|
}
|
||
|
AuthorityContext.getInstance().getPostController().addPostToDepartment(positionId, depID);
|
||
|
return positionId;
|
||
|
}
|
||
|
|
||
|
protected Department cover2Dept(JSONObject body) {
|
||
|
String parent = body.getString("parOrgId");
|
||
|
if (StringUtils.isBlank(parent)) {
|
||
|
parent = null;
|
||
|
}
|
||
|
String id = StringUtils.isBlank(body.getString("bimOrgId"))?body.getString("orgCode"):body.getString("bimOrgId");
|
||
|
return (new Department()).id(id).name(body.getString("orgName")).parentId(parent).creationType(ManualOperationType.KEY).lastOperationType(ManualOperationType.KEY).enable(body.getBoolean("__ENABLE__"));
|
||
|
}
|
||
|
}
|