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.
249 lines
7.7 KiB
249 lines
7.7 KiB
3 years ago
|
package com.eco.plugin.xx.zcgjsync.utils;
|
||
|
|
||
|
import com.fr.decision.authority.AuthorityContext;
|
||
|
import com.fr.decision.authority.controller.DepartmentController;
|
||
|
import com.fr.decision.authority.controller.UserController;
|
||
|
import com.fr.decision.authority.data.Department;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.decision.privilege.TransmissionTool;
|
||
|
import com.fr.decision.webservice.bean.user.*;
|
||
|
import com.fr.decision.webservice.v10.login.ExtendTokenProcessor;
|
||
|
import com.fr.decision.webservice.v10.login.LoginService;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.stable.query.QueryFactory;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class FRUserUtils {
|
||
|
|
||
|
/**
|
||
|
* 获取用户Service
|
||
|
* @return
|
||
|
*/
|
||
|
public static UserService getUserService(){
|
||
|
return UserService.getInstance();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取全量用户
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static List<UserAdditionBean> getAllUsers() throws Exception {
|
||
|
// List<UserBean> userbean = UserMiddleRoleService.getInstance().getAllUsers(false);
|
||
|
List<UserAdditionBean> users = new ArrayList<UserAdditionBean>();
|
||
|
getAllUser(getAdminUser().getUsername(),0,1000,users);
|
||
|
return users;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param adminUsername 管理员用户名
|
||
|
* @param page 页数
|
||
|
* @param num 每页的数据
|
||
|
* @param users 保存用户的列表
|
||
|
*/
|
||
|
private static void getAllUser(String adminUsername,int page,int num,List<UserAdditionBean> users) throws Exception {
|
||
|
Map<String,Object> result = getUserService().getAllUsers(adminUsername,page,num,"","",true);
|
||
|
Long total = (Long)result.get("total");
|
||
|
List<UserAdditionBean> item = (List<UserAdditionBean>)result.get("items");
|
||
|
users.addAll(item);
|
||
|
|
||
|
page = page+1;
|
||
|
|
||
|
if(page * num >= total){
|
||
|
return ;
|
||
|
}
|
||
|
|
||
|
getAllUser(adminUsername,page,num,users);
|
||
|
}
|
||
|
/**
|
||
|
* 添加用户
|
||
|
* @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(User userBean) throws Exception {
|
||
|
UserController userController = AuthorityContext.getInstance().getUserController();
|
||
|
userController.update(userBean);
|
||
|
// UserServiceKit.getInstance().editUser(userBean,getAdminUser().getId());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 删除用户
|
||
|
* @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);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 根据用户名获取用户实体
|
||
|
* @param userName
|
||
|
* @return
|
||
|
*/
|
||
|
public static UserBean getUserBeanByUserName(String userName ) throws Exception {
|
||
|
String id = getUserService().getUserByUserName(userName).getId();
|
||
|
return getUser(id);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 根据id获取用户
|
||
|
* @param id
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static UserBean getUser(String id) throws Exception {
|
||
|
return getUserService().getUser(id);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断是否是管理员
|
||
|
* @param username
|
||
|
* @return
|
||
|
*/
|
||
|
public static boolean isAdmin(String username) throws Exception{
|
||
|
return getUserService().isAdmin(getUserByUserName(username).getId());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 禁用启用用户
|
||
|
* @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, String userid) throws Exception {
|
||
|
AuthorityContext.getInstance().getUserController().addUserToDepartmentAndPost(userid, departmentId, postId);
|
||
|
// 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());
|
||
|
// }
|
||
|
public static User getCurrentUser(HttpServletRequest req) throws Exception {
|
||
|
String username = LoginService.getInstance().getCurrentUserNameFromRequestCookie(req);
|
||
|
|
||
|
if(Utils.isNullStr(username)){
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
return getUserByUserName(username);
|
||
|
}
|
||
|
|
||
|
public static UserBean getCurrentUserBean(HttpServletRequest req) throws Exception {
|
||
|
String username = LoginService.getInstance().getCurrentUserNameFromRequestCookie(req);
|
||
|
|
||
|
if(Utils.isNullStr(username)){
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
return getUserBeanByUserName(username);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取用户部门角色
|
||
|
* @param username
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
private static UserRolesBean getUserRolesBean(String username) throws Exception {
|
||
|
return FRUserUtils.getUserService().getUserDepAndCustomRoles(username);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取部门职务
|
||
|
* @param username
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static List<DepRoleBean> getDepRoleBean(String username) throws Exception{
|
||
|
return getUserRolesBean(username).getDepRoles();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取角色
|
||
|
* @param username
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static List<String> getCustomRoles(String username) throws Exception{
|
||
|
return getUserRolesBean(username).getCustomRoles();
|
||
|
}
|
||
|
|
||
|
public static UserBean getAdminUser() throws Exception {
|
||
|
String adminid = getUserService().getAdminUserIdList().get(0);
|
||
|
return getUser(adminid);
|
||
|
}
|
||
|
|
||
|
public static String getUsernameFromToken(String token){
|
||
|
String username = ExtendTokenProcessor.KEY.getUsername(token);
|
||
|
return username;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 清除用户的机构和职务
|
||
|
* @param userid
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static void clearUserDepAndPost(String userid) throws Exception {
|
||
|
DepartmentController departmentController = AuthorityContext.getInstance().getDepartmentController();
|
||
|
// PostController postController = AuthorityContext.getInstance().getPostController();
|
||
|
List<Department> departments = departmentController.findByUser(userid, QueryFactory.create());
|
||
|
// List<Post> postBeanList = postController.findByUser(userid, QueryFactory.create());
|
||
|
UserController userController = AuthorityContext.getInstance().getUserController();
|
||
|
//
|
||
|
// for(Post post : postBeanList){
|
||
|
// List<Department> departments = departmentController.findByPost(post.getId(),QueryFactory.create());
|
||
|
for(Department department:departments){
|
||
|
userController.removeUserFromDepartmentAndPost(userid, department.getId(), "");
|
||
|
}
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
}
|