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.
199 lines
10 KiB
199 lines
10 KiB
3 years ago
|
package com.fr.plugin.handers;
|
||
|
|
||
|
import com.fr.collections.FineCollections;
|
||
|
import com.fr.collections.api.FineLock;
|
||
|
import com.fr.decision.authority.AuthorityContext;
|
||
|
import com.fr.decision.authority.base.constant.type.operation.ManualOperationType;
|
||
|
import com.fr.decision.authority.controller.DepartmentController;
|
||
|
import com.fr.decision.authority.controller.PostController;
|
||
|
import com.fr.decision.authority.controller.UserController;
|
||
|
import com.fr.decision.authority.data.Department;
|
||
|
import com.fr.decision.authority.data.Post;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.decision.fun.impl.BaseHttpHandler;
|
||
|
import com.fr.decision.privilege.encrpt.PasswordValidator;
|
||
|
import com.fr.decision.webservice.utils.UserSourceFactory;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.json.JSONArray;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.plugin.Aesutils;
|
||
|
import com.fr.plugin.HttpUtils;
|
||
|
import com.fr.plugin.SafeCheckUtils;
|
||
|
import com.fr.plugin.transform.ExecuteFunctionRecord;
|
||
|
import com.fr.plugin.transform.FunctionRecorder;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.query.QueryFactory;
|
||
|
import com.fr.stable.query.restriction.RestrictionFactory;
|
||
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.PrintWriter;
|
||
|
import java.io.StringWriter;
|
||
|
import java.util.List;
|
||
|
import java.util.UUID;
|
||
|
import java.util.concurrent.TimeUnit;
|
||
|
|
||
|
@FunctionRecorder
|
||
|
public class AddAppAccountHander extends BaseHttpHandler {
|
||
|
@Override
|
||
|
public RequestMethod getMethod() {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String getPath() {
|
||
|
return "/accounts";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isPublic() {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
@ExecuteFunctionRecord
|
||
|
public void handle(HttpServletRequest req, HttpServletResponse httpServletResponse) throws Exception {
|
||
|
String body = HttpUtils.inputStream2String(req.getInputStream());
|
||
|
if (StringUtils.isNotBlank(body)) {
|
||
|
String xtime = req.getHeader("xtime");
|
||
|
String xsign = req.getHeader("xkey");
|
||
|
|
||
|
FineLoggerFactory.getLogger().info("addAppAccount: 请求参数 {}", body);
|
||
|
JSONObject jsonObject = new JSONObject(body);
|
||
|
if (jsonObject.has("account")) {
|
||
|
UserService userService = UserService.getInstance();
|
||
|
String account = jsonObject.getString("account");
|
||
|
FineLock lock = FineCollections.getInstance().getClient().getLock("syncuser", "lockedname" + account);
|
||
|
if (lock.tryLock(500L, TimeUnit.MILLISECONDS)) {
|
||
|
try {
|
||
|
User user = userService.getUserByUserName(account);
|
||
|
boolean iscreate = false;
|
||
|
if (user == null) {
|
||
|
iscreate = true;
|
||
|
user = new User();
|
||
|
}
|
||
|
String password = jsonObject.getString("password");
|
||
|
JSONObject person_info = jsonObject.getJSONObject("person_info");
|
||
|
// JSONObject person_info = new JSONObject(pp_info);
|
||
|
String name = person_info.getString("name");
|
||
|
String email = person_info.getString("email");
|
||
|
String mobile = person_info.getString("mobile");
|
||
|
String status = person_info.getString("status");//0启用 1禁用
|
||
|
if(iscreate){
|
||
|
user.setId(account);
|
||
|
}
|
||
|
String userUid = user.getId();
|
||
|
user.setRealName(name);
|
||
|
user.setUserName(account);
|
||
|
user.setEmail(email);
|
||
|
user.setEnable(true);
|
||
|
user.setCreationType(ManualOperationType.KEY);
|
||
|
user.setMobile(mobile);
|
||
|
PasswordValidator passwordValidator = UserSourceFactory.getInstance().getUserSource(ManualOperationType.KEY).getPasswordValidator();
|
||
|
String pwd;
|
||
|
if (StringUtils.isBlank(password)) {
|
||
|
pwd = "frapp.X";
|
||
|
} else {
|
||
|
pwd = Aesutils.getInstance().decrypt(password);
|
||
|
}
|
||
|
UserController userController = AuthorityContext.getInstance().getUserController();
|
||
|
if (iscreate) {
|
||
|
try {
|
||
|
user.setPassword(passwordValidator.encode(account, pwd));
|
||
|
userController.add(user);
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
} else {
|
||
|
userController.update(user);
|
||
|
}
|
||
|
DepartmentController departmentController = AuthorityContext.getInstance().getDepartmentController();
|
||
|
JSONArray groups = person_info.getJSONArray("groups");
|
||
|
//先将用户移除原来部门
|
||
|
PostController postController = AuthorityContext.getInstance().getPostController();
|
||
|
List<Post> posts = postController.findByUser(userUid, QueryFactory.create());
|
||
|
//把用户从原来岗位移除
|
||
|
for (Post post : posts) {
|
||
|
//先查到职位,再查职位关联的部门
|
||
|
List<Department> departments = departmentController.findByPost(post.getId(), QueryFactory.create());
|
||
|
for (Department de : departments) {
|
||
|
userController.removeUserFromDepartmentAndPost(userUid, de.getId(), post.getId());
|
||
|
}
|
||
|
}
|
||
|
for (int i = 0; i < groups.length(); i++) {
|
||
|
JSONObject postions = groups.getJSONObject(i);
|
||
|
String orgName = postions.getString("orgName");
|
||
|
String orgId = postions.getString("orgId");
|
||
|
String orgPid = postions.getString("orgPid");
|
||
|
Department department = departmentController.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", orgId)));
|
||
|
if (department == null) {
|
||
|
department = new Department();
|
||
|
department.setName(orgName);
|
||
|
if (StringUtils.equals(orgPid, "0")) {
|
||
|
orgPid = null;
|
||
|
}
|
||
|
department.setId(orgId);
|
||
|
department.setParentId(orgPid);
|
||
|
department.setEnable(true);
|
||
|
departmentController.add(department);
|
||
|
}
|
||
|
String positionId = postions.getString("positionId");
|
||
|
String positionName = postions.getString("positionName");
|
||
|
Post post = postController.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", positionId)));
|
||
|
if (post == null) {
|
||
|
post = new Post();
|
||
|
post.setName(positionName);
|
||
|
post.setId(positionId);
|
||
|
post.setEnable(true);
|
||
|
postController.add(post);
|
||
|
try {
|
||
|
postController.addPostToDepartment(positionId, orgId);
|
||
|
} catch (Exception e) {
|
||
|
printException2FrLog(e);
|
||
|
}
|
||
|
}
|
||
|
//检查职务和部门是否存在关联关系
|
||
|
List<Department> departments = departmentController.findByPost(post.getId(), QueryFactory.create());
|
||
|
boolean postindeps = false;
|
||
|
for (Department department1 : departments) {
|
||
|
if (StringUtils.equals(department1.getId(), orgId)) {
|
||
|
postindeps = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (!postindeps) {
|
||
|
try {
|
||
|
postController.addPostToDepartment(positionId, orgId);
|
||
|
} catch (Exception e) {
|
||
|
printException2FrLog(e);
|
||
|
}
|
||
|
}
|
||
|
try {
|
||
|
userController.addUserToDepartmentAndPost(userUid, orgId, positionId);
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error("将用户添加到职位失败:", e);
|
||
|
WebUtils.printAsJSON(httpServletResponse, HttpUtils.getErrorByCreateRelation());
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
printException2FrLog(e);
|
||
|
} finally {
|
||
|
lock.unlock();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
WebUtils.printAsJSON(httpServletResponse, HttpUtils.getSuccessJSONObject());
|
||
|
}
|
||
|
|
||
|
public static void printException2FrLog(Throwable e) {
|
||
|
StringWriter writer = new StringWriter();
|
||
|
e.printStackTrace(new PrintWriter(writer));
|
||
|
String s = writer.toString();
|
||
|
FineLoggerFactory.getLogger().error("错误:{}", s);
|
||
|
}
|
||
|
}
|