Skip to content

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

Enforce table collection permissions in search correctly (#66841)

John Swansonae9b3684b914
diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/search_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/search_test.clj
index 243918751a4..268a0407ddb 100644
--- a/enterprise/backend/test/metabase_enterprise/data_studio/search_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_studio/search_test.clj
@@ -5,7 +5,8 @@
[medley.core :as m]
[metabase.collections.models.collection :as collection]
[metabase.search.core :as search]
- [metabase.test :as mt]))
+ [metabase.test :as mt]
+ [metabase.util.random :refer [random-name]]))
(deftest published-table-collection-filter-test
(mt/with-premium-features #{:data-studio}
@@ -113,4 +114,21 @@
:name "ContextTestTableUnpub"
:collection {:id nil}
:database_name "Context Test DB"}]
- our-result))))))))))
+ our-result))))))))
+ (testing "Collection does not matter for permissions for unpublished tables"
+ (let [search-term (random-name)
+ name-1 (str search-term " 1")
+ name-2 (str search-term " 2")]
+ (mt/with-temp [:model/Table {unpub-table :id} {:collection_id nil :is_published false :name name-1}
+ :model/Table {pub-table :id} {:collection_id nil :is_published true :name name-2}]
+ (doseq [engine ["in-place" "appdb"]]
+ (search/reindex! {:async? false :in-place? true})
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (let [results (mt/user-http-request :rasta :get 200 "search"
+ :q search-term :models "table" :search_engine engine)
+ our-result (->> (filter (comp #{pub-table unpub-table} :id) (:data results))
+ (sort-by :id)
+ (map #(select-keys % [:id])))]
+ (testing "only the unpublished table appears"
+ (is (= [{:id unpub-table}]
+ our-result)))))))))))
diff --git a/src/metabase/search/appdb/core.clj b/src/metabase/search/appdb/core.clj
index df4ed614dd4..6ad0ffd9a26 100644
--- a/src/metabase/search/appdb/core.clj
+++ b/src/metabase/search/appdb/core.clj
@@ -69,6 +69,7 @@
:bookmark (pos? (:bookmarked index-row 0))
:score (:total_score index-row 1)
:all-scores (search.scoring/all-scores weights active-scorers index-row))
+ (dissoc :is_published)
(update :created_at parse-datetime)
(update :updated_at parse-datetime)
(update :last_edited_at parse-datetime)))
@@ -89,6 +90,14 @@
:integer)]]
(search.permissions/permitted-tables-clause search-ctx :mt_toplevel.id)]}]]]))
+(def null-collection-id-should-be-ignored-for-unpublished-tables-clause
+ "For every model except tables, a null collection ID means the thing is in Our Analytics.
+
+ For tables, this means there is no associated collection and the collection_id should be completely ignored."
+ [:and
+ [:= :model [:inline "table"]]
+ [:= :is_published false]])
+
(defn add-collection-join-and-where-clauses
"Add a `WHERE` clause to the query to only return Collections the Current User has access to; join against Collection,
so we can return its `:name`."
@@ -97,7 +106,10 @@
permitted-clause (search.permissions/permitted-collections-clause search-ctx collection-id-col)
personal-clause (search.filter/personal-collections-where-clause search-ctx collection-id-col)
excluded-models (search.filter/models-without-collection)
- or-null #(vector :or [:in :search_index.model excluded-models] %)]
+ or-null #(vector :or
+ [:in :search_index.model excluded-models]
+ null-collection-id-should-be-ignored-for-unpublished-tables-clause
+ %)]
(cond-> qry
true (sql.helpers/left-join [:collection :collection] [:= collection-id-col :collection.id])
true (sql.helpers/where (or-null permitted-clause))
diff --git a/src/metabase/search/in_place/legacy.clj b/src/metabase/search/in_place/legacy.clj
index 5def86e04cb..dbf7c70cc4a 100644
--- a/src/metabase/search/in_place/legacy.clj
+++ b/src/metabase/search/in_place/legacy.clj
@@ -114,7 +114,9 @@
:pk_ref :text
:model_index_id :integer
;; returned for Card and Action
- :dataset_query :text))
+ :dataset_query :text
+ ;; returned for Table
+ :is_published :boolean))
(mu/defn- canonical-columns :- [:sequential HoneySQLColumn]
"Returns a seq of lists of canonical columns for the search query with the given `model` Will return column names
@@ -170,7 +172,11 @@
"Add a `WHERE` clause to the query to only return tables the current user has access to"
[qry model search-ctx]
(sql.helpers/where qry (case model
- "table" (search.permissions/permitted-tables-clause search-ctx :table.id)
+ "table" [:and
+ (search.permissions/permitted-tables-clause search-ctx :table.id)
+ [:or
+ [:not :is_published]
+ (search.permissions/permitted-collections-clause search-ctx :collection_id)]]
"search-index" [:or
[:= :search_index.model nil]
[:!= :search_index.model [:inline "table"]]
@@ -188,7 +194,10 @@
"collection" :collection.id
"search-index" :search_index.collection_id
:collection_id)
- permitted-clause (search.permissions/permitted-collections-clause search-ctx collection-id-col)
+ permitted-clause [:or
+ (when (= model "table")
+ [:not :is_published])
+ (search.permissions/permitted-collections-clause search-ctx collection-id-col)]
personal-clause (search.filter/personal-collections-where-clause search-ctx collection-id-col)]
(cond-> honeysql-query
;; add a JOIN against Collection *unless* the source table is already Collection
diff --git a/src/metabase/search/spec.clj b/src/metabase/search/spec.clj
index 38f42ebacc5..5c5d1c53ee7 100644
--- a/src/metabase/search/spec.clj
+++ b/src/metabase/search/spec.clj
@@ -77,7 +77,8 @@
:view-count :int
:non-temporal-dim-ids :text
:has-temporal-dim :boolean
- :display-type :text})
+ :display-type :text
+ :is-published :boolean})
(def ^:private explicit-attrs
"These attributes must be explicitly defined, omitting them could be a source of bugs."
@@ -100,7 +101,8 @@
:view-count
:updated-at
:non-temporal-dim-ids
- :has-temporal-dim])
+ :has-temporal-dim
+ :is-published])
distinct
vec))
diff --git a/src/metabase/warehouse_schema/models/table.clj b/src/metabase/warehouse_schema/models/table.clj
index 2da5c3b2478..7eff6dbd0ca 100644
--- a/src/metabase/warehouse_schema/models/table.clj
+++ b/src/metabase/warehouse_schema/models/table.clj
@@ -485,7 +485,8 @@
:database-id :db_id
:view-count true
:created-at true
- :updated-at true}
+ :updated-at true
+ :is-published :is_published}
:search-terms {:name search.spec/explode-camel-case
:display_name true
:description true}