diff --git a/src/main/java/com/fr/plugin/reset_user_passwd/Controllers/UserController.java b/src/main/java/com/fr/plugin/reset_user_passwd/Controllers/UserController.java new file mode 100644 index 0000000..9669b71 --- /dev/null +++ b/src/main/java/com/fr/plugin/reset_user_passwd/Controllers/UserController.java @@ -0,0 +1,129 @@ +package com.fr.plugin.reset_user_passwd.Controllers; + + +import com.fr.decision.authority.base.constant.type.operation.ManualOperationType; +import com.fr.decision.authority.base.constant.type.operation.OperationType; +import com.fr.decision.authority.data.User; +import com.fr.decision.authorize.impl.AbstractPassport; +import com.fr.decision.authorize.impl.DefaultPassport; +import com.fr.decision.config.FSConfig; +import com.fr.decision.webservice.Response; +import com.fr.decision.webservice.annotation.LoginStatusChecker; +import com.fr.decision.webservice.bean.user.UserAdditionBean; +import com.fr.decision.webservice.bean.user.UserAvailableFilter; +import com.fr.decision.webservice.bean.user.UserBean; +import com.fr.decision.webservice.v10.user.UserService; +import com.fr.log.FineLoggerFactory; +import com.fr.security.encryption.transmission.impl.AESTransmissionEncryption; +import com.fr.stable.StringUtils; +import com.fr.third.springframework.stereotype.Controller; +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 com.fr.web.utils.WebUtils; +import com.fr.decision.webservice.utils.WebServiceUtils; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Controller +@LoginStatusChecker( + required = false //不需要验证是否登录 +) +public class UserController { + + //http://localhost:8075/webroot/decision/user/getAllUsers + @RequestMapping( + value = {"/user/getAllUsers"}, + method = {RequestMethod.GET} + ) + @ResponseBody + public void getAllUsers(HttpServletRequest req, HttpServletResponse res) throws Exception { + List allUsers = this.getAllUserNames(); + PrintWriter printWriter = null; + printWriter = WebUtils.createPrintWriter(res); + Map map = new HashMap<>(); + map.put("allUser", allUsers.toString()); + String page = WebServiceUtils.parseWebPageResourceSafe("com/fr/plugin/reset_user_passwd/allUser.html", map); + printWriter.write(page); + printWriter.flush(); + printWriter.close(); + + //return Response.ok(allUsers); + } + + //http://localhost:8075/webroot/decision/user/resetallpasswd?passwd=123456 + @RequestMapping( + value = {"/user/resetallpasswd"}, + method = {RequestMethod.GET} + ) + @ResponseBody + public Response resetAllUserPasswd(HttpServletRequest req, HttpServletResponse res) throws Exception { + List allUsers = this.getAllUserNames(); + Map passportMap = FSConfig.getInstance().getPassports(); + AbstractPassport passport = passportMap.get(ManualOperationType.KEY); + if(passport == null){ + FSConfig.getInstance().setPassports(passportMap); + } + String newPasswd = WebUtils.getHTTPRequestParameter(req,"passwd"); + if(StringUtils.isNotEmpty(newPasswd)){ + for(String userName : allUsers){ + this.resetUserPasswd(userName,newPasswd); + } + return Response.ok("修改成功!"); + } + else{ + return Response.ok("passwd我为空了."); + } + } + + //http://localhost:8075/webroot/decision/user/resetpasswd?name=admin&passwd=admin + @RequestMapping( + value = {"/user/resetpasswd"}, + method = {RequestMethod.GET} + ) + @ResponseBody + public Response resetUserPasswd(HttpServletRequest req, HttpServletResponse res) throws Exception { + String userName = WebUtils.getHTTPRequestParameter(req,"name"); + String newPasswd = WebUtils.getHTTPRequestParameter(req,"passwd"); + if(StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(newPasswd)){ + String result = this.resetUserPasswd(userName,newPasswd); + return Response.ok(result); + } + else{ + return Response.ok("name或者passwd为空了."); + } + + } + + private String resetUserPasswd(String userName,String newPasswd) throws Exception{ + User user = UserService.getInstance().getUserByUserName(userName); + if(user == null){ + return "用户:"+userName+"不存在"; + } + UserBean userBean = new UserBean(user.getId(),user.getRealName(),user.getUserName(),user.isEnable()); + userBean.setCreationType(user.getCreationType().toInteger()); + userBean.setPassword(AESTransmissionEncryption.getInstance().encrypt(newPasswd)); + UserService.getInstance().resetPassword(userBean); + return "修改成功"; + } + + private List getAllUserNames() throws Exception{ + List allUsers = new ArrayList<>(); + List adminUsers = UserService.getInstance().getAdminUserNameList(); + if(adminUsers.size() > 0){ + allUsers.add(adminUsers.get(0)); + FineLoggerFactory.getLogger().info("admin user:"+adminUsers); + List users = (List)UserService.getInstance().getAllUsers(adminUsers.get(0),1,100,"","userAlias",true,"", UserAvailableFilter.ALL, null).get("items"); + for ( UserAdditionBean userBean : users){ + allUsers.add(userBean.getUsername()); + } + } + return allUsers; + } + +}