JSD-9557 基于SDK的OAuth2单点
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.
 
 

151 lines
5.1 KiB

package com.eco.plugin.xxxx.bssso.function;
import com.eco.plugin.xxxx.bssso.config.PluginSimpleConfig;
import com.eco.plugin.xxxx.bssso.db.bean.DBEntity;
import com.eco.plugin.xxxx.bssso.db.controller.DBController;
import com.eco.plugin.xxxx.bssso.utils.FRUserUtils;
import com.eco.plugin.xxxx.bssso.utils.FRUtils;
import com.eco.plugin.xxxx.bssso.utils.HttpUtils;
import com.eco.plugin.xxxx.bssso.utils.Utils;
import com.fr.decision.webservice.bean.user.UserBean;
import com.fr.general.data.TableDataException;
import com.fr.plugin.context.PluginContexts;
import com.fr.script.AbstractFunction;
import com.fr.stable.fun.Authorize;
import java.util.*;
@Authorize(callSignKey = "com.eco.plugin.fr.open.bssso")
public class AuthFunction extends AbstractFunction {
@Override
public Object run(Object[] objects) {
if(!PluginContexts.currentContext().isAvailable()) {
return "fail";
}
FRUtils.FRLogInfo("1");
//帆软用户
List<UserBean> allUser = new ArrayList<UserBean>();
try {
allUser = FRUserUtils.getAllUsers();
} catch (Exception e) {
FRUtils.FRLogError("获取全部用户异常"+e.getMessage());
return "fail";
}
FRUtils.FRLogInfo("2");
//自定义表用户
List<DBEntity> allStatus = DBController.getAllUser();
FRUtils.FRLogInfo("3");
List<DBEntity> delete = new ArrayList<DBEntity>();
List<DBEntity> addOrUpdate = new ArrayList<DBEntity>();
Map<String,UserBean> fruserMap = new HashMap<String,UserBean>();
Map<String,DBEntity> userMap = new HashMap<String,DBEntity>();
for(UserBean user : allUser){
String username = user.getUsername();
try {
if(Utils.isAdmin(username)){
continue;
}
fruserMap.put(username,user);
} catch (Exception e) {
FRUtils.FRLogError("判断管理员失败:"+e.getMessage());
return "fail";
}
}
FRUtils.FRLogInfo("4");
//处理删除的用户
for(DBEntity user : allStatus){
String username = user.getUsername();
userMap.put(username,user);
//用户表中不存在
if(!fruserMap.containsKey(username)){
//将用户从状态表中删除
delete.add(user);
//禁用sso状态
user.setSelected("false");
callWebservice(user);
}
}
FRUtils.FRLogInfo("5");
//处理新增的用户
for(UserBean user : allUser){
String username = user.getUsername();
try {
if(Utils.isAdmin(username)){
continue;
}
//状态表中不存在
if(!userMap.containsKey(username)){
DBEntity db = new DBEntity();
db.setId(UUID.randomUUID().toString());
db.setUsername(username);
db.setSelected("true");
addOrUpdate.add(db);
//将sso状态设置为开启
callWebservice(db);
}
} catch (Exception e) {
FRUtils.FRLogError("判断管理员失败:"+e.getMessage());
return "fail";
}
}
FRUtils.FRLogInfo("6");
DBController.batch(addOrUpdate,delete);
return "success";
}
/**
* 修改sso用户权限
* @param dbEntity
* @return
* @throws TableDataException
*/
private boolean callWebservice(DBEntity dbEntity){
String webserviceStr = "<soapenv:Envelope\n" +
"xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
"xmlns:web=\"http://webservice.sso.paraview.com/\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <web:setPermission>\n" +
" <arg0>\n" +
" <psid>#psid</psid>\n" +
" <access>#selected</access>\n" +
" <type></type>\n" +
" </arg0>\n" +
" <arg1>#clientid</arg1>\n" +
" <arg2>#secret</arg2>\n" +
" </web:setPermission>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
String psid = dbEntity.getUsername();
FRUtils.FRLogInfo("psid>>"+psid);
String selected = String.valueOf(dbEntity.getSelected());
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
String clientid = psc.getClientId();
String secret = psc.getSecret();
String url = psc.getQxurl();
webserviceStr = webserviceStr.replace("#psid",psid).replace("#selected",selected).replace("#clientid",clientid)
.replace("#secret",secret);
String result = HttpUtils.HttpPostText(url,webserviceStr);
return Utils.isNotNullStr(result);
}
}