2022-10-19 20:40:28 +08:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-10-19 20:40:28 +08:00
|
|
|
|
|
|
|
package mailer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-09-01 00:26:13 +08:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2022-10-19 20:40:28 +08: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 10:48:26 +08:00
|
|
|
org, err := user_model.GetUserByID(ctx, team.OrgID)
|
2022-10-19 20:40:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
locale := translation.NewLocale(inviter.Language)
|
|
|
|
|
2023-09-01 00:26:13 +08: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 22:50:45 +08:00
|
|
|
if (err == nil && user != nil) || setting.Service.DisableRegistration || setting.Service.AllowOnlyExternalRegistration {
|
|
|
|
// user account exists or registration disabled
|
2023-09-01 00:26:13 +08:00
|
|
|
inviteURL = fmt.Sprintf("%suser/login?redirect_to=%s", setting.AppURL, inviteRedirect)
|
|
|
|
}
|
|
|
|
|
2022-10-19 20:40:28 +08:00
|
|
|
subject := locale.Tr("mail.team_invite.subject", inviter.DisplayName(), org.DisplayName())
|
2023-07-05 02:36:08 +08:00
|
|
|
mailMeta := map[string]any{
|
2023-10-31 22:11:48 +08:00
|
|
|
"locale": locale,
|
2022-10-19 20:40:28 +08:00
|
|
|
"Inviter": inviter,
|
|
|
|
"Organization": org,
|
|
|
|
"Team": team,
|
|
|
|
"Invite": invite,
|
|
|
|
"Subject": subject,
|
2023-09-01 00:26:13 +08:00
|
|
|
"InviteURL": inviteURL,
|
2022-10-19 20:40:28 +08: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 22:23:52 +08:00
|
|
|
msg := NewMessage(invite.Email, subject, mailBody.String())
|
2022-10-19 20:40:28 +08:00
|
|
|
msg.Info = subject
|
|
|
|
|
|
|
|
SendAsync(msg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|