mirror of
https://github.com/go-gitea/gitea
synced 2024-11-25 21:13:19 +01:00
Add org.getUserTeams to reduce redundant code
This commit is contained in:
parent
e74630ae3b
commit
a562228c5e
2 changed files with 32 additions and 34 deletions
|
@ -445,34 +445,46 @@ func RemoveOrgRepo(orgID, repoID int64) error {
|
||||||
return removeOrgRepo(x, orgID, repoID)
|
return removeOrgRepo(x, orgID, repoID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserTeamIDs returns of all team IDs of the organization that user is memeber of.
|
func (org *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team, error) {
|
||||||
// It returns [-1] if user is not memeber of any teams.
|
|
||||||
func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
|
|
||||||
teams := make([]*Team, 0, org.NumTeams)
|
teams := make([]*Team, 0, org.NumTeams)
|
||||||
if err := x.Sql(`SELECT team.id FROM team
|
return teams, e.Where("team_user.org_id = ?", org.ID).
|
||||||
INNER JOIN team_user ON team_user.team_id = team.id
|
And("team_user.uid = ?", userID).
|
||||||
WHERE team_user.org_id = ? AND team_user.uid = ?`, org.ID, userID).Find(&teams); err != nil {
|
Join("INNER", "team_user", "team_user.team_id = team.id").
|
||||||
return nil, err
|
Cols(cols...).Find(&teams)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUserTeamIDs returns of all team IDs of the organization that user is memeber of.
|
||||||
|
func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
|
||||||
|
teams, err := org.getUserTeams(x, userID, "team.id")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("getUserTeams [%d]: %v", userID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
teamIDs := make([]int64, len(teams))
|
teamIDs := make([]int64, len(teams))
|
||||||
for i := range teams {
|
for i := range teams {
|
||||||
teamIDs[i] = teams[i].ID
|
teamIDs[i] = teams[i].ID
|
||||||
}
|
}
|
||||||
if len(teamIDs) == 0 {
|
|
||||||
// user has no team but "IN ()" is invalid SQL
|
|
||||||
teamIDs = append(teamIDs, -1) // there is no repo with id=-1
|
|
||||||
}
|
|
||||||
return teamIDs, nil
|
return teamIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserRepositories returns repositories of the organization
|
// GetTeams returns all teams that belong to organization,
|
||||||
// that the user with the given userID has access to.
|
// and that the user has joined.
|
||||||
|
func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
|
||||||
|
return org.getUserTeams(x, userID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUserRepositories returns a range of repositories in organization
|
||||||
|
// that the user with the given userID has access to,
|
||||||
|
// and total number of records based on given condition.
|
||||||
func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) {
|
func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) {
|
||||||
teamIDs, err := org.GetUserTeamIDs(userID)
|
teamIDs, err := org.GetUserTeamIDs(userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, fmt.Errorf("GetUserTeamIDs: %v", err)
|
return nil, 0, fmt.Errorf("GetUserTeamIDs: %v", err)
|
||||||
}
|
}
|
||||||
|
if len(teamIDs) == 0 {
|
||||||
|
// user has no team but "IN ()" is invalid SQL
|
||||||
|
teamIDs = []int64{-1} // there is no repo with id=-1
|
||||||
|
}
|
||||||
|
|
||||||
if page <= 0 {
|
if page <= 0 {
|
||||||
page = 1
|
page = 1
|
||||||
|
@ -513,35 +525,20 @@ func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
|
return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
|
||||||
}
|
}
|
||||||
|
if len(teamIDs) == 0 {
|
||||||
|
teamIDs = []int64{-1}
|
||||||
|
}
|
||||||
|
|
||||||
repos := make([]*Repository, 0, 10)
|
repos := make([]*Repository, 0, 10)
|
||||||
if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
|
if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
|
||||||
INNER JOIN team_repo
|
INNER JOIN team_repo
|
||||||
ON team_repo.repo_id = repository.id AND repository.is_mirror = ?
|
ON team_repo.repo_id = repository.id AND repository.is_mirror = ?
|
||||||
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
|
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
|
||||||
GROUP BY repository.id`,
|
GROUP BY repository.id
|
||||||
|
ORDER BY updated_unix DESC`,
|
||||||
strings.Join(base.Int64sToStrings(teamIDs), ",")),
|
strings.Join(base.Int64sToStrings(teamIDs), ",")),
|
||||||
true, org.ID, false).Find(&repos); err != nil {
|
true, org.ID, false).Find(&repos); err != nil {
|
||||||
return nil, fmt.Errorf("get repositories: %v", err)
|
return nil, fmt.Errorf("get repositories: %v", err)
|
||||||
}
|
}
|
||||||
return repos, nil
|
return repos, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTeams returns all teams that belong to organization,
|
|
||||||
// and that the user has joined.
|
|
||||||
func (org *User) GetUserTeams(userID int64) error {
|
|
||||||
teams := make([]*Team, 0, 5)
|
|
||||||
if err := x.Sql(`SELECT team.* FROM team
|
|
||||||
INNER JOIN team_user ON team_user.team_id = team.id
|
|
||||||
WHERE team_user.org_id = ? AND team_user.uid = ?`,
|
|
||||||
org.ID, userID).Find(&teams); err != nil {
|
|
||||||
return fmt.Errorf("get teams: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
org.Teams = teams
|
|
||||||
|
|
||||||
// FIXME: should I change this value inside method,
|
|
||||||
// or only in location of caller where it's really needed?
|
|
||||||
org.NumTeams = len(org.Teams)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -105,7 +105,8 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := org.GetUserTeams(ctx.User.ID); err != nil {
|
org.Teams, err = org.GetUserTeams(ctx.User.ID)
|
||||||
|
if err != nil {
|
||||||
ctx.Handle(500, "GetUserTeams", err)
|
ctx.Handle(500, "GetUserTeams", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue