Skip to content

Commit

Permalink
Optimize IsUserOrgOwner
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed Jul 28, 2019
1 parent 4f44e72 commit b74810f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ type OrgUser struct {
}

func isOrganizationOwner(e Engine, orgID, uid int64) (bool, error) {
ownerTeam, err := getTeam(e, orgID, ownerTeamName)
ownerTeam, err := getOwnerTeam(e, orgID)
if err != nil {
if err == ErrTeamNotExist {
log.Error("Organization does not have owner team: %d", orgID)
Expand Down
5 changes: 5 additions & 0 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ func GetTeam(orgID int64, name string) (*Team, error) {
return getTeam(x, orgID, name)
}

// getOwnerTeam returns team by given team name and organization.
func getOwnerTeam(e Engine, orgID int64) (*Team, error) {
return getTeam(e, orgID, ownerTeamName)
}

func getTeamByID(e Engine, teamID int64) (*Team, error) {
t := new(Team)
has, err := e.ID(teamID).Get(t)
Expand Down
37 changes: 34 additions & 3 deletions models/userlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package models

import (
"code.gitea.io/gitea/modules/log"
"fmt"
)

Expand All @@ -23,13 +24,43 @@ func (users UserList) getUserIDs() []int64 {
// IsUserOrgOwner returns true if user is in the owner team of given organization.
func (users UserList) IsUserOrgOwner(orgID int64) map[int64]bool {
results := make(map[int64]bool, len(users))
//TODO use directly xorm
for _, u := range users {
results[u.ID] = u.IsUserOrgOwner(orgID)
for _, user := range users {
results[user.ID] = false //Set default to false
}
ownerMaps, err := users.loadOrganizationOwners(x, orgID)
if err == nil {
for _, owner := range ownerMaps {
results[owner.UID] = true
}
}
return results
}

func (users UserList) loadOrganizationOwners(e Engine, orgID int64) (map[int64]*TeamUser, error) {
if len(users) == 0 {
return nil, nil
}
ownerTeam, err := getOwnerTeam(e, orgID)
if err != nil {
if err == ErrTeamNotExist {
log.Error("Organization does not have owner team: %d", orgID)
return nil, nil
}
return nil, err
}

userIDs := users.getUserIDs()
ownerMaps := make(map[int64]*TeamUser)
err = e.In("uid", userIDs).
And("org_id=?", orgID).
And("team_id=?", ownerTeam.ID).
Find(&ownerMaps)
if err != nil {
return nil, fmt.Errorf("find team users: %v", err)
}
return ownerMaps, nil
}

// GetTwoFaStatus return state of 2FA enrollement
func (users UserList) GetTwoFaStatus() map[int64]bool {
results := make(map[int64]bool, len(users))
Expand Down
1 change: 1 addition & 0 deletions models/userlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestUserListIsUserOrgOwner(t *testing.T) {
})
}
}

func testUserListIsUserOrgOwner(t *testing.T, orgID int64, expected map[int64]bool) {
org, err := GetUserByID(orgID)
assert.NoError(t, err)
Expand Down

0 comments on commit b74810f

Please sign in to comment.