diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/fixtures/repo_unit.yml | 8 | ||||
-rw-r--r-- | models/fixtures/team.yml | 4 | ||||
-rw-r--r-- | models/migrations/migrations.go | 2 | ||||
-rw-r--r-- | models/migrations/v31.go | 2 | ||||
-rw-r--r-- | models/migrations/v32.go | 23 | ||||
-rw-r--r-- | models/org_team.go | 22 | ||||
-rw-r--r-- | models/repo.go | 58 | ||||
-rw-r--r-- | models/repo_unit.go | 8 | ||||
-rw-r--r-- | models/unit.go | 80 |
9 files changed, 174 insertions, 33 deletions
diff --git a/models/fixtures/repo_unit.yml b/models/fixtures/repo_unit.yml index a41784db96..443dfff7b7 100644 --- a/models/fixtures/repo_unit.yml +++ b/models/fixtures/repo_unit.yml @@ -12,4 +12,12 @@ type: 2 index: 0 config: "{}" + created_unix: 946684810 + +- + id: 3 + repo_id: 1 + type: 7 + index: 0 + config: "{}" created_unix: 946684810
\ No newline at end of file diff --git a/models/fixtures/team.yml b/models/fixtures/team.yml index 3b47ef0253..1b2a0d6811 100644 --- a/models/fixtures/team.yml +++ b/models/fixtures/team.yml @@ -6,6 +6,7 @@ authorize: 4 # owner num_repos: 2 num_members: 1 + unit_types: '[1,2,3,4,5,6,7,8,9]' - id: 2 @@ -15,6 +16,7 @@ authorize: 2 # write num_repos: 1 num_members: 2 + unit_types: '[1,2,3,4,5,6,7,8,9]' - id: 3 @@ -24,6 +26,7 @@ authorize: 4 # owner num_repos: 0 num_members: 1 + unit_types: '[1,2,3,4,5,6,7,8,9]' - id: 4 @@ -33,3 +36,4 @@ authorize: 4 # owner num_repos: 0 num_members: 1 + unit_types: '[1,2,3,4,5,6,7,8,9]'
\ No newline at end of file diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 000412ae37..2973064152 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -112,6 +112,8 @@ var migrations = []Migration{ NewMigration("add primary key to external login user", addExternalLoginUserPK), // 31 -> 32 NewMigration("add field for login source synchronization", addLoginSourceSyncEnabledColumn), + // v32 -> v33 + NewMigration("add units for team", addUnitsToRepoTeam), } // Migrate database to current version diff --git a/models/migrations/v31.go b/models/migrations/v31.go index 1166a5f6c4..b7ceecfc38 100644 --- a/models/migrations/v31.go +++ b/models/migrations/v31.go @@ -1,4 +1,4 @@ -// Copyright 2017 The Gogs Authors. All rights reserved. +// Copyright 2017 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. diff --git a/models/migrations/v32.go b/models/migrations/v32.go new file mode 100644 index 0000000000..d209fc34f6 --- /dev/null +++ b/models/migrations/v32.go @@ -0,0 +1,23 @@ +// Copyright 2017 The Gitea 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 migrations + +import "github.com/go-xorm/xorm" + +func addUnitsToRepoTeam(x *xorm.Engine) error { + type Team struct { + UnitTypes []int `xorm:"json"` + } + + err := x.Sync(new(Team)) + if err != nil { + return err + } + + _, err = x.Update(&Team{ + UnitTypes: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, + }) + return err +} diff --git a/models/org_team.go b/models/org_team.go index 115e13feab..5c97a7032e 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -24,6 +24,15 @@ type Team struct { Members []*User `xorm:"-"` NumRepos int NumMembers int + UnitTypes []UnitType `xorm:"json"` +} + +// GetUnitTypes returns unit types the team owned, empty means all the unit types +func (t *Team) GetUnitTypes() []UnitType { + if len(t.UnitTypes) == 0 { + return allRepUnitTypes + } + return t.UnitTypes } // IsOwnerTeam returns true if team is owner team. @@ -183,6 +192,19 @@ func (t *Team) RemoveRepository(repoID int64) error { return sess.Commit() } +// EnableUnit returns if the team enables unit type t +func (t *Team) EnableUnit(tp UnitType) bool { + if len(t.UnitTypes) == 0 { + return true + } + for _, u := range t.UnitTypes { + if u == tp { + return true + } + } + return false +} + // IsUsableTeamName tests if a name could be as team name func IsUsableTeamName(name string) error { switch name { diff --git a/models/repo.go b/models/repo.go index 07fe768eac..e1b7014551 100644 --- a/models/repo.go +++ b/models/repo.go @@ -329,8 +329,61 @@ func (repo *Repository) getUnits(e Engine) (err error) { return err } -func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) { - return units, e.Where("repo_id = ?", repoID).Find(&units) +// CheckUnitUser check whether user could visit the unit of this repository +func (repo *Repository) CheckUnitUser(userID int64, unitType UnitType) bool { + if err := repo.getUnitsByUserID(x, userID); err != nil { + return false + } + + for _, unit := range repo.Units { + if unit.Type == unitType { + return true + } + } + return false +} + +// LoadUnitsByUserID loads units according userID's permissions +func (repo *Repository) LoadUnitsByUserID(userID int64) error { + return repo.getUnitsByUserID(x, userID) +} + +func (repo *Repository) getUnitsByUserID(e Engine, userID int64) (err error) { + if repo.Units != nil { + return nil + } + + err = repo.getUnits(e) + if err != nil { + return err + } + + if !repo.Owner.IsOrganization() || userID == 0 { + return nil + } + + teams, err := getUserTeams(e, repo.OwnerID, userID) + if err != nil { + return err + } + + var allTypes = make(map[UnitType]struct{}, len(allRepUnitTypes)) + for _, team := range teams { + for _, unitType := range team.UnitTypes { + allTypes[unitType] = struct{}{} + } + } + + // unique + var newRepoUnits = make([]*RepoUnit, 0, len(repo.Units)) + for _, u := range repo.Units { + if _, ok := allTypes[u.Type]; ok { + newRepoUnits = append(newRepoUnits, u) + } + } + + repo.Units = newRepoUnits + return nil } // EnableUnit if this repository enabled some unit @@ -1595,6 +1648,7 @@ func DeleteRepository(uid, repoID int64) error { &Release{RepoID: repoID}, &Collaboration{RepoID: repoID}, &PullRequest{BaseRepoID: repoID}, + &RepoUnit{RepoID: repoID}, ); err != nil { return fmt.Errorf("deleteBeans: %v", err) } diff --git a/models/repo_unit.go b/models/repo_unit.go index ee61ef4c9d..f8f01c4398 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -135,3 +135,11 @@ func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig { func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig { return r.Config.(*ExternalTrackerConfig) } + +func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) { + return units, e.Where("repo_id = ?", repoID).Find(&units) +} + +func getUnitsByRepoIDAndIDs(e Engine, repoID int64, types []UnitType) (units []*RepoUnit, err error) { + return units, e.Where("repo_id = ?", repoID).In("`type`", types).Find(&units) +} diff --git a/models/unit.go b/models/unit.go index 54bb928ba7..48ef1620eb 100644 --- a/models/unit.go +++ b/models/unit.go @@ -20,6 +20,40 @@ const ( UnitTypeExternalTracker // 9 ExternalTracker ) +var ( + // allRepUnitTypes contains all the unit types + allRepUnitTypes = []UnitType{ + UnitTypeCode, + UnitTypeIssues, + UnitTypePullRequests, + UnitTypeCommits, + UnitTypeReleases, + UnitTypeWiki, + UnitTypeSettings, + UnitTypeExternalWiki, + UnitTypeExternalTracker, + } + + // defaultRepoUnits contains the default unit types + defaultRepoUnits = []UnitType{ + UnitTypeCode, + UnitTypeIssues, + UnitTypePullRequests, + UnitTypeCommits, + UnitTypeReleases, + UnitTypeWiki, + UnitTypeSettings, + } + + // MustRepoUnits contains the units could be disabled currently + MustRepoUnits = []UnitType{ + UnitTypeCode, + UnitTypeCommits, + UnitTypeReleases, + UnitTypeSettings, + } +) + // Unit is a tab page of one repository type Unit struct { Type UnitType @@ -29,13 +63,18 @@ type Unit struct { Idx int } +// CanDisable returns if this unit could be disabled. +func (u *Unit) CanDisable() bool { + return u.Type != UnitTypeSettings +} + // Enumerate all the units var ( UnitCode = Unit{ UnitTypeCode, "repo.code", "/", - "repo.code_desc", + "repo.code.desc", 0, } @@ -43,15 +82,15 @@ var ( UnitTypeIssues, "repo.issues", "/issues", - "repo.issues_desc", + "repo.issues.desc", 1, } UnitExternalTracker = Unit{ UnitTypeExternalTracker, - "repo.issues", + "repo.ext_issues", "/issues", - "repo.issues_desc", + "repo.ext_issues.desc", 1, } @@ -59,7 +98,7 @@ var ( UnitTypePullRequests, "repo.pulls", "/pulls", - "repo.pulls_desc", + "repo.pulls.desc", 2, } @@ -67,7 +106,7 @@ var ( UnitTypeCommits, "repo.commits", "/commits/master", - "repo.commits_desc", + "repo.commits.desc", 3, } @@ -75,7 +114,7 @@ var ( UnitTypeReleases, "repo.releases", "/releases", - "repo.releases_desc", + "repo.releases.desc", 4, } @@ -83,15 +122,15 @@ var ( UnitTypeWiki, "repo.wiki", "/wiki", - "repo.wiki_desc", + "repo.wiki.desc", 5, } UnitExternalWiki = Unit{ UnitTypeExternalWiki, - "repo.wiki", + "repo.ext_wiki", "/wiki", - "repo.wiki_desc", + "repo.ext_wiki.desc", 5, } @@ -99,29 +138,10 @@ var ( UnitTypeSettings, "repo.settings", "/settings", - "repo.settings_desc", + "repo.settings.desc", 6, } - // defaultRepoUnits contains all the default unit types - defaultRepoUnits = []UnitType{ - UnitTypeCode, - UnitTypeIssues, - UnitTypePullRequests, - UnitTypeCommits, - UnitTypeReleases, - UnitTypeWiki, - UnitTypeSettings, - } - - // MustRepoUnits contains the units could be disabled currently - MustRepoUnits = []UnitType{ - UnitTypeCode, - UnitTypeCommits, - UnitTypeReleases, - UnitTypeSettings, - } - // Units contains all the units Units = map[UnitType]Unit{ UnitTypeCode: UnitCode, |