Skip to content

Commit c2ae432

Browse files
authored
Add user filter to issueTrackedTimes, enable usage for issue managers (#14081)
* add user filter to issueTrackedTimes fixes #14024 * update swagger * allow user filter for issue writers * improve swagger doc * return 404 on invalid user
1 parent 6f1dddf commit c2ae432

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

routers/api/v1/repo/issue_tracked_time.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func ListTrackedTimes(ctx *context.APIContext) {
4141
// type: integer
4242
// format: int64
4343
// required: true
44+
// - name: user
45+
// in: query
46+
// description: optional filter by user (available for issue managers)
47+
// type: string
4448
// - name: since
4549
// in: query
4650
// description: Only show times updated after the given time. This is a timestamp in RFC 3339 format
@@ -85,13 +89,34 @@ func ListTrackedTimes(ctx *context.APIContext) {
8589
IssueID: issue.ID,
8690
}
8791

92+
qUser := strings.Trim(ctx.Query("user"), " ")
93+
if qUser != "" {
94+
user, err := models.GetUserByName(qUser)
95+
if models.IsErrUserNotExist(err) {
96+
ctx.Error(http.StatusNotFound, "User does not exist", err)
97+
} else if err != nil {
98+
ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
99+
return
100+
}
101+
opts.UserID = user.ID
102+
}
103+
88104
if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil {
89105
ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
90106
return
91107
}
92108

93-
if !ctx.IsUserRepoAdmin() && !ctx.User.IsAdmin {
94-
opts.UserID = ctx.User.ID
109+
cantSetUser := !ctx.User.IsAdmin &&
110+
opts.UserID != ctx.User.ID &&
111+
!ctx.IsUserRepoWriter([]models.UnitType{models.UnitTypeIssues})
112+
113+
if cantSetUser {
114+
if opts.UserID == 0 {
115+
opts.UserID = ctx.User.ID
116+
} else {
117+
ctx.Error(http.StatusForbidden, "", fmt.Errorf("query by user not allowed; not enough rights"))
118+
return
119+
}
95120
}
96121

97122
trackedTimes, err := models.GetTrackedTimes(opts)
@@ -394,12 +419,7 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
394419
}
395420

396421
if !ctx.IsUserRepoAdmin() && !ctx.User.IsAdmin && ctx.User.ID != user.ID {
397-
ctx.Error(http.StatusForbidden, "", fmt.Errorf("query user not allowed not enouth rights"))
398-
return
399-
}
400-
401-
if !ctx.IsUserRepoAdmin() && !ctx.User.IsAdmin && ctx.User.ID != user.ID {
402-
ctx.Error(http.StatusForbidden, "", fmt.Errorf("query user not allowed not enouth rights"))
422+
ctx.Error(http.StatusForbidden, "", fmt.Errorf("query by user not allowed; not enough rights"))
403423
return
404424
}
405425

@@ -440,7 +460,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
440460
// required: true
441461
// - name: user
442462
// in: query
443-
// description: optional filter by user
463+
// description: optional filter by user (available for issue managers)
444464
// type: string
445465
// - name: since
446466
// in: query
@@ -482,7 +502,9 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
482502
qUser := strings.Trim(ctx.Query("user"), " ")
483503
if qUser != "" {
484504
user, err := models.GetUserByName(qUser)
485-
if err != nil {
505+
if models.IsErrUserNotExist(err) {
506+
ctx.Error(http.StatusNotFound, "User does not exist", err)
507+
} else if err != nil {
486508
ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
487509
return
488510
}
@@ -495,7 +517,11 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
495517
return
496518
}
497519

498-
if !ctx.IsUserRepoAdmin() && !ctx.User.IsAdmin {
520+
cantSetUser := !ctx.User.IsAdmin &&
521+
opts.UserID != ctx.User.ID &&
522+
!ctx.IsUserRepoWriter([]models.UnitType{models.UnitTypeIssues})
523+
524+
if cantSetUser {
499525
if opts.UserID == 0 {
500526
opts.UserID = ctx.User.ID
501527
} else {

templates/swagger/v1_json.tmpl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5840,6 +5840,12 @@
58405840
"in": "path",
58415841
"required": true
58425842
},
5843+
{
5844+
"type": "string",
5845+
"description": "optional filter by user (available for issue managers)",
5846+
"name": "user",
5847+
"in": "query"
5848+
},
58435849
{
58445850
"type": "string",
58455851
"format": "date-time",
@@ -8811,7 +8817,7 @@
88118817
},
88128818
{
88138819
"type": "string",
8814-
"description": "optional filter by user",
8820+
"description": "optional filter by user (available for issue managers)",
88158821
"name": "user",
88168822
"in": "query"
88178823
},

0 commit comments

Comments
 (0)