UserInfoController.java 10.5 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2
/*
 * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
3
 * 
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
4 5 6
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
7
 * 
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 * 
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
10 11 12 13 14 15
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
16
 
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
17

MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
18 19 20 21 22 23 24 25 26 27 28
package org.maxkey.web.contorller;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.apache.mybatis.jpa.persistence.JpaPageResults;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
29
import org.maxkey.constants.ConstantsOperateMessage;
M
MaxKey 已提交
30
import org.maxkey.constants.ConstantsPasswordSetType;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
31
import org.maxkey.crypto.ReciprocalUtils;
M
MaxKey 已提交
32 33
import org.maxkey.entity.ExcelImport;
import org.maxkey.entity.UserInfo;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
34
import org.maxkey.persistence.service.UserInfoService;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
import org.maxkey.util.JsonUtils;
import org.maxkey.util.StringUtils;
import org.maxkey.web.WebContext;
import org.maxkey.web.message.Message;
import org.maxkey.web.message.MessageScope;
import org.maxkey.web.message.MessageType;
import org.maxkey.web.message.OperateType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * @author Crystal.Sea
 *
 */
@Controller
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
64
@RequestMapping(value = { "/userinfo" })
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
65 66
public class UserInfoController {
	final static Logger _logger = LoggerFactory.getLogger(UserInfoController.class);
67
	
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
68 69 70 71
	@Autowired
	@Qualifier("userInfoService")
	private UserInfoService userInfoService;

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	
	/**
	 * 查询用户列表
	 * @param user
	 * @return
	 */
	@RequestMapping(value={"/grid"})
	@ResponseBody
	public JpaPageResults<UserInfo> forwardUsersList(@ModelAttribute("userInfo") UserInfo userInfo){
		return userInfoService.queryPageResults(userInfo);
		
	}
	
	@RequestMapping(value={"/forwardAdd"})
	public ModelAndView forwardSelectUserType(){
		ModelAndView modelAndView=new ModelAndView("/userinfo/userAdd");
		//List<UserType> userTypeList=userTypeService.query(null);
		//modelAndView.addObject("userTypeList", userTypeList);
		return modelAndView;
	}
	
	
	
	
	@RequestMapping(value={"/list"})
	public ModelAndView usersList(){
		return new ModelAndView("/userinfo/usersList");
	}
	
	@RequestMapping(value={"/select"})
	public ModelAndView usersSelect(){
		ModelAndView modelAndView= new ModelAndView("/userinfo/userinfoSelect");
		return modelAndView;
	}
	
	/**
	 * 新增
	 * @param userInfo
	 * @param result
	 * @return
	 */
	@RequestMapping(value="/add") 
	public ModelAndView addUsers(@Valid  @ModelAttribute("userInfo")UserInfo userInfo,BindingResult result) {
		_logger.debug(userInfo.toString());
		if(result.hasErrors()){
			// new Message(WebContext.getValidErrorText(),result);
		}
		
M
MaxKey 已提交
120
		userInfo.setId(WebContext.genId());
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
		//userInfo.setNameZHShortSpell(StringUtils.hanYu2Pinyin(userInfo.getDisplayName(), true));
		//userInfo.setNameZHSpell(StringUtils.hanYu2Pinyin(userInfo.getDisplayName(), false));
		if( userInfoService.insert(userInfo)) {
			  new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_SUCCESS),userInfo,MessageType.success,OperateType.add,MessageScope.DB);
		}
		
		 new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_ERROR),MessageType.error);
		return   WebContext.forward("forwardUpdate/"+userInfo.getId());
	}
	
	@RequestMapping(value={"/forwardUpdate/{id}"})
	public ModelAndView forwardUpdateUsers(@PathVariable("id")String id){
		ModelAndView modelAndView=new ModelAndView("/userinfo/userUpdate");
		UserInfo userInfo=userInfoService.get(id);
		if(userInfo.getPicture()!=null){
			WebContext.getSession().setAttribute(userInfo.getId(), userInfo.getPicture());
		}
		
		modelAndView.addObject("model", userInfo);
		return modelAndView;
	}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
142 143 144 145 146 147 148

	/**
	 * 查询用户,根据id
	 * @param id
	 * @return
	 */
	@ResponseBody
149
	@RequestMapping(value="/getUsers/{id}") 
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
150 151 152 153 154 155 156 157 158 159 160 161
	public UserInfo getUserInfo(@PathVariable("id")String id) {
		_logger.debug(id);
		UserInfo userInfo = userInfoService.get(id);
		if(userInfo!=null&&userInfo.getDecipherable()!=null){
			try{
				userInfo.setPassword(ReciprocalUtils.decoder(userInfo.getDecipherable()));
			}catch (Exception e) {
			}
			userInfo.setDecipherable(userInfo.getPassword());
		}
		return userInfo;
	}
162 163
	
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
164 165 166 167 168
    @ResponseBody
    @RequestMapping(value = "/randomPassword")
    public String randomPassword() {
        return userInfoService.randomPassword();
    }
