The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Unify table visibility with can-query semantics (#69199)
John Swanson88a769d9a85d
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 9141063d3a3..b76caf6e338 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1315,8 +1315,7 @@startuptasktransforms- util- warehouses}}+ util}}secrets{:team "Querying"diff --git a/enterprise/backend/src/metabase_enterprise/data_studio/permissions/published_tables.clj b/enterprise/backend/src/metabase_enterprise/data_studio/permissions/published_tables.cljindex 9269d39cc1c..5b5ae9112b0 100644--- a/enterprise/backend/src/metabase_enterprise/data_studio/permissions/published_tables.clj+++ b/enterprise/backend/src/metabase_enterprise/data_studio/permissions/published_tables.clj@@ -46,3 +46,18 @@[table](when (:is_published table)(mi/current-user-has-full-permissions? (perms/perms-objects-set-for-parent-collection table :read))))++(defenterprise published-table-visible-clause+ "Returns a HoneySQL clause matching published tables that are readable via collection permissions."+ :feature :library+ [table-id-column {:keys [user-id is-superuser?]}]+ [:in table-id-column+ {:select [:id]+ :from [:metabase_table]+ :where [:and+ [:= :is_published true]+ (collection/visible-collection-filter-clause+ :collection_id+ {}+ {:current-user-id user-id+ :is-superuser? is-superuser?})]}])diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/api/collection_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/api/collection_test.cljindex f7425fc048c..4edd1cb397b 100644--- a/enterprise/backend/test/metabase_enterprise/data_studio/api/collection_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_studio/api/collection_test.clj@@ -52,7 +52,7 @@(deftest collection-items-table-permissions-test(mt/with-premium-features #{:library}- (testing "GET /api/collection/:id/items - published tables only require collection read permissions"+ (testing "GET /api/collection/:id/items - published tables require view-data plus query access"(mt/with-no-data-perms-for-all-users!(mt/with-temp [:model/Collection collection {}:model/Table {table-id :id :as table} {:collection_id (u/the-id collection)@@ -61,9 +61,15 @@(perms/add-user-to-group! (mt/user->id :rasta) group-id)(t2/delete! :model/DataPermissions :db_id (mt/id))(perms/grant-collection-read-permissions! (perms/all-users-group) collection)- (testing "with collection read but NO data permissions, user should see published table"+ (testing "with collection read but no view-data, user should not see published table"(data-perms/set-database-permission! group-id (mt/id) :perms/view-data :blocked)(data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :no)+ (let [items (:data (mt/user-http-request :rasta :get 200+ (str "collection/" (u/the-id collection) "/items")))]+ (is (empty? (filter #(= "table" (:model %)) items))+ "User without view-data should not see published tables")))+ (testing "with collection read and view-data, user should see published table"+ (data-perms/set-table-permission! group-id table :perms/view-data :unrestricted)(let [items (:data (mt/user-http-request :rasta :get 200(str "collection/" (u/the-id collection) "/items")))](is (=? [{:id table-id@@ -72,8 +78,8 @@:database_id (mt/id):archived false}](filter #(= "table" (:model %)) items))- "User with collection read permissions should see published tables (even without data permissions)")))- (testing "with collection read AND data permissions, user should still see published table"+ "User with view-data and collection access should see published tables")))+ (testing "with collection read and direct data query perms, user should still see published table"(data-perms/set-table-permission! group-id table :perms/view-data :unrestricted)(data-perms/set-table-permission! group-id table :perms/create-queries :query-builder)(let [items (:data (mt/user-http-request :rasta :get 200@@ -98,16 +104,21 @@(data-perms/set-table-permission! group-id table :perms/create-queries :query-builder)(testing "user cannot access collection at all without collection permissions"(mt/user-http-request :rasta :get 403 (str "collection/" (u/the-id collection) "/items"))))))- (testing "GET /api/collection/root/items - published tables in root do NOT require data permissions"+ (testing "GET /api/collection/root/items - published tables in root still require view-data"(mt/with-no-data-perms-for-all-users!(mt/with-temp [:model/Table {table-id :id :as table} {:collection_id nil:is_published true}:model/PermissionsGroup {group-id :id} {}](perms/add-user-to-group! (mt/user->id :rasta) group-id)(t2/delete! :model/DataPermissions :db_id (mt/id))- (testing "with NO data permissions, user should see published table in root"+ (testing "with no view-data, user should not see published table in root"(data-perms/set-database-permission! group-id (mt/id) :perms/view-data :blocked)(data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :no)+ (let [items (:data (mt/user-http-request :rasta :get 200 "collection/root/items"))]+ (is (empty? (filter #(= table-id (:id %)) (filter #(= "table" (:model %)) items)))+ "Users without view-data should not see published tables in root")))+ (testing "with view-data permissions, user should see published table in root"+ (data-perms/set-table-permission! group-id table :perms/view-data :unrestricted)(let [items (:data (mt/user-http-request :rasta :get 200 "collection/root/items"))](is (=? [{:id table-id:name (:display_name table)@@ -115,8 +126,8 @@:database_id (mt/id):archived false}](filter #(= table-id (:id %)) (filter #(= "table" (:model %)) items)))- "Users should see published tables in root collection (even without data permissions)")))- (testing "with data permissions, user should still see published table in root"+ "User with view-data should see published tables in root")))+ (testing "with direct query permissions, user should still see published table in root"(data-perms/set-table-permission! group-id table :perms/view-data :unrestricted)(data-perms/set-table-permission! group-id table :perms/create-queries :query-builder)(let [items (:data (mt/user-http-request :rasta :get 200 "collection/root/items"))]diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/api/query_metadata_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/api/query_metadata_test.cljindex 151dfcb913c..f02b2cd4997 100644--- a/enterprise/backend/test/metabase_enterprise/data_studio/api/query_metadata_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_studio/api/query_metadata_test.clj@@ -76,7 +76,7 @@(mt/user-http-request :rasta :get 200 (format "table/card__%d/query_metadata" (u/the-id card))))))))))(deftest card-query-metadata-published-table-collection-perms-test- (testing "GET /api/card/:id/query_metadata should include published tables accessible via collection permissions"+ (testing "GET /api/card/:id/query_metadata should include published tables that are queryable"(mt/with-premium-features #{:library}(mt/with-temp [:model/Collection table-coll {}:model/Database db {}@@ -90,7 +90,7 @@:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](mt/with-no-data-perms-for-all-users!- (data-perms/set-database-permission! group-id db :perms/view-data :blocked)+ (data-perms/set-database-permission! group-id db :perms/view-data :unrestricted)(data-perms/set-database-permission! group-id db :perms/create-queries :no)(perms/grant-collection-read-permissions! group-id (u/the-id table-coll))(perms/grant-collection-read-permissions! group-id (u/the-id card-coll))@@ -118,7 +118,7 @@:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](mt/with-no-data-perms-for-all-users!- (data-perms/set-database-permission! group-id db :perms/view-data :blocked)+ (data-perms/set-database-permission! group-id db :perms/view-data :unrestricted)(data-perms/set-database-permission! group-id db :perms/create-queries :no)(perms/grant-collection-read-permissions! group-id (u/the-id table-coll))(perms/grant-collection-read-permissions! group-id (u/the-id card-coll))diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/permissions/published_tables_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/permissions/published_tables_test.cljindex e41aeab0100..200fdf53d8d 100644--- a/enterprise/backend/test/metabase_enterprise/data_studio/permissions/published_tables_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_studio/permissions/published_tables_test.clj@@ -5,7 +5,8 @@[metabase.permissions.core :as perms][metabase.permissions.published-tables :as published-tables][metabase.test :as mt]- [metabase.test.fixtures :as fixtures]))+ [metabase.test.fixtures :as fixtures]+ [toucan2.core :as t2]))(use-fixtures :once (fixtures/initialize :db))@@ -58,3 +59,25 @@(perms/grant-collection-read-permissions! group-id (:id collection))(mt/with-current-user user-id(is (false? (boolean (published-tables/can-access-via-collection? table)))))))))++(deftest published-table-visible-clause-filters-by-collection-perms-test+ (testing "Returns clause that only includes published tables in readable collections"+ (mt/with-premium-features #{:library}+ (mt/with-temp [:model/Collection {allowed-coll-id :id} {}+ :model/Collection {blocked-coll-id :id} {}+ :model/PermissionsGroup {group-id :id} {}+ :model/User {user-id :id} {}+ :model/PermissionsGroupMembership _ {:user_id user-id :group_id group-id}+ :model/Table {allowed-table-id :id} {:is_published true :collection_id allowed-coll-id}+ :model/Table {blocked-table-id :id} {:is_published true :collection_id blocked-coll-id}+ :model/Table _ {:is_published false :collection_id allowed-coll-id}]+ (mt/with-no-data-perms-for-all-users!+ (perms/grant-collection-read-permissions! group-id allowed-coll-id)+ (perms/revoke-collection-permissions! group-id blocked-coll-id)+ (perms/revoke-collection-permissions! (perms/all-users-group) blocked-coll-id)+ (let [clause (published-tables/published-table-visible-clause+ :id+ {:user-id user-id+ :is-superuser? false})]+ (is (= #{allowed-table-id}+ (t2/select-pks-set :model/Table {:where [:and clause [:in :id [allowed-table-id blocked-table-id]]]})))))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/search_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/search_test.cljindex 2ef7a6bc913..8c7533eef85 100644--- a/enterprise/backend/test/metabase_enterprise/data_studio/search_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_studio/search_test.clj@@ -4,6 +4,8 @@[clojure.test :refer :all][medley.core :as m][metabase.collections.models.collection :as collection]+ [metabase.permissions.core :as perms]+ [metabase.permissions.models.data-permissions :as data-perms][metabase.search.core :as search][metabase.test :as mt][metabase.util.random :refer [random-name]]))@@ -115,20 +117,44 @@:collection {:id nil}:database_name "Context Test DB"}]our-result))))))))- (testing "Collection does not matter for permissions for unpublished tables"+ (testing "Users without query permissions cannot discover tables in search"(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}]+ (mt/with-no-data-perms-for-all-users!+ (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}]+ ;; Explicitly deny both view and query perms so this assertion doesn't depend on global defaults.+ (data-perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/view-data :blocked)+ (data-perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/create-queries :no)+ (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 "no tables appear"+ (is (= []+ our-result))))))))))))++(deftest unpublished-table-visible-with-data-perms-test+ (testing "Unpublished tables are discoverable when the user has data/query permissions"+ (let [search-term (random-name)+ table-name (str search-term " unpublished")]+ (mt/with-no-data-perms-for-all-users!+ (mt/with-temp [:model/Table {unpub-table :id} {:collection_id nil :is_published false :name table-name}]+ ;; Explicitly grant data/query perms so this does not rely on global test defaults.+ (data-perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/view-data :unrestricted)+ (data-perms/set-database-permission! (perms/all-users-group) (mt/id) :perms/create-queries :query-builder)(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)))))))))))+ (let [result-ids (->> (mt/user-http-request :rasta :get 200 "search"+ :q search-term :models "table" :search_engine engine)+ :data+ (map :id)+ (into #{}))]+ (is (contains? result-ids unpub-table)+ (format "Unpublished table should be visible with data permissions (engine=%s)" engine))))))))))diff --git a/src/metabase/collections_rest/api.clj b/src/metabase/collections_rest/api.cljindex efe068bb2f3..033ae6114bf 100644--- a/src/metabase/collections_rest/api.clj+++ b/src/metabase/collections_rest/api.clj@@ -753,23 +753,39 @@(defmethod collection-children-query :table[_ collection {:keys [archived? pinned-state]}]- {:select [:t.id- [:t.id :table_id]- [:t.display_name :name]- :t.description- :t.collection_id- [:t.db_id :database_id]- [[:!= :t.archived_at nil] :archived]- [(h2x/literal "table") :model]]- :from [[:metabase_table :t]]- :where [:and- [:= :t.is_published true]- (poison-when-pinned-clause pinned-state)- (collection/visible-collection-filter-clause :t.collection_id {:cte-name :visible_collection_ids})- [:= :t.collection_id (:id collection)]- (if archived?- [:!= :t.archived_at nil]- [:= :t.archived_at nil])]})+ (let [user-info {:user-id api/*current-user-id*+ :is-superuser? api/*is-superuser?*}+ published-clause (perms/published-table-visible-clause :t.id user-info)+ queryable-clause (cond-> [:or+ [:in :t.id (perms/visible-table-filter-select+ :id+ user-info+ {:perms/view-data :unrestricted+ :perms/create-queries :query-builder})]]+ published-clause (conj [:and+ [:in :t.id (perms/visible-table-filter-select+ :id+ user-info+ {:perms/view-data :unrestricted})]+ published-clause]))]+ {:select [:t.id+ [:t.id :table_id]+ [:t.display_name :name]+ :t.description+ :t.collection_id+ [:t.db_id :database_id]+ [[:!= :t.archived_at nil] :archived]+ [(h2x/literal "table") :model]]+ :from [[:metabase_table :t]]+ :where [:and+ [:= :t.is_published true]+ (poison-when-pinned-clause pinned-state)+ (collection/visible-collection-filter-clause :t.collection_id {:cte-name :visible_collection_ids})+ queryable-clause+ [:= :t.collection_id (:id collection)]+ (if archived?+ [:!= :t.archived_at nil]+ [:= :t.archived_at nil])]}))(defn- annotate-collections[parent-coll colls {:keys [show-dashboard-questions?]}]diff --git a/src/metabase/permissions/core.clj b/src/metabase/permissions/core.cljindex 419bb240c96..4c4ef6d478c 100644--- a/src/metabase/permissions/core.clj+++ b/src/metabase/permissions/core.clj@@ -133,7 +133,8 @@can-access-via-collection?user-published-table-permissionuser-has-any-published-table-permission?- user-has-published-table-permission-for-database?])+ user-has-published-table-permission-for-database?+ published-table-visible-clause])(p/import-vars [metabase.permissions.settings use-tenants])diff --git a/src/metabase/permissions/published_tables.clj b/src/metabase/permissions/published_tables.cljindex 11fbadbdb5c..6193bb4dd8b 100644--- a/src/metabase/permissions/published_tables.clj+++ b/src/metabase/permissions/published_tables.clj@@ -37,3 +37,10 @@metabase-enterprise.data-studio.permissions.published-tables[_table]false)++(defenterprise published-table-visible-clause+ "Returns a HoneySQL clause for published tables visible via collection permissions.+ OSS implementation returns nil."+ metabase-enterprise.data-studio.permissions.published-tables+ [_table-id-column _user-info]+ nil)diff --git a/src/metabase/search/impl.clj b/src/metabase/search/impl.cljindex a1bb0f08005..dd9e63edd2f 100644--- a/src/metabase/search/impl.clj+++ b/src/metabase/search/impl.clj@@ -19,7 +19,6 @@[metabase.util.malli :as mu][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms]- [metabase.warehouses.models.database :as database][toucan2.core :as t2][toucan2.instance :as t2.instance][toucan2.realize :as t2.realize]))@@ -37,15 +36,19 @@variables. This might require updating `can-read?` and `can-write?` to take explicit perms sets instead of relyingon dynamic variables."{:style/indent 0}- [current-user-perms & body]- `(with-bindings {(requiring-resolve 'metabase.api.common/*current-user-permissions-set*) (atom ~current-user-perms)}+ [current-user-id current-user-perms & body]+ `(with-bindings {(requiring-resolve 'metabase.api.common/*current-user-id*) ~current-user-id+ (requiring-resolve 'metabase.api.common/*current-user-permissions-set*) (atom ~current-user-perms)}~@body))-(defn- can-write? [{:keys [current-user-perms]} instance]- (ensure-current-user-perms-set-is-bound current-user-perms (mi/can-write? instance)))+(defn- can-write? [{:keys [current-user-id current-user-perms]} instance]+ (ensure-current-user-perms-set-is-bound current-user-id current-user-perms (mi/can-write? instance)))-(defn- can-read? [{:keys [current-user-perms]} instance]- (ensure-current-user-perms-set-is-bound current-user-perms (mi/can-read? instance)))+(defn- can-read? [{:keys [current-user-id current-user-perms]} instance]+ (ensure-current-user-perms-set-is-bound current-user-id current-user-perms (mi/can-read? instance)))++(defn- can-query? [{:keys [current-user-id current-user-perms]} instance]+ (ensure-current-user-perms-set-is-bound current-user-id current-user-perms (mi/can-query? instance)))(defmethod check-permissions-for-model :default[search-ctx instance]@@ -64,13 +67,7 @@;; issue with new pure sql implementation(defmethod check-permissions-for-model :table[search-ctx instance]- ;; we've already filtered out tables w/o collection permissions in the query itself.- (let [instance-id (:id instance)- user-id (:current-user-id search-ctx)- db-id (database/table-id->database-id instance-id)]- (and- (perms/user-has-permission-for-table? user-id :perms/view-data :unrestricted db-id instance-id)- (perms/user-has-permission-for-table? user-id :perms/create-queries :query-builder db-id instance-id))))+ (can-query? search-ctx (assoc instance :db_id (:database_id instance))))(defmethod check-permissions-for-model :indexed-entity[search-ctx instance]diff --git a/src/metabase/search/permissions.clj b/src/metabase/search/permissions.cljindex 959bcad3091..d95510dd3f8 100644--- a/src/metabase/search/permissions.clj+++ b/src/metabase/search/permissions.clj@@ -57,4 +57,5 @@:is-superuser? is-superuser?:is-data-analyst? is-data-analyst?}{:perms/view-data :unrestricted- :perms/create-queries :query-builder}))+ :perms/create-queries :query-builder}+ {:include-published-via-collection? true}))diff --git a/src/metabase/warehouse_schema/models/table.clj b/src/metabase/warehouse_schema/models/table.cljindex 12654e98306..dfbd7472c59 100644--- a/src/metabase/warehouse_schema/models/table.clj+++ b/src/metabase/warehouse_schema/models/table.clj@@ -255,9 +255,8 @@(defmethod mi/can-read? :model/Table;; Check if user can see this table's metadata.;; True if user has:- ;; - Data access permissions (and (view-data :unrestricted) (create-queries :query-builder)), OR+ ;; - Data access permissions (view-data :unrestricted) and either query perms or published-collection access, OR;; - Metadata management permission (manage-table-metadata :yes), OR- ;; - Access via published table in a collection (EE feature)([instance](or;; Has data access permissions@@ -267,31 +266,31 @@:unrestricted(:db_id instance)(:id instance))- (perms/user-has-permission-for-table?- api/*current-user-id*- :perms/create-queries- :query-builder- (:db_id instance)- (:id instance)))+ (or+ (perms/user-has-permission-for-table?+ api/*current-user-id*+ :perms/create-queries+ :query-builder+ (:db_id instance)+ (:id instance))+ ;; Can access via published collection (EE feature)+ (perms/can-access-via-collection? instance)));; Has manage-table-metadata permission (allows viewing metadata without data access)(perms/user-has-permission-for-table?api/*current-user-id*:perms/manage-table-metadata:yes(:db_id instance)- (:id instance))- ;; Can access via published collection (EE feature)- (perms/can-access-via-collection? instance)))+ (:id instance))))([_ pk](mi/can-read? (t2/select-one :model/Table pk))))(defmethod mi/can-query? :model/Table;; Check if user can execute queries against this table.;; True if user has:- ;; - Both view-data AND create-queries permissions, OR- ;; - Access via published table in a collection (EE feature)+ ;; - view-data permission and either create-queries permission or published-collection access (EE feature)([instance]- (or+ (boolean;; Has both view-data and create-queries permissions(and (perms/user-has-permission-for-table?api/*current-user-id*@@ -299,14 +298,15 @@:unrestricted(:db_id instance)(:id instance))- (perms/user-has-permission-for-table?- api/*current-user-id*- :perms/create-queries- :query-builder- (:db_id instance)- (:id instance)))- ;; Can access via published collection (EE feature)- (perms/can-access-via-collection? instance)))+ (or+ (perms/user-has-permission-for-table?+ api/*current-user-id*+ :perms/create-queries+ :query-builder+ (:db_id instance)+ (:id instance))+ ;; Can access via published collection (EE feature)+ (perms/can-access-via-collection? instance)))))([_ pk](mi/can-query? (t2/select-one :model/Table pk))))@@ -378,8 +378,21 @@[_ :- :keywordcolumn-or-exp :- :anyuser-info :- perms/UserInfo- permission-mapping :- perms/PermissionMapping]- (perms/visible-table-filter-with-cte column-or-exp user-info permission-mapping))+ permission-mapping :- perms/PermissionMapping+ & [{:keys [include-published-via-collection?]}]]+ (let [{:keys [clause with]} (perms/visible-table-filter-with-cte column-or-exp user-info permission-mapping)]+ (if-let [published-clause (and include-published-via-collection?+ (perms/published-table-visible-clause column-or-exp user-info))]+ {:clause [:or clause+ [:and+ [:in column-or-exp (perms/visible-table-filter-select+ :id+ user-info+ {:perms/view-data :unrestricted})]+ published-clause]]+ :with with}+ {:clause clause+ :with with})));;; ------------------------------------------------ Serdes Hashing -------------------------------------------------diff --git a/test/metabase/search/filter_test.clj b/test/metabase/search/filter_test.cljindex f38f81ebc6c..2298a201ce6 100644--- a/test/metabase/search/filter_test.clj+++ b/test/metabase/search/filter_test.clj@@ -8,6 +8,7 @@[metabase.search.in-place.filter :as search.in-place.filter][metabase.search.ingestion :as search.ingestion][metabase.search.test-util :as search.tu]+ [metabase.test :as mt][metabase.test.fixtures :as fixtures]))(use-fixtures :once (fixtures/initialize :db))@@ -103,61 +104,62 @@:enabled-transform-source-types #{"mbql"}})(deftest with-filters-test- (testing "The kitchen sink context is complete"- (is (empty? (remove kitchen-sink-filter-context (filter-keys)))))-- (testing "In the general case, we simply filter by models, and exclude dashboard cards"- (is (= {:select [:some :stuff],- :from :somewhere,- :where- [:and- [:= 1 2]- [:or [:= nil :search_index.dashboard_id] nil]]}- (search.filter/with-filters {:models []} {:select [:some :stuff], :from :somewhere})))- (is (= {:select [:some :stuff],- :from :somewhere,- :where- [:and- [:in :search_index.model ["a"]]- [:or [:= nil :search_index.dashboard_id] nil]]}- (search.filter/with-filters {:models ["a"]} {:select [:some :stuff], :from :somewhere}))))-- (testing "We can insert appropriate constraints for all the filters"- (is (= {:select [:some :stuff],- :from :somewhere,- :where- #{[:in :search_index.last_editor_id [321]]- [:in :search_index.creator_id [123]]- [:or [:= :search_index.collection_id 5] [:like :collection.location "%/5/%"]]- [:not= :search_index.model [:inline "table"]]- [:= :search_index.archived true]- [:in :search_index.model ["card" "dataset" "metric" "dashboard" "action"]]- [:or- [:= nil :search_index.dashboard_id]- [:not= [:inline 0] [:coalesce :search_index.dashboardcard_count [:inline 0]]]]- [:in- :search_index.model- #{"dashboard"- "table"- "segment"- "collection"- "measure"- "transform"- "document"- "database"- "action"- "indexed-entity"- "metric"- "card"}]- [:< [:cast :search_index.model_created_at :date] #t "2024-10-02"]- [:in :search_index.model_id ["1" "2" "3" "4"]]- [:< [:cast :search_index.last_edited_at :date] #t "2024-10-03"]- [:>= [:cast :search_index.model_created_at :date] #t "2024-10-01"]- [:= :search_index.non_temporal_dim_ids "[1]"]- [:= :search_index.has_temporal_dim true]- :and- [:= :search_index.database_id 231]- [:in :search_index.display_type ["line"]]- [:>= [:cast :search_index.last_edited_at :date] #t "2024-10-02"]}}- (-> (search.filter/with-filters kitchen-sink-filter-context {:select [:some :stuff], :from :somewhere})- (update :where set))))))+ (mt/with-premium-features #{}+ (testing "The kitchen sink context is complete"+ (is (empty? (remove kitchen-sink-filter-context (filter-keys)))))++ (testing "In the general case, we simply filter by models, and exclude dashboard cards"+ (is (= {:select [:some :stuff],+ :from :somewhere,+ :where+ [:and+ [:= 1 2]+ [:or [:= nil :search_index.dashboard_id] nil]]}+ (search.filter/with-filters {:models []} {:select [:some :stuff], :from :somewhere})))+ (is (= {:select [:some :stuff],+ :from :somewhere,+ :where+ [:and+ [:in :search_index.model ["a"]]+ [:or [:= nil :search_index.dashboard_id] nil]]}+ (search.filter/with-filters {:models ["a"]} {:select [:some :stuff], :from :somewhere}))))++ (testing "We can insert appropriate constraints for all the filters"+ (is (= {:select [:some :stuff],+ :from :somewhere,+ :where+ #{[:in :search_index.last_editor_id [321]]+ [:in :search_index.creator_id [123]]+ [:or [:= :search_index.collection_id 5] [:like :collection.location "%/5/%"]]+ [:not= :search_index.model [:inline "table"]]+ [:= :search_index.archived true]+ [:in :search_index.model ["card" "dataset" "metric" "dashboard" "action"]]+ [:or+ [:= nil :search_index.dashboard_id]+ [:not= [:inline 0] [:coalesce :search_index.dashboardcard_count [:inline 0]]]]+ [:in+ :search_index.model+ #{"dashboard"+ "table"+ "segment"+ "collection"+ "measure"+ "transform"+ "document"+ "database"+ "action"+ "indexed-entity"+ "metric"+ "card"}]+ [:< [:cast :search_index.model_created_at :date] #t "2024-10-02"]+ [:in :search_index.model_id ["1" "2" "3" "4"]]+ [:< [:cast :search_index.last_edited_at :date] #t "2024-10-03"]+ [:>= [:cast :search_index.model_created_at :date] #t "2024-10-01"]+ [:= :search_index.non_temporal_dim_ids "[1]"]+ [:= :search_index.has_temporal_dim true]+ :and+ [:= :search_index.database_id 231]+ [:in :search_index.display_type ["line"]]+ [:>= [:cast :search_index.last_edited_at :date] #t "2024-10-02"]}}+ (-> (search.filter/with-filters kitchen-sink-filter-context {:select [:some :stuff], :from :somewhere})+ (update :where set)))))))