From 62222bb7811785c0e869080fbf89d7cf70d433ca Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 17 Nov 2023 10:07:05 +0800 Subject: [PATCH] fix --- .../internal/logic/createldapuserlogic.go | 5 +++-- utils/email/verify.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 utils/email/verify.go diff --git a/server/ldap-admin/internal/logic/createldapuserlogic.go b/server/ldap-admin/internal/logic/createldapuserlogic.go index 5ce79bd2..3fa92dec 100644 --- a/server/ldap-admin/internal/logic/createldapuserlogic.go +++ b/server/ldap-admin/internal/logic/createldapuserlogic.go @@ -6,6 +6,7 @@ import ( "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/chinese_to_pinyin" + "fusenapi/utils/email" "fusenapi/utils/ldap_lib" "strings" @@ -46,8 +47,8 @@ func (l *CreateLdapUserLogic) CreateLdapUser(req *types.CreateLdapUserReq, useri if req.Password == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "密码不能为空") } - if req.Email == "" { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "邮箱不能为空") + if !email.IsEmailValid(req.Email) { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "邮箱格式不正确") } ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN) //把用户名转pinyin diff --git a/utils/email/verify.go b/utils/email/verify.go new file mode 100644 index 00000000..f389a846 --- /dev/null +++ b/utils/email/verify.go @@ -0,0 +1,15 @@ +package email + +import "regexp" + +// 验证是否邮箱 +func IsEmailValid(email string) bool { + // 邮箱正则表达式 + regex := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` + + // 编译正则表达式 + regexPattern := regexp.MustCompile(regex) + + // 根据正则表达式验证邮箱 + return regexPattern.MatchString(email) +}