Skip to content

Commit 162c32a

Browse files
zeripath6543
andauthored
Send registration email on user autoregistration (#16523)
When users login and are autoregistered send email notification. Fix #16178 * Protect public functions within the mailer by testing if the mailer is configured Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent e29e163 commit 162c32a

10 files changed

+88
-3
lines changed

services/auth/reverseproxy.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/modules/setting"
1515
"code.gitea.io/gitea/modules/web/middleware"
16+
"code.gitea.io/gitea/services/mailer"
1617

1718
gouuid "github.com/google/uuid"
1819
)
@@ -112,5 +113,7 @@ func (r *ReverseProxy) newUser(req *http.Request) *models.User {
112113
return nil
113114
}
114115

116+
mailer.SendRegisterNotifyMail(user)
117+
115118
return user
116119
}

services/auth/source/ldap/source_authenticate.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/services/mailer"
1213
)
1314

1415
// Authenticate queries if login/password is valid against the LDAP directory pool,
@@ -84,8 +85,13 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
8485
}
8586

8687
err := models.CreateUser(user)
88+
if err != nil {
89+
return user, err
90+
}
91+
92+
mailer.SendRegisterNotifyMail(user)
8793

88-
if err == nil && isAttributeSSHPublicKeySet && models.AddPublicKeysBySource(user, source.loginSource, sr.SSHPublicKey) {
94+
if isAttributeSSHPublicKeySet && models.AddPublicKeysBySource(user, source.loginSource, sr.SSHPublicKey) {
8995
err = models.RewriteAllPublicKeys()
9096
}
9197

services/auth/source/pam/source_authenticate.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/auth/pam"
1313
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/services/mailer"
1415

1516
"github.com/google/uuid"
1617
)
@@ -58,5 +59,12 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
5859
LoginName: login, // This is what the user typed in
5960
IsActive: true,
6061
}
61-
return user, models.CreateUser(user)
62+
63+
if err := models.CreateUser(user); err != nil {
64+
return user, err
65+
}
66+
67+
mailer.SendRegisterNotifyMail(user)
68+
69+
return user, nil
6270
}

services/auth/source/smtp/source_authenticate.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/util"
15+
"code.gitea.io/gitea/services/mailer"
1516
)
1617

1718
// Authenticate queries if the provided login/password is authenticates against the SMTP server
@@ -74,5 +75,12 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
7475
LoginName: login,
7576
IsActive: true,
7677
}
77-
return user, models.CreateUser(user)
78+
79+
if err := models.CreateUser(user); err != nil {
80+
return user, err
81+
}
82+
83+
mailer.SendRegisterNotifyMail(user)
84+
85+
return user, nil
7886
}

services/auth/sspi_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/templates"
1717
"code.gitea.io/gitea/modules/web/middleware"
1818
"code.gitea.io/gitea/services/auth/source/sspi"
19+
"code.gitea.io/gitea/services/mailer"
1920

2021
gouuid "github.com/google/uuid"
2122
"github.com/quasoft/websspi"
@@ -197,6 +198,9 @@ func (s *SSPI) newUser(username string, cfg *sspi.Source) (*models.User, error)
197198
if err := models.CreateUser(user); err != nil {
198199
return nil, err
199200
}
201+
202+
mailer.SendRegisterNotifyMail(user)
203+
200204
return user, nil
201205
}
202206

