diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb index 25a5c644ea..ce3864ecc8 100644 --- a/lib/guardian/post_guardian.rb +++ b/lib/guardian/post_guardian.rb @@ -43,7 +43,7 @@ module PostGuardian # post made by staff, but we don't allow staff flags return false if is_flag && (!SiteSetting.allow_flagging_staff?) && - post.user.staff? + post&.user&.staff? if [:notify_user, :notify_moderators].include?(action_key) && (!SiteSetting.enable_personal_messages? || diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb index 5a91e78463..bddb589031 100644 --- a/spec/components/guardian_spec.rb +++ b/spec/components/guardian_spec.rb @@ -120,16 +120,26 @@ describe Guardian do expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to be_truthy end - it "doesn't allow flagging of staff posts when allow_flagging_staff is false" do - SiteSetting.allow_flagging_staff = false - staff_post = Fabricate(:post, user: Fabricate(:moderator)) - expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(false) - end + describe 'when allow_flagging_staff is false' do + let(:staff_post) { Fabricate(:post, user: Fabricate(:moderator)) } - it "allows liking of staff when allow_flagging_staff is false" do - SiteSetting.allow_flagging_staff = false - staff_post = Fabricate(:post, user: Fabricate(:moderator)) - expect(Guardian.new(user).post_can_act?(staff_post, :like)).to eq(true) + before do + SiteSetting.allow_flagging_staff = false + end + + it "doesn't allow flagging of staff posts" do + expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(false) + end + + it "allows flagging of staff posts when staff has been deleted" do + staff_post.user.destroy! + staff_post.reload + expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(true) + end + + it "allows liking of staff" do + expect(Guardian.new(user).post_can_act?(staff_post, :like)).to eq(true) + end end it "returns false when liking yourself" do