Skip to content
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

Send registration email on user autoregistration #16523

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions services/auth/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/services/mailer"

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

mailer.SendRegisterNotifyMail(user)

return user
}
8 changes: 7 additions & 1 deletion services/auth/source/ldap/source_authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/services/mailer"
)

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

err := models.CreateUser(user)
if err != nil {
return user, err
}

mailer.SendRegisterNotifyMail(user)

if err == nil && isAttributeSSHPublicKeySet && models.AddPublicKeysBySource(user, source.loginSource, sr.SSHPublicKey) {
if isAttributeSSHPublicKeySet && models.AddPublicKeysBySource(user, source.loginSource, sr.SSHPublicKey) {
err = models.RewriteAllPublicKeys()
}

Expand Down
10 changes: 9 additions & 1 deletion services/auth/source/pam/source_authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth/pam"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/mailer"

"github.com/google/uuid"
)
Expand Down Expand Up @@ -58,5 +59,12 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
LoginName: login, // This is what the user typed in
IsActive: true,
}
return user, models.CreateUser(user)

if err := models.CreateUser(user); err != nil {
return user, err
}

mailer.SendRegisterNotifyMail(user)

return user, nil
}
10 changes: 9 additions & 1 deletion services/auth/source/smtp/source_authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/mailer"
)

// Authenticate queries if the provided login/password is authenticates against the SMTP server
Expand Down Expand Up @@ -74,5 +75,12 @@ func (source *Source) Authenticate(user *models.User, login, password string) (*
LoginName: login,
IsActive: true,
}
return user, models.CreateUser(user)

if err := models.CreateUser(user); err != nil {
return user, err
}

mailer.SendRegisterNotifyMail(user)

return user, nil
}
4 changes: 4 additions & 0 deletions services/auth/sspi_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/services/auth/source/sspi"
"code.gitea.io/gitea/services/mailer"

gouuid "github.com/google/uuid"
"github.com/quasoft/websspi"
Expand Down Expand Up @@ -197,6 +198,9 @@ func (s *SSPI) newUser(username string, cfg *sspi.Source) (*models.User, error)
if err := models.CreateUser(user); err != nil {
return nil, err
}

mailer.SendRegisterNotifyMail(user)

return user, nil
}

Expand Down
28 changes: 28 additions & 0 deletions services/mailer/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func InitMailRender(subjectTpl *texttmpl.Template, bodyTpl *template.Template) {

// SendTestMail sends a test mail
func SendTestMail(email string) error {
if setting.MailService == nil {
// No mail service configured
return nil
}
return gomail.Send(Sender, NewMessage([]string{email}, "Gitea Test Email!", "Gitea Test Email!").ToMessage())
}

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

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

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

// SendActivateEmailMail sends confirmation email to confirm new email address
func SendActivateEmailMail(u *models.User, email *models.EmailAddress) {
if setting.MailService == nil {
// No mail service configured
return
}
locale := translation.NewLocale(u.Language)
data := map[string]interface{}{
"DisplayName": u.DisplayName(),
Expand Down Expand Up @@ -129,6 +145,10 @@ func SendActivateEmailMail(u *models.User, email *models.EmailAddress) {

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

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

// SendCollaboratorMail sends mail notification to new collaborator.
func SendCollaboratorMail(u, doer *models.User, repo *models.Repository) {
if setting.MailService == nil {
// No mail service configured
return
}
locale := translation.NewLocale(u.Language)
repoName := repo.FullName()

Expand Down Expand Up @@ -344,6 +368,10 @@ func sanitizeSubject(subject string) string {

// SendIssueAssignedMail composes and sends issue assigned email
func SendIssueAssignedMail(issue *models.Issue, doer *models.User, content string, comment *models.Comment, recipients []*models.User) error {
if setting.MailService == nil {
// No mail service configured
return nil
}
langMap := make(map[string][]*models.User)
for _, user := range recipients {
langMap[user.Language] = append(langMap[user.Language], user)
Expand Down
11 changes: 11 additions & 0 deletions services/mailer/mail_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ package mailer
import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

// MailParticipantsComment sends new comment emails to repository watchers and mentioned people.
func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue *models.Issue, mentions []*models.User) error {
if setting.MailService == nil {
// No mail service configured
return nil
}

content := c.Content
if c.Type == models.CommentTypePullPush {
content = ""
Expand All @@ -30,6 +36,11 @@ func MailParticipantsComment(c *models.Comment, opType models.ActionType, issue

// MailMentionsComment sends email to users mentioned in a code comment
func MailMentionsComment(pr *models.PullRequest, c *models.Comment, mentions []*models.User) (err error) {
if setting.MailService == nil {
// No mail service configured
return nil
}

visited := make(map[int64]bool, len(mentions)+1)
visited[c.Poster.ID] = true
if err = mailIssueCommentBatch(
Expand Down
6 changes: 6 additions & 0 deletions services/mailer/mail_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

func fallbackMailSubject(issue *models.Issue) string {
Expand Down Expand Up @@ -163,6 +164,11 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*models.User, visite
// MailParticipants sends new issue thread created emails to repository watchers
// and mentioned people.
func MailParticipants(issue *models.Issue, doer *models.User, opType models.ActionType, mentions []*models.User) error {
if setting.MailService == nil {
// No mail service configured
return nil
}

content := issue.Content
if opType == models.ActionCloseIssue || opType == models.ActionClosePullRequest ||
opType == models.ActionReopenIssue || opType == models.ActionReopenPullRequest ||
Expand Down
5 changes: 5 additions & 0 deletions services/mailer/mail_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const (

// MailNewRelease send new release notify to all all repo watchers.
func MailNewRelease(rel *models.Release) {
if setting.MailService == nil {
// No mail service configured
return
}

watcherIDList, err := models.GetRepoWatchersIDs(rel.RepoID)
if err != nil {
log.Error("GetRepoWatchersIDs(%d): %v", rel.RepoID, err)
Expand Down
6 changes: 6 additions & 0 deletions services/mailer/mail_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import (
"fmt"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/translation"
)

// SendRepoTransferNotifyMail triggers a notification e-mail when a pending repository transfer was created
func SendRepoTransferNotifyMail(doer, newOwner *models.User, repo *models.Repository) error {
if setting.MailService == nil {
// No mail service configured
return nil
}

if newOwner.IsOrganization() {
users, err := models.GetUsersWhoCanCreateOrgRepo(newOwner.ID)
if err != nil {
Expand Down