-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
org/members: display 2FA members states + optimize sql requests (#7621)
* org/members: display 2FA state * fix comment typo * lay down UserList bases * add basic test for previous methods * add comment for UserList type * add valid two-fa account * test new UserList methods * optimize MembersIsPublic by side loading info on GetMembers + fix integrations tests * respect fmt rules * use map for data * Optimize GetTwoFaStatus * rewrite by using existing sub func * Optimize IsUserOrgOwner * remove un-used code * tests: cover empty org + fix import order * tests: add ErrTeamNotExist path * tests: fix wrong expected result
- Loading branch information
1 parent
3566d2c
commit 76408d5
Showing
13 changed files
with
346 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,9 @@ | |
uid: 20 | ||
org_id: 19 | ||
is_public: true | ||
|
||
- | ||
id: 8 | ||
uid: 24 | ||
org_id: 25 | ||
is_public: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,10 @@ | |
id: 11 | ||
org_id: 17 | ||
team_id: 9 | ||
uid: 20 | ||
uid: 20 | ||
|
||
- | ||
id: 12 | ||
org_id: 25 | ||
team_id: 10 | ||
uid: 24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
- | ||
id: 1 | ||
uid: 24 | ||
secret: KlDporn6Ile4vFcKI8z7Z6sqK1Scj2Qp0ovtUzCZO6jVbRW2lAoT7UDxDPtrab8d2B9zKOocBRdBJnS8orsrUNrsyETY+jJHb79M82uZRioKbRUz15sfOpmJmEzkFeSg6S4LicUBQos= | ||
scratch_salt: Qb5bq2DyR2 | ||
scratch_hash: 068eb9b8746e0bcfe332fac4457693df1bda55800eb0f6894d14ebb736ae6a24e0fc8fc5333c19f57f81599788f0b8e51ec1 | ||
last_used_passcode: | ||
created_unix: 1564253724 | ||
updated_unix: 1564253724 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,4 +365,39 @@ | |
is_active: true | ||
num_members: 0 | ||
num_teams: 0 | ||
visibility: 2 | ||
visibility: 2 | ||
|
||
- | ||
id: 24 | ||
lower_name: user24 | ||
name: user24 | ||
full_name: "user24" | ||
email: [email protected] | ||
keep_email_private: true | ||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password | ||
type: 0 # individual | ||
salt: ZogKvWdyEx | ||
is_admin: false | ||
avatar: avatar24 | ||
avatar_email: [email protected] | ||
num_repos: 0 | ||
num_stars: 0 | ||
num_followers: 0 | ||
num_following: 0 | ||
is_active: true | ||
|
||
- | ||
id: 25 | ||
lower_name: org25 | ||
name: org25 | ||
full_name: "org25" | ||
email: [email protected] | ||
passwd: 7d93daa0d1e6f2305cc8fa496847d61dc7320bb16262f9c55dd753480207234cdd96a93194e408341971742f4701772a025a # password | ||
type: 1 # organization | ||
salt: ZogKvWdyEx | ||
is_admin: false | ||
avatar: avatar25 | ||
avatar_email: [email protected] | ||
num_repos: 0 | ||
num_members: 1 | ||
num_teams: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2019 The Gitea 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 models | ||
|
||
import ( | ||
"fmt" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
//UserList is a list of user. | ||
// This type provide valuable methods to retrieve information for a group of users efficiently. | ||
type UserList []*User | ||
|
||
func (users UserList) getUserIDs() []int64 { | ||
userIDs := make([]int64, len(users)) | ||
for _, user := range users { | ||
userIDs = append(userIDs, user.ID) //Considering that user id are unique in the list | ||
} | ||
return userIDs | ||
} | ||
|
||
// 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)) | ||
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)) | ||
for _, user := range users { | ||
results[user.ID] = false //Set default to false | ||
} | ||
tokenMaps, err := users.loadTwoFactorStatus(x) | ||
if err == nil { | ||
for _, token := range tokenMaps { | ||
results[token.UID] = true | ||
} | ||
} | ||
|
||
return results | ||
} | ||
|
||
func (users UserList) loadTwoFactorStatus(e Engine) (map[int64]*TwoFactor, error) { | ||
if len(users) == 0 { | ||
return nil, nil | ||
} | ||
|
||
userIDs := users.getUserIDs() | ||
tokenMaps := make(map[int64]*TwoFactor, len(userIDs)) | ||
err := e. | ||
In("uid", userIDs). | ||
Find(&tokenMaps) | ||
if err != nil { | ||
return nil, fmt.Errorf("find two factor: %v", err) | ||
} | ||
return tokenMaps, nil | ||
} |
Oops, something went wrong.