From 9c72210b14529560bd966c363a10d911a5d9e71d Mon Sep 17 00:00:00 2001 From: Josh Freda Date: Tue, 2 Apr 2024 11:27:58 -0500 Subject: [PATCH] Enable sending an email to users not found in the Google Workspace directory --- internal/api/v2/me.go | 31 +++++++++++++++++++++++++++++++ internal/config/config.go | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/internal/api/v2/me.go b/internal/api/v2/me.go index 44440253f..2a065d322 100644 --- a/internal/api/v2/me.go +++ b/internal/api/v2/me.go @@ -88,6 +88,37 @@ func MeHandler(srv server.Server) http.Handler { nil, "user_email", userEmail, ) + + // If configured, send an email to the user to notify them that their + // account was not found in the directory. + if srv.Config.Email != nil && srv.Config.Email.Enabled && + srv.Config.GoogleWorkspace != nil && + srv.Config.GoogleWorkspace.UserNotFoundEmail != nil && + srv.Config.GoogleWorkspace.UserNotFoundEmail.Enabled && + srv.Config.GoogleWorkspace.UserNotFoundEmail.Body != "" && + srv.Config.GoogleWorkspace.UserNotFoundEmail.Subject != "" { + _, err = srv.GWService.SendEmail( + []string{userEmail}, + srv.Config.Email.FromAddress, + srv.Config.GoogleWorkspace.UserNotFoundEmail.Subject, + srv.Config.GoogleWorkspace.UserNotFoundEmail.Body, + ) + if err != nil { + srv.Logger.Error("error sending user not found email", + "error", err, + "method", r.Method, + "path", r.URL.Path, + "user_email", userEmail, + ) + } else { + srv.Logger.Info("user not found email sent", + "method", r.Method, + "path", r.URL.Path, + "user_email", userEmail, + ) + } + } + return } p := ppl[0] diff --git a/internal/config/config.go b/internal/config/config.go index 199248443..5f6e19ebb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -232,6 +232,10 @@ type GoogleWorkspace struct { // create_docs_as_user is true in the auth block, so document notification // settings will be the same as when a user creates their own document. TemporaryDraftsFolder string `hcl:"temporary_drafts_folder,optional"` + + // UserNotFoundEmail is the configuration to send an email when a user is not + // found in Google Workspace. + UserNotFoundEmail *GoogleWorkspaceUserNotFoundEmail `hcl:"user_not_found_email,block"` } // GoogleWorkspaceOAuth2 is the configuration to use OAuth 2.0 to access Google @@ -249,6 +253,20 @@ type GoogleWorkspaceOAuth2 struct { RedirectURI string `hcl:"redirect_uri,optional"` } +// GoogleWorkspaceUserNotFoundEmail is the configuration to send an email when a +// user is not found in Google Workspace. +type GoogleWorkspaceUserNotFoundEmail struct { + // Body is the body of the email. + Body string `hcl:"body,optional"` + + // Enabled enables sending an email when a user is not found in Google + // Workspace. + Enabled bool `hcl:"enabled,optional"` + + // Subject is the subject of the email. + Subject string `hcl:"subject,optional"` +} + // Jira is the configuration for Hermes to work with Jira. type Jira struct { // APIToken is the API token for authenticating to Jira.