authelia/internal/handlers/util.go

48 lines
1.2 KiB
Go

package handlers
import (
"fmt"
"github.com/authelia/authelia/v4/internal/authentication"
"github.com/authelia/authelia/v4/internal/middlewares"
"github.com/authelia/authelia/v4/internal/templates"
)
func ctxLogEvent(ctx *middlewares.AutheliaCtx, username, description string, eventDetails map[string]any) {
var (
details *authentication.UserDetails
err error
)
ctx.Logger.Debugf("Getting user details for notification")
// Send Notification.
if details, err = ctx.Providers.UserProvider.GetDetails(username); err != nil {
ctx.Logger.Error(err)
return
}
if len(details.Emails) == 0 {
ctx.Logger.Error(fmt.Errorf("user %s has no email address configured", username))
return
}
data := templates.EmailEventValues{
Title: description,
DisplayName: details.DisplayName,
RemoteIP: ctx.RemoteIP().String(),
Details: eventDetails,
}
ctx.Logger.Debugf("Getting user addresses for notification")
addresses := details.Addresses()
ctx.Logger.Debugf("Sending an email to user %s (%s) to inform them of an important event.", username, addresses[0])
if err = ctx.Providers.Notifier.Send(ctx, addresses[0], description, ctx.Providers.Templates.GetEventEmailTemplate(), data); err != nil {
ctx.Logger.Error(err)
return
}
}