enhance: generate error message for parameter error
i18n: use explicit error code for login controlelr
This commit is contained in:
parent
0e5683bc3b
commit
4fe79859a9
3 changed files with 38 additions and 23 deletions
|
@ -58,8 +58,6 @@ const (
|
|||
CodeConflict = 409
|
||||
// CodeUploadFailed 上传出错
|
||||
CodeUploadFailed = 40002
|
||||
// CodeCredentialInvalid 凭证无效
|
||||
CodeCredentialInvalid = 40001
|
||||
// CodeCreateFolderFailed 目录创建失败
|
||||
CodeCreateFolderFailed = 40003
|
||||
// CodeObjectExist 对象已存在
|
||||
|
@ -86,6 +84,20 @@ const (
|
|||
CodeBatchAria2Size = 40015
|
||||
// CodeParentNotExist 父目录不存在
|
||||
CodeParentNotExist = 40016
|
||||
// CodeUserBaned 用户不活跃
|
||||
CodeUserBaned = 40017
|
||||
// CodeUserNotActivated 用户不活跃
|
||||
CodeUserNotActivated = 40018
|
||||
// CodeFeatureNotEnabled 此功能未开启
|
||||
CodeFeatureNotEnabled = 40019
|
||||
// CodeCredentialInvalid 凭证无效
|
||||
CodeCredentialInvalid = 40020
|
||||
// CodeUserNotFound 用户不存在
|
||||
CodeUserNotFound = 40021
|
||||
// Code2FACodeErr 二步验证代码错误
|
||||
Code2FACodeErr = 40022
|
||||
// CodeLoginSessionNotExist 登录会话不存在
|
||||
CodeLoginSessionNotExist = 40023
|
||||
// CodeDBError 数据库操作失败
|
||||
CodeDBError = 50001
|
||||
// CodeEncryptError 加密失败
|
||||
|
|
|
@ -6,30 +6,33 @@ import (
|
|||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
// ParamErrorMsg 根据Validator返回的错误信息给出错误提示
|
||||
func ParamErrorMsg(filed string, tag string) string {
|
||||
// 未通过验证的表单域与中文对应
|
||||
fieldMap := map[string]string{
|
||||
"UserName": "邮箱",
|
||||
"Password": "密码",
|
||||
"Path": "路径",
|
||||
"SourceID": "原始资源",
|
||||
"URL": "链接",
|
||||
"Nick": "昵称",
|
||||
"UserName": "Email",
|
||||
"Password": "Password",
|
||||
"Path": "Path",
|
||||
"SourceID": "Source resource",
|
||||
"URL": "URL",
|
||||
"Nick": "Nickname",
|
||||
}
|
||||
// 未通过的规则与中文对应
|
||||
tagMap := map[string]string{
|
||||
"required": "不能为空",
|
||||
"min": "太短",
|
||||
"max": "太长",
|
||||
"email": "格式不正确",
|
||||
"required": "cannot be empty",
|
||||
"min": "too short",
|
||||
"max": "too long",
|
||||
"email": "format error",
|
||||
}
|
||||
fieldVal, findField := fieldMap[filed]
|
||||
if !findField {
|
||||
fieldVal = filed
|
||||
}
|
||||
tagVal, findTag := tagMap[tag]
|
||||
if findField && findTag {
|
||||
if findTag {
|
||||
// 返回拼接出来的错误信息
|
||||
return fieldVal + tagVal
|
||||
}
|
||||
|
@ -49,10 +52,10 @@ func ErrorResponse(err error) serializer.Response {
|
|||
}
|
||||
|
||||
if _, ok := err.(*json.UnmarshalTypeError); ok {
|
||||
return serializer.ParamErr("JSON类型不匹配", err)
|
||||
return serializer.ParamErr("JSON marshall error", err)
|
||||
}
|
||||
|
||||
return serializer.ParamErr("参数错误", err)
|
||||
return serializer.ParamErr("Parameter error", err)
|
||||
}
|
||||
|
||||
// CurrentUser 获取当前用户
|
||||
|
|
|
@ -101,12 +101,12 @@ func (service *Enable2FA) Login(c *gin.Context) serializer.Response {
|
|||
// 查找用户
|
||||
expectedUser, err := model.GetActiveUserByID(uid)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeNotFound, "用户不存在", nil)
|
||||
return serializer.Err(serializer.CodeUserNotFound, "User not found", nil)
|
||||
}
|
||||
|
||||
// 验证二步验证代码
|
||||
if !totp.Validate(service.Code, expectedUser.TwoFactor) {
|
||||
return serializer.ParamErr("验证代码不正确", nil)
|
||||
return serializer.Err(serializer.Code2FACodeErr, "2FA code not correct", nil)
|
||||
}
|
||||
|
||||
//登陆成功,清空并设置session
|
||||
|
@ -118,7 +118,7 @@ func (service *Enable2FA) Login(c *gin.Context) serializer.Response {
|
|||
return serializer.BuildUserResponse(expectedUser)
|
||||
}
|
||||
|
||||
return serializer.Err(serializer.CodeNotFound, "登录会话不存在", nil)
|
||||
return serializer.Err(serializer.CodeLoginSessionNotExist, "Login session not exist", nil)
|
||||
}
|
||||
|
||||
// Login 用户登录函数
|
||||
|
@ -126,16 +126,16 @@ func (service *UserLoginService) Login(c *gin.Context) serializer.Response {
|
|||
expectedUser, err := model.GetUserByEmail(service.UserName)
|
||||
// 一系列校验
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeCredentialInvalid, "用户邮箱或密码错误", err)
|
||||
return serializer.Err(serializer.CodeCredentialInvalid, "Wrong password or email address", err)
|
||||
}
|
||||
if authOK, _ := expectedUser.CheckPassword(service.Password); !authOK {
|
||||
return serializer.Err(serializer.CodeCredentialInvalid, "用户邮箱或密码错误", nil)
|
||||
return serializer.Err(serializer.CodeCredentialInvalid, "Wrong password or email address", nil)
|
||||
}
|
||||
if expectedUser.Status == model.Baned || expectedUser.Status == model.OveruseBaned {
|
||||
return serializer.Err(403, "该账号已被封禁", nil)
|
||||
return serializer.Err(serializer.CodeUserBaned, "This account has been blocked", nil)
|
||||
}
|
||||
if expectedUser.Status == model.NotActivicated {
|
||||
return serializer.Err(403, "该账号未激活", nil)
|
||||
return serializer.Err(serializer.CodeUserNotActivated, "This account is not activated", nil)
|
||||
}
|
||||
|
||||
if expectedUser.TwoFactor != "" {
|
||||
|
|
Loading…
Add table
Reference in a new issue