diff --git a/JSD-8106-需求确认书V2.docx b/JSD-8106-需求确认书V2.docx new file mode 100644 index 0000000..206f025 Binary files /dev/null and b/JSD-8106-需求确认书V2.docx differ diff --git a/README.md b/README.md index 70e6bd8..687f74c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # open-JSD-8106 -JSD-8106 客户用户同步接口 \ No newline at end of file +JSD-8106 客户用户同步接口\ +免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ +仅作为开发者学习参考使用!禁止用于任何商业用途!\ +为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 \ No newline at end of file diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..843234d --- /dev/null +++ b/plugin.xml @@ -0,0 +1,19 @@ + + com.fr.plugin.jkIT + + yes + 1.0.2 + 10.0 + 2018-07-31 + author + + + com.fr.plugin.jkIT + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/fr/plugin/jkIT/handler/ExtendAttrHandlerProvider.java b/src/main/java/com/fr/plugin/jkIT/handler/ExtendAttrHandlerProvider.java new file mode 100644 index 0000000..d35898e --- /dev/null +++ b/src/main/java/com/fr/plugin/jkIT/handler/ExtendAttrHandlerProvider.java @@ -0,0 +1,13 @@ +package com.fr.plugin.jkIT.handler; + +import com.fr.decision.fun.HttpHandler; +import com.fr.decision.fun.impl.AbstractHttpHandlerProvider; + +public class ExtendAttrHandlerProvider extends AbstractHttpHandlerProvider { + @Override + public HttpHandler[] registerHandlers() { + return new HttpHandler[]{ + new ITHandler() + }; + } +} diff --git a/src/main/java/com/fr/plugin/jkIT/handler/ITHandler.java b/src/main/java/com/fr/plugin/jkIT/handler/ITHandler.java new file mode 100644 index 0000000..b9195ba --- /dev/null +++ b/src/main/java/com/fr/plugin/jkIT/handler/ITHandler.java @@ -0,0 +1,215 @@ +package com.fr.plugin.jkIT.handler; + +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.json.JSONArray; +import com.fr.json.JSONObject; +import com.fr.plugin.jkIT.utils.FRUserUtils; +import com.fr.plugin.jkIT.utils.FRUtils; +import com.fr.plugin.jkIT.utils.ResponseUtils; +import com.fr.plugin.jkIT.utils.Utils; +import com.fr.plugin.transform.FunctionRecorder; +import com.fr.third.springframework.web.bind.annotation.RequestMethod; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@FunctionRecorder +public class ITHandler extends BaseHttpHandler { + + + public ITHandler() { + } + + @Override + public RequestMethod getMethod() { + return RequestMethod.POST; + } + + @Override + public String getPath() { + return "/operateUser"; + } + + @Override + public boolean isPublic() { + return true; + } + + @Override + public void handle(HttpServletRequest req, HttpServletResponse res){ + JSONObject object = Utils.getRequestBody(req); + FRUtils.FRLogInfo("json:"+object.toString()); + //获取请求参数 + + //同步任务流水号 + String requestId = object.getString("requestId"); + String appId = object.getString("appId"); + String appKey = object.getString("appKey"); + //操作类型 1、Add-新增 + //2、Disable-停用 + //3、Enable-启用 + //4、Delete-删除 + //5、Modify-修改 + //6、ModifyPassword-修改密码 + //8、TestConnection-测试服务通信 + String actionType = object.getString("actionType"); + //账户列表 +// String accountList = req.getParameter("accountList"); + JSONObject account = object.getJSONArray("accountList").getJSONObject(0); + + if(Utils.isNullStr(actionType)){ + FRUtils.FRLogInfo("必填参数不能为空!"); + return; + } + + FRUtils.FRLogInfo("param:"+requestId+";"+appId+";"+appKey+";"+actionType); + + //返回json + JSONObject result = new JSONObject(); + + if("Add".equals(actionType)){ + + String username = account.getString("accountId"); + String email = account.getString("email"); + String mobile = account.getString("mobile"); + String realName = account.getString("cn"); + + UserBean userBean = new UserBean(); + userBean.setRealName(realName); + userBean.setPassword("1"); + userBean.setUsername(username); + userBean.setEmail(email); + userBean.setMobile(mobile); + + try { + FRUserUtils.addUser(userBean); + JSONArray resultArray = new JSONArray(); + JSONObject resultObject = new JSONObject(); + resultObject.put("accountId",username); + resultObject.put("accountCode",username); + + resultArray.add(resultObject); + result.put("requestId",requestId); + result.put("returnFlag",true); + result.put("returnCode",0); + result.put("returnMessage",""); + result.put("accountList",resultArray); + + } catch (Exception e) { + FRUtils.FRLogInfo("账号添加异常:"+e.getMessage()); + result.put("requestId",requestId); + result.put("returnFlag",false); + result.put("returnCode",5); + result.put("returnMessage","账号添加异常"); + } + } + else if("Disable".equals(actionType)){ +// JSONObject account = new JSONArray(accountList).getJSONObject(0); + String username = account.getString("accountId"); + + try { + User user = FRUserUtils.getUserByUserName(username); + FRUserUtils.forbidUser(user.getId(),false); + result.put("requestId",requestId); + result.put("returnFlag",true); + result.put("returnCode",0); + result.put("returnMessage",""); + } catch (Exception e) { + FRUtils.FRLogInfo("账号删除异常:"+e.getMessage()); + result.put("requestId",requestId); + result.put("returnFlag",false); + result.put("returnCode",5); + result.put("returnMessage","账号删除异常"); + } + } + else if("Enable".equals(actionType)){ +// JSONObject account = new JSONArray(accountList).getJSONObject(0); + String username = account.getString("accountId"); + + try { + User user = FRUserUtils.getUserByUserName(username); + FRUserUtils.forbidUser(user.getId(),true); + result.put("requestId",requestId); + result.put("returnFlag",true); + result.put("returnCode",0); + result.put("returnMessage",""); + + } catch (Exception e) { + FRUtils.FRLogInfo("账号删除异常:"+e.getMessage()); + result.put("requestId",requestId); + result.put("returnFlag",false); + result.put("returnCode",5); + result.put("returnMessage","账号删除异常"); + } + } + else if("Delete".equals(actionType)){ +// JSONObject account = new JSONArray(accountList).getJSONObject(0); + String username = account.getString("accountId"); + + try { + User user = FRUserUtils.getUserByUserName(username); + FRUserUtils.deleteUser(user); + + result.put("requestId",requestId); + result.put("returnFlag",true); + result.put("returnCode",0); + result.put("returnMessage",""); + + } catch (Exception e) { + FRUtils.FRLogInfo("账号删除异常:"+e.getMessage()); + result.put("requestId",requestId); + result.put("returnFlag",false); + result.put("returnCode",5); + result.put("returnMessage","账号删除异常"); + } + } + else if("Modify".equals(actionType)){ +// JSONObject account = new JSONArray(accountList).getJSONObject(0); + + String username = account.getString("accountId"); + String email = account.getString("email"); + String mobile = account.getString("mobile"); + String realName = account.getString("cn"); + + try { + User user = FRUserUtils.getUserByUserName(username); + UserBean userBean = new UserBean(); + userBean.setId(user.getId()); + userBean.setRealName(realName); + userBean.setEmail(email); + userBean.setMobile(mobile); + FRUserUtils.updateUser(userBean); + + result.put("requestId",requestId); + result.put("returnFlag",true); + result.put("returnCode",0); + result.put("returnMessage",""); + + } catch (Exception e) { + FRUtils.FRLogInfo("账号修改异常:"+e.getMessage()); + result.put("requestId",requestId); + result.put("returnFlag",false); + result.put("returnCode",5); + result.put("returnMessage","账号修改异常"); + } + } + else if("ModifyPassword".equals(actionType)){ + + } + else if("TestConnection".equals(actionType)){ + result.put("requestId",requestId); + result.put("appId",appId); + result.put("appKey",appKey); + result.put("actionType",actionType); + } + + FRUtils.FRLogInfo("result:"+result.toString()); + ResponseUtils.response(res,result); + } + + + + +} + diff --git a/src/main/java/com/fr/plugin/jkIT/handler/URLAliasProvide.java b/src/main/java/com/fr/plugin/jkIT/handler/URLAliasProvide.java new file mode 100644 index 0000000..826e057 --- /dev/null +++ b/src/main/java/com/fr/plugin/jkIT/handler/URLAliasProvide.java @@ -0,0 +1,14 @@ +package com.fr.plugin.jkIT.handler; + +import com.fr.decision.fun.impl.AbstractURLAliasProvider; +import com.fr.decision.webservice.url.alias.URLAlias; +import com.fr.decision.webservice.url.alias.URLAliasFactory; + +public class URLAliasProvide extends AbstractURLAliasProvider { + @Override + public URLAlias[] registerAlias() { + return new URLAlias[]{ + URLAliasFactory.createPluginAlias("/operateUser","/operateUser",true), + }; + } +} diff --git a/src/main/java/com/fr/plugin/jkIT/utils/FRUserUtils.java b/src/main/java/com/fr/plugin/jkIT/utils/FRUserUtils.java new file mode 100644 index 0000000..5bbeb07 --- /dev/null +++ b/src/main/java/com/fr/plugin/jkIT/utils/FRUserUtils.java @@ -0,0 +1,111 @@ +package com.fr.plugin.jkIT.utils; + +import com.fr.decision.authority.data.User; +import com.fr.decision.privilege.TransmissionTool; +import com.fr.decision.webservice.bean.user.UserBean; +import com.fr.decision.webservice.bean.user.UserUpdateBean; +import com.fr.decision.webservice.v10.user.UserService; + +public class FRUserUtils { + + /** + * 获取用户Service + * @return + */ + public static UserService getUserService(){ + return UserService.getInstance(); + } + + /** + * 添加用户 + * @param userBean + */ + public static void addUser(UserBean userBean) throws Exception { + userBean.setPassword(TransmissionTool.defaultEncrypt(userBean.getPassword())); + getUserService().addUser(userBean); + } + + /** + * 删除用户 + * @param userBean + */ + public static void updateUser(UserBean userBean) throws Exception { + getUserService().editUser(userBean); + } + + /** + * 删除用户 + * @param user + * @return + */ + public static int deleteUser(User user) throws Exception { + String userId = user.getId(); + + UserUpdateBean userUpdateBean = new UserUpdateBean(); + userUpdateBean.setRemoveUserIds(new String[]{userId}); + + return getUserService().deleteUsers(userUpdateBean); + } + + /** + * 根据用户名获取用户实体 + * @param userName + * @return + */ + public static User getUserByUserName(String userName) throws Exception { + return getUserService().getUserByUserName(userName); + } + + /** + * 根据id获取用户 + * @param id + * @return + * @throws Exception + */ + public static UserBean getUser(String id) throws Exception { + return getUserService().getUser(id); + } + + /** + * 判断是否是管理员 + * @param userId + * @return + */ + public static boolean isAdmin(String userId){ + return getUserService().isAdmin(userId); + } + + /** + * 禁用启用用户 + * @param userId + * @param state false 禁用 true 启用 + * @throws Exception 异常说明失败 + */ + public static void forbidUser(String userId,boolean state) throws Exception { + getUserService().forbidUser(userId,state); + } + + /** + * 修改用户部门 + * @param departmentId + * @param postId + * @param ud + * @throws Exception + */ + public static void updateDepartmentPostUsers(String departmentId, String postId, UserUpdateBean ud) throws Exception { + getUserService().updateDepartmentPostUsers(departmentId,postId,ud); + } + + +// /** +// * 验证密码是否正确 +// * @param psd 明文密码 +// * @param user 根据用户名获取得用户对象 +// * @return +// */ +// public static boolean checkPsd(String psd,User user){ +// String shaPsd = CipherUtils.jdksha256(psd); +// +// return shaPsd.equals(user.getPassword()); +// } +} diff --git a/src/main/java/com/fr/plugin/jkIT/utils/FRUtils.java b/src/main/java/com/fr/plugin/jkIT/utils/FRUtils.java new file mode 100644 index 0000000..00c5172 --- /dev/null +++ b/src/main/java/com/fr/plugin/jkIT/utils/FRUtils.java @@ -0,0 +1,158 @@ +package com.fr.plugin.jkIT.utils; + +import com.fr.decision.authority.AuthorityContext; +import com.fr.decision.authority.data.User; +import com.fr.decision.webservice.login.LogInOutResultInfo; +import com.fr.decision.webservice.utils.DecisionServiceConstants; +import com.fr.decision.webservice.v10.login.LoginService; +import com.fr.decision.webservice.v10.login.event.LogInOutEvent; +import com.fr.decision.webservice.v10.user.UserService; +import com.fr.event.EventDispatcher; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.StringUtils; +import com.fr.stable.query.QueryFactory; +import com.fr.stable.query.restriction.RestrictionFactory; +import com.fr.web.utils.WebUtils; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.util.List; + +public class FRUtils { + /** + * 判断用户是否存在 + * @param userName + * @return + */ + public static boolean isUserExist(String userName){ + if (StringUtils.isEmpty(userName)) { + return false; + } else { + try { + List var1 = AuthorityContext.getInstance().getUserController().find(QueryFactory.create().addRestriction(RestrictionFactory.eq("userName", userName))); + return var1 != null && !var1.isEmpty(); + } catch (Exception var2) { + FineLoggerFactory.getLogger().error(var2.getMessage()); + return false; + } + } + } + + /** + * 判断是否登录FR + * @param req + * @return + */ + public static boolean isLogin(HttpServletRequest req){ + return LoginService.getInstance().isLogged(req); + } + + /** + * 帆软登录 + * @param httpServletRequest + * @param httpServletResponse + * @param userName + * @param url + */ + public static void login(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String userName,String url){ + + FineLoggerFactory.getLogger().info("FRLOG:用户名:"+userName); + FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url); + + + //判断用户名是否为空 + if(!Utils.isNullStr(userName)){ + if(isUserExist(userName)){ + String FRToken = ""; + + try { + HttpSession session = httpServletRequest.getSession(true); + + FRToken = LoginService.getInstance().login(httpServletRequest, httpServletResponse, userName); + + httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,FRToken); + + session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, FRToken); + EventDispatcher.fire(LogInOutEvent.LOGIN,new LogInOutResultInfo(httpServletRequest,httpServletResponse,userName,true)); + FineLoggerFactory.getLogger().info("FRLOG:登陆成功!"); + + if(!Utils.isNullStr(url)){ + httpServletResponse.sendRedirect(url); + } + } catch (Exception e) { + ResponseUtils.failedResponse(httpServletResponse,"登录异常,请联系管理员!"); + FineLoggerFactory.getLogger().info("FRLOG:登录异常,请联系管理员!"); + FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage()); + } + }else{ + ResponseUtils.failedResponse(httpServletResponse,"用户在报表系统中不存在!"); + FineLoggerFactory.getLogger().info("FRLOG:用户在报表系统中不存在!"); + } + }else{ + ResponseUtils.failedResponse(httpServletResponse,"用户名不能为空!"); + FineLoggerFactory.getLogger().info("FRLOG:用户名不能为空!"); + } + } + + /** + * + * @param httpServletRequest + * @param httpServletResponse + */ + public static void logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) + { + if(!isLogin(httpServletRequest)){ + return ; + } + + try { + LoginService.getInstance().logout(httpServletRequest,httpServletResponse); + } catch (Exception e) { + ResponseUtils.failedResponse(httpServletResponse,"登出异常,请联系管理员!"); + FineLoggerFactory.getLogger().info("FRLOG:登出异常,请联系管理员!"); + FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage()); + } + } + + /** + * 打印FR日志 + * @param message + */ + public static void FRLogInfo(String message){ + FineLoggerFactory.getLogger().info("FRLOG:"+message); + } + + /** + * 根据用户名获取用户信息 + * @param userName + * @return + */ + public static User getFRUserByUserName(String userName){ + try { + return UserService.getInstance().getUserByUserName(userName); + } catch (Exception e) { + FRLogInfo("获取用户信息异常:"+e.getMessage()); + } + + return null; + } + + /** + * 解密FR密码 + * @param password + * @return + */ +// public static String decryptFRPsd(String password){ +// FRLogInfo("解密密码:"+password); +// return TransmissionTool.decrypt(password); +// } + + /** + * 获取带参数的访问链接 + * @return + */ + public static String getAllUrl(HttpServletRequest httpServletRequest){ + return WebUtils.getOriginalURL(httpServletRequest); + } +} diff --git a/src/main/java/com/fr/plugin/jkIT/utils/ResponseUtils.java b/src/main/java/com/fr/plugin/jkIT/utils/ResponseUtils.java new file mode 100644 index 0000000..26cbc5a --- /dev/null +++ b/src/main/java/com/fr/plugin/jkIT/utils/ResponseUtils.java @@ -0,0 +1,94 @@ +package com.fr.plugin.jkIT.utils; + +import com.fr.json.JSONObject; +import com.fr.log.FineLoggerFactory; +import com.fr.web.utils.WebUtils; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.PrintWriter; + +public class ResponseUtils { + private static final int SUCCESS = 200; + private static final int FAILED = -1; + + public static void successResponse(HttpServletResponse res, String body) { + response(res, body, SUCCESS); + } + + public static void failedResponse(HttpServletResponse res, String body) { + response(res, body, FAILED); + } + + private static void response(HttpServletResponse res, String body, int code) { + JSONObject object = new JSONObject(); + PrintWriter pw; + try { + object.put("code", code); + object.put("data", body); + pw = WebUtils.createPrintWriter(res); + } catch (Exception e) { + FineLoggerFactory.getLogger().info(e.getMessage()); + return; + } + res.setContentType("application/json;charset=utf-8"); + String result = object.toString(); + pw.println(result); + pw.flush(); + pw.close(); + } + + public static void response(HttpServletResponse res,JSONObject json){ + PrintWriter pw; + try { + pw = WebUtils.createPrintWriter(res); + } catch (Exception e) { + FineLoggerFactory.getLogger().info(e.getMessage()); + return; + } + res.setContentType("application/json;charset=utf-8"); + String result = json.toString(); + pw.println(result); + pw.flush(); + pw.close(); + } + + public static void responseXml(HttpServletResponse res,String xml){ + PrintWriter pw; + try { + pw = WebUtils.createPrintWriter(res); + } catch (Exception e) { + FineLoggerFactory.getLogger().info(e.getMessage()); + return; + } + res.setContentType("text/xml;charset=utf-8"); + pw.println(xml); + pw.flush(); + pw.close(); + } + + public static void setCSRFHeader(HttpServletResponse httpServletResponse){ + httpServletResponse.setHeader("Access-Control-Allow-Origin", "*"); + httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,HEAD,PUT,PATCH"); + httpServletResponse.setHeader("Access-Control-Max-Age", "36000"); + httpServletResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization,authorization"); + } + + public static void responseJsonp(HttpServletRequest req, HttpServletResponse res, JSONObject json){ + PrintWriter pw; + try { + pw = WebUtils.createPrintWriter(res); + } catch (Exception e) { + FineLoggerFactory.getLogger().info(e.getMessage()); + return; + } + res.setContentType("text/javascript;charset=utf-8;charset=utf-8"); + String result = json.toString(); + + String jsonp=req.getParameter("callback"); + + pw.println(jsonp+"("+result+")"); + pw.flush(); + pw.close(); + } +} diff --git a/src/main/java/com/fr/plugin/jkIT/utils/Utils.java b/src/main/java/com/fr/plugin/jkIT/utils/Utils.java new file mode 100644 index 0000000..a46d60d --- /dev/null +++ b/src/main/java/com/fr/plugin/jkIT/utils/Utils.java @@ -0,0 +1,94 @@ +package com.fr.plugin.jkIT.utils; + +import com.fr.json.JSONObject; +import com.fr.stable.CodeUtils; +import com.fr.third.org.apache.commons.codec.digest.DigestUtils; +import javax.servlet.http.HttpServletRequest; +import java.io.BufferedReader; +import java.util.UUID; + +public class Utils { + + /** + * 判断字符串是否为空 + * @param str + * @return true 空字符串 false 非空字符串 + */ + public static boolean isNullStr(String str){ + return !(str != null && !str.isEmpty() && !"null".equals(str)); + } + + /** + * 判断字符串是否非空 + * @param str + * @return + */ + public static boolean isNotNullStr(String str){ + return !isNullStr(str); + } + + /** + * MD5加密 + * @param str + * @return + */ + public static String getMd5Str(String str) + { + return DigestUtils.md5Hex(str); + } + + + /** + * 获取完整的访问路径 + */ + public static String getAllUrl(HttpServletRequest req, String queryStr){ + String url = req.getRequestURL().toString(); + + if(isNullStr(queryStr)){ + return url; + } + + return url+"?"+queryStr; + } + + /** + * 帆软shaEncode加密 + */ + + public static String shaEncode(String str){ + return CodeUtils.sha256Encode(str); + } + + /** + * 获取uuid + */ + public static String uuid(){ + return UUID.randomUUID().toString(); + } + + public static String replaceNullStr(String str,String replace){ + if(isNullStr(str)){ + return replace; + } + + return str; + } + + public static JSONObject getRequestBody(HttpServletRequest req){ + StringBuffer sb = new StringBuffer(); + String line = null; + try { + BufferedReader reader = req.getReader(); + while ((line = reader.readLine()) != null) + sb.append(line); + } catch (Exception e) { + FRUtils.FRLogInfo("getRequestBody:exception:"+e.getMessage()); + } + //将空格和换行符替换掉避免使用反序列化工具解析对象时失败 + String jsonString = sb.toString().replaceAll("\\s","").replaceAll("\n",""); + + JSONObject json = new JSONObject(jsonString); + + return json; + } +}