debian-mirror-gitlab/app/controllers/concerns/spammable_actions.rb

74 lines
1.8 KiB
Ruby
Raw Normal View History

2016-09-13 17:45:13 +05:30
module SpammableActions
extend ActiveSupport::Concern
2017-08-17 22:00:37 +05:30
include Recaptcha::Verify
2016-09-13 17:45:13 +05:30
included do
before_action :authorize_submit_spammable!, only: :mark_as_spam
end
def mark_as_spam
if SpamService.new(spammable).mark_as_spam!
2017-09-10 17:25:29 +05:30
redirect_to spammable_path, notice: "#{spammable.spammable_entity_type.titlecase} was submitted to Akismet successfully."
2016-09-13 17:45:13 +05:30
else
2017-09-10 17:25:29 +05:30
redirect_to spammable_path, alert: 'Error with Akismet. Please check the logs for more info.'
2016-09-13 17:45:13 +05:30
end
end
private
2017-08-17 22:00:37 +05:30
def ensure_spam_config_loaded!
return @spam_config_loaded if defined?(@spam_config_loaded)
@spam_config_loaded = Gitlab::Recaptcha.load_configurations!
end
def recaptcha_check_with_fallback(&fallback)
if spammable.valid?
2017-09-10 17:25:29 +05:30
redirect_to spammable_path
2017-08-17 22:00:37 +05:30
elsif render_recaptcha?
ensure_spam_config_loaded!
if params[:recaptcha_verification]
flash[:alert] = 'There was an error with the reCAPTCHA. Please solve the reCAPTCHA again.'
end
render :verify
else
yield
end
end
def spammable_params
default_params = { request: request }
recaptcha_check = params[:recaptcha_verification] &&
ensure_spam_config_loaded! &&
verify_recaptcha
return default_params unless recaptcha_check
{ recaptcha_verified: true,
spam_log_id: params[:spam_log_id] }.merge(default_params)
end
2016-09-13 17:45:13 +05:30
def spammable
raise NotImplementedError, "#{self.class} does not implement #{__method__}"
end
2017-09-10 17:25:29 +05:30
def spammable_path
raise NotImplementedError, "#{self.class} does not implement #{__method__}"
end
2016-09-13 17:45:13 +05:30
def authorize_submit_spammable!
access_denied! unless current_user.admin?
end
2017-08-17 22:00:37 +05:30
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-09-13 17:45:13 +05:30
end