mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
FIX: Validate poll arguments. (#6740)
* FIX: Validate number poll. * FEATURE: Poll's min can be 0. * FIX: Fix URL to user profile.
This commit is contained in:

committed by
Régis Hanol

parent
0fca3205b5
commit
e49bcebb35
@ -1,5 +1,8 @@
|
||||
module DiscoursePoll
|
||||
class PollsValidator
|
||||
|
||||
MAX_VALUE = 2_147_483_647
|
||||
|
||||
def initialize(post)
|
||||
@post = post
|
||||
end
|
||||
@ -8,6 +11,8 @@ module DiscoursePoll
|
||||
polls = {}
|
||||
|
||||
DiscoursePoll::Poll::extract(@post.raw, @post.topic_id, @post.user_id).each do |poll|
|
||||
return false unless valid_arguments?(poll)
|
||||
return false unless valid_numbers?(poll)
|
||||
return false unless unique_poll_name?(polls, poll)
|
||||
return false unless unique_options?(poll)
|
||||
return false unless at_least_two_options?(poll)
|
||||
@ -21,6 +26,27 @@ module DiscoursePoll
|
||||
|
||||
private
|
||||
|
||||
def valid_arguments?(poll)
|
||||
valid = true
|
||||
|
||||
if poll["type"].present? && !::Poll.types.has_key?(poll["type"])
|
||||
@post.errors.add(:base, I18n.t("poll.invalid_argument", argument: "type", value: poll["type"]))
|
||||
valid = false
|
||||
end
|
||||
|
||||
if poll["status"].present? && !::Poll.statuses.has_key?(poll["status"])
|
||||
@post.errors.add(:base, I18n.t("poll.invalid_argument", argument: "status", value: poll["status"]))
|
||||
valid = false
|
||||
end
|
||||
|
||||
if poll["results"].present? && !::Poll.results.has_key?(poll["results"])
|
||||
@post.errors.add(:base, I18n.t("poll.invalid_argument", argument: "results", value: poll["results"]))
|
||||
valid = false
|
||||
end
|
||||
|
||||
valid
|
||||
end
|
||||
|
||||
def unique_poll_name?(polls, poll)
|
||||
if polls.has_key?(poll["name"])
|
||||
if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME
|
||||
@ -96,5 +122,45 @@ module DiscoursePoll
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def valid_numbers?(poll)
|
||||
return true if poll["type"] != "number"
|
||||
|
||||
valid = true
|
||||
|
||||
min = poll["min"].to_i
|
||||
max = (poll["max"].presence || MAX_VALUE).to_i
|
||||
step = (poll["step"].presence || 1).to_i
|
||||
|
||||
if min < 0
|
||||
@post.errors.add(:base, "Min #{I18n.t("errors.messages.greater_than", count: 0)}")
|
||||
valid = false
|
||||
elsif min > MAX_VALUE
|
||||
@post.errors.add(:base, "Min #{I18n.t("errors.messages.less_than", count: MAX_VALUE)}")
|
||||
valid = false
|
||||
end
|
||||
|
||||
if max < min
|
||||
@post.errors.add(:base, "Max #{I18n.t("errors.messages.greater_than", count: "min")}")
|
||||
valid = false
|
||||
elsif max > MAX_VALUE
|
||||
@post.errors.add(:base, "Max #{I18n.t("errors.messages.less_than", count: MAX_VALUE)}")
|
||||
valid = false
|
||||
end
|
||||
|
||||
if step <= 0
|
||||
@post.errors.add(:base, "Step #{I18n.t("errors.messages.greater_than", count: 0)}")
|
||||
valid = false
|
||||
elsif ((max - min + 1) / step) < 2
|
||||
if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME
|
||||
@post.errors.add(:base, I18n.t("poll.default_poll_must_have_at_least_2_options"))
|
||||
else
|
||||
@post.errors.add(:base, I18n.t("poll.named_poll_must_have_at_least_2_options", name: poll["name"]))
|
||||
end
|
||||
valid = false
|
||||
end
|
||||
|
||||
valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user