mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-05-01 16:34:14 -04:00
FIX: admins bypass some post validations. This allows them to edit legal docs even if those docs are longer than max post length, for example.
This commit is contained in:
parent
81ff121fe8
commit
e1be478ef4
4 changed files with 67 additions and 9 deletions
app/assets/javascripts/discourse/models
lib/validators
spec/components/validators
test/javascripts/models
|
@ -135,6 +135,7 @@ Discourse.Composer = Discourse.Model.extend({
|
||||||
@property titleLengthValid
|
@property titleLengthValid
|
||||||
**/
|
**/
|
||||||
titleLengthValid: function() {
|
titleLengthValid: function() {
|
||||||
|
if (Discourse.User.currentProp('admin') && this.get('titleLength') > 0) return true;
|
||||||
if (this.get('titleLength') < this.get('minimumTitleLength')) return false;
|
if (this.get('titleLength') < this.get('minimumTitleLength')) return false;
|
||||||
return (this.get('titleLength') <= Discourse.SiteSettings.max_topic_title_length);
|
return (this.get('titleLength') <= Discourse.SiteSettings.max_topic_title_length);
|
||||||
}.property('minimumTitleLength', 'titleLength'),
|
}.property('minimumTitleLength', 'titleLength'),
|
||||||
|
|
|
@ -3,14 +3,16 @@ module Validators; end
|
||||||
class Validators::PostValidator < ActiveModel::Validator
|
class Validators::PostValidator < ActiveModel::Validator
|
||||||
def validate(record)
|
def validate(record)
|
||||||
presence(record)
|
presence(record)
|
||||||
stripped_length(record)
|
unless record.acting_user.try(:admin?)
|
||||||
raw_quality(record)
|
stripped_length(record)
|
||||||
max_posts_validator(record)
|
raw_quality(record)
|
||||||
max_mention_validator(record)
|
max_posts_validator(record)
|
||||||
max_images_validator(record)
|
max_mention_validator(record)
|
||||||
max_attachments_validator(record)
|
max_images_validator(record)
|
||||||
max_links_validator(record)
|
max_attachments_validator(record)
|
||||||
unique_post_validator(record)
|
max_links_validator(record)
|
||||||
|
unique_post_validator(record)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def presence(post)
|
def presence(post)
|
||||||
|
|
|
@ -86,4 +86,22 @@ describe Validators::PostValidator do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "acting_user is an admin" do
|
||||||
|
before do
|
||||||
|
post.acting_user = Fabricate(:admin)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "skips most validations" do
|
||||||
|
validator.expects(:stripped_length).never
|
||||||
|
validator.expects(:raw_quality).never
|
||||||
|
validator.expects(:max_posts_validator).never
|
||||||
|
validator.expects(:max_mention_validator).never
|
||||||
|
validator.expects(:max_images_validator).never
|
||||||
|
validator.expects(:max_attachments_validator).never
|
||||||
|
validator.expects(:max_links_validator).never
|
||||||
|
validator.expects(:unique_post_validator).never
|
||||||
|
validator.validate(post)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
module("Discourse.Composer");
|
module("Discourse.Composer", {
|
||||||
|
setup: function() {
|
||||||
|
sinon.stub(Discourse.User, 'currentProp').withArgs('admin').returns(false);
|
||||||
|
},
|
||||||
|
|
||||||
|
teardown: function() {
|
||||||
|
Discourse.User.currentProp.restore();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test('replyLength', function() {
|
test('replyLength', function() {
|
||||||
var replyLength = function(val, expectedLength) {
|
var replyLength = function(val, expectedLength) {
|
||||||
|
@ -231,3 +239,32 @@ test('open with a quote', function() {
|
||||||
equal(new_composer().get('originalText'), quote, "originalText is the quote" );
|
equal(new_composer().get('originalText'), quote, "originalText is the quote" );
|
||||||
equal(new_composer().get('replyDirty'), false, "replyDirty is initally false with a quote" );
|
equal(new_composer().get('replyDirty'), false, "replyDirty is initally false with a quote" );
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
module("Discourse.Composer as admin", {
|
||||||
|
setup: function() {
|
||||||
|
sinon.stub(Discourse.User, 'currentProp').withArgs('admin').returns(true);
|
||||||
|
},
|
||||||
|
|
||||||
|
teardown: function() {
|
||||||
|
Discourse.User.currentProp.restore();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Title length for regular topics as admin", function() {
|
||||||
|
Discourse.SiteSettings.min_topic_title_length = 5;
|
||||||
|
Discourse.SiteSettings.max_topic_title_length = 10;
|
||||||
|
var composer = Discourse.Composer.create();
|
||||||
|
|
||||||
|
composer.set('title', 'asdf');
|
||||||
|
ok(composer.get('titleLengthValid'), "admins can use short titles");
|
||||||
|
|
||||||
|
composer.set('title', 'this is a long title');
|
||||||
|
ok(composer.get('titleLengthValid'), "admins can use long titles");
|
||||||
|
|
||||||
|
composer.set('title', 'just right');
|
||||||
|
ok(composer.get('titleLengthValid'), "in the range is okay");
|
||||||
|
|
||||||
|
composer.set('title', '');
|
||||||
|
ok(!composer.get('titleLengthValid'), "admins must set title to at least 1 character");
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue