2018-06-21 16:00:13 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2016-03-25 22:04:02 +00: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 11:41:40 +00:00
|
|
|
"context"
|
2016-03-25 22:04:02 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-04-07 13:44:34 +00:00
|
|
|
"sort"
|
2016-03-25 22:04:02 +00:00
|
|
|
"strings"
|
2017-12-21 07:43:26 +00:00
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 11:58:28 +00:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-12-10 01:27:50 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 19:57:58 +00:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-12-21 07:43:26 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-01-27 09:25:21 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2019-10-01 05:32:28 +00:00
|
|
|
"xorm.io/builder"
|
2016-03-25 22:04:02 +00:00
|
|
|
)
|
|
|
|
|
2016-11-28 01:30:08 +00:00
|
|
|
const ownerTeamName = "Owners"
|
2016-03-25 22:04:02 +00:00
|
|
|
|
|
|
|
// Team represents a organization team.
|
|
|
|
type Team struct {
|
2019-11-06 09:37:14 +00:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OrgID int64 `xorm:"INDEX"`
|
|
|
|
LowerName string
|
|
|
|
Name string
|
|
|
|
Description string
|
2022-01-05 03:37:00 +00:00
|
|
|
AccessMode perm.AccessMode `xorm:"'authorize'"`
|
2021-12-10 01:27:50 +00:00
|
|
|
Repos []*repo_model.Repository `xorm:"-"`
|
|
|
|
Members []*user_model.User `xorm:"-"`
|
2019-11-06 09:37:14 +00:00
|
|
|
NumRepos int
|
|
|
|
NumMembers int
|
|
|
|
Units []*TeamUnit `xorm:"-"`
|
|
|
|
IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"`
|
2019-11-20 11:27:49 +00:00
|
|
|
CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"`
|
2017-05-18 14:54:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Team))
|
|
|
|
db.RegisterModel(new(TeamUser))
|
|
|
|
db.RegisterModel(new(TeamRepo))
|
|
|
|
db.RegisterModel(new(TeamUnit))
|
|
|
|
}
|
|
|
|
|
2019-10-01 05:32:28 +00:00
|
|
|
// SearchTeamOptions holds the search options
|
|
|
|
type SearchTeamOptions struct {
|
2021-09-24 11:32:56 +00:00
|
|
|
db.ListOptions
|
2019-10-01 05:32:28 +00:00
|
|
|
UserID int64
|
|
|
|
Keyword string
|
|
|
|
OrgID int64
|
|
|
|
IncludeDesc bool
|
2020-01-24 19:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SearchMembersOptions holds the search options
|
|
|
|
type SearchMembersOptions struct {
|
2021-09-24 11:32:56 +00:00
|
|
|
db.ListOptions
|
2019-10-01 05:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SearchTeam search for teams. Caller is responsible to check permissions.
|
|
|
|
func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
|
|
|
|
if opts.Page <= 0 {
|
|
|
|
opts.Page = 1
|
|
|
|
}
|
|
|
|
if opts.PageSize == 0 {
|
|
|
|
// Default limit
|
|
|
|
opts.PageSize = 10
|
|
|
|
}
|
|
|
|
|
2021-03-14 18:52:12 +00:00
|
|
|
cond := builder.NewCond()
|
2019-10-01 05:32:28 +00:00
|
|
|
|
|
|
|
if len(opts.Keyword) > 0 {
|
|
|
|
lowerKeyword := strings.ToLower(opts.Keyword)
|
|
|
|
var keywordCond builder.Cond = builder.Like{"lower_name", lowerKeyword}
|
|
|
|
if opts.IncludeDesc {
|
|
|
|
keywordCond = keywordCond.Or(builder.Like{"LOWER(description)", lowerKeyword})
|
|
|
|
}
|
|
|
|
cond = cond.And(keywordCond)
|
|
|
|
}
|
|
|
|
|
|
|
|
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
sess := db.GetEngine(db.DefaultContext)
|
2019-10-01 05:32:28 +00:00
|
|
|
|
|
|
|
count, err := sess.
|
|
|
|
Where(cond).
|
|
|
|
Count(new(Team))
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sess = sess.Where(cond)
|
|
|
|
if opts.PageSize == -1 {
|
|
|
|
opts.PageSize = int(count)
|
|
|
|
} else {
|
|
|
|
sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
teams := make([]*Team, 0, opts.PageSize)
|
|
|
|
if err = sess.
|
|
|
|
OrderBy("lower_name").
|
|
|
|
Find(&teams); err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return teams, count, nil
|
|
|
|
}
|
|
|
|
|
2019-04-22 20:40:51 +00:00
|
|
|
// ColorFormat provides a basic color format for a Team
|
|
|
|
func (t *Team) ColorFormat(s fmt.State) {
|
2021-12-16 19:01:14 +00:00
|
|
|
if t == nil {
|
|
|
|
log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v",
|
|
|
|
log.NewColoredIDValue(0),
|
|
|
|
"<nil>",
|
|
|
|
log.NewColoredIDValue(0),
|
|
|
|
0)
|
|
|
|
return
|
|
|
|
}
|
2019-04-22 20:40:51 +00:00
|
|
|
log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v",
|
|
|
|
log.NewColoredIDValue(t.ID),
|
|
|
|
t.Name,
|
|
|
|
log.NewColoredIDValue(t.OrgID),
|
2022-01-05 03:37:00 +00:00
|
|
|
t.AccessMode)
|
2019-04-22 20:40:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 21:50:06 +00:00
|
|
|
// GetUnits return a list of available units for a team
|
|
|
|
func (t *Team) GetUnits() error {
|
2021-09-23 15:45:36 +00:00
|
|
|
return t.getUnits(db.GetEngine(db.DefaultContext))
|
2019-03-19 21:50:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func (t *Team) getUnits(e db.Engine) (err error) {
|
2018-06-21 16:00:13 +00:00
|
|
|
if t.Units != nil {
|
|
|
|
return nil
|
2017-05-18 14:54:24 +00:00
|
|
|
}
|
2018-06-21 16:00:13 +00:00
|
|
|
|
|
|
|
t.Units, err = getUnitsByTeamID(e, t.ID)
|
|
|
|
return err
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 19:45:32 +00:00
|
|
|
// GetUnitNames returns the team units names
|
|
|
|
func (t *Team) GetUnitNames() (res []string) {
|
2022-01-05 03:37:00 +00:00
|
|
|
if t.AccessMode >= perm.AccessModeAdmin {
|
|
|
|
return unit.AllUnitKeyNames()
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:45:32 +00:00
|
|
|
for _, u := range t.Units {
|
2021-11-09 19:57:58 +00:00
|
|
|
res = append(res, unit.Units[u.Type].NameKey)
|
2018-11-10 19:45:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 03:37:00 +00:00
|
|
|
// GetUnitsMap returns the team units permissions
|
|
|
|
func (t *Team) GetUnitsMap() map[string]string {
|
|
|
|
m := make(map[string]string)
|
|
|
|
if t.AccessMode >= perm.AccessModeAdmin {
|
|
|
|
for _, u := range unit.Units {
|
|
|
|
m[u.NameKey] = t.AccessMode.String()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, u := range t.Units {
|
|
|
|
m[u.Unit().NameKey] = u.AccessMode.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// IsOwnerTeam returns true if team is owner team.
|
|
|
|
func (t *Team) IsOwnerTeam() bool {
|
2016-11-28 01:30:08 +00:00
|
|
|
return t.Name == ownerTeamName
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 01:30:08 +00:00
|
|
|
// IsMember returns true if given user is a member of team.
|
2016-11-28 08:33:08 +00:00
|
|
|
func (t *Team) IsMember(userID int64) bool {
|
2017-12-21 07:43:26 +00:00
|
|
|
isMember, err := IsTeamMember(t.OrgID, t.ID, userID)
|
|
|
|
if err != nil {
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Error("IsMember: %v", err)
|
2017-12-21 07:43:26 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return isMember
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func (t *Team) getRepositories(e db.Engine) error {
|
2019-11-06 09:37:14 +00:00
|
|
|
if t.Repos != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-16 04:07:58 +00:00
|
|
|
return e.Join("INNER", "team_repo", "repository.id = team_repo.repo_id").
|
2019-04-07 13:44:34 +00:00
|
|
|
Where("team_repo.team_id=?", t.ID).
|
|
|
|
OrderBy("repository.name").
|
|
|
|
Find(&t.Repos)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 19:00:29 +00:00
|
|
|
// GetRepositories returns paginated repositories in team of organization.
|
|
|
|
func (t *Team) GetRepositories(opts *SearchTeamOptions) error {
|
|
|
|
if opts.Page == 0 {
|
2021-09-23 15:45:36 +00:00
|
|
|
return t.getRepositories(db.GetEngine(db.DefaultContext))
|
2020-01-24 19:00:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:32:56 +00:00
|
|
|
return t.getRepositories(db.GetPaginatedSession(opts))
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func (t *Team) getMembers(e db.Engine) (err error) {
|
2016-03-25 22:04:02 +00:00
|
|
|
t.Members, err = getTeamMembers(e, t.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-24 19:00:29 +00:00
|
|
|
// GetMembers returns paginated members in team of organization.
|
|
|
|
func (t *Team) GetMembers(opts *SearchMembersOptions) (err error) {
|
|
|
|
if opts.Page == 0 {
|
2021-09-23 15:45:36 +00:00
|
|
|
return t.getMembers(db.GetEngine(db.DefaultContext))
|
2020-01-24 19:00:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:32:56 +00:00
|
|
|
return t.getMembers(db.GetPaginatedSession(opts))
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddMember adds new membership of the team to the organization,
|
|
|
|
// the user will have membership to the organization automatically when needed.
|
2016-11-28 08:33:08 +00:00
|
|
|
func (t *Team) AddMember(userID int64) error {
|
2017-02-24 06:25:09 +00:00
|
|
|
return AddTeamMember(t, userID)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveMember removes member from team of organization.
|
2016-11-28 08:33:08 +00:00
|
|
|
func (t *Team) RemoveMember(userID int64) error {
|
2017-02-24 06:25:09 +00:00
|
|
|
return RemoveTeamMember(t, userID)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func (t *Team) hasRepository(e db.Engine, repoID int64) bool {
|
2016-03-25 22:04:02 +00:00
|
|
|
return hasTeamRepo(e, t.OrgID, t.ID, repoID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasRepository returns true if given repository belong to team.
|
|
|
|
func (t *Team) HasRepository(repoID int64) bool {
|
2021-09-23 15:45:36 +00:00
|
|
|
return t.hasRepository(db.GetEngine(db.DefaultContext), repoID)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
func (t *Team) addRepository(ctx context.Context, repo *repo_model.Repository) (err error) {
|
|
|
|
e := db.GetEngine(ctx)
|
2016-03-25 22:04:02 +00:00
|
|
|
if err = addTeamRepo(e, t.OrgID, t.ID, repo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-31 03:08:08 +00:00
|
|
|
if _, err = e.Incr("num_repos").ID(t.ID).Update(new(Team)); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return fmt.Errorf("update team: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-12-31 03:08:08 +00:00
|
|
|
t.NumRepos++
|
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
if err = recalculateTeamAccesses(ctx, repo, 0); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return fmt.Errorf("recalculateAccesses: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-27 09:25:21 +00:00
|
|
|
// Make all team members watch this repo if enabled in global settings
|
|
|
|
if setting.Service.AutoWatchNewRepos {
|
|
|
|
if err = t.getMembers(e); err != nil {
|
|
|
|
return fmt.Errorf("getMembers: %v", err)
|
|
|
|
}
|
|
|
|
for _, u := range t.Members {
|
2021-12-12 15:48:20 +00:00
|
|
|
if err = repo_model.WatchRepoCtx(ctx, u.ID, repo.ID, true); err != nil {
|
2019-01-27 09:25:21 +00:00
|
|
|
return fmt.Errorf("watchRepo: %v", err)
|
|
|
|
}
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-27 09:25:21 +00:00
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:37:14 +00:00
|
|
|
// addAllRepositories adds all repositories to the team.
|
|
|
|
// If the team already has some repositories they will be left unchanged.
|
2021-12-10 01:27:50 +00:00
|
|
|
func (t *Team) addAllRepositories(ctx context.Context) error {
|
|
|
|
var orgRepos []repo_model.Repository
|
|
|
|
e := db.GetEngine(ctx)
|
2019-11-06 09:37:14 +00: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 {
|
|
|
|
if !t.hasRepository(e, repo.ID) {
|
2021-12-10 01:27:50 +00:00
|
|
|
if err := t.addRepository(ctx, &repo); err != nil {
|
2019-11-06 09:37:14 +00:00
|
|
|
return fmt.Errorf("addRepository: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-09 00:39:37 +00:00
|
|
|
// AddAllRepositories adds all repositories to the team
|
|
|
|
func (t *Team) AddAllRepositories() (err error) {
|
2021-11-21 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2019-11-09 00:39:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
2019-11-09 00:39:37 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
if err = t.addAllRepositories(ctx); err != nil {
|
2019-11-09 00:39:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2019-11-09 00:39:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// AddRepository adds new repository to team of organization.
|
2021-12-10 01:27:50 +00:00
|
|
|
func (t *Team) AddRepository(repo *repo_model.Repository) (err error) {
|
2016-03-25 22:04:02 +00:00
|
|
|
if repo.OwnerID != t.OrgID {
|
|
|
|
return errors.New("Repository does not belong to organization")
|
|
|
|
} else if t.HasRepository(repo.ID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
2016-03-25 22:04:02 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
if err = t.addRepository(ctx, repo); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:39:37 +00:00
|
|
|
// RemoveAllRepositories removes all repositories from team and recalculates access
|
|
|
|
func (t *Team) RemoveAllRepositories() (err error) {
|
|
|
|
if t.IncludesAllRepositories {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2019-11-09 00:39:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
2019-11-09 00:39:37 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
if err = t.removeAllRepositories(ctx); err != nil {
|
2019-11-09 00:39:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2019-11-09 00:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeAllRepositories removes all repositories from team and recalculates access
|
|
|
|
// Note: Shall not be called if team includes all repositories
|
2021-12-10 01:27:50 +00:00
|
|
|
func (t *Team) removeAllRepositories(ctx context.Context) (err error) {
|
|
|
|
e := db.GetEngine(ctx)
|
2019-11-09 00:39:37 +00:00
|
|
|
// Delete all accesses.
|
|
|
|
for _, repo := range t.Repos {
|
2021-12-10 01:27:50 +00:00
|
|
|
if err := recalculateTeamAccesses(ctx, repo, t.ID); err != nil {
|
2019-11-09 00:39:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove watches from all users and now unaccessible repos
|
|
|
|
for _, user := range t.Members {
|
2021-12-10 01:27:50 +00:00
|
|
|
has, err := hasAccess(ctx, user.ID, repo)
|
2019-11-09 00:39:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-12 15:48:20 +00:00
|
|
|
if err = repo_model.WatchRepoCtx(ctx, user.ID, repo.ID, false); err != nil {
|
2019-11-09 00:39:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all IssueWatches a user has subscribed to in the repositories
|
|
|
|
if err = removeIssueWatchersByRepoID(e, user.ID, repo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete team-repo
|
|
|
|
if _, err := e.
|
|
|
|
Where("team_id=?", t.ID).
|
|
|
|
Delete(new(TeamRepo)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.NumRepos = 0
|
|
|
|
if _, err = e.ID(t.ID).Cols("num_repos").Update(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:37:14 +00: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)
|
2021-12-10 01:27:50 +00:00
|
|
|
func (t *Team) removeRepository(ctx context.Context, repo *repo_model.Repository, recalculate bool) (err error) {
|
|
|
|
e := db.GetEngine(ctx)
|
2016-03-25 22:04:02 +00:00
|
|
|
if err = removeTeamRepo(e, t.ID, repo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t.NumRepos--
|
2017-10-05 04:43:04 +00:00
|
|
|
if _, err = e.ID(t.ID).Cols("num_repos").Update(t); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't need to recalculate when delete a repository from organization.
|
|
|
|
if recalculate {
|
2021-12-10 01:27:50 +00:00
|
|
|
if err = recalculateTeamAccesses(ctx, repo, t.ID); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 00:51:46 +00:00
|
|
|
teamUsers, err := getTeamUsersByTeamID(e, t.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getTeamUsersByTeamID: %v", err)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
2017-03-21 00:55:00 +00:00
|
|
|
for _, teamUser := range teamUsers {
|
2021-12-10 01:27:50 +00:00
|
|
|
has, err := hasAccess(ctx, teamUser.UID, repo)
|
2016-03-25 22:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-12 15:48:20 +00:00
|
|
|
if err = repo_model.WatchRepoCtx(ctx, teamUser.UID, repo.ID, false); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-06-19 19:44:33 +00:00
|
|
|
|
|
|
|
// Remove all IssueWatches a user has subscribed to in the repositories
|
|
|
|
if err := removeIssueWatchersByRepoID(e, teamUser.UID, repo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRepository removes repository from team of organization.
|
2019-11-06 09:37:14 +00:00
|
|
|
// If the team shall include all repositories the request is ignored.
|
2016-03-25 22:04:02 +00:00
|
|
|
func (t *Team) RemoveRepository(repoID int64) error {
|
|
|
|
if !t.HasRepository(repoID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:37:14 +00:00
|
|
|
if t.IncludesAllRepositories {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
repo, err := repo_model.GetRepositoryByID(repoID)
|
2016-03-25 22:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
2016-03-25 22:04:02 +00:00
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
if err = t.removeRepository(ctx, repo, true); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 08:46:54 +00:00
|
|
|
// UnitEnabled returns if the team has the given unit type enabled
|
2021-11-09 19:57:58 +00:00
|
|
|
func (t *Team) UnitEnabled(tp unit.Type) bool {
|
2021-09-23 15:45:36 +00:00
|
|
|
return t.unitEnabled(db.GetEngine(db.DefaultContext), tp)
|
2018-10-25 10:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 19:57:58 +00:00
|
|
|
func (t *Team) unitEnabled(e db.Engine, tp unit.Type) bool {
|
2022-01-05 03:37:00 +00:00
|
|
|
return t.unitAccessMode(e, tp) > perm.AccessModeNone
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnitAccessMode returns if the team has the given unit type enabled
|
|
|
|
func (t *Team) UnitAccessMode(tp unit.Type) perm.AccessMode {
|
|
|
|
return t.unitAccessMode(db.GetEngine(db.DefaultContext), tp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Team) unitAccessMode(e db.Engine, tp unit.Type) perm.AccessMode {
|
2018-10-25 10:55:16 +00:00
|
|
|
if err := t.getUnits(e); err != nil {
|
2019-10-10 16:45:11 +00:00
|
|
|
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
|
2017-05-18 14:54:24 +00:00
|
|
|
}
|
2018-06-21 16:00:13 +00:00
|
|
|
|
|
|
|
for _, unit := range t.Units {
|
|
|
|
if unit.Type == tp {
|
2022-01-05 03:37:00 +00:00
|
|
|
return unit.AccessMode
|
2017-05-18 14:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-05 03:37:00 +00:00
|
|
|
return perm.AccessModeNone
|
2017-05-18 14:54:24 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 01:30:08 +00:00
|
|
|
// IsUsableTeamName tests if a name could be as team name
|
2017-02-23 01:36:15 +00:00
|
|
|
func IsUsableTeamName(name string) error {
|
|
|
|
switch name {
|
|
|
|
case "new":
|
2021-11-24 09:49:20 +00:00
|
|
|
return db.ErrNameReserved{Name: name}
|
2017-02-23 01:36:15 +00:00
|
|
|
default:
|
|
|
|
return nil
|
2016-11-06 09:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// NewTeam creates a record of new team.
|
|
|
|
// It's caller's responsibility to assign organization ID.
|
2016-11-06 09:07:03 +00:00
|
|
|
func NewTeam(t *Team) (err error) {
|
2016-03-25 22:04:02 +00:00
|
|
|
if len(t.Name) == 0 {
|
|
|
|
return errors.New("empty team name")
|
|
|
|
}
|
|
|
|
|
2016-11-06 09:07:03 +00:00
|
|
|
if err = IsUsableTeamName(t.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).ID(t.OrgID).Get(new(user_model.User))
|
2016-03-25 22:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-12 19:41:28 +00:00
|
|
|
}
|
|
|
|
if !has {
|
2017-07-06 13:30:19 +00:00
|
|
|
return ErrOrgNotExist{t.OrgID, ""}
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.LowerName = strings.ToLower(t.Name)
|
2021-09-23 15:45:36 +00:00
|
|
|
has, err = db.GetEngine(db.DefaultContext).
|
2016-11-10 15:16:32 +00:00
|
|
|
Where("org_id=?", t.OrgID).
|
|
|
|
And("lower_name=?", t.LowerName).
|
|
|
|
Get(new(Team))
|
2016-03-25 22:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-12 19:41:28 +00:00
|
|
|
}
|
|
|
|
if has {
|
2016-03-25 22:04:02 +00:00
|
|
|
return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
2016-03-25 22:04:02 +00:00
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
if err = db.Insert(ctx, t); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-21 16:00:13 +00:00
|
|
|
// insert units for team
|
|
|
|
if len(t.Units) > 0 {
|
|
|
|
for _, unit := range t.Units {
|
|
|
|
unit.TeamID = t.ID
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
if err = db.Insert(ctx, &t.Units); err != nil {
|
2018-06-21 16:00:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:37:14 +00:00
|
|
|
// Add all repositories to the team if it has access to all of them.
|
|
|
|
if t.IncludesAllRepositories {
|
2021-12-10 01:27:50 +00:00
|
|
|
err = t.addAllRepositories(ctx)
|
2019-11-06 09:37:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("addAllRepositories: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// Update organization number of teams.
|
2021-11-21 15:41:00 +00:00
|
|
|
if _, err = db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func getTeam(e db.Engine, orgID int64, name string) (*Team, error) {
|
2016-03-25 22:04:02 +00:00
|
|
|
t := &Team{
|
2016-11-28 01:30:08 +00:00
|
|
|
OrgID: orgID,
|
2016-03-25 22:04:02 +00:00
|
|
|
LowerName: strings.ToLower(name),
|
|
|
|
}
|
|
|
|
has, err := e.Get(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
2019-09-23 20:08:03 +00:00
|
|
|
return nil, ErrTeamNotExist{orgID, 0, name}
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeam returns team by given team name and organization.
|
2016-11-28 01:30:08 +00:00
|
|
|
func GetTeam(orgID int64, name string) (*Team, error) {
|
2021-09-23 15:45:36 +00:00
|
|
|
return getTeam(db.GetEngine(db.DefaultContext), orgID, name)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 23:19:35 +00:00
|
|
|
// GetTeamIDsByNames returns a slice of team ids corresponds to names.
|
|
|
|
func GetTeamIDsByNames(orgID int64, names []string, ignoreNonExistent bool) ([]int64, error) {
|
|
|
|
ids := make([]int64, 0, len(names))
|
|
|
|
for _, name := range names {
|
|
|
|
u, err := GetTeam(orgID, name)
|
|
|
|
if err != nil {
|
|
|
|
if ignoreNonExistent {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ids = append(ids, u.ID)
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:06:28 +00:00
|
|
|
// getOwnerTeam returns team by given team name and organization.
|
2021-09-19 11:49:59 +00:00
|
|
|
func getOwnerTeam(e db.Engine, orgID int64) (*Team, error) {
|
2019-08-02 16:06:28 +00:00
|
|
|
return getTeam(e, orgID, ownerTeamName)
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func getTeamByID(e db.Engine, teamID int64) (*Team, error) {
|
2016-03-25 22:04:02 +00:00
|
|
|
t := new(Team)
|
2017-10-05 04:43:04 +00:00
|
|
|
has, err := e.ID(teamID).Get(t)
|
2016-03-25 22:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
2019-09-23 20:08:03 +00:00
|
|
|
return nil, ErrTeamNotExist{0, teamID, ""}
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamByID returns team by given ID.
|
2016-11-28 01:30:08 +00:00
|
|
|
func GetTeamByID(teamID int64) (*Team, error) {
|
2021-09-23 15:45:36 +00:00
|
|
|
return getTeamByID(db.GetEngine(db.DefaultContext), teamID)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 23:19:35 +00:00
|
|
|
// GetTeamNamesByID returns team's lower name from a list of team ids.
|
|
|
|
func GetTeamNamesByID(teamIDs []int64) ([]string, error) {
|
|
|
|
if len(teamIDs) == 0 {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var teamNames []string
|
2021-09-23 15:45:36 +00:00
|
|
|
err := db.GetEngine(db.DefaultContext).Table("team").
|
2020-02-12 23:19:35 +00:00
|
|
|
Select("lower_name").
|
|
|
|
In("id", teamIDs).
|
|
|
|
Asc("name").
|
|
|
|
Find(&teamNames)
|
|
|
|
|
|
|
|
return teamNames, err
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// UpdateTeam updates information of team.
|
2021-03-14 18:52:12 +00:00
|
|
|
func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
|
2016-03-25 22:04:02 +00: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 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
|
|
|
sess := db.GetEngine(ctx)
|
2016-03-25 22:04:02 +00:00
|
|
|
|
|
|
|
t.LowerName = strings.ToLower(t.Name)
|
2017-01-27 16:11:41 +00:00
|
|
|
has, err := sess.
|
2016-11-10 15:16:32 +00:00
|
|
|
Where("org_id=?", t.OrgID).
|
|
|
|
And("lower_name=?", t.LowerName).
|
|
|
|
And("id!=?", t.ID).
|
|
|
|
Get(new(Team))
|
2016-03-25 22:04:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has {
|
|
|
|
return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
|
|
|
|
}
|
|
|
|
|
2020-01-09 13:15:14 +00: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 22:04:02 +00:00
|
|
|
return fmt.Errorf("update: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-10 19:45:32 +00: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).
|
|
|
|
Delete(new(TeamUnit)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-05 03:37:00 +00:00
|
|
|
if _, err = sess.Cols("org_id", "team_id", "type", "access_mode").Insert(&t.Units); err != nil {
|
2018-11-10 19:45:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// Update access for team members if needed.
|
|
|
|
if authChanged {
|
|
|
|
if err = t.getRepositories(sess); err != nil {
|
2017-02-23 01:36:15 +00:00
|
|
|
return fmt.Errorf("getRepositories: %v", err)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range t.Repos {
|
2021-12-10 01:27:50 +00:00
|
|
|
if err = recalculateTeamAccesses(ctx, repo, 0); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return fmt.Errorf("recalculateTeamAccesses: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 09:37:14 +00:00
|
|
|
// Add all repositories to the team if it has access to all of them.
|
|
|
|
if includeAllChanged && t.IncludesAllRepositories {
|
2021-12-10 01:27:50 +00:00
|
|
|
err = t.addAllRepositories(ctx)
|
2019-11-06 09:37:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("addAllRepositories: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTeam deletes given team.
|
|
|
|
// It's caller's responsibility to assign organization ID.
|
|
|
|
func DeleteTeam(t *Team) error {
|
2020-01-24 19:00:29 +00:00
|
|
|
if err := t.GetRepositories(&SearchTeamOptions{}); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
|
|
|
sess := db.GetEngine(ctx)
|
2016-03-25 22:04:02 +00:00
|
|
|
|
2018-06-19 19:44:33 +00:00
|
|
|
if err := t.getMembers(sess); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-10 01:27:50 +00:00
|
|
|
if err := t.removeAllRepositories(ctx); err != nil {
|
2017-02-23 01:36:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// Delete team-user.
|
2017-03-01 01:09:49 +00:00
|
|
|
if _, err := sess.
|
|
|
|
Where("org_id=?", t.OrgID).
|
2016-11-10 15:16:32 +00:00
|
|
|
Where("team_id=?", t.ID).
|
|
|
|
Delete(new(TeamUser)); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-21 16:00:13 +00:00
|
|
|
// Delete team-unit.
|
|
|
|
if _, err := sess.
|
|
|
|
Where("team_id=?", t.ID).
|
|
|
|
Delete(new(TeamUnit)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// Delete team.
|
2017-10-05 04:43:04 +00:00
|
|
|
if _, err := sess.ID(t.ID).Delete(new(Team)); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Update organization number of teams.
|
2017-03-01 01:09:49 +00:00
|
|
|
if _, err := sess.Exec("UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ___________ ____ ___
|
|
|
|
// \__ ___/___ _____ _____ | | \______ ___________
|
|
|
|
// | |_/ __ \\__ \ / \| | / ___// __ \_ __ \
|
|
|
|
// | |\ ___/ / __ \| Y Y \ | /\___ \\ ___/| | \/
|
|
|
|
// |____| \___ >____ /__|_| /______//____ >\___ >__|
|
|
|
|
// \/ \/ \/ \/ \/
|
|
|
|
|
|
|
|
// TeamUser represents an team-user relation.
|
|
|
|
type TeamUser struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OrgID int64 `xorm:"INDEX"`
|
|
|
|
TeamID int64 `xorm:"UNIQUE(s)"`
|
2016-11-28 01:30:08 +00:00
|
|
|
UID int64 `xorm:"UNIQUE(s)"`
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func isTeamMember(e db.Engine, orgID, teamID, userID int64) (bool, error) {
|
2017-12-21 07:43:26 +00:00
|
|
|
return e.
|
2016-11-10 15:16:32 +00:00
|
|
|
Where("org_id=?", orgID).
|
|
|
|
And("team_id=?", teamID).
|
2016-11-28 08:33:08 +00:00
|
|
|
And("uid=?", userID).
|
2017-12-21 07:43:26 +00:00
|
|
|
Table("team_user").
|
|
|
|
Exist()
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsTeamMember returns true if given user is a member of team.
|
2017-12-21 07:43:26 +00:00
|
|
|
func IsTeamMember(orgID, teamID, userID int64) (bool, error) {
|
2021-09-23 15:45:36 +00:00
|
|
|
return isTeamMember(db.GetEngine(db.DefaultContext), orgID, teamID, userID)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func getTeamUsersByTeamID(e db.Engine, teamID int64) ([]*TeamUser, error) {
|
2016-03-25 22:04:02 +00:00
|
|
|
teamUsers := make([]*TeamUser, 0, 10)
|
2017-03-15 00:51:46 +00:00
|
|
|
return teamUsers, e.
|
2016-11-10 15:16:32 +00:00
|
|
|
Where("team_id=?", teamID).
|
2017-03-15 00:51:46 +00:00
|
|
|
Find(&teamUsers)
|
|
|
|
}
|
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
func getTeamMembers(e db.Engine, teamID int64) (_ []*user_model.User, err error) {
|
2017-03-15 00:51:46 +00:00
|
|
|
teamUsers, err := getTeamUsersByTeamID(e, teamID)
|
|
|
|
if err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return nil, fmt.Errorf("get team-users: %v", err)
|
|
|
|
}
|
2021-11-24 09:49:20 +00:00
|
|
|
members := make([]*user_model.User, len(teamUsers))
|
2017-03-15 00:51:46 +00:00
|
|
|
for i, teamUser := range teamUsers {
|
2021-11-24 09:49:20 +00:00
|
|
|
member, err := user_model.GetUserByIDEngine(e, teamUser.UID)
|
2017-03-15 00:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get user '%d': %v", teamUser.UID, err)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
2017-03-15 00:51:46 +00:00
|
|
|
members[i] = member
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
2019-04-07 13:44:34 +00:00
|
|
|
sort.Slice(members, func(i, j int) bool {
|
|
|
|
return members[i].DisplayName() < members[j].DisplayName()
|
|
|
|
})
|
2016-03-25 22:04:02 +00:00
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamMembers returns all members in given team of organization.
|
2021-11-24 09:49:20 +00:00
|
|
|
func GetTeamMembers(teamID int64) ([]*user_model.User, error) {
|
2021-09-23 15:45:36 +00:00
|
|
|
return getTeamMembers(db.GetEngine(db.DefaultContext), teamID)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func getUserOrgTeams(e db.Engine, orgID, userID int64) (teams []*Team, err error) {
|
2017-02-16 04:06:23 +00:00
|
|
|
return teams, e.
|
|
|
|
Join("INNER", "team_user", "team_user.team_id = team.id").
|
|
|
|
Where("team.org_id = ?", orgID).
|
|
|
|
And("team_user.uid=?", userID).
|
|
|
|
Find(&teams)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func getUserRepoTeams(e db.Engine, orgID, userID, repoID int64) (teams []*Team, err error) {
|
2018-11-09 06:16:52 +00:00
|
|
|
return teams, e.
|
|
|
|
Join("INNER", "team_user", "team_user.team_id = team.id").
|
|
|
|
Join("INNER", "team_repo", "team_repo.team_id = team.id").
|
|
|
|
Where("team.org_id = ?", orgID).
|
|
|
|
And("team_user.uid=?", userID).
|
|
|
|
And("team_repo.repo_id=?", repoID).
|
|
|
|
Find(&teams)
|
|
|
|
}
|
|
|
|
|
2019-01-17 00:39:50 +00:00
|
|
|
// GetUserOrgTeams returns all teams that user belongs to in given organization.
|
|
|
|
func GetUserOrgTeams(orgID, userID int64) ([]*Team, error) {
|
2021-09-23 15:45:36 +00:00
|
|
|
return getUserOrgTeams(db.GetEngine(db.DefaultContext), orgID, userID)
|
2019-01-17 00:39:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// AddTeamMember adds new membership of given team to given organization,
|
|
|
|
// the user will have membership to given organization automatically when needed.
|
2017-02-24 06:25:09 +00:00
|
|
|
func AddTeamMember(team *Team, userID int64) error {
|
2017-12-21 07:43:26 +00:00
|
|
|
isAlreadyMember, err := IsTeamMember(team.OrgID, team.ID, userID)
|
|
|
|
if err != nil || isAlreadyMember {
|
|
|
|
return err
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 06:25:09 +00:00
|
|
|
if err := AddOrgUser(team.OrgID, userID); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get team and its repositories.
|
2020-01-24 19:00:29 +00:00
|
|
|
if err := team.GetRepositories(&SearchTeamOptions{}); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
2016-03-25 22:04:02 +00:00
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
sess := db.GetEngine(ctx)
|
|
|
|
|
|
|
|
if err := db.Insert(ctx, &TeamUser{
|
2016-11-28 08:33:08 +00:00
|
|
|
UID: userID,
|
2017-02-24 06:25:09 +00:00
|
|
|
OrgID: team.OrgID,
|
|
|
|
TeamID: team.ID,
|
|
|
|
}); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
2017-12-31 03:08:08 +00:00
|
|
|
} else if _, err := sess.Incr("num_members").ID(team.ID).Update(new(Team)); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-31 03:08:08 +00:00
|
|
|
team.NumMembers++
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// Give access to team repositories.
|
2017-02-24 06:25:09 +00:00
|
|
|
for _, repo := range team.Repos {
|
2021-12-10 01:27:50 +00:00
|
|
|
if err := recalculateUserAccess(ctx, repo, userID); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-01-27 09:25:21 +00:00
|
|
|
if setting.Service.AutoWatchNewRepos {
|
2021-12-12 15:48:20 +00:00
|
|
|
if err = repo_model.WatchRepoCtx(ctx, userID, repo.ID, true); err != nil {
|
2019-01-27 09:25:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-06-19 19:44:33 +00:00
|
|
|
}
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 11:41:40 +00:00
|
|
|
func removeTeamMember(ctx context.Context, team *Team, userID int64) error {
|
|
|
|
e := db.GetEngine(ctx)
|
2017-12-21 07:43:26 +00:00
|
|
|
isMember, err := isTeamMember(e, team.OrgID, team.ID, userID)
|
|
|
|
if err != nil || !isMember {
|
|
|
|
return err
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user to delete is the last member in owner team.
|
2017-02-24 06:25:09 +00:00
|
|
|
if team.IsOwnerTeam() && team.NumMembers == 1 {
|
2016-11-28 08:33:08 +00:00
|
|
|
return ErrLastOrgOwner{UID: userID}
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 06:25:09 +00:00
|
|
|
team.NumMembers--
|
2016-03-25 22:04:02 +00:00
|
|
|
|
2017-02-24 06:25:09 +00:00
|
|
|
if err := team.getRepositories(e); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-24 06:25:09 +00:00
|
|
|
if _, err := e.Delete(&TeamUser{
|
2016-11-28 08:33:08 +00:00
|
|
|
UID: userID,
|
2017-02-24 06:25:09 +00:00
|
|
|
OrgID: team.OrgID,
|
|
|
|
TeamID: team.ID,
|
|
|
|
}); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
2016-11-10 15:16:32 +00:00
|
|
|
} else if _, err = e.
|
2017-10-05 04:43:04 +00:00
|
|
|
ID(team.ID).
|
2017-09-25 04:59:27 +00:00
|
|
|
Cols("num_members").
|
2017-02-24 06:25:09 +00:00
|
|
|
Update(team); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete access to team repositories.
|
2017-02-24 06:25:09 +00:00
|
|
|
for _, repo := range team.Repos {
|
2021-12-10 01:27:50 +00:00
|
|
|
if err := recalculateUserAccess(ctx, repo, userID); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-06-19 19:44:33 +00:00
|
|
|
|
|
|
|
// Remove watches from now unaccessible
|
2021-12-10 01:27:50 +00:00
|
|
|
if err := reconsiderWatches(ctx, repo, userID); err != nil {
|
2018-06-19 19:44:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-07 21:52:01 +00:00
|
|
|
// Remove issue assignments from now unaccessible
|
2021-12-10 01:27:50 +00:00
|
|
|
if err := reconsiderRepoIssuesAssignee(ctx, repo, userID); err != nil {
|
2018-06-19 19:44:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 08:42:02 +00:00
|
|
|
// Check if the user is a member of any team in the organization.
|
|
|
|
if count, err := e.Count(&TeamUser{
|
|
|
|
UID: userID,
|
|
|
|
OrgID: team.OrgID,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
} else if count == 0 {
|
2021-11-19 11:41:40 +00:00
|
|
|
return removeOrgUser(ctx, team.OrgID, userID)
|
2018-02-23 08:42:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTeamMember removes member from given team of given organization.
|
2017-02-24 06:25:09 +00:00
|
|
|
func RemoveTeamMember(team *Team, userID int64) error {
|
2021-11-19 11:41:40 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-19 11:41:40 +00:00
|
|
|
defer committer.Close()
|
|
|
|
if err := removeTeamMember(ctx, team, userID); err != nil {
|
2016-03-25 22:04:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-19 11:41:40 +00:00
|
|
|
return committer.Commit()
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 08:16:22 +00:00
|
|
|
// IsUserInTeams returns if a user in some teams
|
|
|
|
func IsUserInTeams(userID int64, teamIDs []int64) (bool, error) {
|
2021-09-23 15:45:36 +00:00
|
|
|
return isUserInTeams(db.GetEngine(db.DefaultContext), userID, teamIDs)
|
2019-12-04 01:08:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func isUserInTeams(e db.Engine, userID int64, teamIDs []int64) (bool, error) {
|
2019-12-04 01:08:56 +00:00
|
|
|
return e.Where("uid=?", userID).In("team_id", teamIDs).Exist(new(TeamUser))
|
2017-09-14 08:16:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 11:28:37 +00:00
|
|
|
// UsersInTeamsCount counts the number of users which are in userIDs and teamIDs
|
2021-03-14 18:52:12 +00:00
|
|
|
func UsersInTeamsCount(userIDs, teamIDs []int64) (int64, error) {
|
2019-08-07 04:22:41 +00:00
|
|
|
var ids []int64
|
2021-09-23 15:45:36 +00:00
|
|
|
if err := db.GetEngine(db.DefaultContext).In("uid", userIDs).In("team_id", teamIDs).
|
2019-08-07 04:22:41 +00:00
|
|
|
Table("team_user").
|
|
|
|
Cols("uid").GroupBy("uid").Find(&ids); err != nil {
|
2018-12-11 11:28:37 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2019-08-07 04:22:41 +00:00
|
|
|
return int64(len(ids)), nil
|
2018-12-11 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 22:04:02 +00:00
|
|
|
// ___________ __________
|
|
|
|
// \__ ___/___ _____ _____\______ \ ____ ______ ____
|
|
|
|
// | |_/ __ \\__ \ / \| _// __ \\____ \ / _ \
|
|
|
|
// | |\ ___/ / __ \| Y Y \ | \ ___/| |_> > <_> )
|
|
|
|
// |____| \___ >____ /__|_| /____|_ /\___ > __/ \____/
|
|
|
|
// \/ \/ \/ \/ \/|__|
|
|
|
|
|
|
|
|
// TeamRepo represents an team-repository relation.
|
|
|
|
type TeamRepo struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OrgID int64 `xorm:"INDEX"`
|
|
|
|
TeamID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
RepoID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func hasTeamRepo(e db.Engine, orgID, teamID, repoID int64) bool {
|
2016-11-10 15:16:32 +00:00
|
|
|
has, _ := e.
|
|
|
|
Where("org_id=?", orgID).
|
|
|
|
And("team_id=?", teamID).
|
|
|
|
And("repo_id=?", repoID).
|
|
|
|
Get(new(TeamRepo))
|
2016-03-25 22:04:02 +00:00
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasTeamRepo returns true if given repository belongs to team.
|
|
|
|
func HasTeamRepo(orgID, teamID, repoID int64) bool {
|
2021-09-23 15:45:36 +00:00
|
|
|
return hasTeamRepo(db.GetEngine(db.DefaultContext), orgID, teamID, repoID)
|
2016-03-25 22:04:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func addTeamRepo(e db.Engine, orgID, teamID, repoID int64) error {
|
2016-03-25 22:04:02 +00:00
|
|
|
_, err := e.InsertOne(&TeamRepo{
|
|
|
|
OrgID: orgID,
|
|
|
|
TeamID: teamID,
|
|
|
|
RepoID: repoID,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func removeTeamRepo(e db.Engine, teamID, repoID int64) error {
|
2016-03-25 22:04:02 +00:00
|
|
|
_, err := e.Delete(&TeamRepo{
|
|
|
|
TeamID: teamID,
|
|
|
|
RepoID: repoID,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2017-09-14 08:16:22 +00:00
|
|
|
|
|
|
|
// GetTeamsWithAccessToRepo returns all teams in an organization that have given access level to the repository.
|
2021-11-28 11:58:28 +00:00
|
|
|
func GetTeamsWithAccessToRepo(orgID, repoID int64, mode perm.AccessMode) ([]*Team, error) {
|
2017-09-14 08:16:22 +00:00
|
|
|
teams := make([]*Team, 0, 5)
|
2021-09-23 15:45:36 +00:00
|
|
|
return teams, db.GetEngine(db.DefaultContext).Where("team.authorize >= ?", mode).
|
2017-09-14 08:16:22 +00:00
|
|
|
Join("INNER", "team_repo", "team_repo.team_id = team.id").
|
|
|
|
And("team_repo.org_id = ?", orgID).
|
|
|
|
And("team_repo.repo_id = ?", repoID).
|
|
|
|
Find(&teams)
|
|
|
|
}
|
2018-06-21 16:00:13 +00:00
|
|
|
|
|
|
|
// ___________ ____ ___ .__ __
|
|
|
|
// \__ ___/___ _____ _____ | | \____ |__|/ |_
|
|
|
|
// | |_/ __ \\__ \ / \| | / \| \ __\
|
|
|
|
// | |\ ___/ / __ \| Y Y \ | / | \ || |
|
|
|
|
// |____| \___ >____ /__|_| /______/|___| /__||__|
|
|
|
|
// \/ \/ \/ \/
|
|
|
|
|
|
|
|
// TeamUnit describes all units of a repository
|
|
|
|
type TeamUnit struct {
|
2022-01-05 03:37:00 +00:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OrgID int64 `xorm:"INDEX"`
|
|
|
|
TeamID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
Type unit.Type `xorm:"UNIQUE(s)"`
|
|
|
|
AccessMode perm.AccessMode
|
2018-06-21 16:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unit returns Unit
|
2021-11-09 19:57:58 +00:00
|
|
|
func (t *TeamUnit) Unit() unit.Unit {
|
|
|
|
return unit.Units[t.Type]
|
2018-06-21 16:00:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func getUnitsByTeamID(e db.Engine, teamID int64) (units []*TeamUnit, err error) {
|
2018-06-21 16:00:13 +00:00
|
|
|
return units, e.Where("team_id = ?", teamID).Find(&units)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTeamUnits updates a teams's units
|
|
|
|
func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) {
|
2021-11-21 15:41:00 +00:00
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2018-06-21 16:00:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 15:41:00 +00:00
|
|
|
defer committer.Close()
|
2018-06-21 16:00:13 +00:00
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
if _, err = db.GetEngine(ctx).Where("team_id = ?", team.ID).Delete(new(TeamUnit)); err != nil {
|
2018-06-21 16:00:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-22 15:12:55 +00:00
|
|
|
if len(units) > 0 {
|
2021-11-21 15:41:00 +00:00
|
|
|
if err = db.Insert(ctx, units); err != nil {
|
2020-03-22 15:12:55 +00:00
|
|
|
return err
|
2019-06-12 19:41:28 +00:00
|
|
|
}
|
2018-06-21 16:00:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 15:41:00 +00:00
|
|
|
return committer.Commit()
|
2018-06-21 16:00:13 +00:00
|
|
|
}
|