From faf98ca8184b020bc658e7d410a232d1061c6a11 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 26 Feb 2022 12:34:34 +0100 Subject: [PATCH 1/6] Fix showing issues in your repositories - Make a restriction on which issues can be shown based on if you the user or team has write permission to the repository. - Fixes a issue whereby you wouldn't see any associated issues with a specific team on a organisation if you wasn't a member(fixed by zeroing the User{ID} in the options). - Resolves #18913 --- models/issue.go | 2 ++ routers/web/user/home.go | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/models/issue.go b/models/issue.go index 91d4df32d1cd8..4df9ea015d5e2 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1552,6 +1552,7 @@ const ( FilterModeCreate FilterModeMention FilterModeReviewRequested + FilterModeYourRepositories ) func parseCountResult(results []map[string][]byte) int64 { @@ -1735,6 +1736,7 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) { switch opts.FilterMode { case FilterModeAll: + case FilterModeYourRepositories: stats.OpenCount, err = sess(cond). And("issue.is_closed = ?", false). Count(new(Issue)) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 33512d97c06e4..c69c0eff653bd 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -389,8 +389,10 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { filterMode = models.FilterModeMention case "review_requested": filterMode = models.FilterModeReviewRequested - case "your_repositories": // filterMode already set to All + case "your_repositories": + fallthrough default: + filterMode = models.FilterModeYourRepositories viewType = "your_repositories" } @@ -420,6 +422,26 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { User: ctx.User, } + // Search all repositories which the given user or team + // has write permissions. + repoOpts := &models.SearchRepoOptions{ + Actor: ctx.User, + OwnerID: ctx.User.ID, + Private: true, + AllPublic: false, + AllLimited: false, + } + + if ctxUser.IsOrganization() && ctx.Org.Team != nil { + repoOpts.TeamID = ctx.Org.Team.ID + } + + userRepoIDs, _, err := models.SearchRepositoryIDs(repoOpts) + if err != nil { + ctx.ServerError("models.SearchRepositoryIDs: %v", err) + return + } + switch filterMode { case models.FilterModeAll: case models.FilterModeAssign: @@ -430,6 +452,13 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { opts.MentionedID = ctx.User.ID case models.FilterModeReviewRequested: opts.ReviewRequestedID = ctx.User.ID + case models.FilterModeYourRepositories: + if ctxUser.IsOrganization() && ctx.Org.Team != nil { + // Fixes a issue whereby the user's ID would be used + // to check if it's in the team(which possible isn't the case). + opts.User = nil + } + opts.RepoIDs = userRepoIDs } // keyword holds the search term entered into the search field. @@ -553,6 +582,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { statsOpts := models.UserIssueStatsOptions{ UserID: ctx.User.ID, FilterMode: filterMode, + RepoIDs: userRepoIDs, IsPull: isPullList, IsClosed: isShowClosed, IssueIDs: issueIDsFromSearch, @@ -564,6 +594,10 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { if len(repoIDs) > 0 { statsOpts.RepoIDs = repoIDs } + // Detect when we only should search by team. + if opts.User == nil { + statsOpts.UserID = 0 + } issueStats, err = models.GetUserIssueStats(statsOpts) if err != nil { ctx.ServerError("GetUserIssueStats Shown", err) From 48098269f95dd98257f8438ddafa4756bf6081a6 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 26 Feb 2022 12:56:55 +0100 Subject: [PATCH 2/6] Clarify comment --- routers/web/user/home.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index c69c0eff653bd..f0d898b227bea 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -422,8 +422,18 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { User: ctx.User, } - // Search all repositories which the given user or team - // has write permissions. + // Search all repositories which + // + // As user: + // - Owns the repository. + // - Have collaborator permissions in repository. + // + // As org: + // - Owns the repository. + // + // As team: + // - Team org's owns the repository. + // - Team has read permission to repository. repoOpts := &models.SearchRepoOptions{ Actor: ctx.User, OwnerID: ctx.User.ID, From 176aee7be52c215c0fcea6014988e325b088d5c4 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 26 Feb 2022 12:58:49 +0100 Subject: [PATCH 3/6] Fix linter --- routers/web/user/home.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index f0d898b227bea..9b84427fb202c 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -363,7 +363,7 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { var ( viewType string sortType = ctx.FormString("sort") - filterMode = models.FilterModeAll + filterMode int ) // -------------------------------------------------------------------------------- From 7d76c98e3d32b48e6047b2b2e84a0416f29519cc Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 26 Feb 2022 13:32:17 +0100 Subject: [PATCH 4/6] Fix case statement --- models/issue.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/issue.go b/models/issue.go index 4df9ea015d5e2..d32eb43ef252a 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1735,8 +1735,7 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) { } switch opts.FilterMode { - case FilterModeAll: - case FilterModeYourRepositories: + case FilterModeAll, FilterModeYourRepositories: stats.OpenCount, err = sess(cond). And("issue.is_closed = ?", false). Count(new(Issue)) From 6575eed2e457978aecd4f6d7fa9b1ca058d3dbd3 Mon Sep 17 00:00:00 2001 From: Gusted Date: Tue, 1 Mar 2022 17:06:24 +0100 Subject: [PATCH 5/6] Use Builder.Cond --- models/issue.go | 4 ++++ routers/web/user/home.go | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/models/issue.go b/models/issue.go index d32eb43ef252a..9c8fac23c4ebf 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1697,6 +1697,7 @@ type UserIssueStatsOptions struct { IssueIDs []int64 IsArchived util.OptionalBool LabelIDs []int64 + RepoCond builder.Cond Org *Organization Team *Team } @@ -1714,6 +1715,9 @@ func GetUserIssueStats(opts UserIssueStatsOptions) (*IssueStats, error) { if len(opts.IssueIDs) > 0 { cond = cond.And(builder.In("issue.id", opts.IssueIDs)) } + if opts.RepoCond != nil { + cond = cond.And(opts.RepoCond) + } if opts.UserID > 0 { cond = cond.And(issuePullAccessibleRepoCond("issue.repo_id", opts.UserID, opts.Org, opts.Team, opts.IsPull)) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 9b84427fb202c..a1a94bbaee905 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -446,12 +446,6 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { repoOpts.TeamID = ctx.Org.Team.ID } - userRepoIDs, _, err := models.SearchRepositoryIDs(repoOpts) - if err != nil { - ctx.ServerError("models.SearchRepositoryIDs: %v", err) - return - } - switch filterMode { case models.FilterModeAll: case models.FilterModeAssign: @@ -468,6 +462,12 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { // to check if it's in the team(which possible isn't the case). opts.User = nil } + userRepoIDs, _, err := models.SearchRepositoryIDs(repoOpts) + if err != nil { + ctx.ServerError("models.SearchRepositoryIDs: %v", err) + return + } + opts.RepoIDs = userRepoIDs } @@ -601,8 +601,8 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { Org: org, Team: team, } - if len(repoIDs) > 0 { - statsOpts.RepoIDs = repoIDs + if filterMode == models.FilterModeYourRepositories { + statsOpts.RepoCond = models.SearchRepositoryCondition(repoOpts) } // Detect when we only should search by team. if opts.User == nil { From c57ce3f99c0d5b72726b6903aa689e73e0097bf3 Mon Sep 17 00:00:00 2001 From: Gusted Date: Tue, 1 Mar 2022 17:12:50 +0100 Subject: [PATCH 6/6] Fix building --- routers/web/user/home.go | 1 - 1 file changed, 1 deletion(-) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index a1a94bbaee905..a953297575b28 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -592,7 +592,6 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { statsOpts := models.UserIssueStatsOptions{ UserID: ctx.User.ID, FilterMode: filterMode, - RepoIDs: userRepoIDs, IsPull: isPullList, IsClosed: isShowClosed, IssueIDs: issueIDsFromSearch,