mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 23:04:24 +08:00
fix(i18n): use English runtime text and localize system UI
This commit is contained in:
@@ -156,7 +156,7 @@ func DeleteAccessToken(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.OK("删除成功"))
|
||||
c.JSON(http.StatusOK, response.OK("Deleted successfully"))
|
||||
}
|
||||
|
||||
// RotateAccessToken 轮换一个 AccessToken
|
||||
|
||||
+38
-38
@@ -5,44 +5,44 @@
|
||||
package user
|
||||
|
||||
const (
|
||||
errBindParamsFailed = "参数绑定失败"
|
||||
errInvalidParams = "无效的参数"
|
||||
errPasswordLoginDisabled = "管理员关闭了密码登录" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errUsernameOrPasswordWrong = "用户名或密码错误" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errLoginEmailMissing = "该账号未绑定邮箱,请联系管理员绑定邮箱后再登录"
|
||||
errBindParamsFailed = "Failed to bind parameters"
|
||||
errInvalidParams = "invalid parameters"
|
||||
errPasswordLoginDisabled = "Password login is disabled by the administrator" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errUsernameOrPasswordWrong = "Incorrect username or password" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errLoginEmailMissing = "This account has no email address; contact an administrator to bind an email before signing in"
|
||||
errNeedEmailCodePrefix = "need_email_code:"
|
||||
errSMTPInvalidUseTempCodePrefix = "smtp_invalid:"
|
||||
errSMTPInvalidUseTempCode = "smtp 配置无效,使用临时码登录"
|
||||
errEmailCodeInvalidOrExpired = "验证码错误或已过期"
|
||||
errSaveSessionFailed = "无法保存会话信息,请重试"
|
||||
errRegistrationDisabled = "管理员关闭了注册"
|
||||
errPasswordTooShort = "密码长度不能少于 8 位" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errEmailOrCodeRequired = "邮箱或验证码未填写"
|
||||
errNewPasswordTooShort = "新密码长度不能少于 8 位" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errLoginRequired = "请先登录"
|
||||
errUserNotFound = "未找到该用户"
|
||||
errOldPasswordIncorrect = "原密码不正确" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errPasswordEncryptFailed = "密码加密失败,请重试" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errEmailRequired = "邮箱地址不能为空"
|
||||
errUnsupportedEmailScene = "不支持的验证场景"
|
||||
errEmailAlreadyRegistered = "该邮箱已被注册"
|
||||
errEmailCodeCooldown = "验证码发送频繁,请稍后再试"
|
||||
errLoginRateLimited = "登录尝试过于频繁,请稍后再试"
|
||||
errEmailFormatInvalid = "邮箱格式不正确"
|
||||
errEmailAlreadyBound = "该邮箱已被其他账号绑定"
|
||||
errRenderEmailTemplateFailed = "渲染验证邮件模板失败:%w"
|
||||
errGenerateEmailCodeFailed = "生成验证码失败,请重试"
|
||||
errDispatchEmailTaskFailed = "投递验证邮件发送任务失败,请重试"
|
||||
errTokenNameRequired = "令牌名称不能为空" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errAccessTokenLimitReached = "已达到访问令牌最大创建数量限制" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errGenerateTokenFailed = "生成令牌失败" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errInvalidTokenID = "无效的令牌ID" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errTokenNotFoundOrForbidden = "令牌不存在或无权操作" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errAdminTokenRequiresAdmin = "只有管理员才能创建具有管理员权限的令牌" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errTaskPayloadRequired = "任务参数不能为空"
|
||||
errInvalidJSONFormat = "无效的 JSON 格式: %w"
|
||||
errEmailTaskFieldsRequired = "to、subject、body 不能为空"
|
||||
errParseEmailPayloadFailed = "解析邮件发送参数失败: %w"
|
||||
errSMTPConfigIncomplete = "系统 SMTP 邮件服务配置不完整"
|
||||
errSendMailFailed = "发送邮件失败: %w"
|
||||
errSMTPInvalidUseTempCode = "Invalid SMTP configuration; use a temporary code to sign in"
|
||||
errEmailCodeInvalidOrExpired = "verification code is incorrect or expired"
|
||||
errSaveSessionFailed = "Unable to save session; please try again"
|
||||
errRegistrationDisabled = "Registration is disabled by the administrator"
|
||||
errPasswordTooShort = "password must contain at least 8 characters" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errEmailOrCodeRequired = "email address or verification code is missing"
|
||||
errNewPasswordTooShort = "New password must contain at least 8 characters" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errLoginRequired = "Please sign in first"
|
||||
errUserNotFound = "user was not found"
|
||||
errOldPasswordIncorrect = "current password is incorrect" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errPasswordEncryptFailed = "password encryption failed; please try again" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errEmailRequired = "email address must not be empty"
|
||||
errUnsupportedEmailScene = "Unsupported verification scenario"
|
||||
errEmailAlreadyRegistered = "email address is already registered"
|
||||
errEmailCodeCooldown = "verification codes are being requested too frequently; try again later"
|
||||
errLoginRateLimited = "Too many login attempts; try again later"
|
||||
errEmailFormatInvalid = "invalid email address format"
|
||||
errEmailAlreadyBound = "this email address is bound to another account"
|
||||
errRenderEmailTemplateFailed = "failed to render verification email template: %w"
|
||||
errGenerateEmailCodeFailed = "failed to generate verification code; please try again"
|
||||
errDispatchEmailTaskFailed = "failed to enqueue verification email task; please try again"
|
||||
errTokenNameRequired = "Token name must not be empty" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errAccessTokenLimitReached = "Maximum number of access tokens reached" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errGenerateTokenFailed = "failed to generate token" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errInvalidTokenID = "Invalid token ID" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errTokenNotFoundOrForbidden = "token was not found or access is not permitted" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errAdminTokenRequiresAdmin = "Only administrators can create tokens with administrator privileges" //nolint:gosec // false positive: this is an error message, not hardcoded credentials
|
||||
errTaskPayloadRequired = "task parameters must not be empty"
|
||||
errInvalidJSONFormat = "invalid JSON format: %w"
|
||||
errEmailTaskFieldsRequired = "to, subject and body must not be empty"
|
||||
errParseEmailPayloadFailed = "failed to parse email parameters: %w"
|
||||
errSMTPConfigIncomplete = "system SMTP configuration is incomplete"
|
||||
errSendMailFailed = "failed to send email: %w"
|
||||
)
|
||||
|
||||
@@ -174,7 +174,7 @@ func sendEmailVerificationCode(ctx context.Context, email, scene, templateName s
|
||||
|
||||
tmpl, err := repository.GetTemplateByKey(ctx, templateName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("模板 %s 不存在或不可用: %w", templateName, err)
|
||||
return fmt.Errorf("template %s was not found or is unavailable: %w", templateName, err)
|
||||
}
|
||||
emailSubject, emailBody, err := tmpl.Render(map[string]any{"Code": code})
|
||||
if err != nil {
|
||||
@@ -235,7 +235,7 @@ func processLoginEmailVerification(ctx context.Context, code string, user *model
|
||||
if !isSMTPConfigured(ctx) {
|
||||
msg = errSMTPInvalidUseTempCodePrefix + errSMTPInvalidUseTempCode
|
||||
} else {
|
||||
msg = errSMTPInvalidUseTempCodePrefix + "该账号未绑定邮箱,使用临时码登录"
|
||||
msg = errSMTPInvalidUseTempCodePrefix + "This account has no email address; use a temporary code to sign in"
|
||||
}
|
||||
return LoginEmailVerificationResult{
|
||||
Status: LoginEmailVerificationRejected,
|
||||
@@ -348,9 +348,9 @@ func updateLastLogin(ctx context.Context, user *model.User) error {
|
||||
func registerUserLogic(ctx context.Context, u *model.User) error {
|
||||
if err := repository.RegisterUserWithChecks(ctx, u); err != nil {
|
||||
if strings.Contains(err.Error(), "duplicate key") || strings.Contains(err.Error(), "UNIQUE") {
|
||||
return errors.New("用户名或邮箱已被占用")
|
||||
return errors.New("username or email address is already in use")
|
||||
}
|
||||
return errors.New("注册失败,请稍后再试")
|
||||
return errors.New("registration failed; please try again later")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -370,7 +370,7 @@ func changePasswordLogic(ctx context.Context, userID uint64, oldPass, newPass st
|
||||
}
|
||||
|
||||
if err := repository.UpdateUserPassword(ctx, dbUser.ID, dbUser.Password); err != nil {
|
||||
return errors.New("更新密码失败,请稍后再试")
|
||||
return errors.New("failed to update password; please try again later")
|
||||
}
|
||||
|
||||
// 吊销该用户所有的 Access Token
|
||||
@@ -380,7 +380,7 @@ func changePasswordLogic(ctx context.Context, userID uint64, oldPass, newPass st
|
||||
}
|
||||
}
|
||||
if err := repository.DeleteAccessTokensByUserID(ctx, dbUser.ID); err != nil {
|
||||
return errors.New("吊销 Access Token 失败,请稍后再试")
|
||||
return errors.New("failed to revoke access token; please try again later")
|
||||
}
|
||||
|
||||
oauth.InvalidateCachedUser(ctx, dbUser.ID)
|
||||
@@ -390,7 +390,7 @@ func changePasswordLogic(ctx context.Context, userID uint64, oldPass, newPass st
|
||||
func listAccessTokensLogic(ctx context.Context, userID uint64) ([]model.AccessToken, error) {
|
||||
tokens, err := repository.ListAccessTokensByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, errors.New("获取令牌列表失败,请稍后再试")
|
||||
return nil, errors.New("failed to get token list; please try again later")
|
||||
}
|
||||
return tokens, nil
|
||||
}
|
||||
@@ -398,14 +398,14 @@ func listAccessTokensLogic(ctx context.Context, userID uint64) ([]model.AccessTo
|
||||
func countAccessTokensLogic(ctx context.Context, userID uint64) (int64, error) {
|
||||
count, err := repository.CountAccessTokensByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return 0, errors.New("查询令牌数量失败,请稍后再试")
|
||||
return 0, errors.New("failed to count tokens; please try again later")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func createAccessTokenLogic(ctx context.Context, record *model.AccessToken) error {
|
||||
if err := repository.CreateAccessToken(ctx, record); err != nil {
|
||||
return errors.New("创建令牌失败,请稍后再试")
|
||||
return errors.New("failed to create token; please try again later")
|
||||
}
|
||||
oauth.SetCachedToken(ctx, record.TokenHash, record)
|
||||
return nil
|
||||
@@ -420,7 +420,7 @@ func deleteAccessTokenLogic(ctx context.Context, id, userID uint64) error {
|
||||
|
||||
rows, err := repository.DeleteAccessTokenForUser(ctx, id, userID)
|
||||
if err != nil {
|
||||
return errors.New("删除令牌失败,请稍后再试")
|
||||
return errors.New("failed to delete token; please try again later")
|
||||
}
|
||||
if rows == 0 {
|
||||
return errors.New(errTokenNotFoundOrForbidden)
|
||||
@@ -448,7 +448,7 @@ func rotateAccessTokenLogic(ctx context.Context, id, userID uint64) (string, *mo
|
||||
tokenRecord.MaskedToken = newMaskedToken
|
||||
|
||||
if err := repository.SaveAccessToken(ctx, &tokenRecord); err != nil {
|
||||
return "", nil, errors.New("轮换令牌失败,请稍后再试")
|
||||
return "", nil, errors.New("failed to rotate token; please try again later")
|
||||
}
|
||||
|
||||
oauth.SetCachedToken(ctx, tokenRecord.TokenHash, &tokenRecord)
|
||||
|
||||
@@ -114,7 +114,7 @@ func TestProcessLoginEmailVerificationEmptyEmailFallback(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("processLoginEmailVerification() error = %v, want nil", err)
|
||||
}
|
||||
expected := errSMTPInvalidUseTempCodePrefix + "该账号未绑定邮箱,使用临时码登录"
|
||||
expected := errSMTPInvalidUseTempCodePrefix + "This account has no email address; use a temporary code to sign in"
|
||||
if result.Status != LoginEmailVerificationRejected || result.Message != expected {
|
||||
t.Fatalf("processLoginEmailVerification() = %+v, want rejected with %q", result, expected)
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ func Login(c *gin.Context) {
|
||||
|
||||
user.LastLoginAt = time.Now()
|
||||
if err := updateLastLogin(ctx, user); err != nil {
|
||||
response.AbortBadRequest(c, "更新登录时间失败,请稍后再试")
|
||||
response.AbortBadRequest(c, "Failed to update last login time; please try again later")
|
||||
return
|
||||
}
|
||||
extras := map[string]any{}
|
||||
@@ -313,7 +313,7 @@ func ChangePassword(c *gin.Context) {
|
||||
session.Clear()
|
||||
_ = session.Save()
|
||||
|
||||
c.JSON(http.StatusOK, response.OK("密码修改成功"))
|
||||
c.JSON(http.StatusOK, response.OK("Password changed successfully"))
|
||||
}
|
||||
|
||||
// SendEmailCode 发送邮箱验证码
|
||||
|
||||
@@ -418,7 +418,7 @@ func TestLoginEmailVerificationFallbackForEmptyEmail(t *testing.T) {
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("unmarshal response failed: %v", err)
|
||||
}
|
||||
expectedError := errSMTPInvalidUseTempCodePrefix + "该账号未绑定邮箱,使用临时码登录"
|
||||
expectedError := errSMTPInvalidUseTempCodePrefix + "This account has no email address; use a temporary code to sign in"
|
||||
if resp.ErrorMsg != expectedError {
|
||||
t.Errorf("expected error %q, got %q", expectedError, resp.ErrorMsg)
|
||||
}
|
||||
|
||||
+16
-16
@@ -29,8 +29,8 @@ const (
|
||||
var SendEmailMeta = task.TaskMeta{
|
||||
Type: TaskTypeSendEmail,
|
||||
AsynqTask: SendEmailTask,
|
||||
Name: "发送邮件",
|
||||
Description: "异步发送系统邮件",
|
||||
Name: "Send email",
|
||||
Description: "Send system email asynchronously",
|
||||
SupportsTime: false,
|
||||
MaxRetry: task.DefaultMaxRetry,
|
||||
Queue: task.QueueDefault,
|
||||
@@ -38,27 +38,27 @@ var SendEmailMeta = task.TaskMeta{
|
||||
Params: []task.TaskParam{
|
||||
{
|
||||
Name: "to",
|
||||
Label: "接收邮箱 (To)",
|
||||
Label: "Recipient email (To)",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Placeholder: "receiver@example.com",
|
||||
Description: "接收邮件的目标邮箱地址",
|
||||
Description: "Destination email address",
|
||||
},
|
||||
{
|
||||
Name: "subject",
|
||||
Label: "邮件主题 (Subject)",
|
||||
Label: "Email subject",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Placeholder: "请输入邮件主题",
|
||||
Description: "发送邮件的主题标题",
|
||||
Placeholder: "Enter the email subject",
|
||||
Description: "Subject line of the email to send",
|
||||
},
|
||||
{
|
||||
Name: "body",
|
||||
Label: "邮件内容 (Body)",
|
||||
Label: "Email body",
|
||||
Type: "text",
|
||||
Required: true,
|
||||
Placeholder: "请输入邮件内容(支持 HTML 格式)",
|
||||
Description: "发送邮件的内容主体",
|
||||
Placeholder: "Enter the email body (HTML is supported)",
|
||||
Description: "Content of the email to send",
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -100,11 +100,11 @@ func (h *SendEmailHandler) ValidatePayload(payload []byte) ([]byte, error) {
|
||||
func (h *SendEmailHandler) Execute(ctx context.Context, payload []byte) (*task.TaskResult, error) {
|
||||
var req SendEmailPayload
|
||||
if err := json.Unmarshal(payload, &req); err != nil {
|
||||
task.AppendLog(ctx, "解析邮件发送参数失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to parse email parameters: %v", err)
|
||||
return nil, fmt.Errorf(errParseEmailPayloadFailed, err)
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "开始准备发送邮件到: %s, 主题: %s", req.To, req.Subject)
|
||||
task.AppendLog(ctx, "Preparing to send email to: %s, subject: %s", req.To, req.Subject)
|
||||
|
||||
// 从数据库读取最新的 SMTP 系统配置
|
||||
var smtpHost string
|
||||
@@ -127,7 +127,7 @@ func (h *SendEmailHandler) Execute(ctx context.Context, payload []byte) (*task.T
|
||||
|
||||
if smtpHost == "" || smtpPortVal == "" || smtpUsername == "" {
|
||||
err := errors.New(errSMTPConfigIncomplete)
|
||||
task.AppendLog(ctx, "发送失败: %v", err)
|
||||
task.AppendLog(ctx, "Send failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -143,16 +143,16 @@ func (h *SendEmailHandler) Execute(ctx context.Context, payload []byte) (*task.T
|
||||
Password: smtpPassword,
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "连接 SMTP 服务器: %s:%d, 用户名: %s", smtpHost, smtpPort, smtpUsername)
|
||||
task.AppendLog(ctx, "Connecting to SMTP server: %s:%d, username: %s", smtpHost, smtpPort, smtpUsername)
|
||||
|
||||
// 调用 SendMailHTML 执行邮件发送,这里会有 5s 拨号超时和 10s 读写限制
|
||||
err = mail.SendMailHTML(ctx, cfg, req.To, req.Subject, req.Body)
|
||||
if err != nil {
|
||||
task.AppendLog(ctx, "邮件发送失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to send email: %v", err)
|
||||
return nil, fmt.Errorf(errSendMailFailed, err)
|
||||
}
|
||||
|
||||
msg := "邮件成功发送至: " + req.To
|
||||
msg := "Email sent successfully to: " + req.To
|
||||
task.AppendLog(ctx, "%s", msg)
|
||||
|
||||
return &task.TaskResult{
|
||||
|
||||
Reference in New Issue
Block a user