mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-04-26 06:04:46 +00:00
Prevent reporting comments that do not have content support.
This commit is contained in:
parent
459fa4668b
commit
960b2caf9e
1 changed files with 12 additions and 3 deletions
|
@ -22,16 +22,17 @@ var (
|
||||||
ErrDoerNotAllowed = errors.New("doer not allowed to access the content to be reported")
|
ErrDoerNotAllowed = errors.New("doer not allowed to access the content to be reported")
|
||||||
)
|
)
|
||||||
|
|
||||||
// CanReport checks if doer has access to the content they are reporting (repository, issue, pull request or comment).
|
// CanReport checks if doer has access to the content they are reporting
|
||||||
|
// (user, organization, repository, issue, pull request or comment).
|
||||||
// When reporting repositories the user should have at least read access to any repo unit type.
|
// When reporting repositories the user should have at least read access to any repo unit type.
|
||||||
// When reporting issues, pull requests or comments the user should have at least read access
|
// When reporting issues, pull requests or comments the user should have at least read access
|
||||||
// to 'TypeIssues', respectively 'TypePullRequests' unit for the repository where the content belongs.
|
// to 'TypeIssues', respectively 'TypePullRequests' unit for the repository where the content belongs.
|
||||||
// When reporting users or organizations doer should be able to view the reported user.
|
// When reporting users or organizations doer should be able to view the reported entity.
|
||||||
func CanReport(ctx context.Context, doer *user.User, contentType moderation.ReportedContentType, contentID int64) (bool, error) {
|
func CanReport(ctx context.Context, doer *user.User, contentType moderation.ReportedContentType, contentID int64) (bool, error) {
|
||||||
var hasAccess bool = false
|
var hasAccess bool = false
|
||||||
var issueID int64 = 0
|
var issueID int64 = 0
|
||||||
var repoID int64 = 0
|
var repoID int64 = 0
|
||||||
var unitType unit.Type = unit.TypeInvalid
|
var unitType unit.Type = unit.TypeInvalid // used when checking access for issues, pull requests or comments
|
||||||
|
|
||||||
if contentType == moderation.ReportedContentTypeUser {
|
if contentType == moderation.ReportedContentTypeUser {
|
||||||
reported_user, err := user.GetUserByID(ctx, contentID)
|
reported_user, err := user.GetUserByID(ctx, contentID)
|
||||||
|
@ -45,6 +46,7 @@ func CanReport(ctx context.Context, doer *user.User, contentType moderation.Repo
|
||||||
|
|
||||||
hasAccess = user.IsUserVisibleToViewer(ctx, reported_user, ctx.Doer)
|
hasAccess = user.IsUserVisibleToViewer(ctx, reported_user, ctx.Doer)
|
||||||
} else {
|
} else {
|
||||||
|
// for comments and issues/pulls we need to get the parent repository
|
||||||
if contentType == moderation.ReportedContentTypeComment {
|
if contentType == moderation.ReportedContentTypeComment {
|
||||||
comment, err := issues.GetCommentByID(ctx, contentID)
|
comment, err := issues.GetCommentByID(ctx, contentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -54,6 +56,11 @@ func CanReport(ctx context.Context, doer *user.User, contentType moderation.Repo
|
||||||
}
|
}
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
if !comment.Type.HasContentSupport() {
|
||||||
|
// this is not a comment with text and/or attachments
|
||||||
|
log.Warn("User #%d wanted to report comment #%d but it is not a comment with content.", doer.ID, contentID)
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
issueID = comment.IssueID
|
issueID = comment.IssueID
|
||||||
} else if contentType == moderation.ReportedContentTypeIssue {
|
} else if contentType == moderation.ReportedContentTypeIssue {
|
||||||
issueID = contentID
|
issueID = contentID
|
||||||
|
@ -90,6 +97,7 @@ func CanReport(ctx context.Context, doer *user.User, contentType moderation.Repo
|
||||||
}
|
}
|
||||||
|
|
||||||
if issueID > 0 {
|
if issueID > 0 {
|
||||||
|
// for comments and issues/pulls doer should have at least read access to the corresponding repo unit (issues, respectively pull requests)
|
||||||
hasAccess, err = access_model.HasAccessUnit(ctx, doer, repo, unitType, perm.AccessModeRead)
|
hasAccess, err = access_model.HasAccessUnit(ctx, doer, repo, unitType, perm.AccessModeRead)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
@ -98,6 +106,7 @@ func CanReport(ctx context.Context, doer *user.User, contentType moderation.Repo
|
||||||
return false, ErrDoerNotAllowed
|
return false, ErrDoerNotAllowed
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// for repositories doer should have at least read access to at least one repo unit
|
||||||
perm, err := access_model.GetUserRepoPermission(ctx, repo, doer)
|
perm, err := access_model.GetUserRepoPermission(ctx, repo, doer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
|
Loading…
Reference in a new issue