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.
120 lines
4.4 KiB
120 lines
4.4 KiB
/* |
|
* Copyright (C), 2018-2021 |
|
* Project: starter |
|
* FileName: UserPushController |
|
* Author: Louis |
|
* Date: 2021/3/29 22:36 |
|
*/ |
|
package com.fr.plugin.j7508.sso.request; |
|
|
|
import com.fanruan.api.decision.user.UserKit; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fr.decision.authority.data.User; |
|
import com.fr.decision.webservice.annotation.LoginStatusChecker; |
|
import com.fr.decision.webservice.bean.user.UserBean; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
import com.fr.json.JSONArray; |
|
import com.fr.json.JSONObject; |
|
import com.fr.plugin.j7508.sso.bean.DataResponse; |
|
import com.fr.plugin.j7508.sso.config.SsoConfig; |
|
import com.fr.plugin.j7508.sso.kit.UserServiceKit; |
|
import com.fr.third.springframework.stereotype.Controller; |
|
import com.fr.third.springframework.web.bind.annotation.RequestBody; |
|
import com.fr.third.springframework.web.bind.annotation.RequestMapping; |
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
|
import com.fr.third.springframework.web.bind.annotation.ResponseBody; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
/** |
|
* <Function Description><br> |
|
* <UserPushController> |
|
* |
|
* @author fr.open |
|
* @since 1.0.0 |
|
*/ |
|
@Controller |
|
@RequestMapping("account/iamPush") |
|
public class UserPushController { |
|
private SsoConfig config; |
|
private String adminName; |
|
|
|
public UserPushController() { |
|
this.config = SsoConfig.getInstance(); |
|
} |
|
|
|
@RequestMapping(method = RequestMethod.POST) |
|
@ResponseBody |
|
@LoginStatusChecker(required = false) |
|
public DataResponse doAction(@RequestBody(required = false) String bodyContent, HttpServletResponse res) { |
|
try { |
|
LogKit.info("sso-UserPushController-doAction-bodyContent:{}", bodyContent); |
|
JSONObject bodyJson = new JSONObject(bodyContent); |
|
JSONArray accounts = bodyJson.getJSONArray("data"); |
|
this.adminName = UserService.getInstance().getAdminUserNameList().get(0); |
|
setHeader(res); |
|
operation(accounts); |
|
return DataResponse.success(); |
|
} catch (Exception e) { |
|
LogKit.error(e.getMessage(), e); |
|
return DataResponse.error("1", "error"); |
|
} |
|
} |
|
|
|
/** |
|
* 企业应用业务事件处理 |
|
* |
|
* @param accounts |
|
* @return |
|
*/ |
|
private void operation(JSONArray accounts) throws Exception { |
|
for (int i = 0; i < accounts.size(); i++) { |
|
JSONObject account = accounts.getJSONObject(i); |
|
userSynOperation(account); |
|
} |
|
} |
|
|
|
/** |
|
* 用户同步操作 |
|
* |
|
* @param account |
|
* @throws Exception |
|
*/ |
|
private void userSynOperation(JSONObject account) throws Exception { |
|
String userId = account.getString("uid"); |
|
UserBean userBean; |
|
if (UserKit.existUsername(userId)) { |
|
userBean = UserServiceKit.getInstance().updateUserBean(account); |
|
if (userBean == null) { |
|
return; |
|
} |
|
UserService.getInstance().editUser(userBean); |
|
UserService.getInstance().forbidUser(userBean.getId(), userBean.isEnable()); |
|
UserService.getInstance().updateUserDepartmentPost(UserServiceKit.getInstance().getAdminUserId(), userBean); |
|
} else { |
|
userBean = UserServiceKit.getInstance().createUserBean(account); |
|
try { |
|
UserService.getInstance().addUser(userBean); |
|
User user = UserService.getInstance().getUserByUserName(userBean.getUsername()); |
|
UserService.getInstance().forbidUser(user.getId(), userBean.isEnable()); |
|
} catch (Exception e) { |
|
LogKit.error("sso-UserPushController-userSynOperation-Username:{}, RealName:{}, Mobile:{}, Email:{}", |
|
userBean.getUsername(), userBean.getRealName(), userBean.getMobile(), userBean.getEmail()); |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 解决跨域访问问题 |
|
* |
|
* @param res |
|
*/ |
|
private void setHeader(HttpServletResponse res) { |
|
// 跨域设置header |
|
res.setHeader("Access-Control-Allow-Origin", "*"); |
|
res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); |
|
res.setHeader("Access-Control-Max-Age", "3600"); |
|
res.setHeader("Access-Control-Allow-Headers", "x-requested-with"); |
|
} |
|
} |