aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorDavid Svantesson <davidsvantesson@gmail.com>2019-11-09 01:39:37 +0100
committerLunny Xiao <xiaolunwen@gmail.com>2019-11-09 08:39:37 +0800
commit9ae4c17cb1fde0eaa1556e545a8871b6a70d2efa (patch)
tree0a62a8bd50dcb4d067f915d02b24207e2a714e5f /models
parentd2aee2a3e259de0d9dfd20452d55eb0c34ccc874 (diff)
downloadgitea-9ae4c17cb1fde0eaa1556e545a8871b6a70d2efa.tar.gz
gitea-9ae4c17cb1fde0eaa1556e545a8871b6a70d2efa.zip
Allow to add and remove all repositories to/from team. (#8867)
* Allow to add and remove all repositories to team. * Change style, buttons on same row. * Apply suggestions from code review Grammar Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Move set num repos to lower function. * Make general language sentences
Diffstat (limited to 'models')
-rw-r--r--models/org_team.go109
1 files changed, 79 insertions, 30 deletions
diff --git a/models/org_team.go b/models/org_team.go
index d740e1c240..126a8c896a 100644
--- a/models/org_team.go
+++ b/models/org_team.go
@@ -243,6 +243,21 @@ func (t *Team) addAllRepositories(e Engine) error {
return nil
}
+// AddAllRepositories adds all repositories to the team
+func (t *Team) AddAllRepositories() (err error) {
+ sess := x.NewSession()
+ defer sess.Close()
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+
+ if err = t.addAllRepositories(sess); err != nil {
+ return err
+ }
+
+ return sess.Commit()
+}
+
// AddRepository adds new repository to team of organization.
func (t *Team) AddRepository(repo *Repository) (err error) {
if repo.OwnerID != t.OrgID {
@@ -264,6 +279,69 @@ func (t *Team) AddRepository(repo *Repository) (err error) {
return sess.Commit()
}
+// RemoveAllRepositories removes all repositories from team and recalculates access
+func (t *Team) RemoveAllRepositories() (err error) {
+ if t.IncludesAllRepositories {
+ return nil
+ }
+
+ sess := x.NewSession()
+ defer sess.Close()
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+
+ if err = t.removeAllRepositories(sess); err != nil {
+ return err
+ }
+
+ return sess.Commit()
+}
+
+// removeAllRepositories removes all repositories from team and recalculates access
+// Note: Shall not be called if team includes all repositories
+func (t *Team) removeAllRepositories(e Engine) (err error) {
+ // Delete all accesses.
+ for _, repo := range t.Repos {
+ if err := repo.recalculateTeamAccesses(e, t.ID); err != nil {
+ return err
+ }
+
+ // Remove watches from all users and now unaccessible repos
+ for _, user := range t.Members {
+ has, err := hasAccess(e, user.ID, repo)
+ if err != nil {
+ return err
+ } else if has {
+ continue
+ }
+
+ if err = watchRepo(e, user.ID, repo.ID, false); err != nil {
+ 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
+}
+
// 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)
func (t *Team) removeRepository(e Engine, repo *Repository, recalculate bool) (err error) {
@@ -577,36 +655,7 @@ func DeleteTeam(t *Team) error {
return err
}
- // Delete all accesses.
- for _, repo := range t.Repos {
- if err := repo.recalculateTeamAccesses(sess, t.ID); err != nil {
- return err
- }
-
- // Remove watches from all users and now unaccessible repos
- for _, user := range t.Members {
- has, err := hasAccess(sess, user.ID, repo)
- if err != nil {
- return err
- } else if has {
- continue
- }
-
- if err = watchRepo(sess, user.ID, repo.ID, false); err != nil {
- return err
- }
-
- // Remove all IssueWatches a user has subscribed to in the repositories
- if err = removeIssueWatchersByRepoID(sess, user.ID, repo.ID); err != nil {
- return err
- }
- }
- }
-
- // Delete team-repo
- if _, err := sess.
- Where("team_id=?", t.ID).
- Delete(new(TeamRepo)); err != nil {
+ if err := t.removeAllRepositories(sess); err != nil {
return err
}