增加用户管理功能。管理员可以添加删除用户、修改用户密码、重置pushkey

This commit is contained in:
jiang
2022-07-18 17:09:35 +08:00
parent fc89b7b517
commit c6fbd03276
10 changed files with 617 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
package com.genersoft.iot.vmp.service;
import com.genersoft.iot.vmp.storager.dao.dto.User;
import com.github.pagehelper.PageInfo;
import java.util.List;
@@ -21,4 +22,8 @@ public interface IUserService {
int updateUsers(User user);
boolean checkPushAuthority(String callId, String sign);
PageInfo<User> getUsers(int page, int count);
int resetPushKey(int id);
}

View File

@@ -3,6 +3,8 @@ package com.genersoft.iot.vmp.service.impl;
import com.genersoft.iot.vmp.service.IUserService;
import com.genersoft.iot.vmp.storager.dao.UserMapper;
import com.genersoft.iot.vmp.storager.dao.dto.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@@ -11,7 +13,7 @@ import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@@ -64,4 +66,16 @@ public class UserServiceImpl implements IUserService {
return userMapper.checkPushAuthorityByCallIdAndSign(callId, sign).size() > 0;
}
}
@Override
public PageInfo<User> getUsers(int page, int count) {
PageHelper.startPage(page, count);
List<User> users = userMapper.getUsers();
return new PageInfo<>(users);
}
@Override
public int resetPushKey(int id) {
return userMapper.resetPushKey(id);
}
}

View File

@@ -55,4 +55,11 @@ public interface UserMapper {
@Select("select * from user where md5(pushKey) = '${sign}'")
List<User> checkPushAuthorityByCallId(String sign);
@Select("select u.idu.username,u.pushKey,u.roleId, r.id as roleID, r.name as roleName, r.authority as roleAuthority , r.createTime as roleCreateTime , r.updateTime as roleUpdateTime FROM user u join user_role r on u.roleId=r.id")
@ResultMap(value="roleMap")
List<User> getUsers();
@Delete("update user set pushKey=MD5(NOW()+#{id}) where id=#{id}")
int resetPushKey(int id);
}

View File

@@ -8,6 +8,7 @@ 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.WVPResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
@@ -177,4 +178,67 @@ public class UserController {
result.setData(allUsers);
return new ResponseEntity<>(result, HttpStatus.OK);
}
/**
* 分页查询用户
*
* @param page 当前页
* @param count 每页查询数量
* @return 分页用户列表
*/
@ApiOperation("分页查询用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "当前页", required = true, dataTypeClass = Integer.class),
@ApiImplicitParam(name = "count", value = "每页查询数量", required = true, dataTypeClass = Integer.class),
})
@GetMapping("/users")
public PageInfo<User> users(int page, int count) {
return userService.getUsers(page, count);
}
@ApiOperation("重置pushkey")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", required = true, value = "用户Id", dataTypeClass = Integer.class),
})
@RequestMapping("/resetPushKey")
public ResponseEntity<WVPResult<String>> resetPushKey(@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);
}
int resetPushKeyResult = userService.resetPushKey(id);
result.setCode(resetPushKeyResult > 0 ? 0 : -1);
result.setMsg(resetPushKeyResult > 0 ? "success" : "fail");
return new ResponseEntity<>(result, HttpStatus.OK);
}
@ApiOperation("管理员修改普通用户密码")
@ApiImplicitParams({
@ApiImplicitParam(name = "adminId", required = true, value = "管理员id", dataTypeClass = String.class),
@ApiImplicitParam(name = "userId", required = true, value = "用户id", dataTypeClass = String.class),
@ApiImplicitParam(name = "password", required = true, value = "新密码未md5加密的密码", dataTypeClass = String.class),
})
@PostMapping("/changePasswordForAdmin")
public String changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) {
// 获取当前登录用户id
LoginUser userInfo = SecurityUtils.getUserInfo();
if (userInfo == null) {
return "fail";
}
Role role = userInfo.getRole();
if (role != null && role.getId() == 1) {
boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
if (result) {
return "success";
}
}
return "fail";
}
}