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

97 lines
2.4 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
2017-08-17 22:00:37 +05:30
attr_accessor :spam_log
2018-03-17 18:26:18 +05:30
alias_method :spam?, :spam
2016-09-13 17:45:13 +05:30
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!
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
2020-04-08 14:13:33 +05:30
def invalidate_if_spam
2020-04-22 19:07:51 +05:30
return unless spam?
if Gitlab::Recaptcha.enabled?
needs_recaptcha!
else
unrecoverable_spam_error!
end
2017-08-17 22:00:37 +05:30
end
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