From 7f175f7f1ac84b44313ea9a43f03ee80af9623a3 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 12 Jan 2023 16:03:09 +0100 Subject: [PATCH] fix(project): recursively get all users from all parent projects --- pkg/db/fixtures/projects.yml | 9 ++++ pkg/db/fixtures/team_projects.yml | 18 ++++++++ pkg/db/fixtures/users_projects.yml | 18 ++++++++ pkg/models/prject_test.go | 4 +- pkg/models/user_project.go | 72 ++++++++++++++++++++---------- 5 files changed, 95 insertions(+), 26 deletions(-) diff --git a/pkg/db/fixtures/projects.yml b/pkg/db/fixtures/projects.yml index 75978396c..b8e178687 100644 --- a/pkg/db/fixtures/projects.yml +++ b/pkg/db/fixtures/projects.yml @@ -171,6 +171,7 @@ identifier: test19 owner_id: 7 position: 19 + parent_project_id: 29 updated: 2018-12-02 15:13:12 created: 2018-12-01 15:13:12 # User 1 does not have access to this project @@ -239,11 +240,19 @@ id: 27 title: Test27 owner_id: 6 + position: 2700 updated: 2018-12-02 15:13:12 created: 2018-12-01 15:13:12 - id: 28 title: Test28 owner_id: 6 + position: 2800 + updated: 2018-12-02 15:13:12 + created: 2018-12-01 15:13:12 +- + id: 29 + title: Test29 + owner_id: 6 updated: 2018-12-02 15:13:12 created: 2018-12-01 15:13:12 diff --git a/pkg/db/fixtures/team_projects.yml b/pkg/db/fixtures/team_projects.yml index 103c5be79..cbe0a6e57 100644 --- a/pkg/db/fixtures/team_projects.yml +++ b/pkg/db/fixtures/team_projects.yml @@ -58,3 +58,21 @@ right: 0 updated: 2018-12-02 15:13:12 created: 2018-12-01 15:13:12 +- id: 10 + team_id: 11 + project_id: 29 + right: 0 + updated: 2018-12-02 15:13:12 + created: 2018-12-01 15:13:12 +- id: 11 + team_id: 12 + project_id: 29 + right: 1 + updated: 2018-12-02 15:13:12 + created: 2018-12-01 15:13:12 +- id: 12 + team_id: 13 + project_id: 29 + right: 2 + updated: 2018-12-02 15:13:12 + created: 2018-12-01 15:13:12 diff --git a/pkg/db/fixtures/users_projects.yml b/pkg/db/fixtures/users_projects.yml index a66ccf620..9e0ba347c 100644 --- a/pkg/db/fixtures/users_projects.yml +++ b/pkg/db/fixtures/users_projects.yml @@ -52,3 +52,21 @@ right: 0 updated: 2018-12-02 15:13:12 created: 2018-12-01 15:13:12 +- id: 10 + user_id: 11 + project_id: 29 + right: 0 + updated: 2018-12-02 15:13:12 + created: 2018-12-01 15:13:12 +- id: 11 + user_id: 12 + project_id: 29 + right: 1 + updated: 2018-12-02 15:13:12 + created: 2018-12-01 15:13:12 +- id: 12 + user_id: 13 + project_id: 29 + right: 2 + updated: 2018-12-02 15:13:12 + created: 2018-12-01 15:13:12 diff --git a/pkg/models/prject_test.go b/pkg/models/prject_test.go index 05f953a49..584a09689 100644 --- a/pkg/models/prject_test.go +++ b/pkg/models/prject_test.go @@ -243,7 +243,7 @@ func TestProject_ReadAll(t *testing.T) { projects := []*Project{} _, _, err := getAllProjectsForUser(s, 1, nil, &projectOptions{}, &projects, 0) assert.NoError(t, err) - assert.Equal(t, 10, len(projects)) + assert.Equal(t, 12, len(projects)) _ = s.Close() }) t.Run("only child projects for one project", func(t *testing.T) { @@ -259,7 +259,7 @@ func TestProject_ReadAll(t *testing.T) { assert.NoError(t, err) assert.Equal(t, reflect.TypeOf(projects3).Kind(), reflect.Slice) ls := projects3.([]*Project) - assert.Equal(t, 10, len(ls)) + assert.Equal(t, 12, len(ls)) assert.Equal(t, int64(3), ls[0].ID) // Project 3 has a position of 1 and should be sorted first assert.Equal(t, int64(1), ls[1].ID) assert.Equal(t, int64(6), ls[2].ID) diff --git a/pkg/models/user_project.go b/pkg/models/user_project.go index 848aaaa1e..7c3d33d56 100644 --- a/pkg/models/user_project.go +++ b/pkg/models/user_project.go @@ -34,34 +34,58 @@ func ListUsersFromProject(s *xorm.Session, l *Project, search string) (users []* userids := []*ProjectUIDs{} - err = s. - Select(`l.owner_id as projectOwner, + var currentProject *Project + currentProject, err = GetProjectSimpleByID(s, l.ID) + if err != nil { + return nil, err + } + + for { + currentUserIDs := []*ProjectUIDs{} + err = s. + Select(`l.owner_id as projectOwner, ul.user_id as ulID, tm2.user_id as tlUID`). - Table("projects"). - Alias("l"). - // User stuff - Join("LEFT", []string{"users_projects", "ul"}, "ul.project_id = l.id"). - // Team stuff - Join("LEFT", []string{"team_projects", "tl"}, "l.id = tl.project_id"). - Join("LEFT", []string{"team_members", "tm2"}, "tm2.team_id = tl.team_id"). - // The actual condition - Where( - builder.Or( - builder.Or(builder.Eq{"ul.right": RightRead}), - builder.Or(builder.Eq{"tl.right": RightRead}), + Table("projects"). + Alias("l"). + // User stuff + Join("LEFT", []string{"users_projects", "ul"}, "ul.project_id = l.id"). + // Team stuff + Join("LEFT", []string{"team_projects", "tl"}, "l.id = tl.project_id"). + Join("LEFT", []string{"team_members", "tm2"}, "tm2.team_id = tl.team_id"). + // The actual condition + Where( + builder.Or( + builder.Or(builder.Eq{"ul.right": RightRead}), + builder.Or(builder.Eq{"tl.right": RightRead}), - builder.Or(builder.Eq{"ul.right": RightWrite}), - builder.Or(builder.Eq{"tl.right": RightWrite}), + builder.Or(builder.Eq{"ul.right": RightWrite}), + builder.Or(builder.Eq{"tl.right": RightWrite}), - builder.Or(builder.Eq{"ul.right": RightAdmin}), - builder.Or(builder.Eq{"tl.right": RightAdmin}), - ), - builder.Eq{"l.id": l.ID}, - ). - Find(&userids) - if err != nil { - return + builder.Or(builder.Eq{"ul.right": RightAdmin}), + builder.Or(builder.Eq{"tl.right": RightAdmin}), + ), + builder.Eq{"l.id": currentProject.ID}, + ). + Find(¤tUserIDs) + if err != nil { + return + } + userids = append(userids, currentUserIDs...) + + if currentProject.ParentProjectID == 0 { + break + } + + parent, err := GetProjectSimpleByID(s, currentProject.ParentProjectID) + if err != nil && !IsErrProjectDoesNotExist(err) { + return nil, err + } + if err != nil && IsErrProjectDoesNotExist(err) { + break + } + + currentProject = parent } // Remove duplicates from the project of ids and make it a slice