mirror of https://github.com/gogs/gogs.git
70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
// Copyright 2014 The Gogs 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 db
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"xorm.io/builder"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
// getOwnedOrgsByUserID returns a list of organizations are owned by given user ID.
|
|
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
|
|
orgs := make([]*User, 0, 10)
|
|
return orgs, sess.Where("`org_user`.uid=?", userID).And("`org_user`.is_owner=?", true).
|
|
Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Find(&orgs)
|
|
}
|
|
|
|
// GetOwnedOrganizationsByUserIDDesc returns a list of organizations are owned by
|
|
// given user ID, ordered descending by the given condition.
|
|
func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
|
|
sess := x.NewSession()
|
|
return getOwnedOrgsByUserID(sess.Desc(desc), userID)
|
|
}
|
|
|
|
// getOrgUsersByOrgID returns all organization-user relations by organization ID.
|
|
func getOrgUsersByOrgID(e Engine, orgID int64, limit int) ([]*OrgUser, error) {
|
|
orgUsers := make([]*OrgUser, 0, 10)
|
|
|
|
sess := e.Where("org_id=?", orgID)
|
|
if limit > 0 {
|
|
sess = sess.Limit(limit)
|
|
}
|
|
return orgUsers, sess.Find(&orgUsers)
|
|
}
|
|
|
|
// GetUserMirrorRepositories returns mirror repositories of the organization which the user has access to.
|
|
func (u *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
|
|
teamIDs, err := u.GetUserTeamIDs(userID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
|
|
}
|
|
if len(teamIDs) == 0 {
|
|
teamIDs = []int64{-1}
|
|
}
|
|
|
|
var teamRepoIDs []int64
|
|
err = x.Table("team_repo").In("team_id", teamIDs).Distinct("repo_id").Find(&teamRepoIDs)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get team repository ids: %v", err)
|
|
}
|
|
if len(teamRepoIDs) == 0 {
|
|
// team has no repo but "IN ()" is invalid SQL
|
|
teamRepoIDs = []int64{-1} // there is no repo with id=-1
|
|
}
|
|
|
|
repos := make([]*Repository, 0, 10)
|
|
if err = x.Where("owner_id = ?", u.ID).
|
|
And("is_private = ?", false).
|
|
Or(builder.In("id", teamRepoIDs)).
|
|
And("is_mirror = ?", true). // Don't move up because it's an independent condition
|
|
Desc("updated_unix").
|
|
Find(&repos); err != nil {
|
|
return nil, fmt.Errorf("get user repositories: %v", err)
|
|
}
|
|
return repos, nil
|
|
}
|