Prechádzať zdrojové kódy

[Bug-10394] [Update Possword] Change password shows error (#10437)

* Add a status code for password verification length to improve user interaction

* Modify the length and null value, verify them separately and return

* Modify the length and null value, verify them separately and return

* remove the outer not empty validate and Modify judgment conditions

* add the outer not empty validate and remove inter empty

Co-authored-by: syyang <730122+syyang99@user.noreply.gitee.com>
syyangs799 2 rokov pred
rodič
commit
d16e0ddb2f

+ 1 - 0
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java

@@ -422,6 +422,7 @@ public enum Status {
     QUERY_CAN_USE_K8S_CLUSTER_ERROR(1300014, "login user query can used k8s cluster list error", "查询可用k8s集群错误"),
     RESOURCE_FULL_NAME_TOO_LONG_ERROR(1300015, "resource's fullname is too long error", "资源文件名过长"),
     TENANT_FULL_NAME_TOO_LONG_ERROR(1300016, "tenant's fullname is too long error", "租户名过长"),
+    USER_PASSWORD_LENGTH_ERROR(1300017, "user's password length error", "用户密码长度错误"),
 
     NO_CURRENT_OPERATING_PERMISSION(1400001, "The current user does not have this permission.", "当前用户无此权限"),
     FUNCTION_DISABLED(1400002, "The current feature is disabled.", "当前功能已被禁用"),

+ 3 - 2
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java

@@ -80,6 +80,7 @@ import java.util.Arrays;
 import java.util.stream.Collectors;
 
 import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.USER_MANAGER;
+import static org.apache.dolphinscheduler.common.Constants.USER_PASSWORD_MAX_LENGTH;
 
 /**
  * users service impl
@@ -414,8 +415,8 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService {
         }
 
         if (StringUtils.isNotEmpty(userPassword)) {
-            if (!CheckUtils.checkPassword(userPassword)) {
-                putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, userPassword);
+            if (!CheckUtils.checkPasswordLength(userPassword)) {
+                putMsg(result, Status.USER_PASSWORD_LENGTH_ERROR);
                 return result;
             }
             user.setUserPassword(EncryptionUtils.getMd5(userPassword));

+ 13 - 1
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/CheckUtils.java

@@ -31,6 +31,9 @@ import java.util.regex.Pattern;
 
 import org.hibernate.validator.internal.constraintvalidators.bv.EmailValidator;
 
+import static org.apache.dolphinscheduler.common.Constants.USER_PASSWORD_MAX_LENGTH;
+import static org.apache.dolphinscheduler.common.Constants.USER_PASSWORD_MIN_LENGTH;
+
 /**
  * check utils
  */
@@ -105,7 +108,16 @@ public class CheckUtils {
      * @return true if password regex valid, otherwise return false
      */
     public static boolean checkPassword(String password) {
-        return !StringUtils.isEmpty(password) && password.length() >= 2 && password.length() <= 20;
+        return !StringUtils.isEmpty(password) && checkPasswordLength(password);
+    }
+
+    /**
+     *  check password length
+     * @param password password
+     * @return true if password length valid, otherwise return false
+     */
+    public static boolean checkPasswordLength(String password) {
+        return password.length() >= USER_PASSWORD_MIN_LENGTH && password.length() <= USER_PASSWORD_MAX_LENGTH;
     }
 
     /**

+ 3 - 0
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/utils/CheckUtilsTest.java

@@ -79,6 +79,9 @@ public class CheckUtilsTest {
         assertFalse(CheckUtils.checkPassword("a"));
         assertFalse(CheckUtils.checkPassword("1234567890abcderfasdf2"));
         assertTrue(CheckUtils.checkPassword("123456"));
+        assertFalse(CheckUtils.checkPasswordLength("1"));
+        assertTrue(CheckUtils.checkPasswordLength("dolphinscheduler123"));
+        assertFalse(CheckUtils.checkPasswordLength("dolphinscheduler123456"));
     }
 
     /**

+ 6 - 0
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java

@@ -820,4 +820,10 @@ public final class Constants {
      */
     public static final int SCHEDULE_TIME_MAX_LENGTH = 100;
 
+    /**
+     * password max and min LENGTH
+     */
+    public static final int USER_PASSWORD_MAX_LENGTH = 20;
+
+    public static final int USER_PASSWORD_MIN_LENGTH = 2;
 }