|
@@ -11,7 +11,6 @@ import com.genersoft.iot.vmp.storager.dao.dto.AdminAccount;
|
|
|
import com.genersoft.iot.vmp.storager.dao.dto.Role;
|
|
|
import com.genersoft.iot.vmp.utils.DateUtil;
|
|
|
import com.genersoft.iot.vmp.utils.StpAdminUtil;
|
|
|
-import com.genersoft.iot.vmp.utils.StpUserUtil;
|
|
|
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
|
|
|
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
@@ -62,40 +61,70 @@ public class UserController {
|
|
|
return WVPResult.fail(ErrorCode.ERROR100);
|
|
|
}
|
|
|
StpAdminUtil.login(adminAccount.getId());
|
|
|
+
|
|
|
return WVPResult.success();
|
|
|
}
|
|
|
|
|
|
+ @GetMapping("/logout")
|
|
|
+ @Operation(summary = "注销账户")
|
|
|
+ public WVPResult<Boolean> logout() {
|
|
|
+ StpAdminUtil.logout();
|
|
|
+ return WVPResult.success(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/default")
|
|
|
+ @SaIgnore
|
|
|
+ @Operation(summary = "获取是否为新平台", description = "获取是否为新平台")
|
|
|
+ public WVPResult<Boolean> isDefault() {
|
|
|
+ int accountSize = userService.getUserCount();
|
|
|
+ if (accountSize > 0) {
|
|
|
+ return WVPResult.success(false);
|
|
|
+ }
|
|
|
+ return WVPResult.success(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注册管理员
|
|
|
+ @PostMapping("/register")
|
|
|
+ @SaIgnore
|
|
|
+ @Operation(summary = "注册管理员账户", description = "注册管理员账户")
|
|
|
+ @Parameter(name = "username", description = "用户名", required = true)
|
|
|
+ @Parameter(name = "password", description = "密码(32位md5加密)", required = true)
|
|
|
+ public WVPResult register(@RequestParam String username, @RequestParam String password) {
|
|
|
+
|
|
|
+ int accountSize = userService.getUserCount();
|
|
|
+ if (accountSize > 0) {
|
|
|
+ logger.warn("在已经拥有一个账户的情况下尝试注册管理员");
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR100);
|
|
|
+ }
|
|
|
+ if (!userService.registerAdmin(username, password)) {
|
|
|
+ logger.warn("无法注册管理员账户");
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR100);
|
|
|
+ }
|
|
|
+ return WVPResult.success();
|
|
|
+ }
|
|
|
|
|
|
@PostMapping("/changePassword")
|
|
|
@Operation(summary = "修改密码")
|
|
|
@Parameter(name = "username", description = "用户名", required = true)
|
|
|
@Parameter(name = "oldpassword", description = "旧密码(已md5加密的密码)", required = true)
|
|
|
@Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
|
|
|
- public void changePassword(@RequestParam String oldPassword, @RequestParam String password){
|
|
|
+ public WVPResult changePassword(@RequestParam String oldPassword, @RequestParam String password) {
|
|
|
logger.info("[用户管理] 修改密码");
|
|
|
// 获取当前登录用户id
|
|
|
- LoginUser loginUser = SecurityUtils.getUserInfo();
|
|
|
- if (loginUser== null) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100);
|
|
|
+ String adminId = StpAdminUtil.getLoginId().toString();
|
|
|
+ AdminAccount adminAccount = userService.getUserById(adminId);
|
|
|
+ String username = adminAccount.getUsername();
|
|
|
+ if (oldPassword != adminAccount.getPassword()) {
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR403, "原密码错误");
|
|
|
}
|
|
|
- String username = loginUser.getUsername();
|
|
|
- String passwordMd5 = loginUser.getPassword();
|
|
|
- logger.info("[用户管理] ");
|
|
|
- LoginUser user = null;
|
|
|
- try {
|
|
|
- user = SecurityUtils.login(username, oldPassword, authenticationManager);
|
|
|
- if (user == null) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100);
|
|
|
- }
|
|
|
-// int userId = SecurityUtils.getUserId();
|
|
|
- logger.info("[用户管理] 修改密码,用户id:" + user.getId() + ",用户名:" + username);
|
|
|
- boolean result = userService.changePassword(user.getId(), DigestUtils.md5DigestAsHex(password.getBytes()));
|
|
|
- if (!result) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100);
|
|
|
- }
|
|
|
- } catch (AuthenticationException e) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
|
|
|
+ String passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
|
|
|
+ //
|
|
|
+ logger.info("[用户管理] 修改密码,用户id:" + adminAccount.getId() + ",用户名:" + username);
|
|
|
+ boolean result = userService.changePassword(adminAccount.getId(), passwordMd5);
|
|
|
+ if (!result) {
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR100, "修改密码失败");
|
|
|
}
|
|
|
+ return WVPResult.success();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -103,17 +132,14 @@ public class UserController {
|
|
|
@Operation(summary = "添加用户")
|
|
|
@Parameter(name = "username", description = "用户名", required = true)
|
|
|
@Parameter(name = "password", description = "密码(未md5加密的密码)", required = true)
|
|
|
- @Parameter(name = "roleId", description = "角色ID", required = true)
|
|
|
- public void add(@RequestParam String username,
|
|
|
- @RequestParam String password,
|
|
|
- @RequestParam Integer roleId) {
|
|
|
+ public void add(@RequestParam String username, @RequestParam String password) {
|
|
|
|
|
|
- if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || roleId == null) {
|
|
|
+ if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password)) {
|
|
|
throw new ControllerException(ErrorCode.ERROR400.getCode(), "参数不可为空");
|
|
|
}
|
|
|
|
|
|
// 获取当前登录用户id
|
|
|
- String accountId = StpUserUtil.getLoginId().toString();
|
|
|
+ String accountId = StpAdminUtil.getLoginId().toString();
|
|
|
logger.info("[用户管理] 添加用户,当前用户id:" + accountId);
|
|
|
|
|
|
AdminAccount adminAccount = new AdminAccount();
|
|
@@ -121,12 +147,7 @@ public class UserController {
|
|
|
adminAccount.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
|
|
|
//新增用户的pushKey的生成规则为md5(时间戳+用户名)
|
|
|
adminAccount.setPushKey(DigestUtils.md5DigestAsHex((System.currentTimeMillis() + password).getBytes()));
|
|
|
- Role role = roleService.getRoleById(roleId);
|
|
|
|
|
|
- if (role == null) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR400.getCode(), "角色不存在");
|
|
|
- }
|
|
|
- adminAccount.setRole(role);
|
|
|
adminAccount.setCreateTime(DateUtil.getNow());
|
|
|
adminAccount.setUpdateTime(DateUtil.getNow());
|
|
|
int addResult = userService.addUser(adminAccount);
|
|
@@ -138,46 +159,30 @@ public class UserController {
|
|
|
@DeleteMapping("/delete")
|
|
|
@Operation(summary = "删除用户")
|
|
|
@Parameter(name = "id", description = "用户Id", required = true)
|
|
|
- public void delete(@RequestParam Integer id){
|
|
|
- try {
|
|
|
- // 获取当前登录用户id
|
|
|
- LoginUser loginUser = SecurityUtils.getUserInfo();
|
|
|
- String _username = loginUser.getUsername();
|
|
|
- String _passwordMd5 = loginUser.getPassword();
|
|
|
- LoginUser _user = SecurityUtils.login(_username, _passwordMd5, authenticationManager);
|
|
|
- int currenRoleId = _user.getRole().getId();
|
|
|
- if (currenRoleId != 1) {
|
|
|
- // 只用角色id为0才可以删除和添加用户
|
|
|
- throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
|
|
|
- }
|
|
|
- int deleteResult = userService.deleteUser(id);
|
|
|
- if (deleteResult <= 0) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100);
|
|
|
- }
|
|
|
- } catch (AuthenticationException e) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
|
|
|
+ public WVPResult<String> delete(@RequestParam Integer id) {
|
|
|
+ // 获取当前登录用户id
|
|
|
+ if (!StpAdminUtil.hasRole("primary")) {
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR403, "无权限操作");
|
|
|
+ }
|
|
|
+ int deleteResult = userService.deleteUser(id);
|
|
|
+ if (deleteResult <= 0) {
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR404, "该用户不存在");
|
|
|
}
|
|
|
+ return WVPResult.success("");
|
|
|
}
|
|
|
|
|
|
@GetMapping("/all")
|
|
|
@Operation(summary = "查询用户")
|
|
|
- public List<AdminAccount> all() {
|
|
|
+ public WVPResult<List<AdminAccount>> all() {
|
|
|
// 获取当前登录用户id
|
|
|
- return userService.getAllUsers();
|
|
|
- }
|
|
|
-
|
|
|
- public void register(String username, String password) {
|
|
|
- AdminAccount adminAccount = new AdminAccount();
|
|
|
- adminAccount.setUsername(username);
|
|
|
- adminAccount.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
|
|
|
- adminAccount.setCreateTime(DateUtil.getNow());
|
|
|
- adminAccount.setUpdateTime(DateUtil.getNow());
|
|
|
- int addResult = userService.addUser(adminAccount);
|
|
|
- if (addResult <= 0) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100);
|
|
|
+ if (!StpAdminUtil.hasRole("primary")) {
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR403, "无权限操作");
|
|
|
}
|
|
|
+ List<AdminAccount> adminAccount = userService.getAllUsers();
|
|
|
+ return WVPResult.success(adminAccount);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 分页查询用户
|
|
|
*
|
|
@@ -198,9 +203,9 @@ public class UserController {
|
|
|
@Parameter(name = "pushKey", description = "新的pushKey", required = true)
|
|
|
public WVPResult changePushKey(@RequestParam String pushKey) {
|
|
|
// 获取当前登录用户id
|
|
|
- String accountId = StpUserUtil.getLoginId().toString();
|
|
|
+ String accountId = StpAdminUtil.getLoginId().toString();
|
|
|
logger.info("[用户管理] 修改pushKey,当前用户id:" + accountId);
|
|
|
- int resetPushKeyResult = userService.changePushKey(Integer.parseInt(accountId), pushKey);
|
|
|
+ int resetPushKeyResult = userService.changePushKey(accountId, pushKey);
|
|
|
if (resetPushKeyResult <= 0) {
|
|
|
return WVPResult.fail(ErrorCode.ERROR100);
|
|
|
}
|
|
@@ -209,31 +214,27 @@ public class UserController {
|
|
|
|
|
|
@PostMapping("/changePasswordForAdmin")
|
|
|
@Operation(summary = "管理员修改普通用户密码")
|
|
|
- @Parameter(name = "adminId", description = "管理员id", required = true)
|
|
|
@Parameter(name = "userId", description = "用户id", required = true)
|
|
|
@Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
|
|
|
- public void changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) {
|
|
|
+ public WVPResult<String> changePasswordForAdmin(@RequestParam String userId, @RequestParam String password) {
|
|
|
+ // 获取当前登录用户id
|
|
|
+
|
|
|
// 获取当前登录用户id
|
|
|
- try {
|
|
|
- // 获取当前登录用户id
|
|
|
- LoginUser loginUser = SecurityUtils.getUserInfo();
|
|
|
- String _username = loginUser.getUsername();
|
|
|
- String _passwordMd5 = loginUser.getPassword();
|
|
|
- LoginUser _user = SecurityUtils.login(_username, _passwordMd5, authenticationManager);
|
|
|
- if (_user == null) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100);
|
|
|
- }
|
|
|
- Role role = _user.getRole();
|
|
|
- if (role != null && role.getId() == 1) {
|
|
|
- boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
|
|
|
- if (!result) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- } catch (AuthenticationException e) {
|
|
|
- throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
|
|
|
+ String adminId = StpAdminUtil.getLoginId().toString();
|
|
|
+ AdminAccount adminAccount = userService.getUserById(adminId);
|
|
|
+ AdminAccount changeAdminAccount = userService.getUserById(userId);
|
|
|
+ String _passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
|
|
|
+ boolean result = false;
|
|
|
+ if (changeAdminAccount == null) {
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR404, "要更改的用户不存在");
|
|
|
}
|
|
|
+ if (StpAdminUtil.hasRole("primary")) {
|
|
|
+ result = userService.changePassword(userId, _passwordMd5);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!result) {
|
|
|
+ return WVPResult.fail(ErrorCode.ERROR100, "修改用户密码失败");
|
|
|
+ }
|
|
|
+ return WVPResult.success();
|
|
|
}
|
|
|
}
|