169
	   
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
170 171 172 173 174 175 176
	/**
	 * 修改用户
	 * @param userInfo
	 * @param result
	 * @return
	 */

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	@RequestMapping(value="/update") 
	public ModelAndView updateUsers(@Valid  @ModelAttribute("userInfo")UserInfo userInfo,BindingResult result) {
		_logger.debug(userInfo.toString());
		if(result.hasErrors()){
			// new Message(WebContext.getValidErrorText(),result);
		}
		_logger.info(userInfo.getExtraAttributeName());
		_logger.info(userInfo.getExtraAttributeValue());
		//userInfo.setNameZHShortSpell(StringUtils.hanYu2Pinyin(userInfo.getDisplayName(), true));
		//userInfo.setNameZHSpell(StringUtils.hanYu2Pinyin(userInfo.getDisplayName(), false));
		convertExtraAttribute(userInfo) ;
		_logger.info(userInfo.getExtraAttribute());
		if(userInfoService.update(userInfo)) {
			new Message(WebContext.getI18nValue(ConstantsOperateMessage.UPDATE_SUCCESS),userInfo,MessageType.success,OperateType.add,MessageScope.DB);
			
		}
	    new Message(WebContext.getI18nValue(ConstantsOperateMessage.UPDATE_ERROR),MessageType.error);
		return   WebContext.forward("forwardUpdate/"+userInfo.getId());
	}
	
	
	/**
	 * 批量删除用户
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/batchDelete")  
	public Message batchDeleteUsers(@RequestParam("id")String id) {
		_logger.debug(id);
		if(userInfoService.batchDelete(StringUtils.string2List(id, ","))) {
			return  new Message(WebContext.getI18nValue(ConstantsOperateMessage.DELETE_SUCCESS),MessageType.success);
			
		} else {
			return  new Message(WebContext.getI18nValue(ConstantsOperateMessage.DELETE_ERROR),MessageType.error);
		}
	}
	
	/**
	 * 根据用户id删除用户
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete")  
	public Message deleteUsersById(@RequestParam("id") String id) {
		_logger.debug(id);
		if(userInfoService.batchDelete(StringUtils.string2List(id, ","))) {
			//provisioningPrepare.prepare(userInfo, OPERATEACTION.DELETE_ACTION);
			return  new Message(WebContext.getI18nValue(ConstantsOperateMessage.DELETE_SUCCESS),MessageType.success);
		} else {
			return  new Message(WebContext.getI18nValue(ConstantsOperateMessage.DELETE_ERROR),MessageType.error);
		}
	}
	
	protected void convertExtraAttribute(UserInfo userInfo) {
		if(userInfo.getExtraAttributeValue()!=null){
			String []extraAttributeLabel=userInfo.getExtraAttributeName().split(",");
			String []extraAttributeValue=userInfo.getExtraAttributeValue().split(",");
			Map<String,String> extraAttributeMap=new HashMap<String,String> ();
			for(int i=0;i<extraAttributeLabel.length;i++){
				extraAttributeMap.put(extraAttributeLabel[i], extraAttributeValue[i]);
			}
			String extraAttribute=JsonUtils.object2Json(extraAttributeMap);
			userInfo.setExtraAttribute(extraAttribute);
		}
	}
	
	@RequestMapping(value={"/forwardChangePassword/{id}"})
	public ModelAndView forwardChangePassword(@PathVariable("id")String id){
		ModelAndView modelAndView=new ModelAndView("/userinfo/changePassword");
		UserInfo userInfo=userInfoService.get(id);
		
		modelAndView.addObject("model", userInfo);
		return modelAndView;
	}
	
	@RequestMapping(value={"/forwardChangeUserinfoStatus/{id}"})
	public ModelAndView forwardChangeUserinfoStatus(@PathVariable("id")String id){
		ModelAndView modelAndView=new ModelAndView("/userinfo/changeUserinfoStatus");
		UserInfo userInfo=userInfoService.get(id);
		
		modelAndView.addObject("model", userInfo);
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping(value="/changePassword")  
	public Message changePassword( @ModelAttribute("userInfo")UserInfo userInfo) {
		_logger.debug(userInfo.getId());
M
MaxKey 已提交
268
		userInfo.setPasswordSetType(ConstantsPasswordSetType.PASSWORD_NORMAL);
M
MaxKey 已提交
269
		if(userInfoService.changePassword(userInfo,true)) {
270 271 272 273 274 275 276
			return  new Message(WebContext.getI18nValue(ConstantsOperateMessage.UPDATE_SUCCESS),MessageType.success);
			
		} else {
			return  new Message(WebContext.getI18nValue(ConstantsOperateMessage.UPDATE_ERROR),MessageType.error);
		}
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289
    @RequestMapping(value = "/import")
    public ModelAndView importing(@ModelAttribute("excelImportFile")ExcelImport excelImportFile)  {
        ModelAndView modelAndView=new ModelAndView("/userinfo/usersImport");
        
        if (excelImportFile.getExcelFile() != null && !excelImportFile.getExcelFile().isEmpty() && userInfoService.importing(excelImportFile.getExcelFile())) {
             new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_SUCCESS), null, MessageType.success, OperateType.add, MessageScope.DB);
        }else {
             new Message(WebContext.getI18nValue(ConstantsOperateMessage.INSERT_ERROR), MessageType.error);
        }
        
        return modelAndView;
    }
    
290 291 292 293 294 295 296 297 298 299 300
	@InitBinder
	public void binder(WebDataBinder binder) {
		binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
		    @Override
			public void setAsText(String value) {
		        	if(StringUtils.isNullOrBlank(value)){
		        		setValue(null);
		        	}else{
		        		setValue(value);
		        	}
		    }
Y
yapeng.li 已提交
301

302 303 304 305 306 307
		    
		});
		 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        dateFormat.setLenient(false);  
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
308
}