Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Make transforms available in search for admins only (#65240)

metamben652f725d4155
diff --git a/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj b/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj
index 3f3b4265c81..bf488953ef2 100644
--- a/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj
+++ b/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj
@@ -266,6 +266,7 @@
(search.spec/define-spec "transform"
{:model :model/Transform
+ :visibility :superuser
:attrs {:archived false
:collection-id false
:creator-id false
diff --git a/enterprise/backend/test/metabase_enterprise/transforms/api/search_test.clj b/enterprise/backend/test/metabase_enterprise/transforms/api/search_test.clj
index 314d40dfac2..e717aca535e 100644
--- a/enterprise/backend/test/metabase_enterprise/transforms/api/search_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/transforms/api/search_test.clj
@@ -1,8 +1,16 @@
(ns metabase-enterprise.transforms.api.search-test
(:require
[clojure.test :refer :all]
+ [metabase.search.ingestion :as search.ingestion]
[metabase.search.test-util :as search.tu]
- [metabase.test :as mt]))
+ [metabase.test :as mt]
+ [metabase.test.fixtures :as fixtures]))
+
+(use-fixtures :once (fixtures/initialize :db))
+
+(use-fixtures :each (fn [thunk]
+ (binding [search.ingestion/*force-sync* true]
+ (search.tu/with-new-search-if-available (thunk)))))
(deftest basic-search-test
(testing "Transform can be found with search"
@@ -18,3 +26,20 @@
(testing "Regular user can see no transforms"
(let [results (mt/user-http-request :rasta :get 200 "search" :q "trans" :models "transform")]
(is (empty? (:data results))))))))))
+
+(deftest available-models-visibility-test
+ (testing "Transform model visibility in available_models respects superuser restriction"
+ (mt/with-premium-features #{:transforms}
+ (search.tu/with-temp-index-table
+ (mt/with-temp [:model/Transform {_transform-id :id} {:name "test transform"}]
+ (testing "Superuser sees 'transform' in available_models"
+ (let [response (mt/user-http-request :crowberto :get 200 "search"
+ :q "test"
+ :calculate_available_models true)]
+ (is (contains? (set (:available_models response)) "transform"))))
+
+ (testing "Regular user does not see 'transform' in available_models"
+ (let [response (mt/user-http-request :rasta :get 200 "search"
+ :q "test"
+ :calculate_available_models true)]
+ (is (not (contains? (set (:available_models response)) "transform"))))))))))
diff --git a/src/metabase/search/filter.clj b/src/metabase/search/filter.clj
index b349c97f520..15434ddd451 100644
--- a/src/metabase/search/filter.clj
+++ b/src/metabase/search/filter.clj
@@ -17,8 +17,9 @@
(defn- visible-to? [search-ctx {:keys [visibility] :as _spec}]
(case visibility
- :all true
- :app-user (not (search.permissions/sandboxed-or-impersonated-user? search-ctx))))
+ :all true
+ :app-user (not (search.permissions/sandboxed-or-impersonated-user? search-ctx))
+ :superuser (:is-superuser? search-ctx)))
(def ^:private context-key->filter
"Map the context keys to their corresponding filters"
diff --git a/src/metabase/search/in_place/filter.clj b/src/metabase/search/in_place/filter.clj
index 32a2f4ddf33..2b0e6f7a0cb 100644
--- a/src/metabase/search/in_place/filter.clj
+++ b/src/metabase/search/in_place/filter.clj
@@ -304,9 +304,11 @@
verified
non-temporal-dim-ids
has-temporal-dim
- display-type]} search-context
+ display-type
+ is-superuser?]} search-context
feature->supported-models (feature->supported-models)]
(cond-> models
+ (not is-superuser?) (disj "transform")
(some? created-at) (set/intersection (:created-at feature->supported-models))
(some? created-by) (set/intersection (:created-by feature->supported-models))
(some? last-edited-at) (set/intersection (:last-edited-at feature->supported-models))
diff --git a/src/metabase/search/spec.clj b/src/metabase/search/spec.clj
index 0681cf52aab..01c3779d08b 100644
--- a/src/metabase/search/spec.clj
+++ b/src/metabase/search/spec.clj
@@ -139,7 +139,7 @@
(def ^:private Specification
[:map {:closed true}
[:name SearchModel]
- [:visibility [:enum :all :app-user]]
+ [:visibility [:enum :all :app-user :superuser]]
[:model :keyword]
[:attrs Attrs]
[:search-terms [:or
diff --git a/test/metabase/search/filter_test.clj b/test/metabase/search/filter_test.clj
index 14f14bfca25..2709ab1a8f1 100644
--- a/test/metabase/search/filter_test.clj
+++ b/test/metabase/search/filter_test.clj
@@ -48,8 +48,8 @@
active-filters)))
(deftest search-context->applicable-models-test
- (testing "All models are relevant if we're not looking in the trash"
- (is (= search.config/all-models
+ (testing "All models (except transforms, which are admin-only) are relevant if we're not looking in the trash"
+ (is (= (disj search.config/all-models "transform")
(search.filter/search-context->applicable-models (with-all-models-and-regular-user {:archived? false})))))
(testing "We only search for certain models in the trash"
@@ -57,8 +57,8 @@
config/ee-available? (conj "document"))
(search.filter/search-context->applicable-models (with-all-models-and-regular-user {:archived? true})))))
- (testing "Indexed entities are not visible for sandboxed users"
- (is (= (disj search.config/all-models "indexed-entity")
+ (testing "Indexed entities and transforms (which are admin-only) are not visible for sandboxed users"
+ (is (= (disj search.config/all-models "indexed-entity" "transform")
(search.filter/search-context->applicable-models (with-all-models-and-sandboxed-user {:archived? false})))))
(doseq [active-filters (active-filter-combinations)]
diff --git a/test/metabase/search/in_place/filter_test.clj b/test/metabase/search/in_place/filter_test.clj
index d051f9d2579..b5021a4c505 100644
--- a/test/metabase/search/in_place/filter_test.clj
+++ b/test/metabase/search/in_place/filter_test.clj
@@ -106,6 +106,33 @@
(merge default-search-ctx
{:search-native-query true})))))))
+(deftest ^:parallel transform-superuser-visibility-test
+ (testing "Transform model visibility is restricted to superusers"
+ (testing "Superuser sees transform in applicable models"
+ (is (contains?
+ (search.filter/search-context->applicable-models
+ (merge default-search-ctx
+ {:is-superuser? true
+ :models (cond-> #{"dashboard" "card" "transform"}
+ config/ee-available? (conj "transform"))}))
+ "transform")))
+
+ (testing "Non-superuser does not see transform in applicable models"
+ (is (not (contains?
+ (search.filter/search-context->applicable-models
+ (merge default-search-ctx
+ {:is-superuser? false
+ :models (cond-> #{"dashboard" "card"}
+ config/ee-available? (conj "transform"))}))
+ "transform"))))
+
+ (testing "Non-superuser with transform in models set - transform is filtered out"
+ (is (= #{"dashboard" "card"}
+ (search.filter/search-context->applicable-models
+ (merge default-search-ctx
+ {:is-superuser? false
+ :models #{"dashboard" "card" "transform"}})))))))
+
(deftest joined-with-table?-test
#_{:clj-kondo/ignore [:equals-true]}
(are [expected args]