2016-08-02 17:21:57 -04:00
|
|
|
module SpammableActions
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-01-27 11:25:39 -05:00
|
|
|
include Recaptcha::Verify
|
2017-11-17 07:27:16 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2016-08-02 17:21:57 -04:00
|
|
|
included do
|
|
|
|
before_action :authorize_submit_spammable!, only: :mark_as_spam
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_spam
|
2016-08-09 13:43:47 -04:00
|
|
|
if SpamService.new(spammable).mark_as_spam!
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to spammable_path, notice: "#{spammable.spammable_entity_type.titlecase} was submitted to Akismet successfully."
|
2016-08-02 17:21:57 -04:00
|
|
|
else
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to spammable_path, alert: 'Error with Akismet. Please check the logs for more info.'
|
2016-08-02 17:21:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-06-10 06:28:30 -04:00
|
|
|
def ensure_spam_config_loaded!
|
2017-11-17 07:27:16 -05:00
|
|
|
strong_memoize(:spam_config_loaded) do
|
|
|
|
Gitlab::Recaptcha.load_configurations!
|
|
|
|
end
|
2017-06-10 06:28:30 -04:00
|
|
|
end
|
|
|
|
|
2017-12-08 07:26:39 -05:00
|
|
|
def recaptcha_check_with_fallback(should_redirect = true, &fallback)
|
|
|
|
if should_redirect && spammable.valid?
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to spammable_path
|
2017-02-14 14:07:11 -05:00
|
|
|
elsif render_recaptcha?
|
2017-06-10 06:28:30 -04:00
|
|
|
ensure_spam_config_loaded!
|
|
|
|
|
2017-02-14 14:07:11 -05:00
|
|
|
if params[:recaptcha_verification]
|
|
|
|
flash[:alert] = 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'
|
|
|
|
end
|
|
|
|
|
2017-12-08 07:26:39 -05:00
|
|
|
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
|
2017-02-14 14:07:11 -05:00
|
|
|
else
|
2017-02-22 13:03:32 -05:00
|
|
|
yield
|
2017-02-14 14:07:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def spammable_params
|
|
|
|
default_params = { request: request }
|
|
|
|
|
|
|
|
recaptcha_check = params[:recaptcha_verification] &&
|
2017-06-10 06:28:30 -04:00
|
|
|
ensure_spam_config_loaded! &&
|
2017-02-14 14:07:11 -05:00
|
|
|
verify_recaptcha
|
|
|
|
|
|
|
|
return default_params unless recaptcha_check
|
2017-01-27 11:25:39 -05:00
|
|
|
|
2017-02-14 14:07:11 -05:00
|
|
|
{ recaptcha_verified: true,
|
|
|
|
spam_log_id: params[:spam_log_id] }.merge(default_params)
|
2017-01-27 11:25:39 -05:00
|
|
|
end
|
|
|
|
|
2016-08-02 17:21:57 -04:00
|
|
|
def spammable
|
2016-08-09 13:43:47 -04:00
|
|
|
raise NotImplementedError, "#{self.class} does not implement #{__method__}"
|
2016-08-02 17:21:57 -04:00
|
|
|
end
|
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
def spammable_path
|
|
|
|
raise NotImplementedError, "#{self.class} does not implement #{__method__}"
|
|
|
|
end
|
|
|
|
|
2016-08-02 17:21:57 -04:00
|
|
|
def authorize_submit_spammable!
|
|
|
|
access_denied! unless current_user.admin?
|
|
|
|
end
|
2017-01-27 11:25:39 -05:00
|
|
|
|
|
|
|
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
|
2016-08-02 17:21:57 -04:00
|
|
|
end
|