Răsfoiți Sursa

Merge pull request #420 from porter-dev/beta.3.email-integration

handle password reset for github users
abelanger5 5 ani în urmă
părinte
comite
c09ddffbd9

+ 2 - 1
internal/config/config.go

@@ -38,6 +38,7 @@ type ServerConf struct {
 
 
 	SendgridAPIKey                  string `env:"SENDGRID_API_KEY"`
 	SendgridAPIKey                  string `env:"SENDGRID_API_KEY"`
 	SendgridPWResetTemplateID       string `env:"SENDGRID_PW_RESET_TEMPLATE_ID"`
 	SendgridPWResetTemplateID       string `env:"SENDGRID_PW_RESET_TEMPLATE_ID"`
+	SendgridPWGHTemplateID          string `env:"SENDGRID_PW_GH_TEMPLATE_ID"`
 	SendgridVerifyEmailTemplateID   string `env:"SENDGRID_VERIFY_EMAIL_TEMPLATE_ID"`
 	SendgridVerifyEmailTemplateID   string `env:"SENDGRID_VERIFY_EMAIL_TEMPLATE_ID"`
 	SendgridProjectInviteTemplateID string `env:"SENDGRID_INVITE_TEMPLATE_ID"`
 	SendgridProjectInviteTemplateID string `env:"SENDGRID_INVITE_TEMPLATE_ID"`
 	SendgridSenderEmail             string `env:"SENDGRID_SENDER_EMAIL"`
 	SendgridSenderEmail             string `env:"SENDGRID_SENDER_EMAIL"`
@@ -45,7 +46,7 @@ type ServerConf struct {
 	DOClientID          string `env:"DO_CLIENT_ID"`
 	DOClientID          string `env:"DO_CLIENT_ID"`
 	DOClientSecret      string `env:"DO_CLIENT_SECRET"`
 	DOClientSecret      string `env:"DO_CLIENT_SECRET"`
 	ProvisionerImageTag string `env:"PROV_IMAGE_TAG,default=latest"`
 	ProvisionerImageTag string `env:"PROV_IMAGE_TAG,default=latest"`
-	SegmentClientKey	string `env:"SEGMENT_CLIENT_KEY"`
+	SegmentClientKey    string `env:"SEGMENT_CLIENT_KEY"`
 }
 }
 
 
 // DBConf is the database configuration: if generated from environment variables,
 // DBConf is the database configuration: if generated from environment variables,

+ 33 - 3
internal/integrations/email/sendgrid.go

@@ -1,7 +1,6 @@
 package email
 package email
 
 
 import (
 import (
-	"fmt"
 	"os"
 	"os"
 
 
 	"github.com/sendgrid/sendgrid-go"
 	"github.com/sendgrid/sendgrid-go"
@@ -11,6 +10,7 @@ import (
 type SendgridClient struct {
 type SendgridClient struct {
 	APIKey                  string
 	APIKey                  string
 	PWResetTemplateID       string
 	PWResetTemplateID       string
+	PWGHTemplateID          string
 	VerifyEmailTemplateID   string
 	VerifyEmailTemplateID   string
 	ProjectInviteTemplateID string
 	ProjectInviteTemplateID string
 	SenderEmail             string
 	SenderEmail             string
@@ -48,6 +48,38 @@ func (client *SendgridClient) SendPWResetEmail(url, email string) error {
 	return err
 	return err
 }
 }
 
 
+func (client *SendgridClient) SendGHPWEmail(url, email string) error {
+	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
+	request.Method = "POST"
+
+	sgMail := &mail.SGMailV3{
+		Personalizations: []*mail.Personalization{
+			{
+				To: []*mail.Email{
+					{
+						Address: email,
+					},
+				},
+				DynamicTemplateData: map[string]interface{}{
+					"url":   url,
+					"email": email,
+				},
+			},
+		},
+		From: &mail.Email{
+			Address: client.SenderEmail,
+			Name:    "Porter",
+		},
+		TemplateID: client.PWGHTemplateID,
+	}
+
+	request.Body = mail.GetRequestBody(sgMail)
+
+	_, err := sendgrid.API(request)
+
+	return err
+}
+
 func (client *SendgridClient) SendEmailVerification(url, email string) error {
 func (client *SendgridClient) SendEmailVerification(url, email string) error {
 	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
 	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
 	request.Method = "POST"
 	request.Method = "POST"
@@ -84,8 +116,6 @@ func (client *SendgridClient) SendProjectInviteEmail(url, project, projectOwnerE
 	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
 	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
 	request.Method = "POST"
 	request.Method = "POST"
 
 
-	fmt.Println("GOT HERE", url, project, projectOwnerEmail, email)
-
 	sgMail := &mail.SGMailV3{
 	sgMail := &mail.SGMailV3{
 		Personalizations: []*mail.Personalization{
 		Personalizations: []*mail.Personalization{
 			{
 			{

+ 20 - 1
server/api/user_handler.go

@@ -541,7 +541,7 @@ func (app *App) InitiatePWResetUser(w http.ResponseWriter, r *http.Request) {
 	}
 	}
 
 
 	// check that the email exists; return 200 status code even if it doesn't
 	// check that the email exists; return 200 status code even if it doesn't
-	_, err := app.Repo.User.ReadUserByEmail(form.Email)
+	user, err := app.Repo.User.ReadUserByEmail(form.Email)
 
 
 	if err == gorm.ErrRecordNotFound {
 	if err == gorm.ErrRecordNotFound {
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
@@ -551,6 +551,25 @@ func (app *App) InitiatePWResetUser(w http.ResponseWriter, r *http.Request) {
 		return
 		return
 	}
 	}
 
 
+	// if the user is a Github user, send them a Github email
+	if user.GithubUserID != 0 {
+		sgClient := email.SendgridClient{
+			APIKey:         app.ServerConf.SendgridAPIKey,
+			PWGHTemplateID: app.ServerConf.SendgridPWGHTemplateID,
+			SenderEmail:    app.ServerConf.SendgridSenderEmail,
+		}
+
+		err = sgClient.SendGHPWEmail(
+			fmt.Sprintf("%s/api//oauth/login/github", app.ServerConf.ServerURL),
+			form.Email,
+		)
+
+		if err != nil {
+			app.handleErrorInternal(err, w)
+			return
+		}
+	}
+
 	// convert the form to a project model
 	// convert the form to a project model
 	pwReset, rawToken, err := form.ToPWResetToken()
 	pwReset, rawToken, err := form.ToPWResetToken()