|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package organization |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + user_model "code.gitea.io/gitea/models/user" |
| 13 | + "code.gitea.io/gitea/modules/timeutil" |
| 14 | + "code.gitea.io/gitea/modules/util" |
| 15 | + |
| 16 | + "xorm.io/builder" |
| 17 | +) |
| 18 | + |
| 19 | +type ErrTeamInviteAlreadyExist struct { |
| 20 | + TeamID int64 |
| 21 | + Email string |
| 22 | +} |
| 23 | + |
| 24 | +func IsErrTeamInviteAlreadyExist(err error) bool { |
| 25 | + _, ok := err.(ErrTeamInviteAlreadyExist) |
| 26 | + return ok |
| 27 | +} |
| 28 | + |
| 29 | +func (err ErrTeamInviteAlreadyExist) Error() string { |
| 30 | + return fmt.Sprintf("team invite already exists [team_id: %d, email: %s]", err.TeamID, err.Email) |
| 31 | +} |
| 32 | + |
| 33 | +func (err ErrTeamInviteAlreadyExist) Unwrap() error { |
| 34 | + return util.ErrAlreadyExist |
| 35 | +} |
| 36 | + |
| 37 | +type ErrTeamInviteNotFound struct { |
| 38 | + Token string |
| 39 | +} |
| 40 | + |
| 41 | +func IsErrTeamInviteNotFound(err error) bool { |
| 42 | + _, ok := err.(ErrTeamInviteNotFound) |
| 43 | + return ok |
| 44 | +} |
| 45 | + |
| 46 | +func (err ErrTeamInviteNotFound) Error() string { |
| 47 | + return fmt.Sprintf("team invite was not found [token: %s]", err.Token) |
| 48 | +} |
| 49 | + |
| 50 | +func (err ErrTeamInviteNotFound) Unwrap() error { |
| 51 | + return util.ErrNotExist |
| 52 | +} |
| 53 | + |
| 54 | +// ErrUserEmailAlreadyAdded represents a "user by email already added to team" error. |
| 55 | +type ErrUserEmailAlreadyAdded struct { |
| 56 | + Email string |
| 57 | +} |
| 58 | + |
| 59 | +// IsErrUserEmailAlreadyAdded checks if an error is a ErrUserEmailAlreadyAdded. |
| 60 | +func IsErrUserEmailAlreadyAdded(err error) bool { |
| 61 | + _, ok := err.(ErrUserEmailAlreadyAdded) |
| 62 | + return ok |
| 63 | +} |
| 64 | + |
| 65 | +func (err ErrUserEmailAlreadyAdded) Error() string { |
| 66 | + return fmt.Sprintf("user with email already added [email: %s]", err.Email) |
| 67 | +} |
| 68 | + |
| 69 | +func (err ErrUserEmailAlreadyAdded) Unwrap() error { |
| 70 | + return util.ErrAlreadyExist |
| 71 | +} |
| 72 | + |
| 73 | +// TeamInvite represents an invite to a team |
| 74 | +type TeamInvite struct { |
| 75 | + ID int64 `xorm:"pk autoincr"` |
| 76 | + Token string `xorm:"UNIQUE(token) INDEX NOT NULL DEFAULT ''"` |
| 77 | + InviterID int64 `xorm:"NOT NULL DEFAULT 0"` |
| 78 | + OrgID int64 `xorm:"INDEX NOT NULL DEFAULT 0"` |
| 79 | + TeamID int64 `xorm:"UNIQUE(team_mail) INDEX NOT NULL DEFAULT 0"` |
| 80 | + Email string `xorm:"UNIQUE(team_mail) NOT NULL DEFAULT ''"` |
| 81 | + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` |
| 82 | + UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` |
| 83 | +} |
| 84 | + |
| 85 | +func CreateTeamInvite(ctx context.Context, doer *user_model.User, team *Team, email string) (*TeamInvite, error) { |
| 86 | + has, err := db.GetEngine(ctx).Exist(&TeamInvite{ |
| 87 | + TeamID: team.ID, |
| 88 | + Email: email, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + if has { |
| 94 | + return nil, ErrTeamInviteAlreadyExist{ |
| 95 | + TeamID: team.ID, |
| 96 | + Email: email, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // check if the user is already a team member by email |
| 101 | + exist, err := db.GetEngine(ctx). |
| 102 | + Where(builder.Eq{ |
| 103 | + "team_user.org_id": team.OrgID, |
| 104 | + "team_user.team_id": team.ID, |
| 105 | + "`user`.email": email, |
| 106 | + }). |
| 107 | + Join("INNER", "`user`", "`user`.id = team_user.uid"). |
| 108 | + Table("team_user"). |
| 109 | + Exist() |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + if exist { |
| 115 | + return nil, ErrUserEmailAlreadyAdded{ |
| 116 | + Email: email, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + token, err := util.CryptoRandomString(25) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + |
| 125 | + invite := &TeamInvite{ |
| 126 | + Token: token, |
| 127 | + InviterID: doer.ID, |
| 128 | + OrgID: team.OrgID, |
| 129 | + TeamID: team.ID, |
| 130 | + Email: email, |
| 131 | + } |
| 132 | + |
| 133 | + return invite, db.Insert(ctx, invite) |
| 134 | +} |
| 135 | + |
| 136 | +func RemoveInviteByID(ctx context.Context, inviteID, teamID int64) error { |
| 137 | + _, err := db.DeleteByBean(ctx, &TeamInvite{ |
| 138 | + ID: inviteID, |
| 139 | + TeamID: teamID, |
| 140 | + }) |
| 141 | + return err |
| 142 | +} |
| 143 | + |
| 144 | +func GetInvitesByTeamID(ctx context.Context, teamID int64) ([]*TeamInvite, error) { |
| 145 | + invites := make([]*TeamInvite, 0, 10) |
| 146 | + return invites, db.GetEngine(ctx). |
| 147 | + Where("team_id=?", teamID). |
| 148 | + Find(&invites) |
| 149 | +} |
| 150 | + |
| 151 | +func GetInviteByToken(ctx context.Context, token string) (*TeamInvite, error) { |
| 152 | + invite := &TeamInvite{} |
| 153 | + |
| 154 | + has, err := db.GetEngine(ctx).Where("token=?", token).Get(invite) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + if !has { |
| 159 | + return nil, ErrTeamInviteNotFound{Token: token} |
| 160 | + } |
| 161 | + return invite, nil |
| 162 | +} |
0 commit comments