-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
notifies admins on new user registration #27128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
earl-warren
wants to merge
10
commits into
go-gitea:main
from
earl-warren:wip-gitea-notify-user-creation
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6a4dade
notifies admins on new user registration
realaravinth c65ac7c
performance bottleneck
JakobDev da0c773
Update custom/conf/app.example.ini
earl-warren 90ebec7
Update options/locale/locale_en-US.ini
earl-warren c3e9f90
Update services/notify/null.go
earl-warren 8c79764
Update services/notify/notify.go
earl-warren 5338f81
s/forgejo.org/example.com/
earl-warren 99dbec5
s/NOTIFY_NEW_SIGN_UPS/SEND_NOTIFICATION_EMAIL_ON_NEW_USER/
earl-warren ac8bfe4
move template to mail/notify/admin_new_user.tmpl
earl-warren d852d5c
update config-cheat-sheet
earl-warren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright 2023 The Gitea Authors. All rights reserved. | ||
| // Copyright 2023 The Forgejo Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
| package mailer | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "strconv" | ||
|
|
||
| user_model "code.gitea.io/gitea/models/user" | ||
| "code.gitea.io/gitea/modules/base" | ||
| "code.gitea.io/gitea/modules/log" | ||
| "code.gitea.io/gitea/modules/setting" | ||
| "code.gitea.io/gitea/modules/templates" | ||
| "code.gitea.io/gitea/modules/translation" | ||
| ) | ||
|
|
||
| const ( | ||
| tplNewUserMail base.TplName = "notify/admin_new_user" | ||
| ) | ||
|
|
||
| var sa = SendAsyncs | ||
|
|
||
| // MailNewUser sends notification emails on new user registrations to all admins | ||
| func MailNewUser(ctx context.Context, u *user_model.User) { | ||
| if !setting.Admin.SendNotificationEmailOnNewUser { | ||
| return | ||
| } | ||
|
|
||
| if setting.MailService == nil { | ||
| // No mail service configured | ||
| return | ||
| } | ||
|
|
||
| recipients, err := user_model.GetAllAdmins(ctx) | ||
| if err != nil { | ||
| log.Error("user_model.GetAllAdmins: %v", err) | ||
| return | ||
| } | ||
|
|
||
| langMap := make(map[string][]string) | ||
| for _, r := range recipients { | ||
| langMap[r.Language] = append(langMap[r.Language], r.Email) | ||
| } | ||
|
|
||
| for lang, tos := range langMap { | ||
| mailNewUser(ctx, u, lang, tos) | ||
| } | ||
| } | ||
|
|
||
| func mailNewUser(ctx context.Context, u *user_model.User, lang string, tos []string) { | ||
| locale := translation.NewLocale(lang) | ||
|
|
||
| subject := locale.Tr("mail.admin.new_user.subject", u.Name) | ||
| manageUserURL := setting.AppSubURL + "/admin/users/" + strconv.FormatInt(u.ID, 10) | ||
| body := locale.Tr("mail.admin.new_user.text", manageUserURL) | ||
| mailMeta := map[string]any{ | ||
| "NewUser": u, | ||
| "Subject": subject, | ||
| "Body": body, | ||
| "Language": locale.Language(), | ||
| "locale": locale, | ||
| "Str2html": templates.Str2html, | ||
| } | ||
|
|
||
| var mailBody bytes.Buffer | ||
|
|
||
| if err := bodyTemplates.ExecuteTemplate(&mailBody, string(tplNewUserMail), mailMeta); err != nil { | ||
| log.Error("ExecuteTemplate [%s]: %v", string(tplNewUserMail)+"/body", err) | ||
| return | ||
| } | ||
|
|
||
| msgs := make([]*Message, 0, len(tos)) | ||
| for _, to := range tos { | ||
| msg := NewMessage(to, subject, mailBody.String()) | ||
| msg.Info = subject | ||
| msgs = append(msgs, msg) | ||
| } | ||
| sa(msgs) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright 2023 The Gitea Authors. All rights reserved. | ||
| // Copyright 2023 The Forgejo Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package mailer | ||
|
|
||
| import ( | ||
| "context" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "code.gitea.io/gitea/models/db" | ||
| user_model "code.gitea.io/gitea/models/user" | ||
| "code.gitea.io/gitea/modules/setting" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func getTestUsers() []*user_model.User { | ||
| admin := new(user_model.User) | ||
| admin.Name = "admin" | ||
| admin.IsAdmin = true | ||
| admin.Language = "en_US" | ||
| admin.Email = "admin@example.com" | ||
|
|
||
| newUser := new(user_model.User) | ||
| newUser.Name = "new_user" | ||
| newUser.Language = "en_US" | ||
| newUser.IsAdmin = false | ||
| newUser.Email = "new_user@example.com" | ||
| newUser.LastLoginUnix = 1693648327 | ||
| newUser.CreatedUnix = 1693648027 | ||
|
|
||
| user_model.CreateUser(db.DefaultContext, admin) | ||
| user_model.CreateUser(db.DefaultContext, newUser) | ||
|
|
||
| users := make([]*user_model.User, 0) | ||
| users = append(users, admin) | ||
| users = append(users, newUser) | ||
|
|
||
| return users | ||
| } | ||
|
|
||
| func cleanUpUsers(ctx context.Context, users []*user_model.User) { | ||
| for _, u := range users { | ||
| db.DeleteByID(ctx, u.ID, new(user_model.User)) | ||
| } | ||
| } | ||
|
|
||
| func TestAdminNotificationMail_test(t *testing.T) { | ||
| mailService := setting.Mailer{ | ||
| From: "test@example.com", | ||
| Protocol: "dummy", | ||
| } | ||
|
|
||
| setting.MailService = &mailService | ||
| setting.Domain = "localhost" | ||
| setting.AppSubURL = "http://localhost" | ||
|
|
||
| // test with SEND_NOTIFICATION_EMAIL_ON_NEW_USER enabled | ||
| setting.Admin.SendNotificationEmailOnNewUser = true | ||
|
|
||
| ctx := context.Background() | ||
| NewContext(ctx) | ||
|
|
||
| users := getTestUsers() | ||
| oldSendAsyncs := sa | ||
| defer func() { | ||
| sa = oldSendAsyncs | ||
| cleanUpUsers(ctx, users) | ||
| }() | ||
|
|
||
| sa = func(msgs []*Message) { | ||
| assert.Equal(t, len(msgs), 1, "Test provides only one admin user, so only one email must be sent") | ||
| assert.Equal(t, msgs[0].To, users[0].Email, "checks if the recipient is the admin of the instance") | ||
| manageUserURL := "/admin/users/" + strconv.FormatInt(users[1].ID, 10) | ||
| assert.True(t, strings.ContainsAny(msgs[0].Body, manageUserURL), "checks if the message contains the link to manage the newly created user from the admin panel") | ||
| } | ||
| MailNewUser(ctx, users[1]) | ||
|
|
||
| // test with SEND_NOTIFICATION_EMAIL_ON_NEW_USER disabled; emails shouldn't be sent | ||
| setting.Admin.SendNotificationEmailOnNewUser = false | ||
| sa = func(msgs []*Message) { | ||
| assert.Equal(t, 1, 0, "this shouldn't execute. MailNewUser must exit early since SEND_NOTIFICATION_EMAIL_ON_NEW_USER is disabled") | ||
| } | ||
|
|
||
| MailNewUser(ctx, users[1]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
| <title>{{.Subject}}</title> | ||
|
|
||
| <style> | ||
| blockquote { padding-left: 1em; margin: 1em 0; border-left: 1px solid grey; color: #777} | ||
| .footer { font-size:small; color:#666;} | ||
| </style> | ||
|
|
||
| </head> | ||
|
|
||
| <body> | ||
| <ul> | ||
| <h3>{{.locale.Tr "mail.admin.new_user.user_info"}}</h3> | ||
| <li>{{.locale.Tr "admin.users.created"}}: {{DateTime "full" .NewUser.LastLoginUnix}}</li> | ||
| <li>{{.locale.Tr "admin.users.last_login"}}: {{DateTime "full" .NewUser.CreatedUnix}}</li> | ||
| </ul> | ||
| <p> {{.Body | Str2html}} </p> | ||
| </body> | ||
| </html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I summon @techknowlogick