services/mailer/mail.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func InitMailRender(subjectTpl *texttmpl.Template, bodyTpl *template.Template) {
5757

5858
// SendTestMail sends a test mail
5959
func SendTestMail(email string) error {
60+
if setting.MailService == nil {
61+
// No mail service configured
62+
return nil
63+
}
6064
return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").ToMessage())
6165
}
6266

@@ -90,17 +94,29 @@ func sendUserMail(language string, u *models.User, tpl base.TplName, code, subje
9094

9195
// SendActivateAccountMail sends an activation mail to the user (new user registration)
9296
func SendActivateAccountMail(locale translation.Locale, u *models.User) {
97+
if setting.MailService == nil {
98+
// No mail service configured
99+
return
100+
}
93101
sendUserMail(locale.Language(), u, mailAuthActivate, u.GenerateEmailActivateCode(u.Email), locale.Tr("mail.activate_account"), "activate account")
94102
}
95103

96104
// SendResetPasswordMail sends a password reset mail to the user
97105
func SendResetPasswordMail(u *models.User) {
106+
if setting.MailService == nil {
107+
// No mail service configured
108+
return
109+
}
98110
locale := translation.NewLocale(u.Language)
99111
sendUserMail(u.Language, u, mailAuthResetPassword, u.GenerateEmailActivateCode(u.Email), locale.Tr("mail.reset_password"), "recover account")
100112
}
101113

102114
// SendActivateEmailMail sends confirmation email to confirm new email address
103115
func SendActivateEmailMail(u *models.User, email *models.EmailAddress) {
116+
if setting.MailService == nil {
117+
// No mail service configured
118+
return
119+
}
104120
locale := translation.NewLocale(u.Language)
105121
data := map[string]interface{}{
106122
"DisplayName": u.DisplayName(),
@@ -129,6 +145,10 @@ func SendActivateEmailMail(u *models.User, email *models.EmailAddress) {
129145

130146
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account.
131147
func SendRegisterNotifyMail(u *models.User) {
148+
if setting.MailService == nil {
149+
// No mail service configured
150+
return
151+
}
132152
locale := translation.NewLocale(u.Language)
133153

134154
data := map[string]interface{}{
@@ -156,6 +176,10 @@ func SendRegisterNotifyMail(u *models.User) {
156176

157177
// SendCollaboratorMail sends mail notification to new collaborator.
158178
func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
179+
if setting.MailService == nil {
180+
// No mail service configured
181+
return
182+
}
159183
locale := translation.NewLocale(u.Language)
160184
repoName := repo.FullName()
161185

@@ -344,6 +368,10 @@ func sanitizeSubject(subject string) string {
344368

345369
// SendIssueAssignedMail composes and sends issue assigned email
346370
func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, recipients []*models.User) error {
371+
if setting.MailService == nil {
372+
// No mail service configured
373+
return nil
374+
}
347375
langMap := make(map[string][]*models.User)
348376
for _, user := range recipients {
349377
langMap[user.Language] = append(langMap[user.Language], user)

services/mailer/mail_comment.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ package mailer
77
import (
88
"code.gitea.io/gitea/models"
99
"code.gitea.io/gitea/modules/log"
10+
"code.gitea.io/gitea/modules/setting"
1011
)
1112

1213
// MailParticipantsComment sends new comment emails to repository watchers and mentioned people.
1314
func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*models.User) error {
15+
if setting.MailService == nil {
16+
// No mail service configured
17+
return nil
18+
}
19+
1420
content := c.Content
1521
if c.Type == models.CommentTypePullPush {
1622
content = ""
@@ -30,6 +36,11 @@ func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue
3036

3137
// MailMentionsComment sends email to users mentioned in a code comment
3238
func MailMentionsComment(pr *models.PullRequest, c *models.Comment, mentions []*models.User) (err error) {
39+
if setting.MailService == nil {
40+
// No mail service configured
41+
return nil
42+
}
43+
3344
visited := make(map[int64]bool, len(mentions)+1)
3445
visited[c.Poster.ID] = true
3546
if err = mailIssueCommentBatch(

services/mailer/mail_issue.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/log"
12+
"code.gitea.io/gitea/modules/setting"
1213
)
1314

1415
func fallbackMailSubject(issue *models.Issue) string {
@@ -163,6 +164,11 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*models.User, visite
163164
// MailParticipants sends new issue thread created emails to repository watchers
164165
// and mentioned people.
165166
func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType, mentions []*models.User) error {
167+
if setting.MailService == nil {
168+
// No mail service configured
169+
return nil
170+
}
171+
166172
content := issue.Content
167173
if opType == models.ActionCloseIssue || opType == models.ActionClosePullRequest ||
168174
opType == models.ActionReopenIssue || opType == models.ActionReopenPullRequest ||

services/mailer/mail_release.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const (
2323

2424
// MailNewRelease send new release notify to all all repo watchers.
2525
func MailNewRelease(rel *models.Release) {
26+
if setting.MailService == nil {
27+
// No mail service configured
28+
return
29+
}
30+
2631
watcherIDList, err := models.GetRepoWatchersIDs(rel.RepoID)
2732
if err != nil {
2833
log.Error("GetRepoWatchersIDs(%d): %v", rel.RepoID, err)

services/mailer/mail_repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ import (
99
"fmt"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/setting"
1213
"code.gitea.io/gitea/modules/templates"
1314
"code.gitea.io/gitea/modules/translation"
1415
)
1516

1617
// SendRepoTransferNotifyMail triggers a notification e-mail when a pending repository transfer was created
1718
func SendRepoTransferNotifyMail(doer, newOwner *models.User, repo *models.Repository) error {
19+
if setting.MailService == nil {
20+
// No mail service configured
21+
return nil
22+
}
23+
1824
if newOwner.IsOrganization() {
1925
users, err := models.GetUsersWhoCanCreateOrgRepo(newOwner.ID)
2026
if err != nil {

0 commit comments

Comments
 (0)