支持全局异常和统一返回结果,未完待续
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.genersoft.iot.vmp.vmanager.user;
|
||||
|
||||
import com.genersoft.iot.vmp.conf.exception.ControllerException;
|
||||
import com.genersoft.iot.vmp.conf.security.SecurityUtils;
|
||||
import com.genersoft.iot.vmp.service.IRoleService;
|
||||
import com.genersoft.iot.vmp.storager.dao.dto.Role;
|
||||
import com.genersoft.iot.vmp.utils.DateUtil;
|
||||
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
|
||||
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -29,16 +31,13 @@ public class RoleController {
|
||||
@Operation(summary = "添加角色")
|
||||
@Parameter(name = "name", description = "角色名", required = true)
|
||||
@Parameter(name = "authority", description = "权限(自行定义内容,目前未使用)", required = true)
|
||||
public ResponseEntity<WVPResult<Integer>> add(@RequestParam String name,
|
||||
public void add(@RequestParam String name,
|
||||
@RequestParam(required = false) String authority){
|
||||
WVPResult<Integer> result = new WVPResult<>();
|
||||
// 获取当前登录用户id
|
||||
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
|
||||
if (currenRoleId != 1) {
|
||||
// 只用角色id为1才可以删除和添加用户
|
||||
result.setCode(-1);
|
||||
result.setMsg("用户无权限");
|
||||
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
|
||||
throw new ControllerException(ErrorCode.ERROR403);
|
||||
}
|
||||
|
||||
Role role = new Role();
|
||||
@@ -48,42 +47,33 @@ public class RoleController {
|
||||
role.setUpdateTime(DateUtil.getNow());
|
||||
|
||||
int addResult = roleService.add(role);
|
||||
|
||||
result.setCode(addResult > 0 ? 0 : -1);
|
||||
result.setMsg(addResult > 0 ? "success" : "fail");
|
||||
result.setData(addResult);
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
if (addResult <= 0) {
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete")
|
||||
@Operation(summary = "删除角色")
|
||||
@Parameter(name = "id", description = "用户Id", required = true)
|
||||
public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){
|
||||
public void delete(@RequestParam Integer id){
|
||||
// 获取当前登录用户id
|
||||
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
|
||||
WVPResult<String> result = new WVPResult<>();
|
||||
if (currenRoleId != 1) {
|
||||
// 只用角色id为0才可以删除和添加用户
|
||||
result.setCode(-1);
|
||||
result.setMsg("用户无权限");
|
||||
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
|
||||
throw new ControllerException(ErrorCode.ERROR403);
|
||||
}
|
||||
int deleteResult = roleService.delete(id);
|
||||
|
||||
result.setCode(deleteResult>0? 0 : -1);
|
||||
result.setMsg(deleteResult>0? "success" : "fail");
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
if (deleteResult <= 0) {
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@Operation(summary = "查询角色")
|
||||
public ResponseEntity<WVPResult<List<Role>>> all(){
|
||||
public List<Role> all(){
|
||||
// 获取当前登录用户id
|
||||
List<Role> allRoles = roleService.getAll();
|
||||
WVPResult<List<Role>> result = new WVPResult<>();
|
||||
result.setCode(0);
|
||||
result.setMsg("success");
|
||||
result.setData(allRoles);
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
return roleService.getAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.genersoft.iot.vmp.vmanager.user;
|
||||
|
||||
import com.genersoft.iot.vmp.conf.exception.ControllerException;
|
||||
import com.genersoft.iot.vmp.conf.security.SecurityUtils;
|
||||
import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
|
||||
import com.genersoft.iot.vmp.service.IRoleService;
|
||||
@@ -7,6 +8,7 @@ import com.genersoft.iot.vmp.service.IUserService;
|
||||
import com.genersoft.iot.vmp.storager.dao.dto.Role;
|
||||
import com.genersoft.iot.vmp.storager.dao.dto.User;
|
||||
import com.genersoft.iot.vmp.utils.DateUtil;
|
||||
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
|
||||
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
|
||||
@@ -18,6 +20,7 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -43,25 +46,17 @@ public class UserController {
|
||||
@Operation(summary = "登录")
|
||||
@Parameter(name = "username", description = "用户名", required = true)
|
||||
@Parameter(name = "password", description = "密码(32位md5加密)", required = true)
|
||||
public WVPResult<LoginUser> login(@RequestParam String username, @RequestParam String password){
|
||||
public LoginUser login(@RequestParam String username, @RequestParam String password){
|
||||
LoginUser user = null;
|
||||
WVPResult<LoginUser> result = new WVPResult<>();
|
||||
try {
|
||||
user = SecurityUtils.login(username, password, authenticationManager);
|
||||
} catch (AuthenticationException e) {
|
||||
e.printStackTrace();
|
||||
result.setCode(-1);
|
||||
result.setMsg("fail");
|
||||
throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
|
||||
}
|
||||
if (user != null) {
|
||||
result.setCode(0);
|
||||
result.setMsg("success");
|
||||
result.setData(user);
|
||||
}else {
|
||||
result.setCode(-1);
|
||||
result.setMsg("fail");
|
||||
if (user == null) {
|
||||
throw new ControllerException(ErrorCode.ERROR100.getCode(), "用户名或密码错误");
|
||||
}
|
||||
return result;
|
||||
return user;
|
||||
}
|
||||
|
||||
@PostMapping("/changePassword")
|
||||
@@ -69,27 +64,27 @@ public class UserController {
|
||||
@Parameter(name = "username", description = "用户名", required = true)
|
||||
@Parameter(name = "oldpassword", description = "旧密码(已md5加密的密码)", required = true)
|
||||
@Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
|
||||
public String changePassword(@RequestParam String oldPassword, @RequestParam String password){
|
||||
public void changePassword(@RequestParam String oldPassword, @RequestParam String password){
|
||||
// 获取当前登录用户id
|
||||
LoginUser userInfo = SecurityUtils.getUserInfo();
|
||||
if (userInfo== null) {
|
||||
return "fail";
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
String username = userInfo.getUsername();
|
||||
LoginUser user = null;
|
||||
try {
|
||||
user = SecurityUtils.login(username, oldPassword, authenticationManager);
|
||||
if (user != null) {
|
||||
int userId = SecurityUtils.getUserId();
|
||||
boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
|
||||
if (result) {
|
||||
return "success";
|
||||
}
|
||||
if (user == null) {
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
int userId = SecurityUtils.getUserId();
|
||||
boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
|
||||
if (!result) {
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
} catch (AuthenticationException e) {
|
||||
e.printStackTrace();
|
||||
throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
|
||||
}
|
||||
return "fail";
|
||||
}
|
||||
|
||||
|
||||
@@ -98,22 +93,17 @@ public class UserController {
|
||||
@Parameter(name = "username", description = "用户名", required = true)
|
||||
@Parameter(name = "password", description = "密码(未md5加密的密码)", required = true)
|
||||
@Parameter(name = "roleId", description = "角色ID", required = true)
|
||||
public ResponseEntity<WVPResult<Integer>> add(@RequestParam String username,
|
||||
public void add(@RequestParam String username,
|
||||
@RequestParam String password,
|
||||
@RequestParam Integer roleId){
|
||||
WVPResult<Integer> result = new WVPResult<>();
|
||||
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || roleId == null) {
|
||||
result.setCode(-1);
|
||||
result.setMsg("参数不可为空");
|
||||
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
|
||||
if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || roleId == null) {
|
||||
throw new ControllerException(ErrorCode.ERROR400.getCode(), "参数不可为空");
|
||||
}
|
||||
// 获取当前登录用户id
|
||||
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
|
||||
if (currenRoleId != 1) {
|
||||
// 只用角色id为1才可以删除和添加用户
|
||||
result.setCode(-1);
|
||||
result.setMsg("用户无权限");
|
||||
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
|
||||
throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
|
||||
}
|
||||
User user = new User();
|
||||
user.setUsername(username);
|
||||
@@ -123,53 +113,38 @@ public class UserController {
|
||||
Role role = roleService.getRoleById(roleId);
|
||||
|
||||
if (role == null) {
|
||||
result.setCode(-1);
|
||||
result.setMsg("roleId is not found");
|
||||
// 角色不存在
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
throw new ControllerException(ErrorCode.ERROR400.getCode(), "角色不存在");
|
||||
}
|
||||
user.setRole(role);
|
||||
user.setCreateTime(DateUtil.getNow());
|
||||
user.setUpdateTime(DateUtil.getNow());
|
||||
int addResult = userService.addUser(user);
|
||||
|
||||
|
||||
result.setCode(addResult > 0 ? 0 : -1);
|
||||
result.setMsg(addResult > 0 ? "success" : "fail");
|
||||
result.setData(addResult);
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
if (addResult <= 0) {
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/删除用户")
|
||||
@Operation(summary = "停止视频回放")
|
||||
@Parameter(name = "id", description = "用户Id", required = true)
|
||||
public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){
|
||||
public void delete(@RequestParam Integer id){
|
||||
// 获取当前登录用户id
|
||||
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
|
||||
WVPResult<String> result = new WVPResult<>();
|
||||
if (currenRoleId != 1) {
|
||||
// 只用角色id为0才可以删除和添加用户
|
||||
result.setCode(-1);
|
||||
result.setMsg("用户无权限");
|
||||
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
|
||||
throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
|
||||
}
|
||||
int deleteResult = userService.deleteUser(id);
|
||||
|
||||
result.setCode(deleteResult>0? 0 : -1);
|
||||
result.setMsg(deleteResult>0? "success" : "fail");
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
if (deleteResult <= 0) {
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
@Operation(summary = "查询用户")
|
||||
public ResponseEntity<WVPResult<List<User>>> all(){
|
||||
public List<User> all(){
|
||||
// 获取当前登录用户id
|
||||
List<User> allUsers = userService.getAllUsers();
|
||||
WVPResult<List<User>> result = new WVPResult<>();
|
||||
result.setCode(0);
|
||||
result.setMsg("success");
|
||||
result.setData(allUsers);
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
return userService.getAllUsers();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,21 +166,18 @@ public class UserController {
|
||||
@Operation(summary = "修改pushkey")
|
||||
@Parameter(name = "userId", description = "用户Id", required = true)
|
||||
@Parameter(name = "pushKey", description = "新的pushKey", required = true)
|
||||
public ResponseEntity<WVPResult<String>> changePushKey(@RequestParam Integer userId,@RequestParam String pushKey) {
|
||||
public void changePushKey(@RequestParam Integer userId,@RequestParam String pushKey) {
|
||||
// 获取当前登录用户id
|
||||
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
|
||||
WVPResult<String> result = new WVPResult<>();
|
||||
if (currenRoleId != 1) {
|
||||
// 只用角色id为0才可以删除和添加用户
|
||||
result.setCode(-1);
|
||||
result.setMsg("用户无权限");
|
||||
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
|
||||
throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
|
||||
}
|
||||
int resetPushKeyResult = userService.changePushKey(userId,pushKey);
|
||||
|
||||
result.setCode(resetPushKeyResult > 0 ? 0 : -1);
|
||||
result.setMsg(resetPushKeyResult > 0 ? "success" : "fail");
|
||||
return new ResponseEntity<>(result, HttpStatus.OK);
|
||||
if (resetPushKeyResult <= 0) {
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/changePasswordForAdmin")
|
||||
@@ -213,20 +185,18 @@ public class UserController {
|
||||
@Parameter(name = "adminId", description = "管理员id", required = true)
|
||||
@Parameter(name = "userId", description = "用户id", required = true)
|
||||
@Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
|
||||
public String changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) {
|
||||
public void changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) {
|
||||
// 获取当前登录用户id
|
||||
LoginUser userInfo = SecurityUtils.getUserInfo();
|
||||
if (userInfo == null) {
|
||||
return "fail";
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
Role role = userInfo.getRole();
|
||||
if (role != null && role.getId() == 1) {
|
||||
boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
|
||||
if (result) {
|
||||
return "success";
|
||||
if (!result) {
|
||||
throw new ControllerException(ErrorCode.ERROR100);
|
||||
}
|
||||
}
|
||||
|
||||
return "fail";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user