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.
181 lines
7.4 KiB
181 lines
7.4 KiB
/* |
|
* Copyright (C), 2015-2019 |
|
* FileName: ReportDataHandler |
|
* Author: Louis |
|
* Date: 2019/8/22 8:34 |
|
* Description: ReportDataHandler |
|
* History: |
|
* <author> <time> <version> <desc> |
|
*/ |
|
package com.fr.plugin.icgq.service; |
|
|
|
import com.fanruan.api.i18n.I18nKit; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fr.base.Base64; |
|
import com.fr.data.NetworkHelper; |
|
import com.fr.decision.authority.AuthorityContext; |
|
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.UserBean; |
|
import com.fr.decision.webservice.bean.user.UserUpdateBean; |
|
import com.fr.decision.webservice.v10.user.DepartmentService; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
import com.fr.general.ComparatorUtils; |
|
import com.fr.intelli.record.Focus; |
|
import com.fr.intelli.record.Original; |
|
import com.fr.json.JSONObject; |
|
import com.fr.plugin.context.PluginContexts; |
|
import com.fr.plugin.icgq.config.IcgqConfig; |
|
import com.fr.plugin.icgq.kit.UserServiceKit; |
|
import com.fr.record.analyzer.EnableMetrics; |
|
import com.fr.stable.query.QueryFactory; |
|
import com.fr.stable.query.condition.QueryCondition; |
|
import com.fr.stable.query.restriction.RestrictionFactory; |
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
|
import com.fr.web.utils.WebUtils; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.io.BufferedReader; |
|
import java.io.IOException; |
|
import java.io.InputStreamReader; |
|
import java.nio.charset.StandardCharsets; |
|
|
|
import static com.fr.decision.authority.base.AuthorityConstants.DECISION_DEP_ROOT; |
|
import static com.fr.plugin.icgq.LocaleFinder.PLUGIN_ID; |
|
import static com.fr.plugin.icgq.data.UsersTableDataModel.REQUEST_LOG_ACTION_FLAG; |
|
import static com.fr.plugin.icgq.kit.DepartmentServiceKit.IDT_ORG__NAME; |
|
import static com.fr.plugin.icgq.kit.DepartmentServiceKit.IDT_ORG__ORG_CODE; |
|
import static com.fr.plugin.icgq.kit.UserServiceKit.USER_NAME; |
|
|
|
/** |
|
* 〈Function Description〉<br> |
|
* 〈ReportDataHandler〉 |
|
* |
|
* @author fr.open |
|
* @since 1.0.0 |
|
*/ |
|
@EnableMetrics |
|
public class ReportDataHandler extends BaseHttpHandler { |
|
public static final String IAM_USERS = "/iam/data"; |
|
public static final String BASIC_AUTH = "Basic "; |
|
|
|
@Override |
|
public RequestMethod getMethod() { |
|
return RequestMethod.POST; |
|
} |
|
|
|
@Override |
|
public String getPath() { |
|
return IAM_USERS; |
|
} |
|
|
|
@Override |
|
public boolean isPublic() { |
|
return true; |
|
} |
|
|
|
@Override |
|
@Focus(id = PLUGIN_ID, text = "Plugin-icgq", source = Original.PLUGIN) |
|
public void handle(HttpServletRequest request, HttpServletResponse response) throws Exception { |
|
if (!PluginContexts.currentContext().isAvailable()) { |
|
LogKit.error(I18nKit.getLocText("Plugin-icgq_Licence_Expired")); |
|
printErrorJSON(response, I18nKit.getLocText("Plugin-icgq_Licence_Expired")); |
|
return; |
|
} |
|
String authorization = request.getHeader("Authorization"); |
|
LogKit.info("icgq-ReportDataHandler-authorization:{}", authorization); |
|
if (StringKit.isEmpty(authorization) || !authorization.startsWith(BASIC_AUTH) || !StringKit.equals(authorization, getAuth())) { |
|
printErrorJSON(response, "认证错误"); |
|
return; |
|
} |
|
try { |
|
JSONObject userInfo = this.getParams(request); |
|
if (userInfo.getJSONArray("orgs") != null && !userInfo.getJSONArray("orgs").isEmpty()) { |
|
departmentSynOperation(userInfo.getJSONArray("orgs").getJSONObject(0)); |
|
} |
|
userSynOperation(userInfo); |
|
printResultJSON(response, userInfo.getString("request_log__id")); |
|
} catch (Exception e) { |
|
LogKit.error(e.getMessage(), e); |
|
printErrorJSON(response, e.getMessage()); |
|
} |
|
} |
|
|
|
private String getAuth() { |
|
String auth = IcgqConfig.getInstance().getClientId() + ":" + IcgqConfig.getInstance().getClientSecret(); |
|
return "Basic " + Base64.encode(auth.getBytes()); |
|
} |
|
|
|
/** |
|
* 部门组织的新增更新操作 |
|
* |
|
* @param departmentJo |
|
* @throws Exception |
|
*/ |
|
private void departmentSynOperation(JSONObject departmentJo) throws Exception { |
|
LogKit.info("icgq-ReportDataHandler-departmentSynOperation-departmentJo:{}", departmentJo.encode()); |
|
String departmentId = departmentJo.getString(IDT_ORG__ORG_CODE); |
|
// String parentId = departmentJo.getString(IDT_ORG__SUP_ORG_CODE); |
|
// parentId = DepartmentServiceKit.getInstance().changeRootId(parentId); |
|
String parentId = DECISION_DEP_ROOT; |
|
String depName = departmentJo.getString(IDT_ORG__NAME); |
|
QueryCondition condition = QueryFactory.create().addRestriction(RestrictionFactory.and(RestrictionFactory.eq("name", depName), RestrictionFactory.eq("parentId", null))); |
|
Department sameNameDep = AuthorityContext.getInstance().getDepartmentController().findOne(condition); |
|
if (sameNameDep == null) { |
|
DepartmentService.getInstance().addDepartment(parentId, depName); |
|
} |
|
} |
|
|
|
/** |
|
* 用户新增和更新操作 |
|
* |
|
* @param userJo |
|
*/ |
|
private void userSynOperation(JSONObject userJo) throws Exception { |
|
LogKit.info("icgq-ReportDataHandler-userSynOperation-userJo:{}", userJo.encode()); |
|
UserBean userBean; |
|
if (ComparatorUtils.equals(userJo.getInt(REQUEST_LOG_ACTION_FLAG), 0)) { |
|
userBean = UserServiceKit.getInstance().createUserBean(userJo); |
|
UserService.getInstance().addUser(userBean); |
|
} else if (ComparatorUtils.equals(userJo.getInt(REQUEST_LOG_ACTION_FLAG), 1)) { |
|
userBean = UserServiceKit.getInstance().updateUserBean(userJo); |
|
if (userBean == null) { |
|
return; |
|
} |
|
UserServiceKit.getInstance().editUser(userBean, UserServiceKit.getInstance().getAdminUserId()); |
|
} else if (ComparatorUtils.equals(userJo.getInt(REQUEST_LOG_ACTION_FLAG), 2)) { |
|
User user = UserService.getInstance().getUserByUserName(userJo.getString(USER_NAME)); |
|
String[] removeUserIds = new String[]{user.getId()}; |
|
UserUpdateBean userUpdateBean = new UserUpdateBean(); |
|
userUpdateBean.setRemoveUserIds(removeUserIds); |
|
UserService.getInstance().deleteUsers(userUpdateBean); |
|
} |
|
} |
|
|
|
private JSONObject getParams(HttpServletRequest req) throws IOException { |
|
BufferedReader br = new BufferedReader(new InputStreamReader(NetworkHelper.getRequestInputStream(req), StandardCharsets.UTF_8)); |
|
StringBuilder sb = new StringBuilder(); |
|
String temp; |
|
while ((temp = br.readLine()) != null) { |
|
sb.append(temp); |
|
} |
|
br.close(); |
|
return new JSONObject(sb.toString()); |
|
} |
|
|
|
private void printResultJSON(HttpServletResponse res, String data) throws Exception { |
|
JSONObject result = JSONObject.create(); |
|
result.put("code", "0").put("timestamp", String.valueOf(System.currentTimeMillis())) |
|
.put("data", data).put("msg", "操作成功"); |
|
WebUtils.printAsJSON(res, result); |
|
} |
|
|
|
private void printErrorJSON(HttpServletResponse res, String msg) throws Exception { |
|
JSONObject errorJSON = JSONObject.create(); |
|
errorJSON.put("code", "1").put("msg", msg); |
|
WebUtils.printAsJSON(res, errorJSON); |
|
} |
|
} |