From 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 20 Sep 2021 13:18:24 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-3-stable-ee --- app/helpers/routing/pseudonymization_helper.rb | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 app/helpers/routing/pseudonymization_helper.rb (limited to 'app/helpers/routing/pseudonymization_helper.rb') diff --git a/app/helpers/routing/pseudonymization_helper.rb b/app/helpers/routing/pseudonymization_helper.rb new file mode 100644 index 00000000000..1d9320f0106 --- /dev/null +++ b/app/helpers/routing/pseudonymization_helper.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +module Routing + module PseudonymizationHelper + def masked_page_url + return unless Feature.enabled?(:mask_page_urls, type: :ops) + + mask_params(Rails.application.routes.recognize_path(request.original_fullpath)) + rescue ActionController::RoutingError, URI::InvalidURIError + nil + end + + private + + def mask_params(request_params) + return if request_params[:action] == 'new' + + namespace_type = request_params[:controller].split('/')[1] + + namespace_type.present? ? url_with_namespace_type(request_params, namespace_type) : url_without_namespace_type(request_params) + end + + def url_without_namespace_type(request_params) + masked_url = "#{request.protocol}#{request.host_with_port}" + + masked_url += case request_params[:controller] + when 'groups' + "/namespace:#{group.id}" + when 'projects' + "/namespace:#{project.namespace.id}/project:#{project.id}" + when 'root' + '' + else + "#{request.path}" + end + + masked_url += request.query_string.present? ? "?#{request.query_string}" : '' + + masked_url + end + + def url_with_namespace_type(request_params, namespace_type) + masked_url = "#{request.protocol}#{request.host_with_port}" + + if request_params.has_key?(:project_id) + masked_url += "/namespace:#{project.namespace.id}/project:#{project.id}/-/#{namespace_type}" + end + + if request_params.has_key?(:id) + masked_url += namespace_type == 'blob' ? '/:repository_path' : "/#{request_params[:id]}" + end + + masked_url += request.query_string.present? ? "?#{request.query_string}" : '' + + masked_url + end + end +end -- cgit v1.2.1