Commit 6088f834 by Torkel Ödegaard

feat(invite): inital pass on sending new user invite email, #2353

parent e92f2ece
......@@ -39,7 +39,6 @@ func Register(r *macaron.Macaron) {
r.Get("/dashboard/*", reqSignedIn, Index)
// sign up
r.Get("/signup", Index)
r.Post("/api/user/signup", bind(m.CreateUserCommand{}), wrap(SignUp))
// reset password
......
......@@ -3,7 +3,8 @@ package dtos
import m "github.com/grafana/grafana/pkg/models"
type AddInviteForm struct {
Email string `json:"email" binding:"Required"`
Name string `json:"name"`
Role m.RoleType `json:"role" binding:"Required"`
Email string `json:"email" binding:"Required"`
Name string `json:"name"`
Role m.RoleType `json:"role" binding:"Required"`
SkipEmails bool `json:"skipEmails"`
}
......@@ -5,6 +5,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
......@@ -22,6 +23,9 @@ func AddOrgInvite(c *middleware.Context, inviteDto dtos.AddInviteForm) Response
if !inviteDto.Role.IsValid() {
return ApiError(400, "Invalid role specified", nil)
}
if !util.IsEmail(inviteDto.Email) {
return ApiError(400, "Invalid email specified", nil)
}
// first try get existing user
userQuery := m.GetUserByLoginQuery{LoginOrEmail: inviteDto.Email}
......@@ -52,5 +56,24 @@ func AddOrgInvite(c *middleware.Context, inviteDto dtos.AddInviteForm) Response
return ApiError(500, "Failed to save invite to database", err)
}
// send invite email
if !inviteDto.SkipEmails {
emailCmd := m.SendEmailCommand{
To: []string{inviteDto.Email},
Template: "new_user_invite.html",
Data: map[string]interface{}{
"NameOrEmail": util.StringsFallback2(cmd.Name, cmd.Email),
"OrgName": c.OrgName,
"Email": c.Email,
"LinkUrl": setting.ToAbsUrl("signup/invited/" + cmd.Code),
"InvitedBy": util.StringsFallback2(c.Name, c.Email),
},
}
if err := bus.Dispatch(&emailCmd); err != nil {
return ApiError(500, "Failed to send email invite", err)
}
}
return ApiSuccess("ok, done!")
}
package util
func StringsFallback2(val1 string, val2 string) string {
if val1 != "" {
return val1
}
return val2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment