Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Use realm from current membership for emails (#1445)
Browse files Browse the repository at this point in the history
* use realm from current membership for emails

* one more

* reset password only if membership exists
  • Loading branch information
whaught authored Dec 22, 2020
1 parent 618bd91 commit c014257
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 34 deletions.
27 changes: 6 additions & 21 deletions pkg/controller/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ import (
)

// SendInviteEmailFunc returns a function capable of sending a new user invitation.
func SendInviteEmailFunc(ctx context.Context, db *database.Database, h render.Renderer, email string) (auth.InviteUserEmailFunc, error) {
// Lookup the realm to get the email provider
realm := RealmFromContext(ctx)
if realm == nil {
return nil, nil
}

func SendInviteEmailFunc(ctx context.Context, db *database.Database, h render.Renderer, email string,
realm *database.Realm) (auth.InviteUserEmailFunc, error) {
// Lookup the email provider
emailer, err := realm.EmailProvider(db)
if err != nil {
Expand Down Expand Up @@ -77,13 +72,8 @@ func SendInviteEmailFunc(ctx context.Context, db *database.Database, h render.Re

// SendPasswordResetEmailFunc returns a function capable of sending a password
// reset for the given user.
func SendPasswordResetEmailFunc(ctx context.Context, db *database.Database, h render.Renderer, email string) (auth.ResetPasswordEmailFunc, error) {
// Lookup the realm to get the email provider
realm := RealmFromContext(ctx)
if realm == nil {
return nil, nil
}

func SendPasswordResetEmailFunc(ctx context.Context, db *database.Database, h render.Renderer, email string,
realm *database.Realm) (auth.ResetPasswordEmailFunc, error) {
// Lookup the email provider
emailer, err := realm.EmailProvider(db)
if err != nil {
Expand Down Expand Up @@ -129,13 +119,8 @@ func SendPasswordResetEmailFunc(ctx context.Context, db *database.Database, h re

// SendEmailVerificationEmailFunc returns a function capable of sending an email
// verification email.
func SendEmailVerificationEmailFunc(ctx context.Context, db *database.Database, h render.Renderer, email string) (auth.EmailVerificationEmailFunc, error) {
// Lookup the realm to get the email provider
realm := RealmFromContext(ctx)
if realm == nil {
return nil, nil
}

func SendEmailVerificationEmailFunc(ctx context.Context, db *database.Database, h render.Renderer, email string,
realm *database.Realm) (auth.EmailVerificationEmailFunc, error) {
// Lookup the email provider
emailer, err := realm.EmailProvider(db)
if err != nil {
Expand Down
15 changes: 11 additions & 4 deletions pkg/controller/login/reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"net/http"

"github.com/google/exposure-notifications-verification-server/internal/auth"
"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/google/exposure-notifications-verification-server/pkg/database"
)
Expand Down Expand Up @@ -74,11 +75,17 @@ func (c *Controller) HandleSubmitResetPassword() http.Handler {
return
}

// nil composer falls back to firebase and no custom message.
var resetComposer auth.ResetPasswordEmailFunc

membership := controller.MembershipFromContext(ctx)
// Build the emailer.
resetComposer, err := controller.SendPasswordResetEmailFunc(ctx, c.db, c.h, user.Email)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
if membership != nil {
resetComposer, err = controller.SendPasswordResetEmailFunc(ctx, c.db, c.h, user.Email, membership.Realm)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
}
}

// Reset the password.
Expand Down
9 changes: 3 additions & 6 deletions pkg/controller/login/verify_email_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ func (c *Controller) HandleSubmitVerifyEmail() http.Handler {
}
flash := controller.Flash(session)

currentUser := controller.UserFromContext(ctx)
if currentUser == nil {
controller.MissingUser(w, r, c.h)
return
}
membership := controller.MembershipFromContext(ctx)
currentUser := membership.User

var form FormData
if err := controller.BindForm(w, r, &form); err != nil {
Expand All @@ -68,7 +65,7 @@ func (c *Controller) HandleSubmitVerifyEmail() http.Handler {
}

// Build the email template.
verifyComposer, err := controller.SendEmailVerificationEmailFunc(ctx, c.db, c.h, currentUser.Email)
verifyComposer, err := controller.SendEmailVerificationEmailFunc(ctx, c.db, c.h, currentUser.Email, membership.Realm)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *Controller) HandleCreate() http.Handler {
return
}

inviteComposer, err := controller.SendInviteEmailFunc(ctx, c.db, c.h, user.Email)
inviteComposer, err := controller.SendInviteEmailFunc(ctx, c.db, c.h, user.Email, currentRealm)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/user/importbatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *Controller) importUsers(ctx context.Context,
}

// Create the invitation email composer.
inviteComposer, err := controller.SendInviteEmailFunc(ctx, c.db, c.h, user.Email)
inviteComposer, err := controller.SendInviteEmailFunc(ctx, c.db, c.h, user.Email, realm)
if err != nil {
batchErr = multierror.Append(batchErr, err)
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/user/reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *Controller) HandleResetPassword() http.Handler {
}

// Build the emailer.
resetComposer, err := controller.SendPasswordResetEmailFunc(ctx, c.db, c.h, user.Email)
resetComposer, err := controller.SendPasswordResetEmailFunc(ctx, c.db, c.h, user.Email, currentRealm)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
Expand Down

0 comments on commit c014257

Please sign in to comment.