mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-25 02:48:05 +00:00
Fixes #12564. Fixes #8951. This introduces a new setting, `ADD_MEMBERS_BY_INVITATIONS`, which is turned off by default. When turned on, adding a user to a team issues an invitation instead of adding them directly to the team. A prerequisite for this work was to be able to link invitations to existing users (so far, they were only associated to an email address, since those invitations were meant to be issued to users who didn't have an account yet). --- I plan to work on the following improvements, which I propose to do in separate PRs given that this one is already a bit big: * generate an in-app notification for the invited user * advertise the invitation to the invited user from the org page as well (#12120) * show the list of invited users in the list of organization members (not just on the team page) and various other improvements to invitations (#12570, #12716). Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12845 Reviewed-by: Gusted <gusted@noreply.codeberg.org>
42 lines
1.6 KiB
Go
42 lines
1.6 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package org
|
|
|
|
import (
|
|
"context"
|
|
|
|
"forgejo.org/models"
|
|
org_model "forgejo.org/models/organization"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/setting"
|
|
"forgejo.org/services/mailer"
|
|
)
|
|
|
|
// CreateTeamInviteByEmail makes a persistent invite in db for someone without an account and mails it to them.
|
|
func CreateTeamInviteByEmail(ctx context.Context, inviter *user_model.User, team *org_model.Team, uname string) error {
|
|
invite, err := org_model.CreateTeamInviteByEmail(ctx, inviter, team, uname)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return mailer.MailTeamInvite(ctx, inviter, team, invite)
|
|
}
|
|
|
|
// CreateTeamInviteByUser makes a persistent invite in db for someone with an account already and mails it.
|
|
func CreateTeamInviteByUser(ctx context.Context, inviter, invited *user_model.User, team *org_model.Team) error {
|
|
invite, err := org_model.CreateTeamInviteForUser(ctx, inviter, invited, team)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// TODO: instead of only sending an email, also create an in-app notification
|
|
return mailer.MailTeamInvite(ctx, inviter, team, invite)
|
|
}
|
|
|
|
// InviteOrAddTeamMember invites the user to the team if all team changes should go through invites, or adds them directly otherwise.
|
|
func InviteOrAddTeamMember(ctx context.Context, inviter, invited *user_model.User, team *org_model.Team) error {
|
|
if setting.Service.AddMembersByInvitations && inviter.ID != invited.ID {
|
|
return CreateTeamInviteByUser(ctx, inviter, invited, team)
|
|
}
|
|
return models.AddTeamMember(ctx, team, invited.ID)
|
|
}
|