use credential to store email server auth info

This commit is contained in:
liugq 2023-07-31 15:12:58 +08:00
parent fcf0179335
commit 0965256ecc
3 changed files with 11 additions and 11 deletions

View File

@ -6,6 +6,7 @@ package model
import (
"fmt"
"infini.sh/framework/core/elastic"
"infini.sh/framework/core/orm"
)
@ -15,11 +16,9 @@ type EmailServer struct {
Host string `json:"host" elastic_mapping:"host:{type:keyword}"`
Port int `json:"port" elastic_mapping:"port:{type:keyword}"`
TLS bool `json:"tls" elastic_mapping:"tls:{type:keyword}"`
Auth struct {
Username string `json:"username" elastic_mapping:"username:{type:keyword}"`
Password string `json:"password" elastic_mapping:"password:{type:keyword}"`
} `json:"auth" elastic_mapping:"auth:{type:object}"`
Auth *elastic.BasicAuth `json:"auth" elastic_mapping:"auth:{type:object}"`
Enabled bool `json:"enabled" elastic_mapping:"enabled:{type:boolean}"`
CredentialID string `json:"credential_id" elastic_mapping:"credential_id:{type:keyword}"`
}
func (serv *EmailServer) Validate(requireName bool) error {

View File

@ -36,6 +36,11 @@ func RefreshEmailServer() error {
if err != nil {
return err
}
auth, err := GetBasicAuth(&emailServer)
if err != nil {
return err
}
emailServer.Auth = &auth
servers = append(servers, emailServer)
}
pipeCfgStr := GeneratePipelineConfig(servers)

View File

@ -293,17 +293,13 @@ func (h *EmailAPI) testEmailServer(w http.ResponseWriter, req *http.Request, ps
h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
if reqBody.Auth.Password == "" && reqBody.ID != "" {
obj := model.EmailServer{}
obj.ID = reqBody.ID
_, err := orm.Get(&obj)
if reqBody.Auth.Password == "" && reqBody.CredentialID != "" {
auth, err := common.GetBasicAuth(&reqBody.EmailServer)
if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
if reqBody.Auth.Username == obj.Auth.Username {
reqBody.Auth.Password = obj.Auth.Password
}
reqBody.Auth = &auth
}
message := gomail.NewMessage()
message.SetHeader("From", reqBody.Auth.Username)