2018-06-21 10:00:13 -06:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2016-03-25 16:04:02 -06:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2021-11-19 04:41:40 -07:00
|
|
|
"context"
|
2016-03-25 16:04:02 -06:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2017-12-21 00:43:26 -07:00
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 00:29:02 -06:00
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-05-11 04:09:36 -06:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-03-31 19:53:18 -06:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-01-27 02:25:21 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-03-21 19:09:45 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-01-27 02:25:21 -07:00
|
|
|
|
2019-09-30 23:32:28 -06:00
|
|
|
"xorm.io/builder"
|
2016-03-25 16:04:02 -06:00
|
|
|
)
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
func addRepository(ctx context.Context, t *organization.Team, repo *repo_model.Repository) (err error) {
|
|
|
|
if err = organization.AddTeamRepo(ctx, t.OrgID, t.ID, repo.ID); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if _, err = db.GetEngine(ctx).Incr("num_repos").ID(t.ID).Update(new(organization.Team)); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return fmt.Errorf("update team: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-12-30 20:08:08 -07:00
|
|
|
t.NumRepos++
|
|
|
|
|
2022-05-11 04:09:36 -06:00
|
|
|
if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return fmt.Errorf("recalculateAccesses: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-27 02:25:21 -07:00
|
|
|
// Make all team members watch this repo if enabled in global settings
|
|
|
|
if setting.Service.AutoWatchNewRepos {
|
2022-03-29 00:29:02 -06:00
|
|
|
if err = t.GetMembersCtx(ctx); err != nil {
|
2019-01-27 02:25:21 -07:00
|
|
|
return fmt.Errorf("getMembers: %v", err)
|
|
|
|
}
|
|
|
|
for _, u := range t.Members {
|
2022-05-20 08:08:52 -06:00
|
|
|
if err = repo_model.WatchRepo(ctx, u.ID, repo.ID, true); err != nil {
|
2019-01-27 02:25:21 -07:00
|
|
|
return fmt.Errorf("watchRepo: %v", err)
|
|
|
|
}
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
}
|
2019-01-27 02:25:21 -07:00
|
|
|
|
2016-03-25 16:04:02 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 02:37:14 -07:00
|
|
|
// addAllRepositories adds all repositories to the team.
|
|
|
|
// If the team already has some repositories they will be left unchanged.
|
2022-03-29 00:29:02 -06:00
|
|
|
func addAllRepositories(ctx context.Context, t *organization.Team) error {
|
2021-12-09 18:27:50 -07:00
|
|
|
var orgRepos []repo_model.Repository
|
|
|
|
e := db.GetEngine(ctx)
|
2019-11-06 02:37:14 -07:00
|
|
|
if err := e.Where("owner_id = ?", t.OrgID).Find(&orgRepos); err != nil {
|
|
|
|
return fmt.Errorf("get org repos: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range orgRepos {
|
2022-05-11 04:09:36 -06:00
|
|
|
if !organization.HasTeamRepo(ctx, t.OrgID, t.ID, repo.ID) {
|
2022-03-29 00:29:02 -06:00
|
|
|
if err := addRepository(ctx, t, &repo); err != nil {
|
2019-11-06 02:37:14 -07:00
|
|
|
return fmt.Errorf("addRepository: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-08 17:39:37 -07:00
|
|
|
// AddAllRepositories adds all repositories to the team
|
2022-03-29 00:29:02 -06:00
|
|
|
func AddAllRepositories(t *organization.Team) (err error) {
|
2021-11-21 08:41:00 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2019-11-08 17:39:37 -07:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
defer committer.Close()
|
2019-11-08 17:39:37 -07:00
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err = addAllRepositories(ctx, t); err != nil {
|
2019-11-08 17:39:37 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
return committer.Commit()
|
2019-11-08 17:39:37 -07:00
|
|
|
}
|
|
|
|
|
2016-03-25 16:04:02 -06:00
|
|
|
// AddRepository adds new repository to team of organization.
|
2022-03-29 00:29:02 -06:00
|
|
|
func AddRepository(t *organization.Team, repo *repo_model.Repository) (err error) {
|
2016-03-25 16:04:02 -06:00
|
|
|
if repo.OwnerID != t.OrgID {
|
|
|
|
return errors.New("Repository does not belong to organization")
|
2022-03-29 00:29:02 -06:00
|
|
|
} else if HasRepository(t, repo.ID) {
|
2016-03-25 16:04:02 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
defer committer.Close()
|
2016-03-25 16:04:02 -06:00
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err = addRepository(ctx, t, repo); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
return committer.Commit()
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
2019-11-08 17:39:37 -07:00
|
|
|
// RemoveAllRepositories removes all repositories from team and recalculates access
|
2022-03-29 00:29:02 -06:00
|
|
|
func RemoveAllRepositories(t *organization.Team) (err error) {
|
2019-11-08 17:39:37 -07:00
|
|
|
if t.IncludesAllRepositories {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2019-11-08 17:39:37 -07:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
defer committer.Close()
|
2019-11-08 17:39:37 -07:00
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err = removeAllRepositories(ctx, t); err != nil {
|
2019-11-08 17:39:37 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
return committer.Commit()
|
2019-11-08 17:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeAllRepositories removes all repositories from team and recalculates access
|
|
|
|
// Note: Shall not be called if team includes all repositories
|
2022-03-29 00:29:02 -06:00
|
|
|
func removeAllRepositories(ctx context.Context, t *organization.Team) (err error) {
|
2021-12-09 18:27:50 -07:00
|
|
|
e := db.GetEngine(ctx)
|
2019-11-08 17:39:37 -07:00
|
|
|
// Delete all accesses.
|
|
|
|
for _, repo := range t.Repos {
|
2022-05-11 04:09:36 -06:00
|
|
|
if err := access_model.RecalculateTeamAccesses(ctx, repo, t.ID); err != nil {
|
2019-11-08 17:39:37 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove watches from all users and now unaccessible repos
|
|
|
|
for _, user := range t.Members {
|
2022-05-11 04:09:36 -06:00
|
|
|
has, err := access_model.HasAccess(ctx, user.ID, repo)
|
2019-11-08 17:39:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
if err = repo_model.WatchRepo(ctx, user.ID, repo.ID, false); err != nil {
|
2019-11-08 17:39:37 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all IssueWatches a user has subscribed to in the repositories
|
2022-05-20 08:08:52 -06:00
|
|
|
if err = removeIssueWatchersByRepoID(ctx, user.ID, repo.ID); err != nil {
|
2019-11-08 17:39:37 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete team-repo
|
|
|
|
if _, err := e.
|
|
|
|
Where("team_id=?", t.ID).
|
2022-03-29 00:29:02 -06:00
|
|
|
Delete(new(organization.TeamRepo)); err != nil {
|
2019-11-08 17:39:37 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.NumRepos = 0
|
|
|
|
if _, err = e.ID(t.ID).Cols("num_repos").Update(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-11 04:09:36 -06:00
|
|
|
// HasRepository returns true if given repository belong to team.
|
|
|
|
func HasRepository(t *organization.Team, repoID int64) bool {
|
|
|
|
return organization.HasTeamRepo(db.DefaultContext, t.OrgID, t.ID, repoID)
|
2022-03-29 00:29:02 -06:00
|
|
|
}
|
|
|
|
|
2019-11-06 02:37:14 -07:00
|
|
|
// removeRepository removes a repository from a team and recalculates access
|
|
|
|
// Note: Repository shall not be removed from team if it includes all repositories (unless the repository is deleted)
|
2022-03-29 00:29:02 -06:00
|
|
|
func removeRepository(ctx context.Context, t *organization.Team, repo *repo_model.Repository, recalculate bool) (err error) {
|
2021-12-09 18:27:50 -07:00
|
|
|
e := db.GetEngine(ctx)
|
2022-03-29 00:29:02 -06:00
|
|
|
if err = organization.RemoveTeamRepo(ctx, t.ID, repo.ID); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.NumRepos--
|
2017-10-04 22:43:04 -06:00
|
|
|
if _, err = e.ID(t.ID).Cols("num_repos").Update(t); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't need to recalculate when delete a repository from organization.
|
|
|
|
if recalculate {
|
2022-05-11 04:09:36 -06:00
|
|
|
if err = access_model.RecalculateTeamAccesses(ctx, repo, t.ID); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
teamUsers, err := organization.GetTeamUsersByTeamID(ctx, t.ID)
|
2017-03-14 18:51:46 -06:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getTeamUsersByTeamID: %v", err)
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
2017-03-20 18:55:00 -06:00
|
|
|
for _, teamUser := range teamUsers {
|
2022-05-11 04:09:36 -06:00
|
|
|
has, err := access_model.HasAccess(ctx, teamUser.UID, repo)
|
2016-03-25 16:04:02 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-20 08:08:52 -06:00
|
|
|
if err = repo_model.WatchRepo(ctx, teamUser.UID, repo.ID, false); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2018-06-19 13:44:33 -06:00
|
|
|
|
|
|
|
// Remove all IssueWatches a user has subscribed to in the repositories
|
2022-05-20 08:08:52 -06:00
|
|
|
if err := removeIssueWatchersByRepoID(ctx, teamUser.UID, repo.ID); err != nil {
|
2018-06-19 13:44:33 -06:00
|
|
|
return err
|
|
|
|
}
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRepository removes repository from team of organization.
|
2019-11-06 02:37:14 -07:00
|
|
|
// If the team shall include all repositories the request is ignored.
|
2022-03-29 00:29:02 -06:00
|
|
|
func RemoveRepository(t *organization.Team, repoID int64) error {
|
|
|
|
if !HasRepository(t, repoID) {
|
2016-03-25 16:04:02 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 02:37:14 -07:00
|
|
|
if t.IncludesAllRepositories {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 18:27:50 -07:00
|
|
|
repo, err := repo_model.GetRepositoryByID(repoID)
|
2016-03-25 16:04:02 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
defer committer.Close()
|
2016-03-25 16:04:02 -06:00
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err = removeRepository(ctx, t, repo, true); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
return committer.Commit()
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTeam creates a record of new team.
|
|
|
|
// It's caller's responsibility to assign organization ID.
|
2022-03-29 00:29:02 -06:00
|
|
|
func NewTeam(t *organization.Team) (err error) {
|
2016-03-25 16:04:02 -06:00
|
|
|
if len(t.Name) == 0 {
|
|
|
|
return errors.New("empty team name")
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err = organization.IsUsableTeamName(t.Name); err != nil {
|
2016-11-06 02:07:03 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).ID(t.OrgID).Get(new(user_model.User))
|
2016-03-25 16:04:02 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-12 13:41:28 -06:00
|
|
|
}
|
|
|
|
if !has {
|
2022-03-29 00:29:02 -06:00
|
|
|
return organization.ErrOrgNotExist{ID: t.OrgID}
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
t.LowerName = strings.ToLower(t.Name)
|
2021-09-23 09:45:36 -06:00
|
|
|
has, err = db.GetEngine(db.DefaultContext).
|
2016-11-10 08:16:32 -07:00
|
|
|
Where("org_id=?", t.OrgID).
|
|
|
|
And("lower_name=?", t.LowerName).
|
2022-03-29 00:29:02 -06:00
|
|
|
Get(new(organization.Team))
|
2016-03-25 16:04:02 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-12 13:41:28 -06:00
|
|
|
}
|
|
|
|
if has {
|
2022-03-29 00:29:02 -06:00
|
|
|
return organization.ErrTeamAlreadyExist{OrgID: t.OrgID, Name: t.LowerName}
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
defer committer.Close()
|
2016-03-25 16:04:02 -06:00
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
if err = db.Insert(ctx, t); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-21 10:00:13 -06:00
|
|
|
// insert units for team
|
|
|
|
if len(t.Units) > 0 {
|
|
|
|
for _, unit := range t.Units {
|
|
|
|
unit.TeamID = t.ID
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
if err = db.Insert(ctx, &t.Units); err != nil {
|
2018-06-21 10:00:13 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 02:37:14 -07:00
|
|
|
// Add all repositories to the team if it has access to all of them.
|
|
|
|
if t.IncludesAllRepositories {
|
2022-03-29 00:29:02 -06:00
|
|
|
err = addAllRepositories(ctx, t)
|
2019-11-06 02:37:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("addAllRepositories: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 16:04:02 -06:00
|
|
|
// Update organization number of teams.
|
2021-11-21 08:41:00 -07:00
|
|
|
if _, err = db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
return committer.Commit()
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTeam updates information of team.
|
2022-03-29 00:29:02 -06:00
|
|
|
func UpdateTeam(t *organization.Team, authChanged, includeAllChanged bool) (err error) {
|
2016-03-25 16:04:02 -06:00
|
|
|
if len(t.Name) == 0 {
|
|
|
|
return errors.New("empty team name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(t.Description) > 255 {
|
|
|
|
t.Description = t.Description[:255]
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
defer committer.Close()
|
|
|
|
sess := db.GetEngine(ctx)
|
2016-03-25 16:04:02 -06:00
|
|
|
|
|
|
|
t.LowerName = strings.ToLower(t.Name)
|
2017-01-27 09:11:41 -07:00
|
|
|
has, err := sess.
|
2016-11-10 08:16:32 -07:00
|
|
|
Where("org_id=?", t.OrgID).
|
|
|
|
And("lower_name=?", t.LowerName).
|
|
|
|
And("id!=?", t.ID).
|
2022-03-29 00:29:02 -06:00
|
|
|
Get(new(organization.Team))
|
2016-03-25 16:04:02 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has {
|
2022-03-29 00:29:02 -06:00
|
|
|
return organization.ErrTeamAlreadyExist{OrgID: t.OrgID, Name: t.LowerName}
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
2020-01-09 06:15:14 -07:00
|
|
|
if _, err = sess.ID(t.ID).Cols("name", "lower_name", "description",
|
|
|
|
"can_create_org_repo", "authorize", "includes_all_repositories").Update(t); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return fmt.Errorf("update: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-10 12:45:32 -07:00
|
|
|
// update units for team
|
|
|
|
if len(t.Units) > 0 {
|
|
|
|
for _, unit := range t.Units {
|
|
|
|
unit.TeamID = t.ID
|
|
|
|
}
|
|
|
|
// Delete team-unit.
|
|
|
|
if _, err := sess.
|
|
|
|
Where("team_id=?", t.ID).
|
2022-03-29 00:29:02 -06:00
|
|
|
Delete(new(organization.TeamUnit)); err != nil {
|
2018-11-10 12:45:32 -07:00
|
|
|
return err
|
|
|
|
}
|
2022-01-04 20:37:00 -07:00
|
|
|
if _, err = sess.Cols("org_id", "team_id", "type", "access_mode").Insert(&t.Units); err != nil {
|
2018-11-10 12:45:32 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 16:04:02 -06:00
|
|
|
// Update access for team members if needed.
|
|
|
|
if authChanged {
|
2022-03-29 00:29:02 -06:00
|
|
|
if err = t.GetRepositoriesCtx(ctx); err != nil {
|
2017-02-22 18:36:15 -07:00
|
|
|
return fmt.Errorf("getRepositories: %v", err)
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range t.Repos {
|
2022-05-11 04:09:36 -06:00
|
|
|
if err = access_model.RecalculateTeamAccesses(ctx, repo, 0); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return fmt.Errorf("recalculateTeamAccesses: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 02:37:14 -07:00
|
|
|
// Add all repositories to the team if it has access to all of them.
|
|
|
|
if includeAllChanged && t.IncludesAllRepositories {
|
2022-03-29 00:29:02 -06:00
|
|
|
err = addAllRepositories(ctx, t)
|
2019-11-06 02:37:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("addAllRepositories: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
return committer.Commit()
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTeam deletes given team.
|
|
|
|
// It's caller's responsibility to assign organization ID.
|
2022-03-29 00:29:02 -06:00
|
|
|
func DeleteTeam(t *organization.Team) error {
|
2021-11-21 08:41:00 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
defer committer.Close()
|
|
|
|
sess := db.GetEngine(ctx)
|
2016-03-25 16:04:02 -06:00
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err := t.GetRepositoriesCtx(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.GetMembersCtx(ctx); err != nil {
|
2018-06-19 13:44:33 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-21 19:09:45 -06:00
|
|
|
// update branch protections
|
|
|
|
{
|
|
|
|
protections := make([]*ProtectedBranch, 0, 10)
|
|
|
|
err := sess.In("repo_id",
|
|
|
|
builder.Select("id").From("repository").Where(builder.Eq{"owner_id": t.OrgID})).
|
|
|
|
Find(&protections)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("findProtectedBranches: %v", err)
|
|
|
|
}
|
|
|
|
for _, p := range protections {
|
|
|
|
var matched1, matched2, matched3 bool
|
|
|
|
if len(p.WhitelistTeamIDs) != 0 {
|
|
|
|
p.WhitelistTeamIDs, matched1 = util.RemoveIDFromList(
|
|
|
|
p.WhitelistTeamIDs, t.ID)
|
|
|
|
}
|
|
|
|
if len(p.ApprovalsWhitelistTeamIDs) != 0 {
|
|
|
|
p.ApprovalsWhitelistTeamIDs, matched2 = util.RemoveIDFromList(
|
|
|
|
p.ApprovalsWhitelistTeamIDs, t.ID)
|
|
|
|
}
|
|
|
|
if len(p.MergeWhitelistTeamIDs) != 0 {
|
|
|
|
p.MergeWhitelistTeamIDs, matched3 = util.RemoveIDFromList(
|
|
|
|
p.MergeWhitelistTeamIDs, t.ID)
|
|
|
|
}
|
|
|
|
if matched1 || matched2 || matched3 {
|
|
|
|
if _, err = sess.ID(p.ID).Cols(
|
|
|
|
"whitelist_team_i_ds",
|
|
|
|
"merge_whitelist_team_i_ds",
|
|
|
|
"approvals_whitelist_team_i_ds",
|
|
|
|
).Update(p); err != nil {
|
|
|
|
return fmt.Errorf("updateProtectedBranches: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !t.IncludesAllRepositories {
|
2022-03-29 00:29:02 -06:00
|
|
|
if err := removeAllRepositories(ctx, t); err != nil {
|
2022-03-21 19:09:45 -06:00
|
|
|
return err
|
|
|
|
}
|
2017-02-22 18:36:15 -07:00
|
|
|
}
|
|
|
|
|
2016-03-25 16:04:02 -06:00
|
|
|
// Delete team-user.
|
2017-02-28 18:09:49 -07:00
|
|
|
if _, err := sess.
|
|
|
|
Where("org_id=?", t.OrgID).
|
2016-11-10 08:16:32 -07:00
|
|
|
Where("team_id=?", t.ID).
|
2022-03-29 00:29:02 -06:00
|
|
|
Delete(new(organization.TeamUser)); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-21 10:00:13 -06:00
|
|
|
// Delete team-unit.
|
|
|
|
if _, err := sess.
|
|
|
|
Where("team_id=?", t.ID).
|
2022-03-29 00:29:02 -06:00
|
|
|
Delete(new(organization.TeamUnit)); err != nil {
|
2018-06-21 10:00:13 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-25 16:04:02 -06:00
|
|
|
// Delete team.
|
2022-03-29 00:29:02 -06:00
|
|
|
if _, err := sess.ID(t.ID).Delete(new(organization.Team)); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Update organization number of teams.
|
2017-02-28 18:09:49 -07:00
|
|
|
if _, err := sess.Exec("UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
return committer.Commit()
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddTeamMember adds new membership of given team to given organization,
|
|
|
|
// the user will have membership to given organization automatically when needed.
|
2022-03-29 00:29:02 -06:00
|
|
|
func AddTeamMember(team *organization.Team, userID int64) error {
|
|
|
|
isAlreadyMember, err := organization.IsTeamMember(db.DefaultContext, team.OrgID, team.ID, userID)
|
2017-12-21 00:43:26 -07:00
|
|
|
if err != nil || isAlreadyMember {
|
|
|
|
return err
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err := organization.AddOrgUser(team.OrgID, userID); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 08:41:00 -07:00
|
|
|
defer committer.Close()
|
2016-03-25 16:04:02 -06:00
|
|
|
|
2022-05-14 07:30:19 -06:00
|
|
|
// check in transaction
|
|
|
|
isAlreadyMember, err = organization.IsTeamMember(ctx, team.OrgID, team.ID, userID)
|
|
|
|
if err != nil || isAlreadyMember {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
sess := db.GetEngine(ctx)
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err := db.Insert(ctx, &organization.TeamUser{
|
2016-11-28 01:33:08 -07:00
|
|
|
UID: userID,
|
2017-02-23 23:25:09 -07:00
|
|
|
OrgID: team.OrgID,
|
|
|
|
TeamID: team.ID,
|
|
|
|
}); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
2022-03-29 00:29:02 -06:00
|
|
|
} else if _, err := sess.Incr("num_members").ID(team.ID).Update(new(organization.Team)); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-30 20:08:08 -07:00
|
|
|
team.NumMembers++
|
|
|
|
|
2016-03-25 16:04:02 -06:00
|
|
|
// Give access to team repositories.
|
2022-03-31 19:53:18 -06:00
|
|
|
// update exist access if mode become bigger
|
|
|
|
subQuery := builder.Select("repo_id").From("team_repo").
|
|
|
|
Where(builder.Eq{"team_id": team.ID})
|
|
|
|
|
|
|
|
if _, err := sess.Where("user_id=?", userID).
|
|
|
|
In("repo_id", subQuery).
|
|
|
|
And("mode < ?", team.AccessMode).
|
|
|
|
SetExpr("mode", team.AccessMode).
|
2022-05-11 04:09:36 -06:00
|
|
|
Update(new(access_model.Access)); err != nil {
|
2022-03-31 19:53:18 -06:00
|
|
|
return fmt.Errorf("update user accesses: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// for not exist access
|
|
|
|
var repoIDs []int64
|
|
|
|
accessSubQuery := builder.Select("repo_id").From("access").Where(builder.Eq{"user_id": userID})
|
|
|
|
if err := sess.SQL(subQuery.And(builder.NotIn("repo_id", accessSubQuery))).Find(&repoIDs); err != nil {
|
|
|
|
return fmt.Errorf("select id accesses: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-11 04:09:36 -06:00
|
|
|
accesses := make([]*access_model.Access, 0, 100)
|
2022-03-31 19:53:18 -06:00
|
|
|
for i, repoID := range repoIDs {
|
2022-05-11 04:09:36 -06:00
|
|
|
accesses = append(accesses, &access_model.Access{RepoID: repoID, UserID: userID, Mode: team.AccessMode})
|
2022-03-31 19:53:18 -06:00
|
|
|
if (i%100 == 0 || i == len(repoIDs)-1) && len(accesses) > 0 {
|
|
|
|
if err = db.Insert(ctx, accesses); err != nil {
|
|
|
|
return fmt.Errorf("insert new user accesses: %v", err)
|
2019-01-27 02:25:21 -07:00
|
|
|
}
|
2022-03-31 19:53:18 -06:00
|
|
|
accesses = accesses[:0]
|
2018-06-19 13:44:33 -06:00
|
|
|
}
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
2022-03-31 19:53:18 -06:00
|
|
|
// watch could be failed, so run it in a goroutine
|
|
|
|
if setting.Service.AutoWatchNewRepos {
|
|
|
|
// Get team and its repositories.
|
|
|
|
if err := team.GetRepositoriesCtx(db.DefaultContext); err != nil {
|
|
|
|
log.Error("getRepositories failed: %v", err)
|
|
|
|
}
|
|
|
|
go func(repos []*repo_model.Repository) {
|
|
|
|
for _, repo := range repos {
|
2022-05-20 08:08:52 -06:00
|
|
|
if err = repo_model.WatchRepo(db.DefaultContext, userID, repo.ID, true); err != nil {
|
2022-03-31 19:53:18 -06:00
|
|
|
log.Error("watch repo failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(team.Repos)
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:41:00 -07:00
|
|
|
return committer.Commit()
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
func removeTeamMember(ctx context.Context, team *organization.Team, userID int64) error {
|
2021-11-19 04:41:40 -07:00
|
|
|
e := db.GetEngine(ctx)
|
2022-03-29 00:29:02 -06:00
|
|
|
isMember, err := organization.IsTeamMember(ctx, team.OrgID, team.ID, userID)
|
2017-12-21 00:43:26 -07:00
|
|
|
if err != nil || !isMember {
|
|
|
|
return err
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user to delete is the last member in owner team.
|
2017-02-23 23:25:09 -07:00
|
|
|
if team.IsOwnerTeam() && team.NumMembers == 1 {
|
2022-03-29 00:29:02 -06:00
|
|
|
return organization.ErrLastOrgOwner{UID: userID}
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
2017-02-23 23:25:09 -07:00
|
|
|
team.NumMembers--
|
2016-03-25 16:04:02 -06:00
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if err := team.GetRepositoriesCtx(ctx); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:29:02 -06:00
|
|
|
if _, err := e.Delete(&organization.TeamUser{
|
2016-11-28 01:33:08 -07:00
|
|
|
UID: userID,
|
2017-02-23 23:25:09 -07:00
|
|
|
OrgID: team.OrgID,
|
|
|
|
TeamID: team.ID,
|
|
|
|
}); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
2016-11-10 08:16:32 -07:00
|
|
|
} else if _, err = e.
|
2017-10-04 22:43:04 -06:00
|
|
|
ID(team.ID).
|
2017-09-24 22:59:27 -06:00
|
|
|
Cols("num_members").
|
2017-02-23 23:25:09 -07:00
|
|
|
Update(team); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete access to team repositories.
|
2017-02-23 23:25:09 -07:00
|
|
|
for _, repo := range team.Repos {
|
2022-05-11 04:09:36 -06:00
|
|
|
if err := access_model.RecalculateUserAccess(ctx, repo, userID); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2018-06-19 13:44:33 -06:00
|
|
|
|
|
|
|
// Remove watches from now unaccessible
|
2021-12-09 18:27:50 -07:00
|
|
|
if err := reconsiderWatches(ctx, repo, userID); err != nil {
|
2018-06-19 13:44:33 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:52:01 -06:00
|
|
|
// Remove issue assignments from now unaccessible
|
2021-12-09 18:27:50 -07:00
|
|
|
if err := reconsiderRepoIssuesAssignee(ctx, repo, userID); err != nil {
|
2018-06-19 13:44:33 -06:00
|
|
|
return err
|
|
|
|
}
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:42:02 -07:00
|
|
|
// Check if the user is a member of any team in the organization.
|
2022-03-29 00:29:02 -06:00
|
|
|
if count, err := e.Count(&organization.TeamUser{
|
2018-02-23 01:42:02 -07:00
|
|
|
UID: userID,
|
|
|
|
OrgID: team.OrgID,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
} else if count == 0 {
|
2021-11-19 04:41:40 -07:00
|
|
|
return removeOrgUser(ctx, team.OrgID, userID)
|
2018-02-23 01:42:02 -07:00
|
|
|
}
|
|
|
|
|
2016-03-25 16:04:02 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTeamMember removes member from given team of given organization.
|
2022-03-29 00:29:02 -06:00
|
|
|
func RemoveTeamMember(team *organization.Team, userID int64) error {
|
2021-11-19 04:41:40 -07:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-19 04:41:40 -07:00
|
|
|
defer committer.Close()
|
|
|
|
if err := removeTeamMember(ctx, team, userID); err != nil {
|
2016-03-25 16:04:02 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-19 04:41:40 -07:00
|
|
|
return committer.Commit()
|
2016-03-25 16:04:02 -06:00
|
|
|
}
|