2015-06-04 10:23:46 -05:00
|
|
|
package notifications
|
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"html/template"
|
|
|
|
"path/filepath"
|
2015-06-04 10:23:46 -05:00
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
"github.com/Unknwon/com"
|
|
|
|
"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-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-05 04:08:19 -05:00
|
|
|
code := CreateUserActiveCode(cmd.User, nil)
|
|
|
|
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
|
|
|
|
|
|
|
func CreateUserActiveCode(u *m.User, startInf interface{}) string {
|
|
|
|
minutes := setting.EmailCodeValidMinutes
|
|
|
|
data := com.ToStr(u.Id) + u.Email + u.Login + u.Password + u.Rands
|
|
|
|
code := CreateTimeLimitCode(data, minutes, startInf)
|
|
|
|
|
|
|
|
// add tail hex username
|
|
|
|
code += hex.EncodeToString([]byte(u.Login))
|
|
|
|
return code
|
|
|
|
}
|
2015-06-08 03:57:01 -05:00
|
|
|
|
|
|
|
// // verify active code when active account
|
|
|
|
// func VerifyUserActiveCode(code string) (user *User) {
|
|
|
|
// minutes := setting.Service.ActiveCodeLives
|
|
|
|
//
|
|
|
|
// if user = getVerifyUser(code); user != nil {
|
|
|
|
// // time limit code
|
|
|
|
// prefix := code[:base.TimeLimitCodeLength]
|
|
|
|
// data := com.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
|
|
|
|
//
|
|
|
|
// if base.VerifyTimeLimitCode(data, minutes, prefix) {
|
|
|
|
// return user
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // verify active code when active account
|
|
|
|
// func VerifyUserActiveCode(code string) (user *User) {
|
|
|
|
// minutes := setting.Service.ActiveCodeLives
|
|
|
|
//
|
|
|
|
// if user = getVerifyUser(code); user != nil {
|
|
|
|
// // time limit code
|
|
|
|
// prefix := code[:base.TimeLimitCodeLength]
|
|
|
|
// data := com.ToStr(user.Id) + user.Email + user.LowerName + user.Passwd + user.Rands
|
|
|
|
//
|
|
|
|
// if base.VerifyTimeLimitCode(data, minutes, prefix) {
|
|
|
|
// return user
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return nil
|
|
|
|
// }
|