Skip to content

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

[search] Correctly set the default search filters for in-place searches (#72912)

Braden Shepherdsonbdd58144b255
diff --git a/src/metabase/search/config.clj b/src/metabase/search/config.clj
index 4d48608af23..6967a7382cb 100644
--- a/src/metabase/search/config.clj
+++ b/src/metabase/search/config.clj
@@ -174,12 +174,10 @@
:command-palette {:filter-items-in-personal-collection "exclude-others"}})
(defn filter-default
- "Get the default value for the given filter in the given context. Is non-contextual for legacy search."
- [engine context filter-key]
+ "Get the default value for the given filter in the given context."
+ [_engine context filter-key]
(let [fetch (fn [ctx] (when ctx (-> filter-defaults-by-context (get ctx) (get filter-key))))]
- (if (= engine :search.engine/in-place)
- (fetch :default)
- (or (fetch context) (fetch :default)))))
+ (or (fetch context) (fetch :default))))
;; This gets called *a lot* during a search request, so we'll almost certainly need to optimize it. Maybe just TTL.
(defn weights
diff --git a/test/metabase/search/api_test.clj b/test/metabase/search/api_test.clj
index a221fd041e8..c72b882cced 100644
--- a/test/metabase/search/api_test.clj
+++ b/test/metabase/search/api_test.clj
@@ -29,7 +29,7 @@
[metabase.warehouses.models.database :as database]
[toucan2.core :as t2]))
-(use-fixtures :once (fixtures/initialize :db))
+(use-fixtures :once (fixtures/initialize :db :test-users :test-users-personal-collections))
(comment
;; We need this to ensure the engine hierarchy is registered
@@ -1568,7 +1568,60 @@
(search :rasta "exclude"))))
(testing "getting models should return only models that are applied"
(is (= #{"dashboard" "collection"}
- (get-available-models :q search-term :filter_items_in_personal_collection "exclude")))))))
+ (get-available-models :q search-term :filter_items_in_personal_collection "exclude"))))
+ (testing "admin exclude-others excludes other users' personal collection items"
+ (is (= #{["dashboard" dash-public]
+ ["dashboard" dash-sub-public]
+ ["collection" coll-sub-public]
+ ["dataset" model-crowberto]
+ ["dataset" model-sub-crowberto]
+ ["collection" coll-sub-crowberto]}
+ (search :crowberto "exclude-others"))))
+ (testing "non-admin exclude-others sees own personal items plus public"
+ (is (= #{["dashboard" dash-public]
+ ["dashboard" dash-sub-public]
+ ["collection" coll-sub-public]
+ ["card" card-rasta]
+ ["card" card-sub-rasta]
+ ["collection" coll-sub-rasta]}
+ (search :rasta "exclude-others"))))
+ (testing "admin only-mine"
+ (is (= #{["dataset" model-crowberto]
+ ["dataset" model-sub-crowberto]
+ ["collection" coll-sub-crowberto]}
+ (search :crowberto "only-mine"))))
+ (testing "non-admin only-mine"
+ (is (= #{["card" card-rasta]
+ ["card" card-sub-rasta]
+ ["collection" coll-sub-rasta]}
+ (search :rasta "only-mine"))))
+ (testing "search-app context default excludes others' personal collections for admin"
+ (let [search-with-context (fn [user]
+ (->> (mt/user-http-request user :get 200 "search"
+ :q search-term
+ :context "search-app")
+ :data
+ (map (juxt :model :id))
+ set))]
+ (is (not (contains? (search-with-context :crowberto) ["card" card-rasta]))
+ "Admin should not see rasta's card in search-app context with default filter")
+ (is (not (contains? (search-with-context :crowberto) ["card" card-sub-rasta]))
+ "Admin should not see rasta's sub-collection card in search-app context with default filter")
+ (is (contains? (search-with-context :crowberto) ["dataset" model-crowberto])
+ "Admin should still see their own personal collection items")))
+ (testing "in-place engine: search-app context default should still exclude others' personal collections (#UXW-3238)"
+ (search.tu/with-legacy-search
+ (let [search-with-context (fn [user]
+ (->> (mt/user-http-request user :get 200 "search"
+ :q search-term
+ :context "search-app")
+ :data
+ (map (juxt :model :id))
+ set))]
+ (is (not (contains? (search-with-context :crowberto) ["card" card-rasta]))
+ "Admin should not see rasta's card even with in-place engine")
+ (is (not (contains? (search-with-context :crowberto) ["card" card-sub-rasta]))
+ "Admin should not see rasta's sub-collection card even with in-place engine")))))))
(deftest collection-effective-parent-test
(mt/with-temp [:model/Collection coll-1 {:name "Collection 1"}
diff --git a/test/metabase/search/config_test.clj b/test/metabase/search/config_test.clj
index d3261923050..a258ee06bba 100644
--- a/test/metabase/search/config_test.clj
+++ b/test/metabase/search/config_test.clj
@@ -17,6 +17,8 @@
(search.config/filter-default search-engine :command-palette :filter-items-in-personal-collection)))
(is (= "exclude-others"
(search.config/filter-default search-engine :search-app :filter-items-in-personal-collection))))
- (testing "Legacy search does not support overrides"
- (is (= "all"
- (search.config/filter-default :search.engine/in-place :command-palette :filter-items-in-personal-collection)))))
+ (testing "Legacy search should respect context overrides (#UXW-3238)"
+ (is (= "exclude-others"
+ (search.config/filter-default :search.engine/in-place :command-palette :filter-items-in-personal-collection)))
+ (is (= "exclude-others"
+ (search.config/filter-default :search.engine/in-place :search-app :filter-items-in-personal-collection)))))