Add min_trust_to_create_topic setting to require a certain trust level before users can start new topics

This commit is contained in:
Neil Lalonde 2013-09-03 19:12:22 -04:00
parent 212f1363ae
commit b47eedba00
5 changed files with 39 additions and 2 deletions

View file

@ -0,0 +1,18 @@
require_dependency 'enum_site_setting'
class MinTrustToCreateTopicSetting < EnumSiteSetting
def self.valid_value?(val)
valid_values.any? { |v| v.to_s == val.to_s }
end
def self.values
@values ||= valid_values.map {|x| {name: x.to_s, value: x} }
end
private
def self.valid_values
TrustLevel.levels.values.sort
end
end

View file

@ -205,6 +205,8 @@ class SiteSetting < ActiveRecord::Base
setting(:regular_requires_likes_given, 1)
setting(:regular_requires_topic_reply_count, 3)
setting(:min_trust_to_create_topic, 0, enum: 'MinTrustToCreateTopicSetting')
# Reply by Email Settings
setting(:reply_by_email_enabled, false)
setting(:reply_by_email_address, '')

View file

@ -611,6 +611,8 @@ en:
regular_requires_likes_given: "How many likes a basic user must cast before promotion to regular (2) trust level"
regular_requires_topic_reply_count: "How many topics a basic user must reply to before promotion to regular (2) trust level"
min_trust_to_create_topic: "The minimum trust level required to create a new topic."
newuser_max_links: "How many links a new user can add to a post"
newuser_max_images: "How many images a new user can add to a post"
newuser_max_attachments: "How many attachments a new user can add to a post"

View file

@ -229,11 +229,11 @@ class Guardian
end
def can_create_topic?(parent)
can_create_post?(parent)
user && user.trust_level >= SiteSetting.min_trust_to_create_topic.to_i && can_create_post?(parent)
end
def can_create_topic_on_category?(category)
can_create_post?(nil) && (
can_create_topic?(nil) && (
!category ||
Category.topic_create_allowed(self).where(:id => category.id).count == 1
)

View file

@ -283,6 +283,21 @@ describe Guardian do
category.save
Guardian.new(user).can_create?(Topic,category).should be_false
end
it "is true for new users by default" do
Guardian.new(user).can_create?(Topic,Fabricate(:category)).should be_true
end
it "is false if user has not met minimum trust level" do
SiteSetting.stubs(:min_trust_to_create_topic).returns(1)
Guardian.new(build(:user, trust_level: 0)).can_create?(Topic,Fabricate(:category)).should be_false
end
it "is true if user has met or exceeded the minimum trust level" do
SiteSetting.stubs(:min_trust_to_create_topic).returns(1)
Guardian.new(build(:user, trust_level: 1)).can_create?(Topic,Fabricate(:category)).should be_true
Guardian.new(build(:user, trust_level: 2)).can_create?(Topic,Fabricate(:category)).should be_true
end
end
describe 'a Post' do