完善用户管理与凭据事务处理
This commit is contained in:
1 parent
7f0af703ed
commit
0e2222bcfe
33 files changed
+2135
-68
No files matched your search
@@ -12,28 +12,38 @@ const (
|
||||
MaxLength = 12
|
||||
)
|
||||
|
||||
// Validate 校验管理员密码是否符合策略。
|
||||
func Validate(password, username string) error {
|
||||
return validate(password, username, "管理员密码")
|
||||
}
|
||||
|
||||
// ValidateAccountPassword 校验普通账户密码是否符合策略。
|
||||
func ValidateAccountPassword(password, username string) error {
|
||||
return validate(password, username, "账户密码")
|
||||
}
|
||||
|
||||
func validate(password, username, label string) error {
|
||||
length := utf8.RuneCountInString(password)
|
||||
if length < MinLength {
|
||||
return fmt.Errorf("超级管理员密码至少需要 %d 个字符", MinLength)
|
||||
return fmt.Errorf("%s长度不能少于 %d 个字符", label, MinLength)
|
||||
}
|
||||
if length > MaxLength {
|
||||
return fmt.Errorf("超级管理员密码最多允许 %d 个字符", MaxLength)
|
||||
return fmt.Errorf("%s长度不能超过 %d 个字符", label, MaxLength)
|
||||
}
|
||||
if strings.EqualFold(password, strings.TrimSpace(username)) {
|
||||
return fmt.Errorf("超级管理员密码不能与用户名相同")
|
||||
return fmt.Errorf("%s不能与用户名相同", label)
|
||||
}
|
||||
hasLetter := false
|
||||
hasDigit := false
|
||||
for _, value := range password {
|
||||
if unicode.IsControl(value) {
|
||||
return fmt.Errorf("超级管理员密码不能包含控制字符")
|
||||
return fmt.Errorf("%s不能包含控制字符", label)
|
||||
}
|
||||
hasLetter = hasLetter || unicode.IsLetter(value)
|
||||
hasDigit = hasDigit || unicode.IsDigit(value)
|
||||
}
|
||||
if !hasLetter || !hasDigit {
|
||||
return fmt.Errorf("超级管理员密码必须同时包含字母和数字")
|
||||
return fmt.Errorf("%s必须同时包含字母和数字", label)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user