2018-06-21 21:30:13 +05:30
// Copyright 2018 The Gitea Authors. All rights reserved.
2016-03-26 03:34:02 +05:30
// Copyright 2016 The Gogs Authors. All rights reserved.
2022-11-27 23:50:29 +05:30
// SPDX-License-Identifier: MIT
2016-03-26 03:34:02 +05:30
package models
import (
2021-11-19 17:11:40 +05:30
"context"
2016-03-26 03:34:02 +05:30
"fmt"
"strings"
2017-12-21 13:13:26 +05:30
2021-09-19 17:19:59 +05:30
"code.gitea.io/gitea/models/db"
2022-06-12 21:21:54 +05:30
git_model "code.gitea.io/gitea/models/git"
2022-06-13 15:07:59 +05:30
issues_model "code.gitea.io/gitea/models/issues"
2022-03-29 11:59:02 +05:30
"code.gitea.io/gitea/models/organization"
2022-05-11 15:39:36 +05:30
access_model "code.gitea.io/gitea/models/perm/access"
2021-12-10 06:57:50 +05:30
repo_model "code.gitea.io/gitea/models/repo"
2021-11-24 15:19:20 +05:30
user_model "code.gitea.io/gitea/models/user"
2022-04-01 07:23:18 +05:30
"code.gitea.io/gitea/modules/log"
2019-01-27 14:55:21 +05:30
"code.gitea.io/gitea/modules/setting"
2022-03-22 06:39:45 +05:30
"code.gitea.io/gitea/modules/util"
2019-01-27 14:55:21 +05:30
2019-10-01 11:02:28 +05:30
"xorm.io/builder"
2016-03-26 03:34:02 +05:30
)
2022-08-25 08:01:57 +05:30
func AddRepository ( ctx context . Context , t * organization . Team , repo * repo_model . Repository ) ( err error ) {
2022-03-29 11:59:02 +05:30
if err = organization . AddTeamRepo ( ctx , t . OrgID , t . ID , repo . ID ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2022-08-25 08:01:57 +05:30
if err = organization . IncrTeamRepoNum ( ctx , t . ID ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "update team: %w" , err )
2016-03-26 03:34:02 +05:30
}
2017-12-31 08:38:08 +05:30
t . NumRepos ++
2022-05-11 15:39:36 +05:30
if err = access_model . RecalculateTeamAccesses ( ctx , repo , 0 ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "recalculateAccesses: %w" , err )
2016-03-26 03:34:02 +05:30
}
2019-01-27 14:55:21 +05:30
// Make all team members watch this repo if enabled in global settings
if setting . Service . AutoWatchNewRepos {
2022-12-03 08:18:26 +05:30
if err = t . LoadMembers ( ctx ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "getMembers: %w" , err )
2019-01-27 14:55:21 +05:30
}
for _ , u := range t . Members {
2022-05-20 19:38:52 +05:30
if err = repo_model . WatchRepo ( ctx , u . ID , repo . ID , true ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "watchRepo: %w" , err )
2019-01-27 14:55:21 +05:30
}
2016-03-26 03:34:02 +05:30
}
}
2019-01-27 14:55:21 +05:30
2016-03-26 03:34:02 +05:30
return nil
}
2019-11-06 15:07:14 +05:30
// addAllRepositories adds all repositories to the team.
// If the team already has some repositories they will be left unchanged.
2022-03-29 11:59:02 +05:30
func addAllRepositories ( ctx context . Context , t * organization . Team ) error {
2022-08-25 08:01:57 +05:30
orgRepos , err := organization . GetOrgRepositories ( ctx , t . OrgID )
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "get org repos: %w" , err )
2019-11-06 15:07:14 +05:30
}
for _ , repo := range orgRepos {
2022-05-11 15:39:36 +05:30
if ! organization . HasTeamRepo ( ctx , t . OrgID , t . ID , repo . ID ) {
2022-08-25 08:01:57 +05:30
if err := AddRepository ( ctx , t , repo ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "AddRepository: %w" , err )
2019-11-06 15:07:14 +05:30
}
}
}
return nil
}
2019-11-09 06:09:37 +05:30
// AddAllRepositories adds all repositories to the team
2022-03-29 11:59:02 +05:30
func AddAllRepositories ( t * organization . Team ) ( err error ) {
2022-11-13 01:48:50 +05:30
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 21:11:00 +05:30
if err != nil {
2019-11-09 06:09:37 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2019-11-09 06:09:37 +05:30
2022-03-29 11:59:02 +05:30
if err = addAllRepositories ( ctx , t ) ; err != nil {
2019-11-09 06:09:37 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2019-11-09 06:09:37 +05:30
}
// RemoveAllRepositories removes all repositories from team and recalculates access
2022-03-29 11:59:02 +05:30
func RemoveAllRepositories ( t * organization . Team ) ( err error ) {
2019-11-09 06:09:37 +05:30
if t . IncludesAllRepositories {
return nil
}
2022-11-13 01:48:50 +05:30
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 21:11:00 +05:30
if err != nil {
2019-11-09 06:09:37 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2019-11-09 06:09:37 +05:30
2022-03-29 11:59:02 +05:30
if err = removeAllRepositories ( ctx , t ) ; err != nil {
2019-11-09 06:09:37 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2019-11-09 06:09:37 +05:30
}
// removeAllRepositories removes all repositories from team and recalculates access
// Note: Shall not be called if team includes all repositories
2022-03-29 11:59:02 +05:30
func removeAllRepositories ( ctx context . Context , t * organization . Team ) ( err error ) {
2021-12-10 06:57:50 +05:30
e := db . GetEngine ( ctx )
2019-11-09 06:09:37 +05:30
// Delete all accesses.
for _ , repo := range t . Repos {
2022-05-11 15:39:36 +05:30
if err := access_model . RecalculateTeamAccesses ( ctx , repo , t . ID ) ; err != nil {
2019-11-09 06:09:37 +05:30
return err
}
// Remove watches from all users and now unaccessible repos
for _ , user := range t . Members {
2022-05-11 15:39:36 +05:30
has , err := access_model . HasAccess ( ctx , user . ID , repo )
2019-11-09 06:09:37 +05:30
if err != nil {
return err
} else if has {
continue
}
2022-05-20 19:38:52 +05:30
if err = repo_model . WatchRepo ( ctx , user . ID , repo . ID , false ) ; err != nil {
2019-11-09 06:09:37 +05:30
return err
}
// Remove all IssueWatches a user has subscribed to in the repositories
2022-06-13 15:07:59 +05:30
if err = issues_model . RemoveIssueWatchersByRepoID ( ctx , user . ID , repo . ID ) ; err != nil {
2019-11-09 06:09:37 +05:30
return err
}
}
}
// Delete team-repo
if _ , err := e .
Where ( "team_id=?" , t . ID ) .
2022-03-29 11:59:02 +05:30
Delete ( new ( organization . TeamRepo ) ) ; err != nil {
2019-11-09 06:09:37 +05:30
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 15:39:36 +05:30
// 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 11:59:02 +05:30
}
2019-11-06 15:07:14 +05:30
// 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 11:59:02 +05:30
func removeRepository ( ctx context . Context , t * organization . Team , repo * repo_model . Repository , recalculate bool ) ( err error ) {
2021-12-10 06:57:50 +05:30
e := db . GetEngine ( ctx )
2022-03-29 11:59:02 +05:30
if err = organization . RemoveTeamRepo ( ctx , t . ID , repo . ID ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
t . NumRepos --
2017-10-05 10:13:04 +05:30
if _ , err = e . ID ( t . ID ) . Cols ( "num_repos" ) . Update ( t ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
// Don't need to recalculate when delete a repository from organization.
if recalculate {
2022-05-11 15:39:36 +05:30
if err = access_model . RecalculateTeamAccesses ( ctx , repo , t . ID ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
}
2022-03-29 11:59:02 +05:30
teamUsers , err := organization . GetTeamUsersByTeamID ( ctx , t . ID )
2017-03-15 06:21:46 +05:30
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "getTeamUsersByTeamID: %w" , err )
2016-03-26 03:34:02 +05:30
}
2017-03-21 06:25:00 +05:30
for _ , teamUser := range teamUsers {
2022-05-11 15:39:36 +05:30
has , err := access_model . HasAccess ( ctx , teamUser . UID , repo )
2016-03-26 03:34:02 +05:30
if err != nil {
return err
} else if has {
continue
}
2022-05-20 19:38:52 +05:30
if err = repo_model . WatchRepo ( ctx , teamUser . UID , repo . ID , false ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2018-06-20 01:14:33 +05:30
// Remove all IssueWatches a user has subscribed to in the repositories
2022-06-13 15:07:59 +05:30
if err := issues_model . RemoveIssueWatchersByRepoID ( ctx , teamUser . UID , repo . ID ) ; err != nil {
2018-06-20 01:14:33 +05:30
return err
}
2016-03-26 03:34:02 +05:30
}
return nil
}
// RemoveRepository removes repository from team of organization.
2019-11-06 15:07:14 +05:30
// If the team shall include all repositories the request is ignored.
2022-03-29 11:59:02 +05:30
func RemoveRepository ( t * organization . Team , repoID int64 ) error {
if ! HasRepository ( t , repoID ) {
2016-03-26 03:34:02 +05:30
return nil
}
2019-11-06 15:07:14 +05:30
if t . IncludesAllRepositories {
return nil
}
2022-12-03 08:18:26 +05:30
repo , err := repo_model . GetRepositoryByID ( db . DefaultContext , repoID )
2016-03-26 03:34:02 +05:30
if err != nil {
return err
}
2022-11-13 01:48:50 +05:30
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 21:11:00 +05:30
if err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2016-03-26 03:34:02 +05:30
2022-03-29 11:59:02 +05:30
if err = removeRepository ( ctx , t , repo , true ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2016-03-26 03:34:02 +05:30
}
// NewTeam creates a record of new team.
// It's caller's responsibility to assign organization ID.
2022-03-29 11:59:02 +05:30
func NewTeam ( t * organization . Team ) ( err error ) {
2016-03-26 03:34:02 +05:30
if len ( t . Name ) == 0 {
2022-12-31 17:19:37 +05:30
return util . NewInvalidArgumentErrorf ( "empty team name" )
2016-03-26 03:34:02 +05:30
}
2022-03-29 11:59:02 +05:30
if err = organization . IsUsableTeamName ( t . Name ) ; err != nil {
2016-11-06 14:37:03 +05:30
return err
}
2021-11-24 15:19:20 +05:30
has , err := db . GetEngine ( db . DefaultContext ) . ID ( t . OrgID ) . Get ( new ( user_model . User ) )
2016-03-26 03:34:02 +05:30
if err != nil {
return err
2019-06-13 01:11:28 +05:30
}
if ! has {
2022-03-29 11:59:02 +05:30
return organization . ErrOrgNotExist { ID : t . OrgID }
2016-03-26 03:34:02 +05:30
}
t . LowerName = strings . ToLower ( t . Name )
2021-09-23 21:15:36 +05:30
has , err = db . GetEngine ( db . DefaultContext ) .
2016-11-10 20:46:32 +05:30
Where ( "org_id=?" , t . OrgID ) .
And ( "lower_name=?" , t . LowerName ) .
2022-03-29 11:59:02 +05:30
Get ( new ( organization . Team ) )
2016-03-26 03:34:02 +05:30
if err != nil {
return err
2019-06-13 01:11:28 +05:30
}
if has {
2022-03-29 11:59:02 +05:30
return organization . ErrTeamAlreadyExist { OrgID : t . OrgID , Name : t . LowerName }
2016-03-26 03:34:02 +05:30
}
2022-11-13 01:48:50 +05:30
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 21:11:00 +05:30
if err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2016-03-26 03:34:02 +05:30
2021-11-21 21:11:00 +05:30
if err = db . Insert ( ctx , t ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2018-06-21 21:30:13 +05:30
// insert units for team
if len ( t . Units ) > 0 {
for _ , unit := range t . Units {
unit . TeamID = t . ID
}
2021-11-21 21:11:00 +05:30
if err = db . Insert ( ctx , & t . Units ) ; err != nil {
2018-06-21 21:30:13 +05:30
return err
}
}
2019-11-06 15:07:14 +05:30
// Add all repositories to the team if it has access to all of them.
if t . IncludesAllRepositories {
2022-03-29 11:59:02 +05:30
err = addAllRepositories ( ctx , t )
2019-11-06 15:07:14 +05:30
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "addAllRepositories: %w" , err )
2019-11-06 15:07:14 +05:30
}
}
2016-03-26 03:34:02 +05:30
// Update organization number of teams.
2021-11-21 21:11:00 +05:30
if _ , err = db . Exec ( ctx , "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?" , t . OrgID ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2016-03-26 03:34:02 +05:30
}
// UpdateTeam updates information of team.
2022-03-29 11:59:02 +05:30
func UpdateTeam ( t * organization . Team , authChanged , includeAllChanged bool ) ( err error ) {
2016-03-26 03:34:02 +05:30
if len ( t . Name ) == 0 {
2022-12-31 17:19:37 +05:30
return util . NewInvalidArgumentErrorf ( "empty team name" )
2016-03-26 03:34:02 +05:30
}
if len ( t . Description ) > 255 {
t . Description = t . Description [ : 255 ]
}
2022-11-13 01:48:50 +05:30
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 21:11:00 +05:30
if err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2016-03-26 03:34:02 +05:30
t . LowerName = strings . ToLower ( t . Name )
2017-01-27 21:41:41 +05:30
has , err := sess .
2016-11-10 20:46:32 +05:30
Where ( "org_id=?" , t . OrgID ) .
And ( "lower_name=?" , t . LowerName ) .
And ( "id!=?" , t . ID ) .
2022-03-29 11:59:02 +05:30
Get ( new ( organization . Team ) )
2016-03-26 03:34:02 +05:30
if err != nil {
return err
} else if has {
2022-03-29 11:59:02 +05:30
return organization . ErrTeamAlreadyExist { OrgID : t . OrgID , Name : t . LowerName }
2016-03-26 03:34:02 +05:30
}
2020-01-09 18:45:14 +05:30
if _ , err = sess . ID ( t . ID ) . Cols ( "name" , "lower_name" , "description" ,
"can_create_org_repo" , "authorize" , "includes_all_repositories" ) . Update ( t ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "update: %w" , err )
2016-03-26 03:34:02 +05:30
}
2018-11-11 01:15:32 +05:30
// 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 11:59:02 +05:30
Delete ( new ( organization . TeamUnit ) ) ; err != nil {
2018-11-11 01:15:32 +05:30
return err
}
2022-01-05 09:07:00 +05:30
if _ , err = sess . Cols ( "org_id" , "team_id" , "type" , "access_mode" ) . Insert ( & t . Units ) ; err != nil {
2018-11-11 01:15:32 +05:30
return err
}
}
2016-03-26 03:34:02 +05:30
// Update access for team members if needed.
if authChanged {
2022-12-03 08:18:26 +05:30
if err = t . LoadRepositories ( ctx ) ; err != nil {
return fmt . Errorf ( "LoadRepositories: %w" , err )
2016-03-26 03:34:02 +05:30
}
for _ , repo := range t . Repos {
2022-05-11 15:39:36 +05:30
if err = access_model . RecalculateTeamAccesses ( ctx , repo , 0 ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "recalculateTeamAccesses: %w" , err )
2016-03-26 03:34:02 +05:30
}
}
}
2019-11-06 15:07:14 +05:30
// Add all repositories to the team if it has access to all of them.
if includeAllChanged && t . IncludesAllRepositories {
2022-03-29 11:59:02 +05:30
err = addAllRepositories ( ctx , t )
2019-11-06 15:07:14 +05:30
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "addAllRepositories: %w" , err )
2019-11-06 15:07:14 +05:30
}
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2016-03-26 03:34:02 +05:30
}
// DeleteTeam deletes given team.
// It's caller's responsibility to assign organization ID.
2022-03-29 11:59:02 +05:30
func DeleteTeam ( t * organization . Team ) error {
2022-11-13 01:48:50 +05:30
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 21:11:00 +05:30
if err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2016-03-26 03:34:02 +05:30
2022-12-03 08:18:26 +05:30
if err := t . LoadRepositories ( ctx ) ; err != nil {
2022-03-29 11:59:02 +05:30
return err
}
2022-12-03 08:18:26 +05:30
if err := t . LoadMembers ( ctx ) ; err != nil {
2018-06-20 01:14:33 +05:30
return err
}
2022-03-22 06:39:45 +05:30
// update branch protections
{
2022-06-12 21:21:54 +05:30
protections := make ( [ ] * git_model . ProtectedBranch , 0 , 10 )
2023-01-16 13:30:22 +05:30
err := db . GetEngine ( ctx ) . In ( "repo_id" ,
2022-03-22 06:39:45 +05:30
builder . Select ( "id" ) . From ( "repository" ) . Where ( builder . Eq { "owner_id" : t . OrgID } ) ) .
Find ( & protections )
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "findProtectedBranches: %w" , err )
2022-03-22 06:39:45 +05:30
}
for _ , p := range protections {
2023-01-16 13:30:22 +05:30
if err := git_model . RemoveTeamIDFromProtectedBranch ( ctx , p , t . ID ) ; err != nil {
return err
2022-03-22 06:39:45 +05:30
}
}
}
if ! t . IncludesAllRepositories {
2022-03-29 11:59:02 +05:30
if err := removeAllRepositories ( ctx , t ) ; err != nil {
2022-03-22 06:39:45 +05:30
return err
}
2017-02-23 07:06:15 +05:30
}
2022-10-19 18:10:28 +05:30
if err := db . DeleteBeans ( ctx ,
& organization . Team { ID : t . ID } ,
& organization . TeamUser { OrgID : t . OrgID , TeamID : t . ID } ,
& organization . TeamUnit { TeamID : t . ID } ,
& organization . TeamInvite { TeamID : t . ID } ,
2023-04-20 05:20:00 +05:30
& issues_model . Review { Type : issues_model . ReviewTypeRequest , ReviewerTeamID : t . ID } , // batch delete the binding relationship between team and PR (request review from team)
2022-10-19 18:10:28 +05:30
) ; err != nil {
2018-06-21 21:30:13 +05:30
return err
}
2016-03-26 03:34:02 +05:30
// Update organization number of teams.
2023-01-16 13:30:22 +05:30
if _ , err := db . Exec ( ctx , "UPDATE `user` SET num_teams=num_teams-1 WHERE id=?" , t . OrgID ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-21 21:11:00 +05:30
return committer . Commit ( )
2016-03-26 03:34:02 +05:30
}
// AddTeamMember adds new membership of given team to given organization,
// the user will have membership to given organization automatically when needed.
2022-03-29 11:59:02 +05:30
func AddTeamMember ( team * organization . Team , userID int64 ) error {
isAlreadyMember , err := organization . IsTeamMember ( db . DefaultContext , team . OrgID , team . ID , userID )
2017-12-21 13:13:26 +05:30
if err != nil || isAlreadyMember {
return err
2016-03-26 03:34:02 +05:30
}
2022-03-29 11:59:02 +05:30
if err := organization . AddOrgUser ( team . OrgID , userID ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2022-11-13 01:48:50 +05:30
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 21:11:00 +05:30
if err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-21 21:11:00 +05:30
defer committer . Close ( )
2016-03-26 03:34:02 +05:30
2022-05-14 19:00:19 +05:30
// check in transaction
isAlreadyMember , err = organization . IsTeamMember ( ctx , team . OrgID , team . ID , userID )
if err != nil || isAlreadyMember {
return err
}
2021-11-21 21:11:00 +05:30
sess := db . GetEngine ( ctx )
2022-03-29 11:59:02 +05:30
if err := db . Insert ( ctx , & organization . TeamUser {
2016-11-28 14:03:08 +05:30
UID : userID ,
2017-02-24 11:55:09 +05:30
OrgID : team . OrgID ,
TeamID : team . ID ,
} ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
2022-03-29 11:59:02 +05:30
} else if _ , err := sess . Incr ( "num_members" ) . ID ( team . ID ) . Update ( new ( organization . Team ) ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2017-12-31 08:38:08 +05:30
team . NumMembers ++
2016-03-26 03:34:02 +05:30
// Give access to team repositories.
2022-04-01 07:23:18 +05:30
// 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 15:39:36 +05:30
Update ( new ( access_model . Access ) ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "update user accesses: %w" , err )
2022-04-01 07:23:18 +05:30
}
// 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 {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "select id accesses: %w" , err )
2022-04-01 07:23:18 +05:30
}
2022-05-11 15:39:36 +05:30
accesses := make ( [ ] * access_model . Access , 0 , 100 )
2022-04-01 07:23:18 +05:30
for i , repoID := range repoIDs {
2022-05-11 15:39:36 +05:30
accesses = append ( accesses , & access_model . Access { RepoID : repoID , UserID : userID , Mode : team . AccessMode } )
2022-04-01 07:23:18 +05:30
if ( i % 100 == 0 || i == len ( repoIDs ) - 1 ) && len ( accesses ) > 0 {
if err = db . Insert ( ctx , accesses ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "insert new user accesses: %w" , err )
2019-01-27 14:55:21 +05:30
}
2022-04-01 07:23:18 +05:30
accesses = accesses [ : 0 ]
2018-06-20 01:14:33 +05:30
}
2016-03-26 03:34:02 +05:30
}
2022-12-03 08:18:26 +05:30
if err := committer . Commit ( ) ; err != nil {
return err
}
committer . Close ( )
// this behaviour may spend much time so run it in a goroutine
// FIXME: Update watch repos batchly
2022-04-01 07:23:18 +05:30
if setting . Service . AutoWatchNewRepos {
// Get team and its repositories.
2022-12-03 08:18:26 +05:30
if err := team . LoadRepositories ( db . DefaultContext ) ; err != nil {
2022-04-01 07:23:18 +05:30
log . Error ( "getRepositories failed: %v" , err )
}
go func ( repos [ ] * repo_model . Repository ) {
for _ , repo := range repos {
2022-05-20 19:38:52 +05:30
if err = repo_model . WatchRepo ( db . DefaultContext , userID , repo . ID , true ) ; err != nil {
2022-04-01 07:23:18 +05:30
log . Error ( "watch repo failed: %v" , err )
}
}
} ( team . Repos )
}
2022-12-03 08:18:26 +05:30
return nil
2016-03-26 03:34:02 +05:30
}
2022-03-29 11:59:02 +05:30
func removeTeamMember ( ctx context . Context , team * organization . Team , userID int64 ) error {
2021-11-19 17:11:40 +05:30
e := db . GetEngine ( ctx )
2022-03-29 11:59:02 +05:30
isMember , err := organization . IsTeamMember ( ctx , team . OrgID , team . ID , userID )
2017-12-21 13:13:26 +05:30
if err != nil || ! isMember {
return err
2016-03-26 03:34:02 +05:30
}
// Check if the user to delete is the last member in owner team.
2017-02-24 11:55:09 +05:30
if team . IsOwnerTeam ( ) && team . NumMembers == 1 {
2022-03-29 11:59:02 +05:30
return organization . ErrLastOrgOwner { UID : userID }
2016-03-26 03:34:02 +05:30
}
2017-02-24 11:55:09 +05:30
team . NumMembers --
2016-03-26 03:34:02 +05:30
2022-12-03 08:18:26 +05:30
if err := team . LoadRepositories ( ctx ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2022-03-29 11:59:02 +05:30
if _ , err := e . Delete ( & organization . TeamUser {
2016-11-28 14:03:08 +05:30
UID : userID ,
2017-02-24 11:55:09 +05:30
OrgID : team . OrgID ,
TeamID : team . ID ,
} ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
2016-11-10 20:46:32 +05:30
} else if _ , err = e .
2017-10-05 10:13:04 +05:30
ID ( team . ID ) .
2017-09-25 10:29:27 +05:30
Cols ( "num_members" ) .
2017-02-24 11:55:09 +05:30
Update ( team ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
// Delete access to team repositories.
2017-02-24 11:55:09 +05:30
for _ , repo := range team . Repos {
2022-05-11 15:39:36 +05:30
if err := access_model . RecalculateUserAccess ( ctx , repo , userID ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2018-06-20 01:14:33 +05:30
// Remove watches from now unaccessible
2021-12-10 06:57:50 +05:30
if err := reconsiderWatches ( ctx , repo , userID ) ; err != nil {
2018-06-20 01:14:33 +05:30
return err
}
2020-04-08 03:22:01 +05:30
// Remove issue assignments from now unaccessible
2021-12-10 06:57:50 +05:30
if err := reconsiderRepoIssuesAssignee ( ctx , repo , userID ) ; err != nil {
2018-06-20 01:14:33 +05:30
return err
}
2016-03-26 03:34:02 +05:30
}
2018-02-23 14:12:02 +05:30
// Check if the user is a member of any team in the organization.
2022-03-29 11:59:02 +05:30
if count , err := e . Count ( & organization . TeamUser {
2018-02-23 14:12:02 +05:30
UID : userID ,
OrgID : team . OrgID ,
} ) ; err != nil {
return err
} else if count == 0 {
2021-11-19 17:11:40 +05:30
return removeOrgUser ( ctx , team . OrgID , userID )
2018-02-23 14:12:02 +05:30
}
2016-03-26 03:34:02 +05:30
return nil
}
// RemoveTeamMember removes member from given team of given organization.
2022-03-29 11:59:02 +05:30
func RemoveTeamMember ( team * organization . Team , userID int64 ) error {
2022-11-13 01:48:50 +05:30
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-19 17:11:40 +05:30
if err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-19 17:11:40 +05:30
defer committer . Close ( )
if err := removeTeamMember ( ctx , team , userID ) ; err != nil {
2016-03-26 03:34:02 +05:30
return err
}
2021-11-19 17:11:40 +05:30
return committer . Commit ( )
2016-03-26 03:34:02 +05:30
}