Procházet zdrojové kódy

fix:
1. 用户新增接口
2. 删除用户接口
3. 修改推流key接口
4. 管理员账户更改普通用户接口

kindring před 2 roky
rodič
revize
7ad48a5972

+ 2 - 0
src/main/java/com/genersoft/iot/vmp/conf/security/SecurityUtils.java

@@ -57,8 +57,10 @@ public class SecurityUtils {
 //                LoginUser user = (LoginUser) authentication.getPrincipal();
 
                 String username = (String) principal;
+                String password = (String) authentication.getCredentials();
                 User user = new User();
                 user.setUsername(username);
+                user.setPassword(password);
                 LoginUser loginUser = new LoginUser(user, LocalDateTime.now());
                 return loginUser;
             }

+ 87 - 52
src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java

@@ -111,31 +111,40 @@ public class UserController {
     public void add(@RequestParam String username,
                                                  @RequestParam String password,
                                                  @RequestParam Integer roleId){
-        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才可以删除和添加用户
-            throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
-        }
-        User user = new User();
-        user.setUsername(username);
-        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
-        //新增用户的pushKey的生成规则为md5(时间戳+用户名)
-        user.setPushKey(DigestUtils.md5DigestAsHex((System.currentTimeMillis()+password).getBytes()));
-        Role role = roleService.getRoleById(roleId);
-
-        if (role == null) {
-            throw new ControllerException(ErrorCode.ERROR400.getCode(), "角色不存在");
-        }
-        user.setRole(role);
-        user.setCreateTime(DateUtil.getNow());
-        user.setUpdateTime(DateUtil.getNow());
-        int addResult = userService.addUser(user);
-        if (addResult <= 0) {
-            throw new ControllerException(ErrorCode.ERROR100);
+        try {
+            if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || roleId == null) {
+                throw new ControllerException(ErrorCode.ERROR400.getCode(), "参数不可为空");
+            }
+            LoginUser loginUser = SecurityUtils.getUserInfo();
+            String _username = loginUser.getUsername();
+            String _passwordMd5 = loginUser.getPassword();
+            LoginUser _user = SecurityUtils.login(_username, _passwordMd5, authenticationManager);
+            // 获取当前登录用户id
+            int currenRoleId = _user.getRole().getId();
+            logger.info("[用户管理] 添加用户,当前用户角色id:" + currenRoleId);
+            if (currenRoleId != 1) {
+                // 只用角色id为1才可以删除和添加用户
+                throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
+            }
+            User user = new User();
+            user.setUsername(username);
+            user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
+            //新增用户的pushKey的生成规则为md5(时间戳+用户名)
+            user.setPushKey(DigestUtils.md5DigestAsHex((System.currentTimeMillis() + password).getBytes()));
+            Role role = roleService.getRoleById(roleId);
+
+            if (role == null) {
+                throw new ControllerException(ErrorCode.ERROR400.getCode(), "角色不存在");
+            }
+            user.setRole(role);
+            user.setCreateTime(DateUtil.getNow());
+            user.setUpdateTime(DateUtil.getNow());
+            int addResult = userService.addUser(user);
+            if (addResult <= 0) {
+                throw new ControllerException(ErrorCode.ERROR100);
+            }
+        }catch (AuthenticationException e) {
+            throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
         }
     }
 
@@ -143,15 +152,23 @@ public class UserController {
     @Operation(summary = "删除用户")
     @Parameter(name = "id", description = "用户Id", required = true)
     public void delete(@RequestParam Integer id){
-        // 获取当前登录用户id
-        int currenRoleId = SecurityUtils.getUserInfo().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);
+        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());
         }
     }
 
@@ -182,16 +199,24 @@ public class UserController {
     @Parameter(name = "userId", description = "用户Id", required = true)
     @Parameter(name = "pushKey", description = "新的pushKey", required = true)
     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才可以删除和添加用户
-            throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
-        }
-        int resetPushKeyResult = userService.changePushKey(userId,pushKey);
-        if (resetPushKeyResult <= 0) {
-            throw new ControllerException(ErrorCode.ERROR100);
+        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();
+            WVPResult<String> result = new WVPResult<>();
+            if (currenRoleId != 1) {
+                // 只用角色id为0才可以删除和添加用户
+                throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
+            }
+            int resetPushKeyResult = userService.changePushKey(userId,pushKey);
+            if (resetPushKeyResult <= 0) {
+                throw new ControllerException(ErrorCode.ERROR100);
+            }
+        } catch (AuthenticationException e) {
+            throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
         }
     }
 
@@ -202,16 +227,26 @@ public class UserController {
     @Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
     public void changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) {
         // 获取当前登录用户id
-        LoginUser userInfo = SecurityUtils.getUserInfo();
-        if (userInfo == null) {
-            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) {
+        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());
         }
     }
 }

+ 12 - 1
web_src/src/components/layoutCom/user_header.vue

@@ -27,7 +27,18 @@ export default {
     initHeader(){
       this.activeIndex = this.$route.path;
       let user = userService.getUser();
-      this.editUser = user.roleId===1;
+      console.log(user);
+      if(user&&user.role){
+        let roleId = user.role.id;
+        console.log(roleId)
+        console.log(typeof roleId)
+        this.editUser = roleId === 1;
+        console.log(this.editUser)
+      }else{
+        this.editUser = false;
+        // todo: 重新获取用户信息,或者重新登录
+      }
+
     }
   }
 }