diff options
author | Unknwon <u@gogs.io> | 2016-02-04 12:52:11 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-02-04 12:52:11 -0500 |
commit | 739d5aa1d32229769acbdf53bd99222d13441b47 (patch) | |
tree | 7ca9e508ade455e660ff29a0995b26cefd8b8816 /models | |
parent | 04be8c0de5295f57e06e152e56c481b29b1076e5 (diff) | |
parent | c3ff476ed67b69431e96fce8e5a16450976701f6 (diff) | |
download | gitea-739d5aa1d32229769acbdf53bd99222d13441b47.tar.gz gitea-739d5aa1d32229769acbdf53bd99222d13441b47.zip |
Merge pull request #2530 from fnkr/hide-other-teams-repos-from-org-page
Hide other teams & repos from organization page
Diffstat (limited to 'models')
-rw-r--r-- | models/org.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/models/org.go b/models/org.go index 3de89e4bd3..6844445591 100644 --- a/models/org.go +++ b/models/org.go @@ -9,6 +9,7 @@ import ( "fmt" "os" "strings" + "strconv" "github.com/go-xorm/xorm" ) @@ -1048,3 +1049,59 @@ func removeOrgRepo(e Engine, orgID, repoID int64) error { func RemoveOrgRepo(orgID, repoID int64) error { return removeOrgRepo(x, orgID, repoID) } + +// GetUserRepositories gets all repositories of an organization, +// that the user with the given userID has access to. +func (org *User) GetUserRepositories(userID int64) (err error) { + teams := make([]*Team, 0, 10) + if err := x.Cols("`team`.id"). + Where("`team_user`.org_id=?", org.Id). + And("`team_user`.uid=?", userID). + Join("INNER", "`team_user`", "`team_user`.team_id=`team`.id"). + Find(&teams); err != nil { + return fmt.Errorf("getUserRepositories: get teams: %v", err) + } + + var teamIDs []string + for _, team := range teams { + teamIDs = append(teamIDs, strconv.FormatInt(team.ID, 10)) + } + if len(teamIDs) == 0 { + // user has no team but "IN ()" is invalid SQL + teamIDs = append(teamIDs, "-1") // there is no repo with id=-1 + } + + // Due to a bug in xorm using IN() together with OR() is impossible. + // As a workaround, we have to build the IN statement on our own, until this is fixed. + // https://github.com/go-xorm/xorm/issues/342 + + if err := x.Cols("`repository`.*"). + Join("INNER", "`team_repo`", "`team_repo`.repo_id=`repository`.id"). + Where("`repository`.owner_id=?", org.Id). + And("`repository`.is_private=?", false). + Or("`team_repo`.team_id=(?)", strings.Join(teamIDs, ",")). + GroupBy("`repository`.id"). + Find(&org.Repos); err != nil { + return fmt.Errorf("getUserRepositories: get repositories: %v", err) + } + + org.NumRepos = len(org.Repos) + + return +} + +// GetTeams returns all teams that belong to organization, +// and that the user has joined. +func (org *User) GetUserTeams(userID int64) (err error) { + if err := x.Cols("`team`.*"). + Where("`team_user`.org_id=?", org.Id). + And("`team_user`.uid=?", userID). + Join("INNER", "`team_user`", "`team_user`.team_id=`team`.id"). + Find(&org.Teams); err != nil { + return fmt.Errorf("getUserTeams: %v", err) + } + + org.NumTeams = len(org.Teams) + + return +} |