forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.go
174 lines (144 loc) · 4.45 KB
/
record.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package mails
import (
"html/template"
"log"
"net/mail"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/mails/templates"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/settings"
"github.com/pocketbase/pocketbase/tokens"
"github.com/pocketbase/pocketbase/tools/mailer"
)
// SendRecordPasswordReset sends a password reset request email to the specified user.
func SendRecordPasswordReset(app core.App, authRecord *models.Record) error {
token, tokenErr := tokens.NewRecordResetPasswordToken(app, authRecord)
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.ResetPasswordTemplate)
if err != nil {
return err
}
message := &mailer.Message{
From: mail.Address{
Name: app.Settings().Meta.SenderName,
Address: app.Settings().Meta.SenderAddress,
},
To: []mail.Address{{Address: authRecord.Email()}},
Subject: subject,
HTML: body,
}
event := new(core.MailerRecordEvent)
event.MailClient = mailClient
event.Message = message
event.Collection = authRecord.Collection()
event.Record = authRecord
event.Meta = map[string]any{"token": token}
sendErr := app.OnMailerBeforeRecordResetPasswordSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.MailClient.Send(e.Message)
})
if sendErr == nil {
if err := app.OnMailerAfterRecordResetPasswordSend().Trigger(event); err != nil && app.IsDebug() {
log.Println(err)
}
}
return sendErr
}
// SendRecordVerification sends a verification request email to the specified user.
func SendRecordVerification(app core.App, authRecord *models.Record) error {
token, tokenErr := tokens.NewRecordVerifyToken(app, authRecord)
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.VerificationTemplate)
if err != nil {
return err
}
message := &mailer.Message{
From: mail.Address{
Name: app.Settings().Meta.SenderName,
Address: app.Settings().Meta.SenderAddress,
},
To: []mail.Address{{Address: authRecord.Email()}},
Subject: subject,
HTML: body,
}
event := new(core.MailerRecordEvent)
event.MailClient = mailClient
event.Message = message
event.Collection = authRecord.Collection()
event.Record = authRecord
event.Meta = map[string]any{"token": token}
sendErr := app.OnMailerBeforeRecordVerificationSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.MailClient.Send(e.Message)
})
if sendErr == nil {
if err := app.OnMailerAfterRecordVerificationSend().Trigger(event); err != nil && app.IsDebug() {
log.Println(err)
}
}
return sendErr
}
// SendUserChangeEmail sends a change email confirmation email to the specified user.
func SendRecordChangeEmail(app core.App, record *models.Record, newEmail string) error {
token, tokenErr := tokens.NewRecordChangeEmailToken(app, record, newEmail)
if tokenErr != nil {
return tokenErr
}
mailClient := app.NewMailClient()
subject, body, err := resolveEmailTemplate(app, token, app.Settings().Meta.ConfirmEmailChangeTemplate)
if err != nil {
return err
}
message := &mailer.Message{
From: mail.Address{
Name: app.Settings().Meta.SenderName,
Address: app.Settings().Meta.SenderAddress,
},
To: []mail.Address{{Address: newEmail}},
Subject: subject,
HTML: body,
}
event := new(core.MailerRecordEvent)
event.MailClient = mailClient
event.Message = message
event.Collection = record.Collection()
event.Record = record
event.Meta = map[string]any{
"token": token,
"newEmail": newEmail,
}
sendErr := app.OnMailerBeforeRecordChangeEmailSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.MailClient.Send(e.Message)
})
if sendErr == nil {
if err := app.OnMailerAfterRecordChangeEmailSend().Trigger(event); err != nil && app.IsDebug() {
log.Println(err)
}
}
return sendErr
}
func resolveEmailTemplate(
app core.App,
token string,
emailTemplate settings.EmailTemplate,
) (subject string, body string, err error) {
subject, rawBody, _ := emailTemplate.Resolve(
app.Settings().Meta.AppName,
app.Settings().Meta.AppUrl,
token,
)
params := struct {
HtmlContent template.HTML
}{
HtmlContent: template.HTML(rawBody),
}
body, err = resolveTemplateContent(params, templates.Layout, templates.HtmlBody)
if err != nil {
return "", "", err
}
return subject, body, nil
}