summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-02-02 08:40:04 +0000
committerGitHub <noreply@github.com>2022-02-02 08:40:04 +0000
commit9c318a17f576887736e97c7bc2971d2ad7579a33 (patch)
tree3eeb48ec3f45a6401215cc2b71761ba76d9a0af9 /models
parent72fa108cbc0a3a7582ca7aaefc46787ec096640a (diff)
downloadgitea-9c318a17f576887736e97c7bc2971d2ad7579a33.tar.gz
gitea-9c318a17f576887736e97c7bc2971d2ad7579a33.zip
Add `GetUserTeams` (#18499) (#18531)
Backport #18499 * Correct use `UserID` in `SearchTeams` - Use `UserID` in the `SearchTeams` function, currently it was useless to pass such information. Now it does a INNER statement to `team_user` which obtains UserID -> TeamID data. - Make OrgID optional. - Resolves #18484 * Seperate searching specific user * Add condition back * Use correct struct type Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'models')
-rw-r--r--models/org_team.go61
-rw-r--r--models/org_team_test.go4
2 files changed, 55 insertions, 10 deletions
diff --git a/models/org_team.go b/models/org_team.go
index bce4afb061..17f95bb5b0 100644
--- a/models/org_team.go
+++ b/models/org_team.go
@@ -49,22 +49,67 @@ func init() {
db.RegisterModel(new(TeamUnit))
}
-// SearchTeamOptions holds the search options
-type SearchTeamOptions struct {
+// SearchOrgTeamOptions holds the search options
+type SearchOrgTeamOptions struct {
db.ListOptions
- UserID int64
Keyword string
OrgID int64
IncludeDesc bool
}
+// GetUserTeamOptions holds the search options.
+type GetUserTeamOptions struct {
+ db.ListOptions
+ UserID int64
+}
+
// SearchMembersOptions holds the search options
type SearchMembersOptions struct {
db.ListOptions
}
-// SearchTeam search for teams. Caller is responsible to check permissions.
-func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
+// GetUserTeams search for org teams. Caller is responsible to check permissions.
+func GetUserTeams(opts *GetUserTeamOptions) ([]*Team, int64, error) {
+ if opts.Page <= 0 {
+ opts.Page = 1
+ }
+ if opts.PageSize == 0 {
+ // Default limit
+ opts.PageSize = 10
+ }
+
+ sess := db.GetEngine(db.DefaultContext)
+
+ sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id").
+ And("team_user.uid=?", opts.UserID)
+
+ count, err := sess.
+ Count(new(Team))
+ if err != nil {
+ return nil, 0, err
+ }
+
+ if opts.PageSize == -1 {
+ opts.PageSize = int(count)
+ } else {
+ sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
+ }
+
+ sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id").
+ And("team_user.uid=?", opts.UserID)
+
+ teams := make([]*Team, 0, opts.PageSize)
+ if err = sess.
+ OrderBy("lower_name").
+ Find(&teams); err != nil {
+ return nil, 0, err
+ }
+
+ return teams, count, nil
+}
+
+// SearchOrgTeams search for org teams. Caller is responsible to check permissions.
+func SearchOrgTeams(opts *SearchOrgTeamOptions) ([]*Team, int64, error) {
if opts.Page <= 0 {
opts.Page = 1
}
@@ -196,7 +241,7 @@ func (t *Team) getRepositories(e db.Engine) error {
}
// GetRepositories returns paginated repositories in team of organization.
-func (t *Team) GetRepositories(opts *SearchTeamOptions) error {
+func (t *Team) GetRepositories(opts *SearchOrgTeamOptions) error {
if opts.Page == 0 {
return t.getRepositories(db.GetEngine(db.DefaultContext))
}
@@ -716,7 +761,7 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
// DeleteTeam deletes given team.
// It's caller's responsibility to assign organization ID.
func DeleteTeam(t *Team) error {
- if err := t.GetRepositories(&SearchTeamOptions{}); err != nil {
+ if err := t.GetRepositories(&SearchOrgTeamOptions{}); err != nil {
return err
}
@@ -858,7 +903,7 @@ func AddTeamMember(team *Team, userID int64) error {
}
// Get team and its repositories.
- if err := team.GetRepositories(&SearchTeamOptions{}); err != nil {
+ if err := team.GetRepositories(&SearchOrgTeamOptions{}); err != nil {
return err
}
diff --git a/models/org_team_test.go b/models/org_team_test.go
index aa62cc58e2..cf3a797991 100644
--- a/models/org_team_test.go
+++ b/models/org_team_test.go
@@ -46,7 +46,7 @@ func TestTeam_GetRepositories(t *testing.T) {
test := func(teamID int64) {
team := unittest.AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
- assert.NoError(t, team.GetRepositories(&SearchTeamOptions{}))
+ assert.NoError(t, team.GetRepositories(&SearchOrgTeamOptions{}))
assert.Len(t, team.Repos, team.NumRepos)
for _, repo := range team.Repos {
unittest.AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID})
@@ -292,7 +292,7 @@ func TestGetTeamMembers(t *testing.T) {
func TestGetUserTeams(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
test := func(userID int64) {
- teams, _, err := SearchTeam(&SearchTeamOptions{UserID: userID})
+ teams, _, err := GetUserTeams(&GetUserTeamOptions{UserID: userID})
assert.NoError(t, err)
for _, team := range teams {
unittest.AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})