BigW Consortium Gitlab

spammable_actions.rb 1.54 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, notice: "#{spammable.spammable_entity_type.titlecase} was submitted to Akismet successfully."
13
    else
14
      redirect_to spammable, 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 26 27 28 29
  def recaptcha_check_with_fallback(&fallback)
    if spammable.valid?
      redirect_to spammable
    elsif render_recaptcha?
      if params[:recaptcha_verification]
        flash[:alert] = 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'
      end

      render :verify
    else
Douwe Maan committed
30
      yield
31 32 33 34 35 36 37 38 39 40 41
    end
  end

  def spammable_params
    default_params = { request: request }

    recaptcha_check = params[:recaptcha_verification] &&
      Gitlab::Recaptcha.load_configurations! &&
      verify_recaptcha

    return default_params unless recaptcha_check
42

43 44
    { recaptcha_verified: true,
      spam_log_id: params[:spam_log_id] }.merge(default_params)
45 46
  end

47
  def spammable
48
    raise NotImplementedError, "#{self.class} does not implement #{__method__}"
49 50 51 52 53
  end

  def authorize_submit_spammable!
    access_denied! unless current_user.admin?
  end
54 55 56 57 58 59 60

  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
61
end