debian-mirror-gitlab/app/models/concerns/spammable.rb

123 lines
3.1 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module Spammable
extend ActiveSupport::Concern
2018-11-20 20:47:30 +05:30
class_methods do
2016-09-13 17:45:13 +05:30
def attr_spammable(attr, options = {})
spammable_attrs << [attr.to_s, options]
end
end
included do
2017-09-10 17:25:29 +05:30
has_one :user_agent_detail, as: :subject, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
2016-09-13 17:45:13 +05:30
attr_accessor :spam
2020-05-24 23:13:21 +05:30
attr_accessor :needs_recaptcha
2017-08-17 22:00:37 +05:30
attr_accessor :spam_log
2020-05-24 23:13:21 +05:30
2018-03-17 18:26:18 +05:30
alias_method :spam?, :spam
2020-05-24 23:13:21 +05:30
alias_method :needs_recaptcha?, :needs_recaptcha
2016-09-13 17:45:13 +05:30
2020-05-24 23:13:21 +05:30
# if spam errors are added before validation, they will be wiped
2020-04-08 14:13:33 +05:30
after_validation :invalidate_if_spam, on: [:create, :update]
2016-09-13 17:45:13 +05:30
cattr_accessor :spammable_attrs, instance_accessor: false do
[]
end
delegate :ip_address, :user_agent, to: :user_agent_detail, allow_nil: true
end
2017-08-17 22:00:37 +05:30
def submittable_as_spam_by?(current_user)
current_user && current_user.admin? && submittable_as_spam?
end
2016-09-13 17:45:13 +05:30
def submittable_as_spam?
if user_agent_detail
2018-03-17 18:26:18 +05:30
user_agent_detail.submittable? && Gitlab::CurrentSettings.current_application_settings.akismet_enabled
2016-09-13 17:45:13 +05:30
else
false
end
end
2020-04-22 19:07:51 +05:30
def needs_recaptcha!
2020-05-24 23:13:21 +05:30
self.needs_recaptcha = true
2020-04-22 19:07:51 +05:30
end
2021-03-11 19:13:27 +05:30
##
# Indicates if a recaptcha should be rendered before allowing this model to be saved.
#
def render_recaptcha?
return false unless Gitlab::Recaptcha.enabled?
return false if self.errors.count > 1 # captcha should not be rendered if are still other errors
self.needs_recaptcha?
end
2020-05-24 23:13:21 +05:30
def spam!
self.spam = true
2020-04-22 19:07:51 +05:30
end
2020-05-24 23:13:21 +05:30
def clear_spam_flags!
self.spam = false
self.needs_recaptcha = false
end
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
def invalidate_if_spam
if needs_recaptcha? && Gitlab::Recaptcha.enabled?
recaptcha_error!
elsif needs_recaptcha? || spam?
2020-04-22 19:07:51 +05:30
unrecoverable_spam_error!
end
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
def recaptcha_error!
self.errors.add(:base, "Your #{spammable_entity_type} has been recognized as spam. "\
"Please, change the content or solve the reCAPTCHA to proceed.")
end
def unrecoverable_spam_error!
self.errors.add(:base, "Your #{spammable_entity_type} has been recognized as spam and has been discarded.")
end
2017-08-17 22:00:37 +05:30
def spammable_entity_type
self.class.name.underscore
2016-09-13 17:45:13 +05:30
end
def spam_title
attr = self.class.spammable_attrs.find do |_, options|
options.fetch(:spam_title, false)
end
2018-03-17 18:26:18 +05:30
public_send(attr.first) if attr && respond_to?(attr.first.to_sym) # rubocop:disable GitlabSecurity/PublicSend
2016-09-13 17:45:13 +05:30
end
def spam_description
attr = self.class.spammable_attrs.find do |_, options|
options.fetch(:spam_description, false)
end
2018-03-17 18:26:18 +05:30
public_send(attr.first) if attr && respond_to?(attr.first.to_sym) # rubocop:disable GitlabSecurity/PublicSend
2016-09-13 17:45:13 +05:30
end
def spammable_text
result = self.class.spammable_attrs.map do |attr|
2018-03-17 18:26:18 +05:30
public_send(attr.first) # rubocop:disable GitlabSecurity/PublicSend
2016-09-13 17:45:13 +05:30
end
result.reject(&:blank?).join("\n")
end
# Override in Spammable if further checks are necessary
def check_for_spam?
true
end
2019-12-21 20:55:43 +05:30
# Override in Spammable if differs
def allow_possible_spam?
Feature.enabled?(:allow_possible_spam, project)
end
2016-09-13 17:45:13 +05:30
end