From 747b8c0b00eb39b42b401e71d6e8c8733edc04f6 Mon Sep 17 00:00:00 2001 From: xiangwb Date: Sat, 31 Mar 2018 15:27:37 +0800 Subject: [PATCH] merge --- .../xwbing/service/sys/SysUserService.java | 102 +++++++++++------- boot-web/pom.xml | 5 - .../xwbing/controller/sys/SysUserControl.java | 19 ++-- pom.xml | 6 ++ 4 files changed, 77 insertions(+), 55 deletions(-) diff --git a/boot-service/src/main/java/com/xwbing/service/sys/SysUserService.java b/boot-service/src/main/java/com/xwbing/service/sys/SysUserService.java index eb30a1c..5105d8e 100644 --- a/boot-service/src/main/java/com/xwbing/service/sys/SysUserService.java +++ b/boot-service/src/main/java/com/xwbing/service/sys/SysUserService.java @@ -11,6 +11,8 @@ import com.xwbing.exception.BusinessException; import com.xwbing.util.*; import org.apache.commons.collections.CollectionUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.subject.Subject; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -95,23 +97,27 @@ public class SysUserService { if (old == null) { throw new BusinessException("该用户不存在"); } - if (id.equals(CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID))) { - throw new BusinessException("不能删除当前登录用户"); + SysUser currentInfo = getCurrentInfo(); + if (currentInfo != null) { + if (id.equals(currentInfo.getId())) { + throw new BusinessException("不能删除当前登录用户"); + } + if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) { + throw new BusinessException("不能对管理员进行删除操作"); + } + //删除用户 + sysUserRepository.delete(id); + //删除用户角色 + List sysUserRoles = sysUserRoleService.listByUserId(id); + if (CollectionUtils.isNotEmpty(sysUserRoles)) { + sysUserRoleService.removeBatch(sysUserRoles); + } + result.setMessage("删除成功"); + result.setSuccess(true); + return result; + } else { + throw new BusinessException("未能获取当前登录用户信息"); } - if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) { - throw new BusinessException("不能对管理员进行删除操作"); - } - //删除用户 - sysUserRepository.delete(id); - //删除用户角色 - List sysUserRoles = sysUserRoleService.listByUserId(id); - if (CollectionUtils.isNotEmpty(sysUserRoles)) { - sysUserRoleService.removeBatch(sysUserRoles); - } - result.setMessage("删除成功"); - result.setSuccess(true); - return result; - } /** @@ -127,9 +133,6 @@ public class SysUserService { if (old == null) { throw new BusinessException("该用户不存在"); } - if (old.getId().equals(CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID))) { - throw new BusinessException("不能修改当前登录用户"); - } if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) { throw new BusinessException("不能对管理员进行修改操作"); } @@ -160,6 +163,20 @@ public class SysUserService { return sysUserRepository.findOne(id); } + /** + * 获取用户对象 + * + * @return + */ + public SysUser getCurrentInfo() { + Subject subject = SecurityUtils.getSubject(); + if (subject != null && subject.getPrincipal() != null) { + return (SysUser) subject.getPrincipal(); + } else { + return null; + } + } + /** * 列表查询 * @@ -193,28 +210,33 @@ public class SysUserService { if (old == null) { throw new BusinessException("未查询到用户信息"); } - if (CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID).equals(id)) { - throw new BusinessException("不能重置当前登录用户"); + SysUser currentInfo = getCurrentInfo(); + if (currentInfo != null) { + if (id.equals(currentInfo.getId())) { + throw new BusinessException("不能重置当前登录用户"); + } + if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) { + throw new BusinessException("管理员密码不能重置"); + } + //生成密码 + String[] str = PassWordUtil.getUserSecret(null, null); + old.setSalt(str[1]); + old.setPassword(str[2]); + old.setModifiedTime(new Date()); + SysUser save = sysUserRepository.save(old); + if (save == null) { + throw new BusinessException("重置密码失败"); + } + boolean send = sendEmail(old, str[0]); + if (!send) { + throw new BusinessException("发送密码邮件错误"); + } + result.setSuccess(true); + result.setMessage("重置密码成功"); + return result; + } else { + throw new BusinessException("未能获取当前登录用户信息"); } - if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) { - throw new BusinessException("管理员密码不能重置"); - } - //生成密码 - String[] str = PassWordUtil.getUserSecret(null, null); - old.setSalt(str[1]); - old.setPassword(str[2]); - old.setModifiedTime(new Date()); - SysUser save = sysUserRepository.save(old); - if (save == null) { - throw new BusinessException("重置密码失败"); - } - boolean send = sendEmail(old, str[0]); - if (!send) { - throw new BusinessException("发送密码邮件错误"); - } - result.setSuccess(true); - result.setMessage("重置密码成功"); - return result; } /** diff --git a/boot-web/pom.xml b/boot-web/pom.xml index 8fddf7b..86ce348 100644 --- a/boot-web/pom.xml +++ b/boot-web/pom.xml @@ -81,11 +81,6 @@ commons-fileupload ${fileUpLoad.version} - - org.apache.shiro - shiro-spring - 1.4.0 - boot-web diff --git a/boot-web/src/main/java/com/xwbing/controller/sys/SysUserControl.java b/boot-web/src/main/java/com/xwbing/controller/sys/SysUserControl.java index 5d2e044..e73651e 100644 --- a/boot-web/src/main/java/com/xwbing/controller/sys/SysUserControl.java +++ b/boot-web/src/main/java/com/xwbing/controller/sys/SysUserControl.java @@ -2,14 +2,12 @@ package com.xwbing.controller.sys; import com.alibaba.fastjson.JSONObject; import com.xwbing.annotation.LogInfo; -import com.xwbing.constant.CommonConstant; import com.xwbing.constant.CommonEnum; import com.xwbing.domain.entity.sys.*; import com.xwbing.domain.entity.vo.*; import com.xwbing.redis.RedisService; import com.xwbing.service.sys.*; import com.xwbing.shiro.UsernamePasswordCaptchaToken; -import com.xwbing.util.CommonDataUtil; import com.xwbing.util.IpUtil; import com.xwbing.util.JsonResult; import com.xwbing.util.RestMessage; @@ -132,7 +130,7 @@ public class SysUserControl { return JsonResult.toJSONObj(new Object(), "登录成功"); } }else { - return JsonResult.toJSONObj("登录失败"); + return JsonResult.toJSONObj("你认证未通过,请重新登录"); } } @@ -161,17 +159,18 @@ public class SysUserControl { return JsonResult.toJSONObj("没有获取到用户登录信息"); } - @LogInfo("修改密码") - @ApiOperation(value = "修改密码", response = RestMessageVo.class) + @LogInfo("当前用修改密码") + @ApiOperation(value = "当前用户修改密码", response = RestMessageVo.class) @PostMapping("updatePassWord") - public JSONObject updatePassWord(@RequestParam String newPassWord, @RequestParam String oldPassWord, @RequestParam String id) { - if (StringUtils.isEmpty(id)) { - return JsonResult.toJSONObj("主键不能为空"); + public JSONObject updatePassWord(@RequestParam String newPassWord, @RequestParam String oldPassWord) { + SysUser currentInfo = sysUserService.getCurrentInfo(); + if(currentInfo==null){ + return JsonResult.toJSONObj("没有获取到当前登录用户信息"); } if (StringUtils.isEmpty(newPassWord) || StringUtils.isEmpty(oldPassWord)) { return JsonResult.toJSONObj("原密码或新密码不能为空"); } - RestMessage restMessage = sysUserService.updatePassWord(newPassWord, oldPassWord, id); + RestMessage restMessage = sysUserService.updatePassWord(newPassWord, oldPassWord, currentInfo.getId()); return JsonResult.toJSONObj(restMessage); } @@ -190,7 +189,7 @@ public class SysUserControl { @ApiOperation(value = "获取当前登录用户信息") @GetMapping("getLoginUserInfo") public JSONObject getLoginUserInfo() { - SysUser sysUser = sysUserService.getById((String) CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID)); + SysUser sysUser = sysUserService.getCurrentInfo(); if (sysUser == null) { return JsonResult.toJSONObj("未获取到当前登录用户信息"); } diff --git a/pom.xml b/pom.xml index 29275aa..b0df825 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,12 @@ org.springframework spring-web + + + org.apache.shiro + shiro-spring + 1.4.0 + org.springframework.boot