debian-mirror-gitlab/app/services/application_settings/update_service.rb

124 lines
3.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module ApplicationSettings
class UpdateService < ApplicationSettings::BaseService
2019-07-07 11:18:12 +05:30
include ValidatesClassificationLabel
2018-11-08 19:23:39 +05:30
attr_reader :params, :application_setting
2019-09-04 21:01:54 +05:30
MARKDOWN_CACHE_INVALIDATING_PARAMS = %w(asset_proxy_enabled asset_proxy_url asset_proxy_secret_key asset_proxy_whitelist).freeze
2017-08-17 22:00:37 +05:30
def execute
2021-02-22 17:27:13 +05:30
result = update_settings
auto_approve_blocked_users if result
result
end
private
def update_settings
2019-12-04 20:38:33 +05:30
validate_classification_label(application_setting, :external_authorization_service_default_label) unless bypass_external_auth?
2019-07-07 11:18:12 +05:30
if application_setting.errors.any?
return false
end
2018-10-15 14:42:47 +05:30
update_terms(@params.delete(:terms))
2019-10-12 21:52:04 +05:30
add_to_outbound_local_requests_whitelist(@params.delete(:add_to_outbound_local_requests_whitelist))
2018-11-08 19:23:39 +05:30
if params.key?(:performance_bar_allowed_group_path)
2019-12-21 20:55:43 +05:30
group_id = process_performance_bar_allowed_group_id
return false if application_setting.errors.any?
params[:performance_bar_allowed_group_id] = group_id
2018-11-08 19:23:39 +05:30
end
2018-11-20 20:47:30 +05:30
if usage_stats_updated? && !params.delete(:skip_usage_stats_user)
params[:usage_stats_set_by_user_id] = current_user.id
end
2019-09-04 21:01:54 +05:30
@application_setting.assign_attributes(params)
if invalidate_markdown_cache?
@application_setting[:local_markdown_version] = @application_setting.local_markdown_version + 1
end
@application_setting.save
2017-08-17 22:00:37 +05:30
end
2018-10-15 14:42:47 +05:30
2018-11-20 20:47:30 +05:30
def usage_stats_updated?
params.key?(:usage_ping_enabled) || params.key?(:version_check_enabled)
end
2019-10-12 21:52:04 +05:30
def add_to_outbound_local_requests_whitelist(values)
values_array = Array(values).reject(&:empty?)
return if values_array.empty?
@application_setting.add_to_outbound_local_requests_whitelist(values_array)
end
2019-09-04 21:01:54 +05:30
def invalidate_markdown_cache?
!params.key?(:local_markdown_version) &&
(@application_setting.changes.keys & MARKDOWN_CACHE_INVALIDATING_PARAMS).any?
end
2018-10-15 14:42:47 +05:30
def update_terms(terms)
return unless terms.present?
# Avoid creating a new terms record if the text is exactly the same.
terms = terms.strip
return if terms == @application_setting.terms
ApplicationSetting::Term.create(terms: terms)
@application_setting.reset_memoized_terms
end
2018-11-08 19:23:39 +05:30
2019-12-21 20:55:43 +05:30
def process_performance_bar_allowed_group_id
2018-11-08 19:23:39 +05:30
group_full_path = params.delete(:performance_bar_allowed_group_path)
2019-12-21 20:55:43 +05:30
enable_param_on = Gitlab::Utils.to_boolean(params.delete(:performance_bar_enabled))
performance_bar_enabled = enable_param_on.nil? || enable_param_on # Default to true
return if group_full_path.blank?
return if enable_param_on == false # Explicitly disabling
unless performance_bar_enabled
application_setting.errors.add(:performance_bar_allowed_group_id, 'not allowed when performance bar is disabled')
return
end
group = Group.find_by_full_path(group_full_path.chomp('/'))
unless group
application_setting.errors.add(:performance_bar_allowed_group_id, 'not found')
return
end
2018-11-08 19:23:39 +05:30
2019-12-21 20:55:43 +05:30
group.id
2018-11-08 19:23:39 +05:30
end
2019-12-04 20:38:33 +05:30
def bypass_external_auth?
params.key?(:external_authorization_service_enabled) && !Gitlab::Utils.to_boolean(params[:external_authorization_service_enabled])
end
2021-02-22 17:27:13 +05:30
def auto_approve_blocked_users
return unless should_auto_approve_blocked_users?
ApproveBlockedPendingApprovalUsersWorker.perform_async(current_user.id)
end
def should_auto_approve_blocked_users?
return false unless application_setting.previous_changes.key?(:require_admin_approval_after_user_signup)
enabled_previous, enabled_current = application_setting.previous_changes[:require_admin_approval_after_user_signup]
enabled_previous && !enabled_current
end
2017-08-17 22:00:37 +05:30
end
end
2019-12-04 20:38:33 +05:30
ApplicationSettings::UpdateService.prepend_if_ee('EE::ApplicationSettings::UpdateService')