123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- // 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"
- )
- // 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
- }
|