fix(smtp): Check From Name in SMTP is not a Email

This commit is contained in:
LinkinStar 2022-12-26 11:49:38 +08:00
parent 7085cfa7ed
commit 48a54e8139
3 changed files with 19 additions and 0 deletions

View File

@ -111,6 +111,9 @@ backend:
other: "No permission to update."
cannot_set_synonym_as_itself:
other: "You cannot set the synonym of the current tag as itself."
smtp:
config_from_name_cannot_be_email:
other: "The From Name cannot be a email address."
theme:
not_found:
other: "Theme not found."

View File

@ -60,4 +60,5 @@ const (
UserCannotUpdateYourRole = "error.user.cannot_update_your_role"
TagCannotSetSynonymAsItself = "error.tag.cannot_set_synonym_as_itself"
NotAllowedRegistration = "error.user.not_allowed_registration"
SMTPConfigFromNameCannotBeEmail = "error.smtp.config_from_name_cannot_be_email"
)

View File

@ -3,10 +3,14 @@ package schema
import (
"context"
"fmt"
"net/mail"
"net/url"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/base/validator"
"github.com/segmentfault/pacman/errors"
)
const PermaLinkQuestionIDAndTitle = 1
@ -190,6 +194,17 @@ type UpdateSMTPConfigReq struct {
TestEmailRecipient string `validate:"omitempty,email" json:"test_email_recipient"`
}
func (r *UpdateSMTPConfigReq) Check() (errField []*validator.FormErrorField, err error) {
_, err = mail.ParseAddress(r.FromName)
if err == nil {
return append(errField, &validator.FormErrorField{
ErrorField: "from_name",
ErrorMsg: reason.SMTPConfigFromNameCannotBeEmail,
}), errors.BadRequest(reason.SMTPConfigFromNameCannotBeEmail)
}
return nil, nil
}
// GetSMTPConfigResp get smtp config response
type GetSMTPConfigResp struct {
FromEmail string `json:"from_email"`