Commit 82598c64 by fengshuonan

更改结构

parent db383a16
...@@ -27,7 +27,6 @@ import cn.stylefeng.guns.core.common.constant.state.ManagerStatus; ...@@ -27,7 +27,6 @@ import cn.stylefeng.guns.core.common.constant.state.ManagerStatus;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum; import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder; import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit; import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.entity.User; import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.factory.UserFactory; import cn.stylefeng.guns.modular.system.factory.UserFactory;
import cn.stylefeng.guns.modular.system.model.UserDto; import cn.stylefeng.guns.modular.system.model.UserDto;
...@@ -167,7 +166,8 @@ public class UserMgrController extends BaseController { ...@@ -167,7 +166,8 @@ public class UserMgrController extends BaseController {
if (ToolUtil.isEmpty(userId)) { if (ToolUtil.isEmpty(userId)) {
throw new RequestEmptyException(); throw new RequestEmptyException();
} }
assertAuth(userId);
this.userService.assertAuth(userId);
User user = this.userService.selectById(userId); User user = this.userService.selectById(userId);
Map<String, Object> map = UserFactory.removeUnSafeFields(user); Map<String, Object> map = UserFactory.removeUnSafeFields(user);
...@@ -191,17 +191,8 @@ public class UserMgrController extends BaseController { ...@@ -191,17 +191,8 @@ public class UserMgrController extends BaseController {
if (ToolUtil.isOneEmpty(oldPassword, newPassword)) { if (ToolUtil.isOneEmpty(oldPassword, newPassword)) {
throw new RequestEmptyException(); throw new RequestEmptyException();
} }
Long userId = ShiroKit.getUserNotNull().getId(); this.userService.changePwd(oldPassword,newPassword);
User user = userService.selectById(userId); return SUCCESS_TIP;
String oldMd5 = ShiroKit.md5(oldPassword, user.getSalt());
if (user.getPassword().equals(oldMd5)) {
String newMd5 = ShiroKit.md5(newPassword, user.getSalt());
user.setPassword(newMd5);
this.userService.updateById(user);
return SUCCESS_TIP;
} else {
throw new ServiceException(BizExceptionEnum.OLD_PWD_NOT_RIGHT);
}
} }
/** /**
...@@ -251,18 +242,7 @@ public class UserMgrController extends BaseController { ...@@ -251,18 +242,7 @@ public class UserMgrController extends BaseController {
if (result.hasErrors()) { if (result.hasErrors()) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
} }
this.userService.addUser(user);
// 判断账号是否重复
User theUser = userService.getByAccount(user.getAccount());
if (theUser != null) {
throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG);
}
// 完善账号信息
String salt = ShiroKit.getRandomSalt(5);
String password = ShiroKit.md5(user.getPassword(), salt);
this.userService.insert(UserFactory.createUser(user, password, salt));
return SUCCESS_TIP; return SUCCESS_TIP;
} }
...@@ -279,22 +259,8 @@ public class UserMgrController extends BaseController { ...@@ -279,22 +259,8 @@ public class UserMgrController extends BaseController {
if (result.hasErrors()) { if (result.hasErrors()) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
} }
this.userService.editUser(user);
User oldUser = userService.selectById(user.getUserId()); return SUCCESS_TIP;
if (ShiroKit.hasRole(Const.ADMIN_NAME)) {
this.userService.updateById(UserFactory.editUser(user, oldUser));
return SUCCESS_TIP;
} else {
assertAuth(user.getUserId());
ShiroUser shiroUser = ShiroKit.getUserNotNull();
if (shiroUser.getId().equals(user.getUserId())) {
this.userService.updateById(UserFactory.editUser(user, oldUser));
return SUCCESS_TIP;
} else {
throw new ServiceException(BizExceptionEnum.NO_PERMITION);
}
}
} }
/** /**
...@@ -311,12 +277,7 @@ public class UserMgrController extends BaseController { ...@@ -311,12 +277,7 @@ public class UserMgrController extends BaseController {
if (ToolUtil.isEmpty(userId)) { if (ToolUtil.isEmpty(userId)) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
} }
//不能删除超级管理员 this.userService.deleteUser(userId);
if (userId.equals(Const.ADMIN_ID)) {
throw new ServiceException(BizExceptionEnum.CANT_DELETE_ADMIN);
}
assertAuth(userId);
this.userService.setStatus(userId, ManagerStatus.DELETED.getCode());
return SUCCESS_TIP; return SUCCESS_TIP;
} }
...@@ -332,7 +293,7 @@ public class UserMgrController extends BaseController { ...@@ -332,7 +293,7 @@ public class UserMgrController extends BaseController {
if (ToolUtil.isEmpty(userId)) { if (ToolUtil.isEmpty(userId)) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
} }
assertAuth(userId); this.userService.assertAuth(userId);
return this.userService.selectById(userId); return this.userService.selectById(userId);
} }
...@@ -350,7 +311,7 @@ public class UserMgrController extends BaseController { ...@@ -350,7 +311,7 @@ public class UserMgrController extends BaseController {
if (ToolUtil.isEmpty(userId)) { if (ToolUtil.isEmpty(userId)) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
} }
assertAuth(userId); this.userService.assertAuth(userId);
User user = this.userService.selectById(userId); User user = this.userService.selectById(userId);
user.setSalt(ShiroKit.getRandomSalt(5)); user.setSalt(ShiroKit.getRandomSalt(5));
user.setPassword(ShiroKit.md5(Const.DEFAULT_PWD, user.getSalt())); user.setPassword(ShiroKit.md5(Const.DEFAULT_PWD, user.getSalt()));
...@@ -376,7 +337,7 @@ public class UserMgrController extends BaseController { ...@@ -376,7 +337,7 @@ public class UserMgrController extends BaseController {
if (userId.equals(Const.ADMIN_ID)) { if (userId.equals(Const.ADMIN_ID)) {
throw new ServiceException(BizExceptionEnum.CANT_FREEZE_ADMIN); throw new ServiceException(BizExceptionEnum.CANT_FREEZE_ADMIN);
} }
assertAuth(userId); this.userService.assertAuth(userId);
this.userService.setStatus(userId, ManagerStatus.FREEZED.getCode()); this.userService.setStatus(userId, ManagerStatus.FREEZED.getCode());
return SUCCESS_TIP; return SUCCESS_TIP;
} }
...@@ -395,7 +356,7 @@ public class UserMgrController extends BaseController { ...@@ -395,7 +356,7 @@ public class UserMgrController extends BaseController {
if (ToolUtil.isEmpty(userId)) { if (ToolUtil.isEmpty(userId)) {
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
} }
assertAuth(userId); this.userService.assertAuth(userId);
this.userService.setStatus(userId, ManagerStatus.OK.getCode()); this.userService.setStatus(userId, ManagerStatus.OK.getCode());
return SUCCESS_TIP; return SUCCESS_TIP;
} }
...@@ -418,7 +379,7 @@ public class UserMgrController extends BaseController { ...@@ -418,7 +379,7 @@ public class UserMgrController extends BaseController {
if (userId.equals(Const.ADMIN_ID)) { if (userId.equals(Const.ADMIN_ID)) {
throw new ServiceException(BizExceptionEnum.CANT_CHANGE_ADMIN); throw new ServiceException(BizExceptionEnum.CANT_CHANGE_ADMIN);
} }
assertAuth(userId); this.userService.assertAuth(userId);
this.userService.setRoles(userId, roleIds); this.userService.setRoles(userId, roleIds);
return SUCCESS_TIP; return SUCCESS_TIP;
} }
...@@ -442,25 +403,4 @@ public class UserMgrController extends BaseController { ...@@ -442,25 +403,4 @@ public class UserMgrController extends BaseController {
} }
return pictureName; return pictureName;
} }
/**
* 判断当前登录的用户是否有操作这个用户的权限
*
* @author fengshuonan
* @Date 2018/12/24 22:44
*/
private void assertAuth(Long userId) {
if (ShiroKit.isAdmin()) {
return;
}
List<Long> deptDataScope = ShiroKit.getDeptDataScope();
User user = this.userService.selectById(userId);
Long deptId = user.getDeptId();
if (deptDataScope.contains(deptId)) {
return;
} else {
throw new ServiceException(BizExceptionEnum.NO_PERMITION);
}
}
} }
package cn.stylefeng.guns.modular.system.service; package cn.stylefeng.guns.modular.system.service;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.constant.state.ManagerStatus;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.node.MenuNode; import cn.stylefeng.guns.core.common.node.MenuNode;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.util.ApiMenuFilter; import cn.stylefeng.guns.core.util.ApiMenuFilter;
import cn.stylefeng.guns.modular.system.entity.User; import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.factory.UserFactory;
import cn.stylefeng.guns.modular.system.mapper.UserMapper; import cn.stylefeng.guns.modular.system.mapper.UserMapper;
import cn.stylefeng.guns.modular.system.model.UserDto;
import cn.stylefeng.roses.core.datascope.DataScope; import cn.stylefeng.roses.core.datascope.DataScope;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -28,6 +36,65 @@ public class UserService extends ServiceImpl<UserMapper, User> { ...@@ -28,6 +36,65 @@ public class UserService extends ServiceImpl<UserMapper, User> {
private MenuService menuService; private MenuService menuService;
/** /**
* 添加用戶
*
* @author fengshuonan
* @Date 2018/12/24 22:51
*/
public void addUser(UserDto user){
// 判断账号是否重复
User theUser = this.getByAccount(user.getAccount());
if (theUser != null) {
throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG);
}
// 完善账号信息
String salt = ShiroKit.getRandomSalt(5);
String password = ShiroKit.md5(user.getPassword(), salt);
this.insert(UserFactory.createUser(user, password, salt));
}
/**
* 修改用户
*
* @author fengshuonan
* @Date 2018/12/24 22:53
*/
public void editUser(UserDto user){
User oldUser = this.selectById(user.getUserId());
if (ShiroKit.hasRole(Const.ADMIN_NAME)) {
this.updateById(UserFactory.editUser(user, oldUser));
} else {
this.assertAuth(user.getUserId());
ShiroUser shiroUser = ShiroKit.getUserNotNull();
if (shiroUser.getId().equals(user.getUserId())) {
this.updateById(UserFactory.editUser(user, oldUser));
} else {
throw new ServiceException(BizExceptionEnum.NO_PERMITION);
}
}
}
/**
* 删除用户
*
* @author fengshuonan
* @Date 2018/12/24 22:54
*/
public void deleteUser(Long userId){
//不能删除超级管理员
if (userId.equals(Const.ADMIN_ID)) {
throw new ServiceException(BizExceptionEnum.CANT_DELETE_ADMIN);
}
this.assertAuth(userId);
this.setStatus(userId, ManagerStatus.DELETED.getCode());
}
/**
* 修改用户状态 * 修改用户状态
* *
* @author fengshuonan * @author fengshuonan
...@@ -43,8 +110,19 @@ public class UserService extends ServiceImpl<UserMapper, User> { ...@@ -43,8 +110,19 @@ public class UserService extends ServiceImpl<UserMapper, User> {
* @author fengshuonan * @author fengshuonan
* @Date 2018/12/24 22:45 * @Date 2018/12/24 22:45
*/ */
public int changePwd(Long userId, String pwd) { public void changePwd(String oldPassword,String newPassword) {
return this.baseMapper.changePwd(userId, pwd); Long userId = ShiroKit.getUserNotNull().getId();
User user = this.selectById(userId);
String oldMd5 = ShiroKit.md5(oldPassword, user.getSalt());
if (user.getPassword().equals(oldMd5)) {
String newMd5 = ShiroKit.md5(newPassword, user.getSalt());
user.setPassword(newMd5);
this.updateById(user);
} else {
throw new ServiceException(BizExceptionEnum.OLD_PWD_NOT_RIGHT);
}
} }
/** /**
...@@ -94,4 +172,25 @@ public class UserService extends ServiceImpl<UserMapper, User> { ...@@ -94,4 +172,25 @@ public class UserService extends ServiceImpl<UserMapper, User> {
} }
/**
* 判断当前登录的用户是否有操作这个用户的权限
*
* @author fengshuonan
* @Date 2018/12/24 22:44
*/
public void assertAuth(Long userId) {
if (ShiroKit.isAdmin()) {
return;
}
List<Long> deptDataScope = ShiroKit.getDeptDataScope();
User user = this.selectById(userId);
Long deptId = user.getDeptId();
if (deptDataScope.contains(deptId)) {
return;
} else {
throw new ServiceException(BizExceptionEnum.NO_PERMITION);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment