diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-05-18 22:54:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-18 22:54:24 +0800 |
commit | fd6034aaf23af1ce03bcba0babcbe7675ce093ee (patch) | |
tree | 9720f416facdef02a76bd56ca50669cc1f17eb88 /models/repo.go | |
parent | 5db5e16ab61f25f21cb17ee4895679b9830641a5 (diff) | |
download | gitea-fd6034aaf23af1ce03bcba0babcbe7675ce093ee.tar.gz gitea-fd6034aaf23af1ce03bcba0babcbe7675ce093ee.zip |
Add units to team (#947)
* add units to team
* fix lint
* finish team setting backend
* finished permission controll on routes
* fix import blank line
* add unit check on ssh/http pull and push and fix test failed
* fix fixtures data
* remove unused code
Diffstat (limited to 'models/repo.go')
-rw-r--r-- | models/repo.go | 58 |
1 files changed, 56 insertions, 2 deletions
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) } |