If an admin or moderator edits a visitor's post, the restrictions should be based

on the *editors* access rights, not the original poster.
This commit is contained in:
Robin Ward 2013-04-05 13:59:00 -04:00
parent ab85d4a757
commit af9b27358c
5 changed files with 52 additions and 6 deletions

View file

@ -123,8 +123,19 @@ class Post < ActiveRecord::Base
total
end
# Sometimes the post is being edited by someone else, for example, a mod.
# If that's the case, they should not be bound by the original poster's
# restrictions, for example on not posting images.
def acting_user
@acting_user || user
end
def acting_user=(pu)
@acting_user = pu
end
def max_mention_validator
if user.present? && user.has_trust_level?(:basic)
if acting_user.present? && acting_user.has_trust_level?(:basic)
errors.add(:base, I18n.t(:too_many_mentions, count: SiteSetting.max_mentions_per_post)) if raw_mentions.size > SiteSetting.max_mentions_per_post
else
errors.add(:base, I18n.t(:too_many_mentions_visitor, count: SiteSetting.visitor_max_mentions_per_post)) if raw_mentions.size > SiteSetting.visitor_max_mentions_per_post
@ -132,12 +143,12 @@ class Post < ActiveRecord::Base
end
def max_images_validator
return if user.present? && user.has_trust_level?(:basic)
return if acting_user.present? && acting_user.has_trust_level?(:basic)
errors.add(:base, I18n.t(:too_many_images, count: SiteSetting.visitor_max_images)) if image_count > SiteSetting.visitor_max_images
end
def max_links_validator
return if user.present? && user.has_trust_level?(:basic)
return if acting_user.present? && acting_user.has_trust_level?(:basic)
errors.add(:base, I18n.t(:too_many_links, count: SiteSetting.visitor_max_links)) if link_count > SiteSetting.visitor_max_links
end

View file

@ -4,7 +4,8 @@ require_dependency 'site_content_class_methods'
class SiteContent < ActiveRecord::Base
extend SiteContentClassMethods
set_primary_key :content_type
self.primary_key = 'content_type'
validates_presence_of :content
def self.formats

View file

@ -10,6 +10,8 @@ class PostRevisor
def revise!(user, new_raw, opts = {})
@user, @new_raw, @opts = user, new_raw, opts
return false if not should_revise?
@post.acting_user = @user
revise_post
update_category_description
post_process_post

View file

@ -4,10 +4,10 @@ require 'post_revisor'
describe PostRevisor do
let(:topic) { Fabricate(:topic) }
let(:post_args) { {user: topic.user, topic: topic} }
let(:visitor) { Fabricate(:visitor) }
let(:post_args) { {user: visitor, topic: topic} }
context 'revise' do
let(:post) { Fabricate(:post, post_args) }
let(:first_version_at) { post.last_version_at }
@ -186,6 +186,32 @@ describe PostRevisor do
end
end
describe "admin editing a visitor's post" do
let(:changed_by) { Fabricate(:admin) }
before do
SiteSetting.stubs(:too_many_images).returns(0)
subject.revise!(changed_by, "So, post them here!\nhttp://i.imgur.com/FGg7Vzu.gif")
end
it "allows an admin to insert images into a visitor's post" do
post.errors.should be_blank
end
end
describe "visitor editing their own post" do
before do
SiteSetting.stubs(:too_many_images).returns(0)
subject.revise!(post.user, "So, post them here!\nhttp://i.imgur.com/FGg7Vzu.gif")
end
it "allows an admin to insert images into a visitor's post" do
post.errors.should be_present
end
end
describe 'with a new body' do
let(:changed_by) { Fabricate(:coding_horror) }
let!(:result) { subject.revise!(changed_by, 'updated body') }

View file

@ -49,3 +49,9 @@ Fabricator(:another_admin, from: :user) do
admin true
end
Fabricator(:visitor, from: :user) do
name 'Newbie Newperson'
username 'newbie'
email 'newbie@new.com'
trust_level TrustLevel.levels[:visitor]
end