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