9 changed files with 722 additions and 1 deletions
Binary file not shown.
@ -1,3 +1,6 @@
|
||||
# open-JSD-8708 |
||||
|
||||
JSD-8708 开源任务材料 |
||||
JSD-8708 OA/企业微信 (定时)角色反向同步 开源任务材料\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||
<plugin> |
||||
<id>com.fr.plugin.patrol.sync</id> |
||||
<name><![CDATA[门户同步角色]]></name> |
||||
<active>yes</active> |
||||
<version>1.5</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2018-07-31</jartime> |
||||
<vendor>fr.open</vendor> |
||||
<description><![CDATA[门户同步角色]]></description> |
||||
<change-notes><![CDATA[]]></change-notes> |
||||
<extra-decision> |
||||
<GlobalRequestFilterProvider class="com.fr.plugin.patrol.sync.SyncFilter"/> |
||||
</extra-decision> |
||||
<function-recorder class="com.fr.plugin.patrol.sync.SyncFilter"/> |
||||
</plugin> |
@ -0,0 +1,310 @@
|
||||
package com.fr.plugin.patrol.sync; |
||||
|
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.third.org.apache.http.HttpResponse; |
||||
import com.fr.third.org.apache.http.HttpStatus; |
||||
import com.fr.third.org.apache.http.client.HttpClient; |
||||
import com.fr.third.org.apache.http.client.methods.HttpPost; |
||||
import com.fr.third.org.apache.http.config.Registry; |
||||
import com.fr.third.org.apache.http.config.RegistryBuilder; |
||||
import com.fr.third.org.apache.http.conn.socket.ConnectionSocketFactory; |
||||
import com.fr.third.org.apache.http.conn.socket.PlainConnectionSocketFactory; |
||||
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||
import com.fr.third.org.apache.http.entity.StringEntity; |
||||
import com.fr.third.org.apache.http.impl.client.HttpClients; |
||||
import com.fr.third.org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
||||
import com.fr.third.org.apache.http.util.EntityUtils; |
||||
|
||||
import javax.net.ssl.*; |
||||
import java.io.*; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
import java.net.URLEncoder; |
||||
import java.security.cert.CertificateException; |
||||
import java.util.Iterator; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2021/7/29 |
||||
*/ |
||||
public class HttpUtil { |
||||
|
||||
private static HostnameVerifier hv = new HostnameVerifier() { |
||||
@Override |
||||
public boolean verify(String urlHostName, SSLSession session) { |
||||
System.out.println("Warning: URL Host: " + urlHostName + " vs. " |
||||
+ session.getPeerHost()); |
||||
return true; |
||||
} |
||||
}; |
||||
|
||||
/** |
||||
* 发送get请求 |
||||
* |
||||
* @param url |
||||
* @param param |
||||
* @param header |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public static String sendGet(String url, Map<String, String> param, Map<String, String> header) { |
||||
String result = ""; |
||||
BufferedReader in = null; |
||||
String urlNameString = url; |
||||
try { |
||||
if (param != null) { |
||||
urlNameString += "?"; |
||||
urlNameString += param.entrySet() |
||||
.stream() |
||||
.map(entry -> entry.getKey() + "=" + entry.getValue()) |
||||
.collect(Collectors.joining("&")); |
||||
} |
||||
|
||||
URL realUrl = new URL(urlNameString); |
||||
// 打开和URL之间的连接
|
||||
HttpURLConnection connection; |
||||
if (url.startsWith("https")) { |
||||
trustAllHttpsCertificates(); |
||||
HttpsURLConnection.setDefaultHostnameVerifier(hv); |
||||
connection = (HttpURLConnection) realUrl.openConnection(); |
||||
} else { |
||||
connection = (HttpURLConnection) realUrl.openConnection(); |
||||
} |
||||
//设置超时时间
|
||||
connection.setDoInput(true); |
||||
connection.setRequestMethod("GET"); |
||||
connection.setConnectTimeout(5000); |
||||
connection.setReadTimeout(15000); |
||||
// 设置通用的请求属性
|
||||
if (header != null) { |
||||
Iterator<Map.Entry<String, String>> it = header.entrySet().iterator(); |
||||
while (it.hasNext()) { |
||||
Map.Entry<String, String> entry = it.next(); |
||||
System.out.println(entry.getKey() + ":::" + entry.getValue()); |
||||
connection.setRequestProperty(entry.getKey(), entry.getValue()); |
||||
} |
||||
} |
||||
connection.setRequestProperty("accept", "*/*"); |
||||
connection.setRequestProperty("connection", "Keep-Alive"); |
||||
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
||||
// 建立实际的连接
|
||||
connection.connect(); |
||||
// 定义 BufferedReader输入流来读取URL的响应,设置utf8防止中文乱码
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); |
||||
String line; |
||||
while ((line = in.readLine()) != null) { |
||||
result += line; |
||||
} |
||||
if (in != null) { |
||||
in.close(); |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e, "get url error ,url is:{},error is {}", urlNameString, e.getMessage()); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
public static String sendPost(String url, Map<String, Object> header, JSONObject body) { |
||||
PrintWriter out = null; |
||||
BufferedReader in = null; |
||||
String result = null; |
||||
String res = null; |
||||
try { |
||||
String urlNameString = url; |
||||
|
||||
URL realUrl = new URL(urlNameString); |
||||
// 打开和URL之间的连接
|
||||
HttpURLConnection conn; |
||||
if (url.startsWith("https")) { |
||||
trustAllHttpsCertificates(); |
||||
HttpsURLConnection.setDefaultHostnameVerifier(hv); |
||||
conn = (HttpURLConnection) realUrl.openConnection(); |
||||
} else { |
||||
conn = (HttpURLConnection) realUrl.openConnection(); |
||||
} |
||||
// 设置通用的请求属性
|
||||
conn.setRequestProperty("accept", "*/*"); |
||||
conn.setRequestProperty("connection", "Keep-Alive"); |
||||
// conn.setRequestProperty("user-agent",
|
||||
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); |
||||
if (header != null) { |
||||
header.forEach((k, v) -> { |
||||
conn.setRequestProperty(k, String.valueOf(v)); |
||||
}); |
||||
} |
||||
// 发送POST请求必须设置如下两行
|
||||
conn.setDoOutput(true); |
||||
conn.setDoInput(true); |
||||
//获取请求头
|
||||
|
||||
// 获取URLConnection对象对应的输出流
|
||||
out = new PrintWriter(conn.getOutputStream()); |
||||
// 发送请求参数
|
||||
if (body != null) { |
||||
FineLoggerFactory.getLogger().info("content data: {}", body.toString()); |
||||
FineLoggerFactory.getLogger().info("content cover data: {}", new String(body.toString().getBytes("UTF-8"), "UTF-8")); |
||||
out.print(new String(body.toString().getBytes("UTF-8"), "UTF-8")); |
||||
} |
||||
// flush输出流的缓冲
|
||||
out.flush(); |
||||
// 定义BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader( |
||||
new InputStreamReader(conn.getInputStream())); |
||||
String line; |
||||
while ((line = in.readLine()) != null) { |
||||
result += line; |
||||
} |
||||
res = result; |
||||
if (res.startsWith("null")) { |
||||
res = res.replace("null", ""); |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
//使用finally块来关闭输出流、输入流
|
||||
finally { |
||||
try { |
||||
if (out != null) { |
||||
out.close(); |
||||
} |
||||
if (in != null) { |
||||
in.close(); |
||||
} |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
return res; |
||||
} |
||||
|
||||
|
||||
public static String doPost(String url, Map<String, Object> header, JSONObject json) { |
||||
HttpClient client = HttpClients.createDefault(); |
||||
if (url.startsWith("https")) { |
||||
SSLContext sslcontext = createIgnoreVerifySSL(); |
||||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() |
||||
.register("http", PlainConnectionSocketFactory.INSTANCE) |
||||
.register("https", new SSLConnectionSocketFactory(sslcontext)) |
||||
.build(); |
||||
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); |
||||
HttpClients.custom().setConnectionManager(connManager); |
||||
client = HttpClients.custom().setConnectionManager(connManager).build(); |
||||
} |
||||
HttpPost post = new HttpPost(url); |
||||
post.setHeader("accept", "*/*"); |
||||
post.setHeader("connection", "Keep-Alive"); |
||||
post.setHeader("Content-Type", "application/json"); |
||||
if (header != null) { |
||||
header.forEach((k, v) -> { |
||||
post.setHeader(k, String.valueOf(v)); |
||||
}); |
||||
} |
||||
try { |
||||
StringEntity s = new StringEntity(json.toString(),"UTF-8"); |
||||
s.setContentEncoding("UTF-8"); |
||||
s.setContentType("application/json; charset=UTF-8");//发送json数据需要设置contentType
|
||||
post.setEntity(s); |
||||
HttpResponse res = client.execute(post); |
||||
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { |
||||
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
|
||||
return result; |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static void trustAllHttpsCertificates() throws Exception { |
||||
TrustManager[] trustAllCerts = new TrustManager[1]; |
||||
TrustManager tm = new miTM(); |
||||
trustAllCerts[0] = tm; |
||||
SSLContext sc = SSLContext.getInstance("SSL", "SunJSSE"); |
||||
sc.init(null, trustAllCerts, null); |
||||
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* encode url by UTF-8 |
||||
* |
||||
* @param url url before encoding |
||||
* @return url after encoding |
||||
*/ |
||||
public static String encodeUrl(String url) { |
||||
String eurl = url; |
||||
try { |
||||
eurl = URLEncoder.encode(url, "UTF-8"); |
||||
} catch (UnsupportedEncodingException e) { |
||||
} |
||||
return eurl; |
||||
} |
||||
|
||||
private static class miTM implements TrustManager, |
||||
X509TrustManager { |
||||
@Override |
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
||||
return null; |
||||
} |
||||
|
||||
public boolean isServerTrusted( |
||||
java.security.cert.X509Certificate[] certs) { |
||||
return true; |
||||
} |
||||
|
||||
public boolean isClientTrusted( |
||||
java.security.cert.X509Certificate[] certs) { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public void checkServerTrusted( |
||||
java.security.cert.X509Certificate[] certs, String authType) |
||||
throws CertificateException { |
||||
return; |
||||
} |
||||
|
||||
@Override |
||||
public void checkClientTrusted( |
||||
java.security.cert.X509Certificate[] certs, String authType) |
||||
throws CertificateException { |
||||
return; |
||||
} |
||||
} |
||||
|
||||
public static SSLContext createIgnoreVerifySSL() { |
||||
try { |
||||
SSLContext sc = SSLContext.getInstance("SSLv3"); |
||||
|
||||
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
||||
X509TrustManager trustManager = new X509TrustManager() { |
||||
@Override |
||||
public void checkClientTrusted( |
||||
java.security.cert.X509Certificate[] paramArrayOfX509Certificate, |
||||
String paramString) throws CertificateException { |
||||
} |
||||
|
||||
@Override |
||||
public void checkServerTrusted( |
||||
java.security.cert.X509Certificate[] paramArrayOfX509Certificate, |
||||
String paramString) throws CertificateException { |
||||
} |
||||
|
||||
@Override |
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
||||
return null; |
||||
} |
||||
}; |
||||
|
||||
sc.init(null, new TrustManager[]{trustManager}, null); |
||||
return sc; |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,18 @@
|
||||
package com.fr.plugin.patrol.sync; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @Author fr.open |
||||
* @Date 2021/3/1 |
||||
* @Description |
||||
**/ |
||||
public class PluginConstants { |
||||
|
||||
public static final String PLUGIN_ID = "com.fr.plugin.patrol.sync"; |
||||
|
||||
public static Set<String> NAMES = new HashSet(Arrays.asList(new String[]{"安监日报","营销BI-xxxx","营销BI-xxx","营销BI-xx"})); |
||||
|
||||
} |
@ -0,0 +1,76 @@
|
||||
package com.fr.plugin.patrol.sync; |
||||
|
||||
import com.fr.base.PropertiesUtils; |
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.context.PluginContexts; |
||||
import com.fr.plugin.patrol.sync.schedule.SyncThread; |
||||
import com.fr.plugin.transform.FunctionRecorder; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
import com.fr.scheduler.QuartzContext; |
||||
import com.fr.scheduler.ScheduleJobManager; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.fun.Authorize; |
||||
import com.fr.third.v2.org.quartz.CronScheduleBuilder; |
||||
import com.fr.third.v2.org.quartz.JobKey; |
||||
import com.fr.third.v2.org.quartz.SchedulerException; |
||||
import com.fr.third.v2.org.quartz.TriggerBuilder; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @Author fr.open |
||||
* @Date 2021/7/29 |
||||
* @Description |
||||
**/ |
||||
@FunctionRecorder |
||||
@Authorize(callSignKey = PluginConstants.PLUGIN_ID) |
||||
@EnableMetrics |
||||
public class SyncFilter extends AbstractGlobalRequestFilterProvider { |
||||
@Override |
||||
public String filterName() { |
||||
return "syncPatrol"; |
||||
} |
||||
|
||||
@Override |
||||
@Focus(id = PluginConstants.PLUGIN_ID, text = "门户角色同步", source = Original.PLUGIN) |
||||
public String[] urlPatterns() { |
||||
if (PluginContexts.currentContext().isAvailable()) { |
||||
initSchedule(); |
||||
} |
||||
return new String[]{"/syncPatrol"}; |
||||
} |
||||
|
||||
public static void initSchedule() { |
||||
JobKey jobKey = new JobKey("syncPatrol", "syncGroup"); |
||||
try { |
||||
if (QuartzContext.getInstance().getScheduler().checkExists(jobKey)) { |
||||
ScheduleJobManager.getInstance().removeJob(jobKey.getName(), jobKey.getGroup()); |
||||
} |
||||
} catch (SchedulerException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
addSchedule(jobKey); |
||||
} |
||||
|
||||
private static void addSchedule(JobKey jobKey) { |
||||
Map<String, Object> param = new HashMap(); |
||||
TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger(); |
||||
triggerBuilder.forJob(jobKey.getName(), jobKey.getGroup()).withIdentity(jobKey.getName(), jobKey.getGroup()).startNow(); |
||||
String cron = PropertiesUtils.getProperties("patrol").getProperty("cron"); |
||||
if (StringUtils.isBlank(cron)) { |
||||
FineLoggerFactory.getLogger().error("cron is null schedule start failed"); |
||||
return; |
||||
} |
||||
CronScheduleBuilder schedule = CronScheduleBuilder.cronSchedule(cron); |
||||
triggerBuilder.withSchedule(schedule); |
||||
try { |
||||
ScheduleJobManager.getInstance().addJob(jobKey.getName(), jobKey.getGroup(), "sync job", SyncThread.class, triggerBuilder.build(), param); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,243 @@
|
||||
package com.fr.plugin.patrol.sync.schedule; |
||||
|
||||
import com.fr.base.TableData; |
||||
import com.fr.cluster.core.ClusterNode; |
||||
import com.fr.decision.authority.AuthorityContext; |
||||
import com.fr.decision.authority.data.CustomRole; |
||||
import com.fr.decision.authority.data.User; |
||||
import com.fr.decision.webservice.service.user.UserMiddleRoleService; |
||||
import com.fr.file.TableDataConfig; |
||||
import com.fr.general.PropertiesUtils; |
||||
import com.fr.general.data.DataModel; |
||||
import com.fr.json.JSONArray; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.patrol.sync.HttpUtil; |
||||
import com.fr.plugin.patrol.sync.PluginConstants; |
||||
import com.fr.scheduler.job.FineScheduleJob; |
||||
import com.fr.script.Calculator; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.query.QueryFactory; |
||||
import com.fr.third.v2.org.quartz.JobExecutionContext; |
||||
|
||||
import java.util.*; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @Author fr.open |
||||
* @Date 2021/7/29 |
||||
* @Description |
||||
**/ |
||||
public class SyncThread extends FineScheduleJob { |
||||
|
||||
private DataModel dataModel; |
||||
|
||||
private String addRole; |
||||
|
||||
private String getUser; |
||||
|
||||
private String addUser; |
||||
|
||||
private String deleteUser; |
||||
|
||||
private String createId; |
||||
|
||||
private String findRole; |
||||
|
||||
private String editRole; |
||||
|
||||
|
||||
public SyncThread() { |
||||
String name = PropertiesUtils.getProperties("patrol").getProperty("name"); |
||||
FineLoggerFactory.getLogger().info("get table data config is {}", name); |
||||
|
||||
addRole = PropertiesUtils.getProperties("patrol").getProperty("addRole"); |
||||
FineLoggerFactory.getLogger().info("get addRole config is {}", addRole); |
||||
|
||||
getUser = PropertiesUtils.getProperties("patrol").getProperty("getUser"); |
||||
FineLoggerFactory.getLogger().info("get getUser config is {}", getUser); |
||||
|
||||
addUser = PropertiesUtils.getProperties("patrol").getProperty("addUser"); |
||||
FineLoggerFactory.getLogger().info("get addUser config is {}", addUser); |
||||
|
||||
deleteUser = PropertiesUtils.getProperties("patrol").getProperty("deleteUser"); |
||||
FineLoggerFactory.getLogger().info("get deleteUser config is {}", deleteUser); |
||||
|
||||
createId = PropertiesUtils.getProperties("patrol").getProperty("createId"); |
||||
FineLoggerFactory.getLogger().info("get createId config is {}", createId); |
||||
|
||||
findRole = PropertiesUtils.getProperties("patrol").getProperty("findRole"); |
||||
FineLoggerFactory.getLogger().info("get findRole config is {}", findRole); |
||||
|
||||
editRole = PropertiesUtils.getProperties("patrol").getProperty("editRole"); |
||||
FineLoggerFactory.getLogger().info("get editRole config is {}", editRole); |
||||
TableData tableData = TableDataConfig.getInstance().getTableData(name); |
||||
dataModel = tableData.createDataModel(Calculator.createCalculator()); |
||||
} |
||||
|
||||
@Override |
||||
public void run(JobExecutionContext jobExecutionContext, ClusterNode clusterNode) throws Exception { |
||||
List<CustomRole> customRoles = AuthorityContext.getInstance().getCustomRoleController().find(QueryFactory.create()); |
||||
customRoles = customRoles.stream().filter(e -> !"superusers".equals(e.getName())).collect(Collectors.toList()); |
||||
FineLoggerFactory.getLogger().info("get role size is {}", customRoles.size()); |
||||
Map<String, String> roleMap = getRoleMap(); |
||||
if(roleMap.isEmpty() || roleMap.size() < 4){ |
||||
FineLoggerFactory.getLogger().error("default tag is not exist,map is {}",roleMap); |
||||
return; |
||||
} |
||||
Map<String, Set<String>> remoteMap = new HashMap<>(); |
||||
Map<String, Set<String>> existMap = new HashMap<>(); |
||||
for(CustomRole role : customRoles) { |
||||
String roleName = getTagName(role.getName()); |
||||
if (StringUtils.isBlank(roleName)) { |
||||
FineLoggerFactory.getLogger().info("role {} get Mapping failed"); |
||||
continue; |
||||
} |
||||
String roleCode = roleMap.get(roleName); |
||||
//系统下的角色用户
|
||||
Map<String, String> users = getUsers(role.getId()); |
||||
//门户角色下用户
|
||||
Set<String> roleUsers = getRoleUser(roleCode); |
||||
remoteMap.put(roleCode,roleUsers); |
||||
Set<String> ids = existMap.get(roleCode); |
||||
if(ids == null){ |
||||
ids = new HashSet<>(); |
||||
} |
||||
ids.addAll(users.keySet()); |
||||
existMap.put(roleCode,ids); |
||||
|
||||
Set<String> add = users.keySet().stream().filter(e -> !roleUsers.contains(e)).collect(Collectors.toSet()); |
||||
addRoleUser(roleCode, add); |
||||
|
||||
} |
||||
|
||||
remoteMap.forEach((k,v)->{ |
||||
Set<String> ids = existMap.get(k); |
||||
Set<String> delete = v.stream().filter(e -> !ids.contains(e)).collect(Collectors.toSet()); |
||||
FineLoggerFactory.getLogger().info("remote users is {} exist users is {}",v,ids); |
||||
FineLoggerFactory.getLogger().info("delete users {} to tag:{}",delete,k); |
||||
deleteRoleUser(k, delete); |
||||
}); |
||||
/*Map<String, String> roleMap = getRoleMap(); |
||||
for (CustomRole role : customRoles) { |
||||
String roleCode = role.getId().replace("-", ""); |
||||
if (roleMap.containsKey(roleCode)) { |
||||
//角色名称不同则更新角色名称
|
||||
if (!role.getName().equals(roleMap.get(roleCode))) { |
||||
editRole(roleCode, role.getName()); |
||||
} |
||||
} else { |
||||
//首先新建角色
|
||||
roleCode = addRole(role); |
||||
} |
||||
//系统下的角色用户
|
||||
Map<String, String> users = getUsers(role.getId()); |
||||
//门户角色下用户
|
||||
Set<String> roleUsers = getRoleUser(roleCode); |
||||
|
||||
Set<String> delete = roleUsers.stream().filter(e -> !users.keySet().contains(e)).collect(Collectors.toSet()); |
||||
deleteRoleUser(roleCode, delete); |
||||
|
||||
Set<String> add = users.keySet().stream().filter(e -> !delete.contains(e)).collect(Collectors.toSet()); |
||||
addRoleUser(roleCode, add); |
||||
|
||||
}*/ |
||||
} |
||||
private String getTagName(String name) { |
||||
Iterator<String> iterator = PluginConstants.NAMES.iterator(); |
||||
while (iterator.hasNext()){ |
||||
String key = iterator.next(); |
||||
if(key.equals("安监日报") && name.startsWith("安监-")){ |
||||
return key; |
||||
} |
||||
if(name.startsWith(key)){ |
||||
return key; |
||||
} |
||||
} |
||||
return StringUtils.EMPTY; |
||||
} |
||||
|
||||
private void editRole(String code, String name) { |
||||
String res = HttpUtil.sendPost(editRole, null, new JSONObject().put("roleId", code).put("roleName", name).put("createId", createId)); |
||||
FineLoggerFactory.getLogger().info("edit role res is {}", res); |
||||
} |
||||
|
||||
private Map<String, String> getRoleMap() { |
||||
String res = HttpUtil.sendPost(findRole, null, new JSONObject().put("createId", createId)); |
||||
FineLoggerFactory.getLogger().info("get role res is {}", res); |
||||
JSONObject object = new JSONObject(res); |
||||
Map<String, String> map = new HashMap<>(); |
||||
JSONArray date = object.getJSONArray("data"); |
||||
for (int i = 0; i < date.size(); i++) { |
||||
JSONObject entries = date.getJSONObject(i); |
||||
if(PluginConstants.NAMES.contains(entries.getString("roleName"))){ |
||||
map.put(entries.getString("roleName"),entries.getString("roleId")); |
||||
} |
||||
} |
||||
return map; |
||||
} |
||||
|
||||
private void addRoleUser(String roleCode, Set<String> add) { |
||||
if (add == null || add.isEmpty()) { |
||||
return; |
||||
} |
||||
Iterator<String> it = add.iterator(); |
||||
while (it.hasNext()) { |
||||
String next = it.next(); |
||||
String res = HttpUtil.sendPost(addUser, null, new JSONObject().put("roleId", roleCode).put("authorizationUser", next).put("createId", createId)); |
||||
FineLoggerFactory.getLogger().info("add role users res is {}", res); |
||||
} |
||||
} |
||||
|
||||
private void deleteRoleUser(String roleCode, Set<String> delete) { |
||||
if (delete == null || delete.isEmpty()) { |
||||
return; |
||||
} |
||||
Iterator<String> it = delete.iterator(); |
||||
while (it.hasNext()) { |
||||
String next = it.next(); |
||||
String res = HttpUtil.sendPost(deleteUser, null, new JSONObject().put("roleId", roleCode).put("authorizationUser", next).put("createId", createId)); |
||||
FineLoggerFactory.getLogger().info("delete role users res is {}", res); |
||||
} |
||||
} |
||||
|
||||
private Set<String> getRoleUser(String roleCode) { |
||||
String res = HttpUtil.sendPost(getUser, null, new JSONObject().put("roleId", roleCode).put("createId", createId)); |
||||
FineLoggerFactory.getLogger().info("get role users res is {}", res); |
||||
JSONObject object = new JSONObject(res); |
||||
JSONArray date = object.getJSONObject("data").getJSONArray("userAuth"); |
||||
Set<String> users = new HashSet<>(); |
||||
for (int i = 0; i < date.size(); i++) { |
||||
JSONObject entries = date.getJSONObject(i); |
||||
users.add(entries.getString("userCode")); |
||||
} |
||||
return users; |
||||
} |
||||
|
||||
private String addRole(CustomRole role) { |
||||
String code = role.getId().replaceAll("-", ""); |
||||
JSONObject object = new JSONObject().put("roleId", code).put("roleName", role.getName()).put("createId", createId); |
||||
String res = HttpUtil.sendPost(addRole, null, object); |
||||
FineLoggerFactory.getLogger().info("add role res is {}", res); |
||||
return code; |
||||
} |
||||
|
||||
|
||||
private Map<String, String> getUsers(String roleId) throws Exception { |
||||
Set<String> ids = UserMiddleRoleService.getInstance().getAllUserIdsByCustomRole(roleId); |
||||
List<User> all = AuthorityContext.getInstance().getUserController().find(QueryFactory.create()); |
||||
Set<String> usersMap = all.stream().filter(e -> ids.contains(e.getId())).map(e -> e.getUserName()).collect(Collectors.toSet()); |
||||
Map<String, String> result = new HashMap<>(); |
||||
try { |
||||
for (int i = 0; i < dataModel.getRowCount(); i++) { |
||||
Object valueAt = dataModel.getValueAt(i, 4); |
||||
if (valueAt != null && usersMap.contains(valueAt.toString())) { |
||||
result.put(String.valueOf(dataModel.getValueAt(i, 7)), valueAt.toString()); |
||||
} |
||||
} |
||||
} catch (Exception e) { |
||||
|
||||
} |
||||
return result; |
||||
} |
||||
} |
@ -0,0 +1,37 @@
|
||||
package com.fr.plugin.patrol.sync.schedule; |
||||
|
||||
/** |
||||
* @Author fr.open |
||||
* @Date 2021/7/29 |
||||
* @Description |
||||
**/ |
||||
public class UserInfo { |
||||
|
||||
private String name; |
||||
|
||||
private String id; |
||||
|
||||
public UserInfo() { |
||||
} |
||||
|
||||
public UserInfo(String name, String id) { |
||||
this.name = name; |
||||
this.id = id; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public String getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(String id) { |
||||
this.id = id; |
||||
} |
||||
} |
@ -0,0 +1,18 @@
|
||||
##定时器参数,此配置为默认每小时执行一次 |
||||
cron=0 0/1 * * * ? * |
||||
##增加角色 |
||||
addRole=http://ip:port/api/FndRole/add |
||||
##获取角色下用户 |
||||
getUser=http://ip:port/api/FndRole/getUserList |
||||
##向角色添加用户 |
||||
addUser=http://ip:port/api/FndRole/addUser |
||||
##向角色删除角色 |
||||
deleteUser=http://ip:port/api/FndRole/deleteUser |
||||
##更新角色 |
||||
editRole=http://ip:port/api/FndRole/roleModify |
||||
##获取角色 |
||||
findRole=http://ip:port/api/FndRole/getRoleList |
||||
##授权系统 |
||||
createId=xxxx |
||||
##数据集名称 |
||||
name=xxxx |
Loading…
Reference in new issue