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.
master
Unknwon 2017-03-03 18:26:51 -05:00
parent f7b7d008b6
commit ebd95dd082
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
4 changed files with 16 additions and 20 deletions

View File

@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.10.4.0302" const APP_VER = "0.10.4.0303"
func init() { func init() {
setting.AppVer = APP_VER setting.AppVer = APP_VER

View File

@ -303,16 +303,15 @@ func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
return getOwnedOrgsByUserID(sess.Desc(desc), userID) return getOwnedOrgsByUserID(sess.Desc(desc), userID)
} }
// GetOrgUsersByUserID returns all organization-user relations by user ID. // GetOrgIDsByUserID returns a list of organization IDs that user belongs to.
func GetOrgUsersByUserID(uid int64, all bool) ([]*OrgUser, error) { // The showPrivate indicates whether to include private memberships.
ous := make([]*OrgUser, 0, 10) func GetOrgIDsByUserID(userID int64, showPrivate bool) ([]int64, error) {
sess := x.Where("uid=?", uid) orgIDs := make([]int64, 0, 5)
if !all { sess := x.Table("org_user").Where("uid = ?", userID)
// Only show public organizations if !showPrivate {
sess.And("is_public=?", true) sess.And("is_public = ?", true)
} }
err := sess.Find(&ous) return orgIDs, sess.Distinct("org_id").Find(&orgIDs)
return ous, err
} }
func getOrgUsersByOrgID(e Engine, orgID int64) ([]*OrgUser, error) { func getOrgUsersByOrgID(e Engine, orgID int64) ([]*OrgUser, error) {

View File

@ -448,18 +448,15 @@ func (u *User) GetOwnedOrganizations() (err error) {
} }
// GetOrganizations returns all organizations that user belongs to. // GetOrganizations returns all organizations that user belongs to.
func (u *User) GetOrganizations(all bool) error { func (u *User) GetOrganizations(showPrivate bool) error {
ous, err := GetOrgUsersByUserID(u.ID, all) orgIDs, err := GetOrgIDsByUserID(u.ID, showPrivate)
if err != nil { if err != nil {
return err return fmt.Errorf("GetOrgIDsByUserID: %v", err)
} }
u.Orgs = make([]*User, len(ous)) u.Orgs = make([]*User, 0, len(orgIDs))
for i, ou := range ous { if err = x.In("id", orgIDs).Find(&u.Orgs); err != nil {
u.Orgs[i], err = GetUserByID(ou.OrgID) return err
if err != nil {
return err
}
} }
return nil return nil
} }

View File

@ -1 +1 @@
0.10.4.0302 0.10.4.0303