forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_email_send.go
116 lines (101 loc) · 3.03 KB
/
test_email_send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package forms
import (
"errors"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/mails"
)
const (
TestTemplateVerification = "verification"
TestTemplatePasswordReset = "password-reset"
TestTemplateEmailChange = "email-change"
TestTemplateOTP = "otp"
TestTemplateAuthAlert = "login-alert"
)
// TestEmailSend is a email template test request form.
type TestEmailSend struct {
app core.App
Email string `form:"email" json:"email"`
Template string `form:"template" json:"template"`
Collection string `form:"collection" json:"collection"` // optional, fallbacks to _superusers
}
// NewTestEmailSend creates and initializes new TestEmailSend form.
func NewTestEmailSend(app core.App) *TestEmailSend {
return &TestEmailSend{app: app}
}
// Validate makes the form validatable by implementing [validation.Validatable] interface.
func (form *TestEmailSend) Validate() error {
return validation.ValidateStruct(form,
validation.Field(
&form.Collection,
validation.Length(1, 255),
validation.By(form.checkAuthCollection),
),
validation.Field(
&form.Email,
validation.Required,
validation.Length(1, 255),
is.EmailFormat,
),
validation.Field(
&form.Template,
validation.Required,
validation.In(
TestTemplateVerification,
TestTemplatePasswordReset,
TestTemplateEmailChange,
TestTemplateOTP,
TestTemplateAuthAlert,
),
),
)
}
func (form *TestEmailSend) checkAuthCollection(value any) error {
v, _ := value.(string)
if v == "" {
return nil // nothing to check
}
c, _ := form.app.FindCollectionByNameOrId(v)
if c == nil || !c.IsAuth() {
return validation.NewError("validation_invalid_auth_collection", "Must be a valid auth collection id or name.")
}
return nil
}
// Submit validates and sends a test email to the form.Email address.
func (form *TestEmailSend) Submit() error {
if err := form.Validate(); err != nil {
return err
}
collectionIdOrName := form.Collection
if collectionIdOrName == "" {
collectionIdOrName = core.CollectionNameSuperusers
}
collection, err := form.app.FindCollectionByNameOrId(collectionIdOrName)
if err != nil {
return err
}
record := core.NewRecord(collection)
for _, field := range collection.Fields {
if field.GetHidden() {
continue
}
record.Set(field.GetName(), "__pb_test_"+field.GetName()+"__")
}
record.RefreshTokenKey()
record.SetEmail(form.Email)
switch form.Template {
case TestTemplateVerification:
return mails.SendRecordVerification(form.app, record)
case TestTemplatePasswordReset:
return mails.SendRecordPasswordReset(form.app, record)
case TestTemplateEmailChange:
return mails.SendRecordChangeEmail(form.app, record, form.Email)
case TestTemplateOTP:
return mails.SendRecordOTP(form.app, record, "_PB_TEST_OTP_ID_", "123456")
case TestTemplateAuthAlert:
return mails.SendRecordAuthAlert(form.app, record)
default:
return errors.New("unknown template " + form.Template)
}
}