From 195530a59ec35e27ec71e3098e778686039f9d76 Mon Sep 17 00:00:00 2001 From: Tim Wade Date: Fri, 4 Aug 2017 10:11:20 -0700 Subject: [PATCH] Support compressed ids in href slugs --- lib/api/utils.rb | 4 ++-- spec/lib/api/utils_spec.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/api/utils.rb b/lib/api/utils.rb index a07159364b3..d8c6fd215c0 100644 --- a/lib/api/utils.rb +++ b/lib/api/utils.rb @@ -3,7 +3,7 @@ module Utils def self.build_href_slug(klass, id) return unless id collection = Api::CollectionConfig.new.name_for_subclass(klass) - "#{collection}/#{id}" if collection + "#{collection}/#{ApplicationRecord.compress_id(id)}" if collection end def self.resource_search_by_href_slug(href_slug, user = User.current_user) @@ -16,7 +16,7 @@ def self.resource_search_by_href_slug(href_slug, user = User.current_user) raise _("User must be defined") unless user klass = collection_config.klass(collection) - Rbac.filtered_object(klass.find(id), :user => user, :class => klass) + Rbac.filtered_object(klass.find(ApplicationRecord.uncompress_id(id)), :user => user, :class => klass) end end end diff --git a/spec/lib/api/utils_spec.rb b/spec/lib/api/utils_spec.rb index 95723f0e888..e60fdc7a8c3 100644 --- a/spec/lib/api/utils_spec.rb +++ b/spec/lib/api/utils_spec.rb @@ -100,5 +100,16 @@ expect(actual).to eq(nil) end + + it "can interpret slugs with compressed ids" do + owner_tenant = FactoryGirl.create(:tenant) + owner_group = FactoryGirl.create(:miq_group, :tenant => owner_tenant) + owner = FactoryGirl.create(:user, :miq_groups => [owner_group]) + vm = FactoryGirl.create(:vm_vmware, :tenant => owner_tenant) + + actual = described_class.resource_search_by_href_slug("vms/#{vm.compressed_id}", owner) + + expect(actual).to eq(vm) + end end end