spammable_actions.rb 2.24 KB
Newer Older
1 2 3
module SpammableActions
  extend ActiveSupport::Concern

4 5
  include Recaptcha::Verify

6 7 8 9 10
  included do
    before_action :authorize_submit_spammable!, only: :mark_as_spam
  end

  def mark_as_spam
11
    if SpamService.new(spammable).mark_as_spam!
12
      redirect_to spammable_path, notice: "#{spammable.spammable_entity_type.titlecase} was submitted to Akismet successfully."
13
    else
14
      redirect_to spammable_path, alert: 'Error with Akismet. Please check the logs for more info.'
15 16 17 18 19
    end
  end

  private

20 21 22 23 24 25
  def ensure_spam_config_loaded!
    return @spam_config_loaded if defined?(@spam_config_loaded)

    @spam_config_loaded = Gitlab::Recaptcha.load_configurations!
  end

26 27
  def recaptcha_check_with_fallback(should_redirect = true, &fallback)
    if should_redirect && spammable.valid?
28
      redirect_to spammable_path
29
    elsif render_recaptcha?
30 31
      ensure_spam_config_loaded!

32 33 34 35
      if params[:recaptcha_verification]
        flash[:alert] = 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'
      end

36 37 38 39 40 41 42 43 44 45 46 47
      respond_to do |format|
        format.html do
          render :verify
        end

        format.json do
          locals = { spammable: spammable, script: false, has_submit: false }
          recaptcha_html = render_to_string(partial: 'shared/recaptcha_form', formats: :html, locals: locals)

          render json: { recaptcha_html: recaptcha_html }
        end
      end
48
    else
Douwe Maan's avatar
Douwe Maan committed
49
      yield
50 51 52 53 54 55 56
    end
  end

  def spammable_params
    default_params = { request: request }

    recaptcha_check = params[:recaptcha_verification] &&
57
      ensure_spam_config_loaded! &&
58 59 60
      verify_recaptcha

    return default_params unless recaptcha_check
61

62 63
    { recaptcha_verified: true,
      spam_log_id: params[:spam_log_id] }.merge(default_params)
64 65
  end

66
  def spammable
67
    raise NotImplementedError, "#{self.class} does not implement #{__method__}"
68 69
  end

70 71 72 73
  def spammable_path
    raise NotImplementedError, "#{self.class} does not implement #{__method__}"
  end

74 75 76
  def authorize_submit_spammable!
    access_denied! unless current_user.admin?
  end
77 78 79 80 81 82 83

  def render_recaptcha?
    return false if spammable.errors.count > 1 # re-render "new" template in case there are other errors
    return false unless Gitlab::Recaptcha.enabled?

    spammable.spam
  end
84
end