diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
commit | 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c (patch) | |
tree | 4dcc884cf6d81db44adae4aa99f8ec1233a41f55 /app/helpers/routing/pseudonymization_helper.rb | |
parent | 744144d28e3e7fddc117924fef88de5d9674fe4c (diff) |
Add latest changes from gitlab-org/gitlab@14-3-stable-eev14.3.0-rc42
Diffstat (limited to 'app/helpers/routing/pseudonymization_helper.rb')
-rw-r--r-- | app/helpers/routing/pseudonymization_helper.rb | 58 |
1 files changed, 58 insertions, 0 deletions
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 |