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.
86 lines
3.9 KiB
86 lines
3.9 KiB
2 years ago
|
package com.fr.plugin.xx.zyjn.schedule;
|
||
|
|
||
|
import com.fr.cluster.core.ClusterNode;
|
||
|
import com.fr.decision.authority.AuthorityContext;
|
||
|
import com.fr.decision.authority.base.constant.type.operation.ManualOperationType;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.json.JSONArray;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.plugin.xx.zyjn.conf.GxkgSsoConfig;
|
||
|
import com.fr.plugin.xx.zyjn.utils.HttpUtil;
|
||
|
import com.fr.plugin.xx.zyjn.utils.LogUtils;
|
||
|
import com.fr.scheduler.job.FineScheduleJob;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.third.v2.org.quartz.JobExecutionContext;
|
||
|
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* @Author xx
|
||
|
* @Date 2022/7/18
|
||
|
* @Description
|
||
|
**/
|
||
|
public class SyncUserSchedule extends FineScheduleJob {
|
||
|
|
||
|
private static final String TOKEN_URL = "%s/api/sys/getToken";
|
||
|
|
||
|
private static final String USER_URL = "%s/api/rest/data/acct";
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public void run(JobExecutionContext jobExecutionContext, ClusterNode clusterNode) throws Exception {
|
||
|
GxkgSsoConfig config = GxkgSsoConfig.getInstance();
|
||
|
if (StringUtils.isBlank(config.getSyncUser()) || StringUtils.isBlank(config.getSyncPass())) {
|
||
|
LogUtils.error("同步授权账户信息为空");
|
||
|
return;
|
||
|
}
|
||
|
JSONObject tokenBody = JSONObject.create().put("username", config.getSyncUser()).put("password", config.getSyncPass());
|
||
|
String tokenUrl = String.format(TOKEN_URL, config.getSyncHost());
|
||
|
LogUtils.debug4plugin("token url is {}", tokenUrl);
|
||
|
String tokenRes = HttpUtil.sendPost(tokenUrl, null, tokenBody);
|
||
|
LogUtils.debug4plugin("token res is {}", tokenRes);
|
||
|
if (StringUtils.isBlank(tokenRes)) {
|
||
|
LogUtils.error("token 返回空");
|
||
|
return;
|
||
|
}
|
||
|
JSONObject tokenObject = new JSONObject(tokenRes);
|
||
|
String token = tokenObject.getString("msg");
|
||
|
if (StringUtils.isBlank(token)) {
|
||
|
LogUtils.error("msg 返回空");
|
||
|
return;
|
||
|
}
|
||
|
String userUrl = String.format(USER_URL, config.getSyncHost());
|
||
|
Map<String, String> header = new HashMap();
|
||
|
header.put("uim-login-user-id", token);
|
||
|
LogUtils.debug4plugin("user url is {}", userUrl);
|
||
|
String userRes = HttpUtil.sendPost(userUrl, header, JSONObject.create().put("appSn", config.getAppSn()));
|
||
|
LogUtils.debug4plugin("user res is {}", userRes);
|
||
|
JSONObject object = new JSONObject(userRes);
|
||
|
if (object.containsKey("data")) {
|
||
|
JSONArray array = object.getJSONArray("data");
|
||
|
LogUtils.debug4plugin("get sync user size is {}", array.size());
|
||
|
int created = 0, updated = 0;
|
||
|
for (int i = 0; i < array.size(); i++) {
|
||
|
JSONObject body = array.getJSONObject(i);
|
||
|
String id = body.getString("id");
|
||
|
User user = AuthorityContext.getInstance().getUserController().getById(id);
|
||
|
if (user == null) {
|
||
|
user = new User().id(id).userName(body.getString("loginName"))
|
||
|
.realName(body.getString("userName")).creationType(ManualOperationType.KEY)
|
||
|
.lastOperationType(ManualOperationType.KEY).enable(body.getInt("status") == 1);
|
||
|
AuthorityContext.getInstance().getUserController().add(user);
|
||
|
created++;
|
||
|
} else {
|
||
|
user.id(id).userName(body.getString("loginName"))
|
||
|
.realName(body.getString("userName")).creationType(ManualOperationType.KEY)
|
||
|
.lastOperationType(ManualOperationType.KEY).enable(body.getInt("status") == 1);
|
||
|
AuthorityContext.getInstance().getUserController().update(user);
|
||
|
updated++;
|
||
|
}
|
||
|
}
|
||
|
LogUtils.debug4plugin("sync user create size is {} updated size is {}", created, updated);
|
||
|
}
|
||
|
}
|
||
|
}
|