Skip to content

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

Check the data actions root for actions without a model (#83966)

Alexander Polyankin01160ea50fa6
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/db.clj b/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
index bc26addcb97..f84a1df56b4 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/db.clj
@@ -113,7 +113,9 @@
:where [:and
(collection/visible-collection-filter-clause
(keyword (name table-name) "collection_id")
- {:include-archived-items include-archived-items}
+ {:include-archived-items include-archived-items
+ :root-namespace (when (= model :model/NativeQuerySnippet)
+ collection/snippets-ns)}
{:current-user-id user-id
:is-superuser? is-superuser?})
(case include-archived-items
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
index a2bc5df2708..aeb454922aa 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
@@ -775,7 +775,23 @@
(is (contains? dependent-ids (:id readable-card))
"Should see readable card as dependent")
(is (not (contains? dependent-ids (:id unreadable-card)))
- "Should not see unreadable card as dependent")))))))))))
+ "Should not see unreadable card as dependent")))
+ (testing "User sees a root snippet with the snippets root permission, not the default root one"
+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms collection/snippets-ns
+ (let [snippet-node? (fn []
+ (contains? (->> (mt/user-http-request :rasta :get 200 "ee/dependencies/graph"
+ :id (:id readable-card)
+ :type "card")
+ :nodes
+ (map (juxt :type :id))
+ set)
+ ["snippet" snippet-id]))]
+ (perms/grant-collection-read-permissions! (perms/all-users-group) collection/root-collection)
+ (is (not (snippet-node?)))
+ (perms/revoke-collection-permissions! (perms/all-users-group) collection/root-collection)
+ (perms/grant-collection-read-permissions! (perms/all-users-group)
+ (assoc collection/root-collection :namespace collection/snippets-ns))
+ (is (snippet-node?)))))))))))))
(deftest graph-table-permission-filtering-test
(testing "GET /api/ee/dependencies/graph filters out tables when user lacks table permissions"
diff --git a/src/metabase/actions/models.clj b/src/metabase/actions/models.clj
index c6d58be1995..3b6f694606a 100644
--- a/src/metabase/actions/models.clj
+++ b/src/metabase/actions/models.clj
@@ -666,7 +666,7 @@
:search-terms [:name :description]
:render-terms {:model-id :model.id
:model-name :model.name}
- :where [:= :collection.namespace nil]
+ :where [:and [:= :collection.namespace nil] [:not= :this.model_id nil]]
:joins {:model [:model/Card [:= :model.id :this.model_id]]
:query_action [:model/QueryAction [:= :query_action.action_id :this.id]]
:collection [:model/Collection [:= :collection.id :this.collection_id]]}})
diff --git a/src/metabase/actions_rest/db.clj b/src/metabase/actions_rest/db.clj
index 8511d3d61d6..44f484c1f1b 100644
--- a/src/metabase/actions_rest/db.clj
+++ b/src/metabase/actions_rest/db.clj
@@ -22,10 +22,18 @@
(if model-id
[:= :model_id model-id]
[:or
- (collection/visible-collection-filter-clause
- :collection_id
- {:include-archived-items (if archived :all :exclude)})
- [:and [:= :model_id nil] [:= :collection_id nil]]])]}))
+ [:and
+ [:not= :model_id nil]
+ (collection/visible-collection-filter-clause
+ :collection_id
+ {:include-archived-items (if archived :all :exclude)
+ :root-namespace nil})]
+ [:and
+ [:= :model_id nil]
+ (collection/visible-collection-filter-clause
+ :collection_id
+ {:include-archived-items (if archived :all :exclude)
+ :root-namespace collection/data-actions-ns})]])]}))
(mu/defn public-actions
"The name, id, public uuid, and model id of the unarchived Actions that are publicly shared."
diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.clj
index 0751d84c736..5b11ebdb9d1 100644
--- a/src/metabase/collections/models/collection.clj
+++ b/src/metabase/collections/models/collection.clj
@@ -886,7 +886,8 @@
[:include-archived-items {:optional true} [:enum :only :exclude :all]]
[:archive-operation-id {:optional true} [:maybe :string]]
[:permission-level {:optional true} [:enum :read :write]]
- [:effective-child-of {:optional true} [:maybe CollectionWithLocationAndIDOrRoot]]])
+ [:effective-child-of {:optional true} [:maybe CollectionWithLocationAndIDOrRoot]]
+ [:root-namespace {:optional true} [:maybe [:or :keyword :string]]]])
(def ^:private UserScope
[:map {:closed true}
@@ -899,24 +900,26 @@
:include-trash-collection? false
:effective-child-of nil
:archive-operation-id nil
- :permission-level :read})
+ :permission-level :read
+ :root-namespace nil})
-(def ^:private ^{:arglists '([user-scope read-or-write])} can-access-root-collection?
- "Cached function to determine whether the current user can access the root collection"
+(def ^:private ^{:arglists '([user-scope read-or-write root-namespace])} can-access-root-collection?
+ "Cached function to determine whether the current user can access the root collection of `root-namespace`"
(memoize/ttl
- ^{::memoize/args-fn (fn [[{:keys [current-user-id]} read-or-write]]
+ ^{::memoize/args-fn (fn [[{:keys [current-user-id]} read-or-write root-namespace]]
;; If this is running in the context of a request, cache it for the duration of that request.
;; Otherwise, don't cache the results at all.)
(if-let [req-id *request-id*]
- [req-id current-user-id read-or-write]
- [(random-uuid) current-user-id read-or-write]))}
+ [req-id current-user-id read-or-write (some-> root-namespace name)]
+ [(random-uuid) current-user-id read-or-write (some-> root-namespace name)]))}
(fn can-access-root-collection?*
- [{:keys [current-user-id is-superuser?]} read-or-write]
+ [{:keys [current-user-id is-superuser?]} read-or-write root-namespace]
(or is-superuser?
- (collections.db/user-has-root-collection-permission?
- current-user-id
- (cond-> ["/collection/root/"]
- (= :read read-or-write) (conj "/collection/root/read/")))))
+ (let [root (assoc root-collection :namespace root-namespace)]
+ (collections.db/user-has-root-collection-permission?
+ current-user-id
+ (cond-> [(perms/collection-readwrite-path root)]
+ (= :read read-or-write) (conj (perms/collection-read-path root)))))))
;; cache the results for 10 seconds. This is a bit arbitrary but should be long enough to cover ~all requests.
:ttl/threshold (* 10 1000)))
@@ -930,7 +933,7 @@
([user-scope visibility-config]
(and
;; we have permission for it.
- (can-access-root-collection? user-scope (:permission-level visibility-config))
+ (can-access-root-collection? user-scope (:permission-level visibility-config) (:root-namespace visibility-config))
;; we're not *only* looking for archived items
(not= :only (:include-archived-items visibility-config))
;; we're not looking for a particular `archive_operation_id`
diff --git a/src/metabase/search/in_place/legacy.clj b/src/metabase/search/in_place/legacy.clj
index f4ea78f6649..068b3a569aa 100644
--- a/src/metabase/search/in_place/legacy.clj
+++ b/src/metabase/search/in_place/legacy.clj
@@ -514,6 +514,7 @@
[:= :model.id :action.model_id])
(sql.helpers/left-join :query_action
[:= :query_action.action_id :action.id])
+ (sql.helpers/where [:not= :action.model_id nil])
(add-collection-join-and-where-clauses model search-ctx)))
(defmethod search-query-for-model "card"
diff --git a/src/metabase/typed_schemas/db.clj b/src/metabase/typed_schemas/db.clj
index 76f10e6b448..031dd0a1ea6 100644
--- a/src/metabase/typed_schemas/db.clj
+++ b/src/metabase/typed_schemas/db.clj
@@ -65,7 +65,8 @@
[:= :model_id nil]
[:= :type "query"]
[:= :archived false]
- (collection/visible-collection-filter-clause :collection_id)
+ (collection/visible-collection-filter-clause :collection_id
+ {:root-namespace collection/data-actions-ns})
(when (seq excluded-collection-ids)
[:or [:= :collection_id nil] [:not-in :collection_id excluded-collection-ids]])
(when database-ids
diff --git a/test/metabase/collections/models/collection_test.clj b/test/metabase/collections/models/collection_test.clj
index 5a55f87572d..fa7865ea3b5 100644
--- a/test/metabase/collections/models/collection_test.clj
+++ b/test/metabase/collections/models/collection_test.clj
@@ -479,6 +479,24 @@
:include-archived-items :all
:include-trash-collection? true})))))))))
+(deftest visible-collection-ids-root-namespace-test
+ (mt/with-temp [:model/Collection {default-id :id} {}
+ :model/Collection {data-actions-id :id} {:namespace "data-actions"}]
+ (letfn [(visible-collection-ids* [config]
+ (into #{}
+ (keep {default-id 'default, data-actions-id 'data-actions, "root" 'root})
+ (visible-collection-ids (merge {:permission-level :read} config))))]
+ (with-current-user-perms-for-collections! [default-id data-actions-id]
+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms :data-actions
+ (testing "the default root needs the default root's permission"
+ (is (= '#{root default data-actions} (visible-collection-ids* {}))))
+ (testing "another namespace's root needs that root's permission"
+ (is (= '#{default data-actions} (visible-collection-ids* {:root-namespace :data-actions}))))
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-all-users-permission "/collection/namespace/data-actions/root/read/"
+ (is (= '#{root default data-actions} (visible-collection-ids* {:root-namespace :data-actions})))
+ (is (= '#{default data-actions} (visible-collection-ids* {}))))))))))
+
(deftest effective-location-path-test
(mt/with-dynamic-fn-redefs [audit/is-collection-id-audit? (constantly false)]
(testing "valid input"
diff --git a/test/metabase/search/api_test.clj b/test/metabase/search/api_test.clj
index 826899a471b..87c088146c2 100644
--- a/test/metabase/search/api_test.clj
+++ b/test/metabase/search/api_test.clj
@@ -1450,6 +1450,19 @@
(is (= "Failed to parse datetime value: today~"
(mt/user-http-request :crowberto :get 400 "search" :q search-term :created_at "today~" :creator_id (mt/user->id :rasta))))))))
+(deftest action-without-model-not-searchable-test
+ (testing "search returns an Action with a model but not one without a model"
+ (let [search-term "action-without-model-search"]
+ (mt/with-temp [:model/Card {model-id :id} {:name (str search-term " model") :type :model}
+ :model/Action {action-id :id} {:name (str search-term " with model") :model_id model-id :type :query}
+ :model/Action _ {:name (str search-term " without model") :model_id nil :type :query}]
+ (search.tu/with-appdb-search-and-legacy-search
+ (is (= #{action-id}
+ (->> (mt/user-http-request :crowberto :get 200 "search" :q search-term :models "action")
+ :data
+ (map :id)
+ set))))))))
+
(deftest filter-by-last-edited-at-test
(let [search-term "last-edited-at-filtering"]
(mt/with-temp
diff --git a/test/metabase/typed_schemas/source_test.clj b/test/metabase/typed_schemas/source_test.clj
index 044f626b94c..cb02bd051d6 100644
--- a/test/metabase/typed_schemas/source_test.clj
+++ b/test/metabase/typed_schemas/source_test.clj
@@ -143,4 +143,11 @@
(testing "an action in a collection the user cannot read is left out"
(mt/with-non-admin-groups-no-collection-perms hidden-coll-id
(mt/with-current-user (mt/user->id :rasta)
- (is (not (contains? (action-ids #{(mt/id)}) hidden)))))))))
+ (is (not (contains? (action-ids #{(mt/id)}) hidden))))))
+ (testing "an action in the data actions root follows that root's permissions, not the default root's"
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms :data-actions
+ (is (not (contains? (action-ids #{(mt/id)}) standalone)))
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-all-users-permission "/collection/namespace/data-actions/root/read/"
+ (is (contains? (action-ids #{(mt/id)}) standalone))))))))))
diff --git a/test/metabase/typed_schemas_rest/api_test.clj b/test/metabase/typed_schemas_rest/api_test.clj
index 0f4c9bd54cc..df2bacb9cc4 100644
--- a/test/metabase/typed_schemas_rest/api_test.clj
+++ b/test/metabase/typed_schemas_rest/api_test.clj
@@ -108,6 +108,23 @@
(mt/with-dynamic-fn-redefs [perms/data-app-collection-ids (constantly #{(:id copies)})]
(is (not (str/includes? (schema :database (mt/id) :include-actions true) "copiedOrder")))))))))
+(deftest actions-in-data-actions-root-test
+ (mt/with-actions-enabled
+ (mt/with-temp [:model/Action root-action {:name "Discount order", :type :query}
+ :model/QueryAction _ {:action_id (:id root-action)
+ :dataset_query (lib/native-query (mt/metadata-provider)
+ "UPDATE orders SET discount = 0")}]
+ (let [rasta-schema #(:body (mt/user-http-request-full-response :rasta :get 200 "typed-schemas/v1/typescript"
+ :database (mt/id) :include-actions true))]
+ (testing "permissions on the default root do not reveal an action in the data actions root"
+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms :data-actions
+ (is (t2/exists? :model/Permissions :group_id (:id (perms/all-users-group)) :object "/collection/root/"))
+ (is (not (str/includes? (rasta-schema) "discountOrder")))))
+ (testing "permissions on the data actions root reveal it without the default root"
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-all-users-permission "/collection/namespace/data-actions/root/read/"
+ (is (str/includes? (rasta-schema) "discountOrder")))))))))
+
(deftest collection-and-database-query-params-are-mutually-exclusive-test
(mt/user-http-request-full-response
:crowberto