diff --git a/model/user.go b/model/user.go index 6979c70b..ca6e28bc 100644 --- a/model/user.go +++ b/model/user.go @@ -3,6 +3,7 @@ package model import ( "errors" "fmt" + "regexp" "github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/helper" @@ -92,6 +93,13 @@ func (user *User) Insert(inviterId int) error { return err } } + // 在这里添加对用户名的正则表达式检查 + regExp := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`) + if !regExp.MatchString(user.Username) { + return errors.New("用户名包含非法字符,仅支持字母、数字、下划线(_)和横杠(-)") + } + + // 用户名通过检查后,继续其他注册逻辑 user.Quota = config.QuotaForNewUser user.AccessToken = helper.GetUUID() user.AffCode = helper.GetRandomString(4) @@ -144,6 +152,10 @@ func (user *User) ValidateAndFill() (err error) { if user.Username == "" || password == "" { return errors.New("用户名或密码为空") } + // 检查是否使用邮箱作为用户名,减少对已有的用户数据的影响,只以@符号作为验证标准 + if strings.Contains(user.Username, "@") { + return errors.New("本站仅支持使用用户名登录,不支持使用邮箱登录") + } err = DB.Where("username = ?", user.Username).First(user).Error if err != nil { // we must make sure check username firstly