From 290cf75f9343a43d9770b1d6f8f3332a23357e27 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 27 Mar 2021 17:45:26 +0100 Subject: [PATCH] [refactor] Unify the export of user data via API (#15144) * [refactor] unify how user data is exported via API * test time via unix timestamp --- integrations/api_issue_reaction_test.go | 6 +- integrations/api_team_user_test.go | 10 ++- modules/convert/git_commit.go | 8 +-- modules/convert/issue.go | 6 +- modules/convert/issue_comment.go | 2 +- modules/convert/pull.go | 2 +- modules/convert/pull_review.go | 13 +--- modules/convert/release.go | 2 +- modules/convert/repository.go | 2 +- modules/convert/status.go | 2 +- modules/convert/user.go | 25 ++++++- modules/convert/user_test.go | 6 +- modules/notification/webhook/webhook.go | 80 +++++++++++------------ routers/api/v1/admin/user.go | 6 +- routers/api/v1/org/member.go | 2 +- routers/api/v1/org/team.go | 4 +- routers/api/v1/repo/collaborators.go | 2 +- routers/api/v1/repo/hook.go | 4 +- routers/api/v1/repo/issue_reaction.go | 12 ++-- routers/api/v1/repo/issue_subscription.go | 2 +- routers/api/v1/repo/star.go | 2 +- routers/api/v1/repo/subscriber.go | 2 +- routers/api/v1/user/follower.go | 2 +- routers/api/v1/user/key.go | 4 +- routers/api/v1/user/user.go | 6 +- routers/repo/webhook.go | 2 +- 26 files changed, 117 insertions(+), 97 deletions(-) diff --git a/integrations/api_issue_reaction_test.go b/integrations/api_issue_reaction_test.go index 20b83db2aab..ad4adc1eae7 100644 --- a/integrations/api_issue_reaction_test.go +++ b/integrations/api_issue_reaction_test.go @@ -61,7 +61,7 @@ func TestAPIIssuesReactions(t *testing.T) { DecodeJSON(t, resp, &apiReactions) expectResponse := make(map[int]api.Reaction) expectResponse[0] = api.Reaction{ - User: convert.ToUser(user2, true, true), + User: convert.ToUser(user2, user2), Reaction: "eyes", Created: time.Unix(1573248003, 0), } @@ -121,12 +121,12 @@ func TestAPICommentReactions(t *testing.T) { DecodeJSON(t, resp, &apiReactions) expectResponse := make(map[int]api.Reaction) expectResponse[0] = api.Reaction{ - User: convert.ToUser(user2, true, true), + User: convert.ToUser(user2, user2), Reaction: "laugh", Created: time.Unix(1573248004, 0), } expectResponse[1] = api.Reaction{ - User: convert.ToUser(user1, true, true), + User: convert.ToUser(user1, user1), Reaction: "laugh", Created: time.Unix(1573248005, 0), } diff --git a/integrations/api_team_user_test.go b/integrations/api_team_user_test.go index eec8634cb03..5a8fba512ff 100644 --- a/integrations/api_team_user_test.go +++ b/integrations/api_team_user_test.go @@ -31,5 +31,13 @@ func TestAPITeamUser(t *testing.T) { user2.Created = user2.Created.In(time.Local) user := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) - assert.Equal(t, convert.ToUser(user, true, false), user2) + expectedUser := convert.ToUser(user, user) + + // test time via unix timestamp + assert.EqualValues(t, expectedUser.LastLogin.Unix(), user2.LastLogin.Unix()) + assert.EqualValues(t, expectedUser.Created.Unix(), user2.Created.Unix()) + expectedUser.LastLogin = user2.LastLogin + expectedUser.Created = user2.Created + + assert.Equal(t, expectedUser, user2) } diff --git a/modules/convert/git_commit.go b/modules/convert/git_commit.go index 4e30ec2c0b3..c647dd4e184 100644 --- a/modules/convert/git_commit.go +++ b/modules/convert/git_commit.go @@ -86,13 +86,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] } if ok { - apiAuthor = ToUser(cacheAuthor, false, false) + apiAuthor = ToUser(cacheAuthor, nil) } else { author, err := models.GetUserByEmail(commit.Author.Email) if err != nil && !models.IsErrUserNotExist(err) { return nil, err } else if err == nil { - apiAuthor = ToUser(author, false, false) + apiAuthor = ToUser(author, nil) if userCache != nil { userCache[commit.Author.Email] = author } @@ -108,13 +108,13 @@ func ToCommit(repo *models.Repository, commit *git.Commit, userCache map[string] } if ok { - apiCommitter = ToUser(cacheCommitter, false, false) + apiCommitter = ToUser(cacheCommitter, nil) } else { committer, err := models.GetUserByEmail(commit.Committer.Email) if err != nil && !models.IsErrUserNotExist(err) { return nil, err } else if err == nil { - apiCommitter = ToUser(committer, false, false) + apiCommitter = ToUser(committer, nil) if userCache != nil { userCache[commit.Committer.Email] = committer } diff --git a/modules/convert/issue.go b/modules/convert/issue.go index b773e78a6b5..da09aeaca41 100644 --- a/modules/convert/issue.go +++ b/modules/convert/issue.go @@ -31,7 +31,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { URL: issue.APIURL(), HTMLURL: issue.HTMLURL(), Index: issue.Index, - Poster: ToUser(issue.Poster, false, false), + Poster: ToUser(issue.Poster, nil), Title: issue.Title, Body: issue.Content, Ref: issue.Ref, @@ -66,9 +66,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue { } if len(issue.Assignees) > 0 { for _, assignee := range issue.Assignees { - apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, false, false)) + apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, nil)) } - apiIssue.Assignee = ToUser(issue.Assignees[0], false, false) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee` + apiIssue.Assignee = ToUser(issue.Assignees[0], nil) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee` } if issue.IsPull { if err := issue.LoadPullRequest(); err != nil { diff --git a/modules/convert/issue_comment.go b/modules/convert/issue_comment.go index cf65c876d3d..1610b9f0d88 100644 --- a/modules/convert/issue_comment.go +++ b/modules/convert/issue_comment.go @@ -13,7 +13,7 @@ import ( func ToComment(c *models.Comment) *api.Comment { return &api.Comment{ ID: c.ID, - Poster: ToUser(c.Poster, false, false), + Poster: ToUser(c.Poster, nil), HTMLURL: c.HTMLURL(), IssueURL: c.IssueURL(), PRURL: c.PRURL(), diff --git a/modules/convert/pull.go b/modules/convert/pull.go index 3c24f4532f3..8bdf17a0494 100644 --- a/modules/convert/pull.go +++ b/modules/convert/pull.go @@ -159,7 +159,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest { if pr.HasMerged { apiPullRequest.Merged = pr.MergedUnix.AsTimePtr() apiPullRequest.MergedCommitID = &pr.MergedCommitID - apiPullRequest.MergedBy = ToUser(pr.Merger, false, false) + apiPullRequest.MergedBy = ToUser(pr.Merger, nil) } return apiPullRequest diff --git a/modules/convert/pull_review.go b/modules/convert/pull_review.go index 418cb711dc5..18c6c8e58c8 100644 --- a/modules/convert/pull_review.go +++ b/modules/convert/pull_review.go @@ -20,14 +20,9 @@ func ToPullReview(r *models.Review, doer *models.User) (*api.PullReview, error) r.Reviewer = models.NewGhostUser() } - auth := false - if doer != nil { - auth = doer.IsAdmin || doer.ID == r.ReviewerID - } - result := &api.PullReview{ ID: r.ID, - Reviewer: ToUser(r.Reviewer, doer != nil, auth), + Reviewer: ToUser(r.Reviewer, doer), ReviewerTeam: ToTeam(r.ReviewerTeam), State: api.ReviewStateUnknown, Body: r.Content, @@ -88,14 +83,10 @@ func ToPullReviewCommentList(review *models.Review, doer *models.User) ([]*api.P for _, lines := range review.CodeComments { for _, comments := range lines { for _, comment := range comments { - auth := false - if doer != nil { - auth = doer.IsAdmin || doer.ID == comment.Poster.ID - } apiComment := &api.PullReviewComment{ ID: comment.ID, Body: comment.Content, - Reviewer: ToUser(comment.Poster, doer != nil, auth), + Reviewer: ToUser(comment.Poster, doer), ReviewID: review.ID, Created: comment.CreatedUnix.AsTime(), Updated: comment.UpdatedUnix.AsTime(), diff --git a/modules/convert/release.go b/modules/convert/release.go index d9def896378..70f0d6e7640 100644 --- a/modules/convert/release.go +++ b/modules/convert/release.go @@ -29,7 +29,7 @@ func ToRelease(r *models.Release) *api.Release { IsPrerelease: r.IsPrerelease, CreatedAt: r.CreatedUnix.AsTime(), PublishedAt: r.CreatedUnix.AsTime(), - Publisher: ToUser(r.Publisher, false, false), + Publisher: ToUser(r.Publisher, nil), Attachments: assets, } } diff --git a/modules/convert/repository.go b/modules/convert/repository.go index 63a22bb04ea..9a4fbb97cae 100644 --- a/modules/convert/repository.go +++ b/modules/convert/repository.go @@ -102,7 +102,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) return &api.Repository{ ID: repo.ID, - Owner: ToUser(repo.Owner, mode != models.AccessModeNone, mode >= models.AccessModeAdmin), + Owner: ToUserWithAccessMode(repo.Owner, mode), Name: repo.Name, FullName: repo.FullName(), Description: repo.Description, diff --git a/modules/convert/status.go b/modules/convert/status.go index f972fc333c1..bb2f964fe44 100644 --- a/modules/convert/status.go +++ b/modules/convert/status.go @@ -24,7 +24,7 @@ func ToCommitStatus(status *models.CommitStatus) *api.CommitStatus { if status.CreatorID != 0 { creator, _ := models.GetUserByID(status.CreatorID) - apiStatus.Creator = ToUser(creator, false, false) + apiStatus.Creator = ToUser(creator, nil) } return apiStatus diff --git a/modules/convert/user.go b/modules/convert/user.go index f5d853fd4d1..f3a33aee411 100644 --- a/modules/convert/user.go +++ b/modules/convert/user.go @@ -11,11 +11,32 @@ import ( ) // ToUser convert models.User to api.User -// signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself -func ToUser(user *models.User, signed, authed bool) *api.User { +// if doer is set, private information is added if the doer has the permission to see it +func ToUser(user, doer *models.User) *api.User { if user == nil { return nil } + authed := false + signed := false + if doer != nil { + signed = true + authed = doer.ID == user.ID || doer.IsAdmin + } + return toUser(user, signed, authed) +} + +// ToUserWithAccessMode convert models.User to api.User +// AccessMode is not none show add some more information +func ToUserWithAccessMode(user *models.User, accessMode models.AccessMode) *api.User { + if user == nil { + return nil + } + return toUser(user, accessMode != models.AccessModeNone, false) +} + +// toUser convert models.User to api.User +// signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself +func toUser(user *models.User, signed, authed bool) *api.User { result := &api.User{ ID: user.ID, UserName: user.Name, diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go index eff60d51830..39396534410 100644 --- a/modules/convert/user_test.go +++ b/modules/convert/user_test.go @@ -15,14 +15,14 @@ func TestUser_ToUser(t *testing.T) { user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) - apiUser := ToUser(user1, true, true) + apiUser := toUser(user1, true, true) assert.True(t, apiUser.IsAdmin) user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) - apiUser = ToUser(user2, true, true) + apiUser = toUser(user2, true, true) assert.False(t, apiUser.IsAdmin) - apiUser = ToUser(user1, false, false) + apiUser = toUser(user1, false, false) assert.False(t, apiUser.IsAdmin) } diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go index a7357a51cab..90dc59021c5 100644 --- a/modules/notification/webhook/webhook.go +++ b/modules/notification/webhook/webhook.go @@ -53,7 +53,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *model Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } else { err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{ @@ -61,7 +61,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *model Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } if err != nil { @@ -77,7 +77,7 @@ func (m *webhookNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo if err := webhook_services.PrepareWebhooks(oldRepo, models.HookEventFork, &api.ForkPayload{ Forkee: convert.ToRepo(oldRepo, oldMode), Repo: convert.ToRepo(repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err) } @@ -89,8 +89,8 @@ func (m *webhookNotifier) NotifyForkRepository(doer *models.User, oldRepo, repo if err := webhook_services.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(repo, models.AccessModeOwner), - Organization: convert.ToUser(u, false, false), - Sender: convert.ToUser(doer, false, false), + Organization: convert.ToUser(u, nil), + Sender: convert.ToUser(doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -102,8 +102,8 @@ func (m *webhookNotifier) NotifyCreateRepository(doer *models.User, u *models.Us if err := webhook_services.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(repo, models.AccessModeOwner), - Organization: convert.ToUser(u, false, false), - Sender: convert.ToUser(doer, false, false), + Organization: convert.ToUser(u, nil), + Sender: convert.ToUser(doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -115,8 +115,8 @@ func (m *webhookNotifier) NotifyDeleteRepository(doer *models.User, repo *models if err := webhook_services.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoDeleted, Repository: convert.ToRepo(repo, models.AccessModeOwner), - Organization: convert.ToUser(u, false, false), - Sender: convert.ToUser(doer, false, false), + Organization: convert.ToUser(u, nil), + Sender: convert.ToUser(doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -127,8 +127,8 @@ func (m *webhookNotifier) NotifyMigrateRepository(doer *models.User, u *models.U if err := webhook_services.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ Action: api.HookRepoCreated, Repository: convert.ToRepo(repo, models.AccessModeOwner), - Organization: convert.ToUser(u, false, false), - Sender: convert.ToUser(doer, false, false), + Organization: convert.ToUser(u, nil), + Sender: convert.ToUser(doer, nil), }); err != nil { log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) } @@ -147,7 +147,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *mo Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), } if removed { apiPullRequest.Action = api.HookIssueUnassigned @@ -165,7 +165,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(doer *models.User, issue *mo Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), } if removed { apiIssue.Action = api.HookIssueUnassigned @@ -199,7 +199,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *models.User, issue *model }, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } else { err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ @@ -212,7 +212,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(doer *models.User, issue *model }, Issue: convert.ToAPIIssue(issue), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(issue.Poster, false, false), + Sender: convert.ToUser(issue.Poster, nil), }) } @@ -234,7 +234,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *mode Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), } if isClosed { apiPullRequest.Action = api.HookIssueClosed @@ -247,7 +247,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(doer *models.User, issue *mode Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), } if isClosed { apiIssue.Action = api.HookIssueClosed @@ -277,7 +277,7 @@ func (m *webhookNotifier) NotifyNewIssue(issue *models.Issue, mentions []*models Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(issue.Poster, false, false), + Sender: convert.ToUser(issue.Poster, nil), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -303,7 +303,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(pull *models.PullRequest, mention Index: pull.Issue.Index, PullRequest: convert.ToAPIPullRequest(pull), Repository: convert.ToRepo(pull.Issue.Repo, mode), - Sender: convert.ToUser(pull.Issue.Poster, false, false), + Sender: convert.ToUser(pull.Issue.Poster, nil), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -324,7 +324,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *mod }, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } else { err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ @@ -337,7 +337,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *mod }, Issue: convert.ToAPIIssue(issue), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } if err != nil { @@ -374,7 +374,7 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme }, }, Repository: convert.ToRepo(c.Issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), IsPull: true, }) } else { @@ -388,7 +388,7 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme }, }, Repository: convert.ToRepo(c.Issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), IsPull: false, }) } @@ -409,7 +409,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *mode Issue: convert.ToAPIIssue(issue), Comment: convert.ToComment(comment), Repository: convert.ToRepo(repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), IsPull: true, }) } else { @@ -418,7 +418,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *mode Issue: convert.ToAPIIssue(issue), Comment: convert.ToComment(comment), Repository: convert.ToRepo(repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), IsPull: false, }) } @@ -453,7 +453,7 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models Issue: convert.ToAPIIssue(comment.Issue), Comment: convert.ToComment(comment), Repository: convert.ToRepo(comment.Issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), IsPull: true, }) } else { @@ -462,7 +462,7 @@ func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models Issue: convert.ToAPIIssue(comment.Issue), Comment: convert.ToComment(comment), Repository: convert.ToRepo(comment.Issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), IsPull: false, }) } @@ -502,7 +502,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(doer *models.User, issue *mode Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: convert.ToRepo(issue.Repo, models.AccessModeNone), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } else { err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{ @@ -510,7 +510,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(doer *models.User, issue *mode Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } if err != nil { @@ -544,7 +544,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m Index: issue.Index, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } else { err = webhook_services.PrepareWebhooks(issue.Repo, models.HookEventIssueMilestone, &api.IssuePayload{ @@ -552,7 +552,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m Index: issue.Index, Issue: convert.ToAPIIssue(issue), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) } if err != nil { @@ -561,7 +561,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m } func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - apiPusher := convert.ToUser(pusher, false, false) + apiPusher := convert.ToUser(pusher, nil) apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) @@ -610,7 +610,7 @@ func (*webhookNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mod Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr), Repository: convert.ToRepo(pr.Issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), Action: api.HookIssueClosed, } @@ -643,7 +643,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(doer *models.User, }, PullRequest: convert.ToAPIPullRequest(issue.PullRequest), Repository: convert.ToRepo(issue.Repo, mode), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }) if err != nil { @@ -682,7 +682,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review Index: review.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr), Repository: convert.ToRepo(review.Issue.Repo, mode), - Sender: convert.ToUser(review.Reviewer, false, false), + Sender: convert.ToUser(review.Reviewer, nil), Review: &api.ReviewPayload{ Type: string(reviewHookType), Content: review.Content, @@ -693,7 +693,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(pr *models.PullRequest, review } func (m *webhookNotifier) NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { - apiPusher := convert.ToUser(pusher, false, false) + apiPusher := convert.ToUser(pusher, nil) apiRepo := convert.ToRepo(repo, models.AccessModeNone) refName := git.RefEndName(refFullName) @@ -737,14 +737,14 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(doer *models.User, pr *m Index: pr.Issue.Index, PullRequest: convert.ToAPIPullRequest(pr), Repository: convert.ToRepo(pr.Issue.Repo, models.AccessModeNone), - Sender: convert.ToUser(doer, false, false), + Sender: convert.ToUser(doer, nil), }); err != nil { log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err) } } func (m *webhookNotifier) NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { - apiPusher := convert.ToUser(pusher, false, false) + apiPusher := convert.ToUser(pusher, nil) apiRepo := convert.ToRepo(repo, models.AccessModeNone) refName := git.RefEndName(refFullName) @@ -770,7 +770,7 @@ func sendReleaseHook(doer *models.User, rel *models.Release, action api.HookRele Action: action, Release: convert.ToRelease(rel), Repository: convert.ToRepo(rel.Repo, mode), - Sender: convert.ToUser(rel.Publisher, false, false), + Sender: convert.ToUser(rel.Publisher, nil), }); err != nil { log.Error("PrepareWebhooks: %v", err) } @@ -789,7 +789,7 @@ func (m *webhookNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Rel } func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { - apiPusher := convert.ToUser(pusher, false, false) + apiPusher := convert.ToUser(pusher, nil) apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) if err != nil { log.Error("commits.ToAPIPayloadCommits failed: %v", err) diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index 116c622048d..cbb9f6b21c4 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -116,7 +116,7 @@ func CreateUser(ctx *context.APIContext) { if form.SendNotify { mailer.SendRegisterNotifyMail(ctx.Locale, u) } - ctx.JSON(http.StatusCreated, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin)) + ctx.JSON(http.StatusCreated, convert.ToUser(u, ctx.User)) } // EditUser api for modifying a user's information @@ -238,7 +238,7 @@ func EditUser(ctx *context.APIContext) { } log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name) - ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin)) + ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.User)) } // DeleteUser api for deleting a user @@ -403,7 +403,7 @@ func GetAllUsers(ctx *context.APIContext) { results := make([]*api.User, len(users)) for i := range users { - results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin) + results[i] = convert.ToUser(users[i], ctx.User) } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) diff --git a/routers/api/v1/org/member.go b/routers/api/v1/org/member.go index 5f0e36386ef..09abad25572 100644 --- a/routers/api/v1/org/member.go +++ b/routers/api/v1/org/member.go @@ -32,7 +32,7 @@ func listMembers(ctx *context.APIContext, publicOnly bool) { apiMembers := make([]*api.User, len(members)) for i, member := range members { - apiMembers[i] = convert.ToUser(member, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) + apiMembers[i] = convert.ToUser(member, ctx.User) } ctx.JSON(http.StatusOK, apiMembers) diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index c749751ac4d..99bbd9eefee 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -337,7 +337,7 @@ func GetTeamMembers(ctx *context.APIContext) { } members := make([]*api.User, len(team.Members)) for i, member := range team.Members { - members[i] = convert.ToUser(member, ctx.IsSigned, ctx.User.IsAdmin) + members[i] = convert.ToUser(member, ctx.User) } ctx.JSON(http.StatusOK, members) } @@ -380,7 +380,7 @@ func GetTeamMember(ctx *context.APIContext) { ctx.NotFound() return } - ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User.IsAdmin)) + ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.User)) } // AddTeamMember api for add a member to a team diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index a4fc1d8f114..d0936019fad 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -54,7 +54,7 @@ func ListCollaborators(ctx *context.APIContext) { } users := make([]*api.User, len(collaborators)) for i, collaborator := range collaborators { - users[i] = convert.ToUser(collaborator.User, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) + users[i] = convert.ToUser(collaborator.User, ctx.User) } ctx.JSON(http.StatusOK, users) } diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 520a7a02027..5a0911544a0 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -148,8 +148,8 @@ func TestHook(ctx *context.APIContext) { convert.ToPayloadCommit(ctx.Repo.Repository, ctx.Repo.Commit), }, Repo: convert.ToRepo(ctx.Repo.Repository, models.AccessModeNone), - Pusher: convert.ToUser(ctx.User, ctx.IsSigned, false), - Sender: convert.ToUser(ctx.User, ctx.IsSigned, false), + Pusher: convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone), + Sender: convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone), }); err != nil { ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err) return diff --git a/routers/api/v1/repo/issue_reaction.go b/routers/api/v1/repo/issue_reaction.go index 3994c6b9413..d0ba8dac658 100644 --- a/routers/api/v1/repo/issue_reaction.go +++ b/routers/api/v1/repo/issue_reaction.go @@ -81,7 +81,7 @@ func GetIssueCommentReactions(ctx *context.APIContext) { var result []api.Reaction for _, r := range reactions { result = append(result, api.Reaction{ - User: convert.ToUser(r.User, ctx.IsSigned, false), + User: convert.ToUser(r.User, ctx.User), Reaction: r.Type, Created: r.CreatedUnix.AsTime(), }) @@ -203,7 +203,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp ctx.Error(http.StatusForbidden, err.Error(), err) } else if models.IsErrReactionAlreadyExist(err) { ctx.JSON(http.StatusOK, api.Reaction{ - User: convert.ToUser(ctx.User, true, true), + User: convert.ToUser(ctx.User, ctx.User), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) @@ -214,7 +214,7 @@ func changeIssueCommentReaction(ctx *context.APIContext, form api.EditReactionOp } ctx.JSON(http.StatusCreated, api.Reaction{ - User: convert.ToUser(ctx.User, true, true), + User: convert.ToUser(ctx.User, ctx.User), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) @@ -299,7 +299,7 @@ func GetIssueReactions(ctx *context.APIContext) { var result []api.Reaction for _, r := range reactions { result = append(result, api.Reaction{ - User: convert.ToUser(r.User, ctx.IsSigned, false), + User: convert.ToUser(r.User, ctx.User), Reaction: r.Type, Created: r.CreatedUnix.AsTime(), }) @@ -412,7 +412,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i ctx.Error(http.StatusForbidden, err.Error(), err) } else if models.IsErrReactionAlreadyExist(err) { ctx.JSON(http.StatusOK, api.Reaction{ - User: convert.ToUser(ctx.User, true, true), + User: convert.ToUser(ctx.User, ctx.User), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) @@ -423,7 +423,7 @@ func changeIssueReaction(ctx *context.APIContext, form api.EditReactionOption, i } ctx.JSON(http.StatusCreated, api.Reaction{ - User: convert.ToUser(ctx.User, true, true), + User: convert.ToUser(ctx.User, ctx.User), Reaction: reaction.Type, Created: reaction.CreatedUnix.AsTime(), }) diff --git a/routers/api/v1/repo/issue_subscription.go b/routers/api/v1/repo/issue_subscription.go index 2bbd72299ab..8acd378cc5e 100644 --- a/routers/api/v1/repo/issue_subscription.go +++ b/routers/api/v1/repo/issue_subscription.go @@ -279,7 +279,7 @@ func GetIssueSubscribers(ctx *context.APIContext) { } apiUsers := make([]*api.User, 0, len(users)) for i := range users { - apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, false) + apiUsers[i] = convert.ToUser(users[i], ctx.User) } ctx.JSON(http.StatusOK, apiUsers) diff --git a/routers/api/v1/repo/star.go b/routers/api/v1/repo/star.go index 774b326472a..3af0a4ac125 100644 --- a/routers/api/v1/repo/star.go +++ b/routers/api/v1/repo/star.go @@ -50,7 +50,7 @@ func ListStargazers(ctx *context.APIContext) { } users := make([]*api.User, len(stargazers)) for i, stargazer := range stargazers { - users[i] = convert.ToUser(stargazer, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) + users[i] = convert.ToUser(stargazer, ctx.User) } ctx.JSON(http.StatusOK, users) } diff --git a/routers/api/v1/repo/subscriber.go b/routers/api/v1/repo/subscriber.go index ac61710bd2b..37bf3c29d46 100644 --- a/routers/api/v1/repo/subscriber.go +++ b/routers/api/v1/repo/subscriber.go @@ -50,7 +50,7 @@ func ListSubscribers(ctx *context.APIContext) { } users := make([]*api.User, len(subscribers)) for i, subscriber := range subscribers { - users[i] = convert.ToUser(subscriber, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) + users[i] = convert.ToUser(subscriber, ctx.User) } ctx.JSON(http.StatusOK, users) } diff --git a/routers/api/v1/user/follower.go b/routers/api/v1/user/follower.go index 39f3c209273..4d316425cd0 100644 --- a/routers/api/v1/user/follower.go +++ b/routers/api/v1/user/follower.go @@ -18,7 +18,7 @@ import ( func responseAPIUsers(ctx *context.APIContext, users []*models.User) { apiUsers := make([]*api.User, len(users)) for i := range users { - apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) + apiUsers[i] = convert.ToUser(users[i], ctx.User) } ctx.JSON(http.StatusOK, &apiUsers) } diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index df8a11c61f1..780cdf417c0 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -25,13 +25,13 @@ func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defa apiKey.KeyType = "user" if defaultUser.ID == key.OwnerID { - apiKey.Owner = convert.ToUser(defaultUser, true, true) + apiKey.Owner = convert.ToUser(defaultUser, defaultUser) } else { user, err := models.GetUserByID(key.OwnerID) if err != nil { return apiKey, err } - apiKey.Owner = convert.ToUser(user, true, true) + apiKey.Owner = convert.ToUser(user, user) } } else { apiKey.KeyType = "unknown" diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index ecc149fe52d..6e811bf0f8a 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -75,7 +75,7 @@ func Search(ctx *context.APIContext) { results := make([]*api.User, len(users)) for i := range users { - results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) + results[i] = convert.ToUser(users[i], ctx.User) } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) @@ -112,7 +112,7 @@ func GetInfo(ctx *context.APIContext) { return } - ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.IsSigned, ctx.User != nil && (ctx.User.ID == u.ID || ctx.User.IsAdmin))) + ctx.JSON(http.StatusOK, convert.ToUser(u, ctx.User)) } // GetAuthenticatedUser get current user's information @@ -126,7 +126,7 @@ func GetAuthenticatedUser(ctx *context.APIContext) { // "200": // "$ref": "#/responses/User" - ctx.JSON(http.StatusOK, convert.ToUser(ctx.User, ctx.IsSigned, ctx.User != nil)) + ctx.JSON(http.StatusOK, convert.ToUser(ctx.User, ctx.User)) } // GetUserHeatmapData is the handler to get a users heatmap diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index d1d4dc02082..1a5090a24ec 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -1083,7 +1083,7 @@ func TestWebhook(ctx *context.Context) { } } - apiUser := convert.ToUser(ctx.User, true, true) + apiUser := convert.ToUserWithAccessMode(ctx.User, models.AccessModeNone) p := &api.PushPayload{ Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch, Before: commit.ID.String(),