2018-08-02 00:09:29 +02:00
|
|
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Jeremy Lecour <jlecour@evolix.fr>, 2018 Evolix <info@evolix.fr>
|
|
|
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
|
|
|
|
2018-08-30 18:40:23 +02:00
|
|
|
class ChecksController < ApplicationController # rubocop:disable Metrics/ClassLength
|
2018-05-29 13:11:22 +02:00
|
|
|
before_action :authenticate_user!
|
2018-08-29 16:42:00 +02:00
|
|
|
before_action :set_check, except: [:index, :new, :create, :supports]
|
2018-05-29 13:11:22 +02:00
|
|
|
after_action :verify_authorized, except: :index
|
|
|
|
after_action :verify_policy_scoped, only: :index
|
|
|
|
|
2018-07-03 20:11:52 +02:00
|
|
|
has_scope :kind
|
|
|
|
has_scope :by_domain
|
2018-07-24 14:57:26 +02:00
|
|
|
has_scope :recurrent_failures, type: :boolean do |_controller, scope, _value|
|
|
|
|
scope.consecutive_failures(Rails.configuration.chexpire.interface.consecutive_failures_as_error)
|
|
|
|
end
|
2018-07-03 20:11:52 +02:00
|
|
|
|
2018-05-29 13:11:22 +02:00
|
|
|
def index
|
2018-07-05 10:24:26 +02:00
|
|
|
@checks = apply_scopes(policy_scope(Check)).order(Hash[*current_sort]).page(params[:page])
|
2018-05-29 13:11:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@check = Check.new
|
|
|
|
authorize @check
|
2018-07-03 17:38:06 +02:00
|
|
|
|
|
|
|
if params[:kind].present?
|
|
|
|
return not_found unless Check.kinds.key?(params[:kind])
|
|
|
|
@check.kind = params[:kind]
|
|
|
|
end
|
|
|
|
|
|
|
|
build_empty_notification
|
2018-05-29 13:11:22 +02:00
|
|
|
end
|
|
|
|
|
2018-08-30 18:40:23 +02:00
|
|
|
def create # rubocop:disable Metrics/AbcSize
|
|
|
|
@check = Check.new(new_check_params.merge(user: current_user))
|
2018-05-29 13:11:22 +02:00
|
|
|
authorize @check
|
|
|
|
|
|
|
|
if @check.save
|
2018-08-31 10:06:16 +02:00
|
|
|
flash[:notice] = t("checks.created", scope: :flashes)
|
2018-05-29 13:11:22 +02:00
|
|
|
redirect_to checks_path
|
|
|
|
else
|
2018-08-31 10:06:16 +02:00
|
|
|
flash.now[:alert] = t("checks.invalid", scope: :flashes)
|
2018-08-30 18:40:23 +02:00
|
|
|
|
|
|
|
fill_or_build_new_notification
|
2018-05-29 13:11:22 +02:00
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-04 20:39:53 +02:00
|
|
|
def edit
|
|
|
|
build_empty_notification
|
|
|
|
end
|
2018-05-29 13:11:22 +02:00
|
|
|
|
|
|
|
def update
|
|
|
|
if @check.update(update_check_params)
|
2018-08-31 10:06:16 +02:00
|
|
|
flash[:notice] = t("checks.updated", scope: :flashes)
|
2018-05-29 13:11:22 +02:00
|
|
|
redirect_to checks_path
|
|
|
|
else
|
2018-08-31 10:06:16 +02:00
|
|
|
flash.now[:alert] = t("checks.invalid", scope: :flashes)
|
2018-08-30 18:40:23 +02:00
|
|
|
|
|
|
|
fill_or_build_new_notification
|
2018-05-29 13:11:22 +02:00
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@check.destroy!
|
|
|
|
|
2018-08-31 10:06:16 +02:00
|
|
|
flash[:notice] = t("checks.destroyed", scope: :flashes)
|
2018-05-29 13:11:22 +02:00
|
|
|
redirect_to checks_path
|
|
|
|
end
|
|
|
|
|
2018-08-29 16:42:00 +02:00
|
|
|
def supports
|
|
|
|
@check = Check.new(new_check_params)
|
|
|
|
authorize @check
|
|
|
|
end
|
|
|
|
|
2018-05-29 13:11:22 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_check
|
|
|
|
@check = Check.find(params[:id])
|
|
|
|
authorize @check
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_check_params
|
|
|
|
check_params(:kind)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_check_params
|
|
|
|
check_params(:active)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_params(*others)
|
2018-08-30 18:40:23 +02:00
|
|
|
permitted = params.require(:check)
|
|
|
|
.permit(:domain, :domain_expires_at, :comment, :vendor,
|
|
|
|
:round_robin, *others,
|
|
|
|
notification_ids: [],
|
|
|
|
notifications_attributes: [:channel, :label, :recipient, :interval])
|
|
|
|
|
|
|
|
merge_current_user!(permitted)
|
|
|
|
|
|
|
|
permitted
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_current_user!(permitted)
|
|
|
|
return unless permitted[:notifications_attributes].present?
|
|
|
|
|
|
|
|
permitted[:notifications_attributes].each_pair do |_key, attributes|
|
|
|
|
attributes.merge!(user: current_user)
|
|
|
|
end
|
2018-06-04 20:39:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_empty_notification
|
2018-08-30 18:40:23 +02:00
|
|
|
@new_notification = @check.notifications.build
|
|
|
|
@new_notification.recipient = current_user.email
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_or_build_new_notification
|
|
|
|
last_notification = @check.notifications.last
|
|
|
|
|
|
|
|
# user has filled a new notification: we use it for the form
|
2018-08-31 14:40:34 +02:00
|
|
|
if last_notification.present? && last_notification.new_record?
|
2018-08-30 18:40:23 +02:00
|
|
|
@new_notification = last_notification
|
|
|
|
else # otherwise, set a new empty notification
|
|
|
|
build_empty_notification
|
|
|
|
end
|
2018-05-29 13:11:22 +02:00
|
|
|
end
|
2018-07-03 20:11:52 +02:00
|
|
|
|
|
|
|
def current_sort
|
|
|
|
@current_sort ||= clean_sort || Check.default_sort
|
|
|
|
end
|
|
|
|
helper_method :current_sort
|
|
|
|
|
|
|
|
def clean_sort
|
|
|
|
return unless params[:sort].present?
|
|
|
|
field, _, direction = params[:sort].rpartition("_").map(&:to_sym)
|
|
|
|
|
|
|
|
valid_fields = [:domain, :domain_expires_at]
|
|
|
|
valid_directions = [:asc, :desc]
|
|
|
|
|
|
|
|
return unless valid_fields.include?(field)
|
|
|
|
return unless valid_directions.include?(direction)
|
|
|
|
|
2018-07-05 10:24:26 +02:00
|
|
|
[field, direction]
|
2018-07-03 20:11:52 +02:00
|
|
|
end
|
2018-05-29 13:11:22 +02:00
|
|
|
end
|