2015-06-04 10:23:46 -05:00
|
|
|
package notifications
|
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"html/template"
|
|
|
|
"path/filepath"
|
2015-06-04 10:23:46 -05:00
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mailTemplates *template.Template
|
|
|
|
var tmplResetPassword = "reset_password.html"
|
|
|
|
|
|
|
|
func Init() error {
|
2015-06-04 10:23:46 -05:00
|
|
|
bus.AddHandler("email", sendResetPasswordEmail)
|
2015-06-08 06:39:02 -05:00
|
|
|
bus.AddHandler("email", validateResetPasswordCode)
|
2015-06-05 04:08:19 -05:00
|
|
|
|
|
|
|
mailTemplates = template.New("name")
|
|
|
|
mailTemplates.Funcs(template.FuncMap{
|
|
|
|
"Subject": subjectTemplateFunc,
|
|
|
|
})
|
|
|
|
|
|
|
|
templatePattern := filepath.Join(setting.StaticRootPath, "emails/*.html")
|
|
|
|
_, err := mailTemplates.ParseGlob(templatePattern)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !util.IsEmail(setting.Smtp.FromAddress) {
|
|
|
|
return errors.New("Invalid email address for smpt from_adress config")
|
|
|
|
}
|
|
|
|
|
2015-06-08 03:57:01 -05:00
|
|
|
if setting.EmailCodeValidMinutes == 0 {
|
|
|
|
setting.EmailCodeValidMinutes = 120
|
|
|
|
}
|
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var dispatchMail = func(cmd *m.SendEmailCommand) error {
|
|
|
|
return bus.Dispatch(cmd)
|
2015-06-04 10:23:46 -05:00
|
|
|
}
|
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
func subjectTemplateFunc(obj map[string]interface{}, value string) string {
|
|
|
|
obj["value"] = value
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendResetPasswordEmail(cmd *m.SendResetPasswordEmailCommand) error {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
2015-06-08 03:57:01 -05:00
|
|
|
var data = getMailTmplData(cmd.User)
|
2015-06-08 06:39:02 -05:00
|
|
|
code := createUserEmailCode(cmd.User, nil)
|
2015-06-05 04:08:19 -05:00
|
|
|
data["Code"] = code
|
|
|
|
|
|
|
|
mailTemplates.ExecuteTemplate(&buffer, tmplResetPassword, data)
|
|
|
|
|
|
|
|
dispatchMail(&m.SendEmailCommand{
|
|
|
|
To: []string{cmd.User.Email},
|
|
|
|
From: setting.Smtp.FromAddress,
|
|
|
|
Subject: data["Subject"].(map[string]interface{})["value"].(string),
|
|
|
|
Body: buffer.String(),
|
|
|
|
})
|
|
|
|
|
2015-06-05 01:15:38 -05:00
|
|
|
return nil
|
2015-06-04 10:23:46 -05:00
|
|
|
}
|
2015-06-05 04:08:19 -05:00
|
|
|
|
2015-06-08 06:39:02 -05:00
|
|
|
func validateResetPasswordCode(query *m.ValidateResetPasswordCodeQuery) error {
|
|
|
|
login := getLoginForEmailCode(query.Code)
|
|
|
|
if login == "" {
|
|
|
|
return m.ErrInvalidEmailCode
|
|
|
|
}
|
2015-06-05 04:08:19 -05:00
|
|
|
|
2015-06-08 06:39:02 -05:00
|
|
|
userQuery := m.GetUserByLoginQuery{LoginOrEmail: login}
|
|
|
|
if err := bus.Dispatch(&userQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-08 03:57:01 -05:00
|
|
|
|
2015-06-08 06:39:02 -05:00
|
|
|
if !validateUserEmailCode(userQuery.Result, query.Code) {
|
|
|
|
return m.ErrInvalidEmailCode
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = userQuery.Result
|
|
|
|
return nil
|
|
|
|
}
|