diff options
author | Unknwon <u@gogs.io> | 2017-03-03 18:26:51 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2017-03-03 18:26:51 -0500 |
commit | ebd95dd082705f42f864f99ac050e29c9c6a4dbd (patch) | |
tree | 51508c0ec75285e0544fa7cea2cc2799765c1c39 /models/org.go | |
parent | f7b7d008b684a3475a1834b8b574e5ce082a723c (diff) |
models/org: reduce to 2 SQL executions for GetOrgIDsByUserID
This also addresses #4231. It is now ignoring nonexistent
organizations returned from 'org_user' table.
This was a bug caused in older version that didn't cleanup
'org_user' table when delete an organization.
Diffstat (limited to 'models/org.go')
-rw-r--r-- | models/org.go | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/models/org.go b/models/org.go index 1deba7f4..9bf57ad9 100644 --- a/models/org.go +++ b/models/org.go @@ -303,16 +303,15 @@ func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) { return getOwnedOrgsByUserID(sess.Desc(desc), userID) } -// GetOrgUsersByUserID returns all organization-user relations by user ID. -func GetOrgUsersByUserID(uid int64, all bool) ([]*OrgUser, error) { - ous := make([]*OrgUser, 0, 10) - sess := x.Where("uid=?", uid) - if !all { - // Only show public organizations - sess.And("is_public=?", true) - } - err := sess.Find(&ous) - return ous, err +// GetOrgIDsByUserID returns a list of organization IDs that user belongs to. +// The showPrivate indicates whether to include private memberships. +func GetOrgIDsByUserID(userID int64, showPrivate bool) ([]int64, error) { + orgIDs := make([]int64, 0, 5) + sess := x.Table("org_user").Where("uid = ?", userID) + if !showPrivate { + sess.And("is_public = ?", true) + } + return orgIDs, sess.Distinct("org_id").Find(&orgIDs) } func getOrgUsersByOrgID(e Engine, orgID int64) ([]*OrgUser, error) { |