2022-10-19 12:40:28 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-10-19 12:40:28 +00:00
|
|
|
|
|
|
|
package mailer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-08-31 16:26:13 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2022-10-19 12:40:28 +00:00
|
|
|
|
|
|
|
org_model "code.gitea.io/gitea/models/organization"
|
|
|
|
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/translation"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplTeamInviteMail base.TplName = "team_invite"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MailTeamInvite sends team invites
|
|
|
|
func MailTeamInvite(ctx context.Context, inviter *user_model.User, team *org_model.Team, invite *org_model.TeamInvite) error {
|
|
|
|
if setting.MailService == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-03 02:48:26 +00:00
|
|
|
org, err := user_model.GetUserByID(ctx, team.OrgID)
|
2022-10-19 12:40:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
locale := translation.NewLocale(inviter.Language)
|
|
|
|
|
2023-08-31 16:26:13 +00:00
|
|
|
// check if a user with this email already exists
|
|
|
|
user, err := user_model.GetUserByEmail(ctx, invite.Email)
|
|
|
|
if err != nil && !user_model.IsErrUserNotExist(err) {
|
|
|
|
return err
|
|
|
|
} else if user != nil && user.ProhibitLogin {
|
|
|
|
return fmt.Errorf("login is prohibited for the invited user")
|
|
|
|
}
|
|
|
|
|
|
|
|
inviteRedirect := url.QueryEscape(fmt.Sprintf("/org/invite/%s", invite.Token))
|
|
|
|
inviteURL := fmt.Sprintf("%suser/sign_up?redirect_to=%s", setting.AppURL, inviteRedirect)
|
|
|
|
|
2023-09-07 14:50:45 +00:00
|
|
|
if (err == nil && user != nil) || setting.Service.DisableRegistration || setting.Service.AllowOnlyExternalRegistration {
|
|
|
|
// user account exists or registration disabled
|
2023-08-31 16:26:13 +00:00
|
|
|
inviteURL = fmt.Sprintf("%suser/login?redirect_to=%s", setting.AppURL, inviteRedirect)
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:40:28 +00:00
|
|
|
subject := locale.Tr("mail.team_invite.subject", inviter.DisplayName(), org.DisplayName())
|
2023-07-04 18:36:08 +00:00
|
|
|
mailMeta := map[string]any{
|
2023-10-31 16:35:55 +00:00
|
|
|
"locale": locale,
|
2022-10-19 12:40:28 +00:00
|
|
|
"Inviter": inviter,
|
|
|
|
"Organization": org,
|
|
|
|
"Team": team,
|
|
|
|
"Invite": invite,
|
|
|
|
"Subject": subject,
|
2023-08-31 16:26:13 +00:00
|
|
|
"InviteURL": inviteURL,
|
2022-10-19 12:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var mailBody bytes.Buffer
|
|
|
|
if err := bodyTemplates.ExecuteTemplate(&mailBody, string(tplTeamInviteMail), mailMeta); err != nil {
|
|
|
|
log.Error("ExecuteTemplate [%s]: %v", string(tplTeamInviteMail)+"/body", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-22 14:23:52 +00:00
|
|
|
msg := NewMessage(invite.Email, subject, mailBody.String())
|
2022-10-19 12:40:28 +00:00
|
|
|
msg.Info = subject
|
|
|
|
|
|
|
|
SendAsync(msg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|