FIX: All admins should be allowed to see deleted PM posts regardless of their mod status (#30206)

Admins and moderators can see a user's deleted posts via the `/u/:username/deleted-posts` route. Admins can always see any post on the site, but that's not always the case for moderators, e.g., they can't see all PMs. So, this route accounts for that and excludes posts that a moderator wouldn't be allowed to see if they were not deleted.

However, there's currently a problem with that logic where admins who also have moderation privileges, are treated the same way as moderators and prevented from seeing posts that pure moderators can't see. This commit fixes that problem and only applies the permission checks to moderators who don't have admin privileges.

Internal topic: t/143107.
This commit is contained in:
Osama Sayegh 2024-12-23 12:48:03 +03:00 committed by GitHub
parent b7971e17c2
commit e2cd1da26d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View File

@ -814,7 +814,7 @@ class PostsController < ApplicationController
.order(created_at: :desc)
end
if guardian.user.moderator?
if guardian.user.moderator? && !guardian.user.admin?
# Awful hack, but you can't seem to remove the `default_scope` when joining
# So instead I grab the topics separately
topic_ids = posts.dup.pluck(:topic_id)

View File

@ -2492,6 +2492,21 @@ RSpec.describe PostsController do
expect(data.length).to eq(0)
end
it "returns PMs for admins who are also moderators" do
admin.update!(moderator: true)
pm_post = Fabricate(:private_message_post)
PostDestroyer.new(admin, pm_post).destroy
sign_in(admin)
get "/posts/#{pm_post.user.username}/deleted.json"
expect(response.status).to eq(200)
expect(response.parsed_body.size).to eq(1)
expect(response.parsed_body.first["id"]).to eq(pm_post.id)
end
it "only shows posts deleted by other users" do
create_post(user: user)
post_deleted_by_user = create_post(user: user)