Email errors should be logged now.

Reordered the registration logic so the session is created before any, if any, email errors fire.
Fixed a bug where the analytics phrases would load after the graph is drawn.
This commit is contained in:
Azareal 2019-03-03 13:19:32 +10:00
parent ce04b6001e
commit e9b46e1cd6
4 changed files with 27 additions and 25 deletions

View File

@ -13,7 +13,7 @@ type Email struct {
Token string Token string
} }
func SendValidationEmail(username string, email string, token string) bool { func SendValidationEmail(username string, email string, token string) error {
var schema = "http" var schema = "http"
if Site.EnableSsl { if Site.EnableSsl {
schema += "s" schema += "s"
@ -27,49 +27,48 @@ func SendValidationEmail(username string, email string, token string) bool {
// TODO: Refactor this // TODO: Refactor this
// TODO: Add support for TLS // TODO: Add support for TLS
func SendEmail(email string, subject string, msg string) bool { func SendEmail(email string, subject string, msg string) error {
// This hook is useful for plugin_sendmail or for testing tools. Possibly to hook it into some sort of mail server? // This hook is useful for plugin_sendmail or for testing tools. Possibly to hook it into some sort of mail server?
ret, hasHook := GetHookTable().VhookNeedHook("email_send_intercept", email, subject, msg) ret, hasHook := GetHookTable().VhookNeedHook("email_send_intercept", email, subject, msg)
if hasHook { if hasHook {
return ret.(bool) return ret.(error)
} }
body := "Subject: " + subject + "\n\n" + msg + "\n" body := "Subject: " + subject + "\n\n" + msg + "\n"
con, err := smtp.Dial(Config.SMTPServer + ":" + Config.SMTPPort) con, err := smtp.Dial(Config.SMTPServer + ":" + Config.SMTPPort)
if err != nil { if err != nil {
return false return err
} }
if Config.SMTPUsername != "" { if Config.SMTPUsername != "" {
auth := smtp.PlainAuth("", Config.SMTPUsername, Config.SMTPPassword, Config.SMTPServer) auth := smtp.PlainAuth("", Config.SMTPUsername, Config.SMTPPassword, Config.SMTPServer)
err = con.Auth(auth) err = con.Auth(auth)
if err != nil { if err != nil {
return false return err
} }
} }
err = con.Mail(Site.Email) err = con.Mail(Site.Email)
if err != nil { if err != nil {
return false return err
} }
err = con.Rcpt(email) err = con.Rcpt(email)
if err != nil { if err != nil {
return false return err
} }
emailData, err := con.Data() emailData, err := con.Data()
if err != nil { if err != nil {
return false return err
} }
_, err = fmt.Fprintf(emailData, body) _, err = fmt.Fprintf(emailData, body)
if err != nil { if err != nil {
return false return err
} }
err = emailData.Close() err = emailData.Close()
if err != nil { if err != nil {
return false return err
} }
return con.Quit()
return con.Quit() == nil
} }

View File

@ -54,19 +54,18 @@ func sendSendmail(data ...interface{}) interface{} {
sendmail := exec.Command("/usr/sbin/sendmail", "-t", "-i") sendmail := exec.Command("/usr/sbin/sendmail", "-t", "-i")
stdin, err := sendmail.StdinPipe() stdin, err := sendmail.StdinPipe()
if err != nil { if err != nil {
return false // Possibly disable the plugin and show an error to the admin on the dashboard? Plugin log file? return err // Possibly disable the plugin and show an error to the admin on the dashboard? Plugin log file?
} }
err = sendmail.Start() err = sendmail.Start()
if err != nil { if err != nil {
return false return err
} }
io.WriteString(stdin, msg) io.WriteString(stdin, msg)
err = stdin.Close() err = stdin.Close()
if err != nil { if err != nil {
return false return err
} }
return sendmail.Wait()
return sendmail.Wait() == nil
} }

View File

@ -321,6 +321,12 @@ func AccountRegisterSubmit(w http.ResponseWriter, r *http.Request, user common.U
return common.InternalError(err, w, r) return common.InternalError(err, w, r)
} }
session, err := common.Auth.CreateSession(uid)
if err != nil {
return common.InternalError(err, w, r)
}
common.Auth.SetCookies(w, uid, session)
// Check if this user actually owns this email, if email activation is on, automatically flip their account to active when the email is validated. Validation is also useful for determining whether this user should receive any alerts, etc. via email // Check if this user actually owns this email, if email activation is on, automatically flip their account to active when the email is validated. Validation is also useful for determining whether this user should receive any alerts, etc. via email
if common.Site.EnableEmails { if common.Site.EnableEmails {
token, err := common.GenerateSafeString(80) token, err := common.GenerateSafeString(80)
@ -334,17 +340,13 @@ func AccountRegisterSubmit(w http.ResponseWriter, r *http.Request, user common.U
return common.InternalError(err, w, r) return common.InternalError(err, w, r)
} }
if !common.SendValidationEmail(username, email, token) { err = common.SendValidationEmail(username, email, token)
if err != nil {
common.LogWarning(err)
return common.LocalError(phrases.GetErrorPhrase("register_email_fail"), w, r, user) return common.LocalError(phrases.GetErrorPhrase("register_email_fail"), w, r, user)
} }
} }
session, err := common.Auth.CreateSession(uid)
if err != nil {
return common.InternalError(err, w, r)
}
common.Auth.SetCookies(w, uid, session)
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
return nil return nil
} }

View File

@ -10,5 +10,7 @@ let seriesData = [{{range .Graph.Series}}[{{range .}}
let legendNames = [{{range .Graph.Legends}} let legendNames = [{{range .Graph.Legends}}
{{.}},{{end}} {{.}},{{end}}
]; ];
buildStatsChart(rawLabels, seriesData, "{{.TimeRange}}",legendNames); addInitHook("after_phrases", () => {
buildStatsChart(rawLabels, seriesData, "{{.TimeRange}}",legendNames);
});
</script> </script>