jit_approved.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package email
  2. import (
  3. "context"
  4. "fmt"
  5. "log/slog"
  6. "github.com/gravitl/netmaker/models"
  7. "github.com/gravitl/netmaker/schema"
  8. )
  9. // JITApprovedMail - mail for notifying users when their JIT request is approved
  10. type JITApprovedMail struct {
  11. BodyBuilder EmailBodyBuilder
  12. Grant *schema.JITGrant
  13. Request *schema.JITRequest
  14. Network models.Network
  15. }
  16. // SendJITApprovalEmail - sends email notification to user when JIT request is approved
  17. func SendJITApprovalEmail(grant *schema.JITGrant, request *schema.JITRequest, network models.Network) error {
  18. mail := JITApprovedMail{
  19. BodyBuilder: &EmailBodyBuilderWithH1HeadlineAndImage{},
  20. Grant: grant,
  21. Request: request,
  22. Network: network,
  23. }
  24. // Skip sending email if username is not a valid email address
  25. if !IsValid(request.UserName) {
  26. slog.Warn("skipping JIT request approval email with non-email username", "user", request.UserName)
  27. return nil
  28. }
  29. notification := Notification{
  30. RecipientMail: request.UserName, // Assuming username is email
  31. RecipientName: request.UserName,
  32. }
  33. return GetClient().SendEmail(context.Background(), notification, mail)
  34. }
  35. // GetSubject - gets the subject of the email
  36. func (mail JITApprovedMail) GetSubject(info Notification) string {
  37. return fmt.Sprintf("JIT Access Approved: %s", mail.Network.NetID)
  38. }
  39. // GetBody - gets the body of the email
  40. func (mail JITApprovedMail) GetBody(info Notification) string {
  41. content := mail.BodyBuilder.
  42. WithHeadline("JIT Access Approved").
  43. WithParagraph(fmt.Sprintf("Your request for Just-In-Time access to network <strong>%s</strong> has been approved.", mail.Network.NetID)).
  44. WithParagraph("Access Details:").
  45. WithHtml("<ul>").
  46. WithHtml(fmt.Sprintf("<li><strong>Network:</strong> %s</li>", mail.Network.NetID)).
  47. WithHtml(fmt.Sprintf("<li><strong>Granted At:</strong> %s</li>", formatUTCTime(mail.Grant.GrantedAt))).
  48. WithHtml(fmt.Sprintf("<li><strong>Expires At:</strong> %s</li>", formatUTCTime(mail.Grant.ExpiresAt))).
  49. WithHtml(fmt.Sprintf("<li><strong>Approved By:</strong> %s</li>", mail.Request.ApprovedBy)).
  50. WithHtml("</ul>").
  51. WithParagraph("You can now connect to this network using the Netmaker Desktop App. Your access will automatically expire when the granted duration ends.").
  52. WithParagraph("Best Regards,").
  53. WithParagraph("The Netmaker Team").
  54. Build()
  55. return content
  56. }