Bladeren bron

update UserServiceImpl

reghao 2 jaren geleden
bovenliggende
commit
8909188022

+ 24 - 5
manager/src/main/java/cn/reghao/devops/manager/rbac/service/impl/UserServiceImpl.java

@@ -77,7 +77,11 @@ public class UserServiceImpl implements UserService {
     // TODO 密码修改后是否应该清除用户 session?
     @Override
     public void modifyUserPassword(Integer userId, String newPassword) {
-        User userEntity = userRepository.getOne(userId);
+        User userEntity = userRepository.findById(userId).orElse(null);
+        if (userEntity == null) {
+            return;
+        }
+
         userEntity.setPassword(newPassword);
         setEncryptPassword(userEntity);
         userRepository.save(userEntity);
@@ -85,7 +89,11 @@ public class UserServiceImpl implements UserService {
 
     @Override
     public void modifyUserInfo(UserInfo userInfo) {
-        User userEntity = userRepository.getOne(userInfo.getUserId());
+        User userEntity = userRepository.findById(userInfo.getUserId()).orElse(null);
+        if (userEntity == null) {
+            return;
+        }
+
         userEntity.setNickname(userInfo.getNickname());
         userEntity.setMobilePhone(userInfo.getMobilePhone());
         userEntity.setEmail(userInfo.getEmail());
@@ -94,7 +102,10 @@ public class UserServiceImpl implements UserService {
 
     @Override
     public void setUserRoles(UserRole userRole) {
-        User userEntity = userRepository.getOne(userRole.getUserId());
+        User userEntity = userRepository.findById(userRole.getUserId()).orElse(null);
+        if (userEntity == null) {
+            return;
+        }
         /*Set<UserAuthority> authorities = userRole.getRoles().stream()
                 .map(role -> new UserAuthority(role.getTitle()))
                 .collect(Collectors.toSet());
@@ -109,7 +120,11 @@ public class UserServiceImpl implements UserService {
 
     @Override
     public void setUserStatus(Integer userId, Boolean enable) {
-        User userEntity = userRepository.getOne(userId);
+        User userEntity = userRepository.findById(userId).orElse(null);
+        if (userEntity == null) {
+            return;
+        }
+
         userEntity.setIsEnabled(enable);
         userRepository.save(userEntity);
     }
@@ -117,7 +132,11 @@ public class UserServiceImpl implements UserService {
     // TODO 删除用户后还需清除用户 session
     @Override
     public void deleteUser(Integer userId) {
-        User userEntity = userRepository.getOne(userId);
+        User userEntity = userRepository.findById(userId).orElse(null);
+        if (userEntity == null) {
+            return;
+        }
+
         if (userEntity.getId() == 1) {
             log.error("不能删除管理员帐号");
         } else {

+ 6 - 3
manager/src/test/java/AccountTest.java

@@ -28,9 +28,12 @@ public class AccountTest {
     @Test
     public void createAccount() {
         String username = "admin";
-        String password = "123456";
-        String role = RoleType.ROLE_ADMIN.name();
+        int userId = 102;
+        String password = "ecUgpUeIX2ub84EYPsiQ";
+        userService.modifyUserPassword(userId, password);
+
+        /*String role = RoleType.ROLE_ADMIN.name();
         User user = new User(username, password, Set.of(new UserAuthority(role)));
-        userService.createUser(user);
+        userService.createUser(user);*/
     }
 }