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
feat: apply remote sync settings to snippets (#68902)
Edward Paget898b7b4c38d7
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 1632c44e281..4e1fb7f4646 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -863,6 +863,7 @@modelspermissionspremium-features+ remote-syncutil}}notification@@ -2263,6 +2264,7 @@native-query-snippetspermissionspremium-features+ remote-syncutil}}enterprise/ssodiff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/core.cljindex b2207b335e6..036c6375434 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj@@ -3,6 +3,7 @@[metabase-enterprise.remote-sync.settings :as settings][metabase-enterprise.remote-sync.source :as source][metabase-enterprise.remote-sync.source.protocol :as source.p]+ [metabase-enterprise.remote-sync.spec :as spec][metabase.api.common :as api][metabase.collections.core :as collections][metabase.events.core :as events]@@ -65,6 +66,18 @@(or (not (settings/remote-sync-enabled))(= (settings/remote-sync-type) :read-write)))+(defenterprise model-editable?+ "Determines if a model instance is editable based on remote sync configuration."+ :feature :none+ [model-key instance]+ (spec/model-editable? model-key instance))++(defenterprise batch-model-editable?+ "Batch version of model-editable?. Returns a map of instance-id -> editable? boolean."+ :feature :none+ [model-key instances]+ (spec/batch-model-editable? model-key instances))+(mu/defn bulk-set-remote-sync :- :nil"Sets remote sync to true/false on one or collections in a single transaction. Checks that the remote sync stateafterwards is consistent in terms of dependency rules. Collections are provided as a map of collection-id -> sync state."diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.cljindex 5becdc7c078..b6d1a5e49c2 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj@@ -498,6 +498,49 @@[_ _]false)+(defmulti batch-check-eligibility+ "Batch version of check-eligibility. Returns a map of instance-id -> eligible? boolean."+ {:arglists '([spec instances])}+ (fn [spec _instances] (get-in spec [:eligibility :type])))++(defmethod batch-check-eligibility :library-synced+ [_spec instances]+ (let [eligible? (rs-settings/library-is-remote-synced?)]+ (into {} (map (fn [inst] [(:id inst) eligible?])) instances)))++(defmethod batch-check-eligibility :default+ [spec instances]+ (into {} (map (fn [inst] [(:id inst) (check-eligibility spec inst)])) instances))++;;; -------------------------------------------- Editability Checking ------------------------------------------------++(defn model-editable?+ "Determines if a model instance is editable based on remote sync configuration.++ Returns false if:+ - The model has a spec in remote-sync-specs AND+ - The instance is eligible for sync (via check-eligibility) AND+ - remote-sync-type is :read-only++ For models with global eligibility (e.g., :library-synced, :setting), the instance+ argument can be nil or an empty map since eligibility doesn't depend on instance data."+ [model-key instance]+ (if-let [spec (spec-for-model-key model-key)]+ (or (= (rs-settings/remote-sync-type) :read-write)+ (not (check-eligibility spec instance)))+ ;; Model not in spec, always editable+ true))++(defn batch-model-editable?+ "Batch version of model-editable?. Returns a map of instance-id -> editable? boolean."+ [model-key instances]+ (if-let [spec (spec-for-model-key model-key)]+ (if (= (rs-settings/remote-sync-type) :read-write)+ (into {} (map (fn [inst] [(:id inst) true])) instances)+ (let [eligibility-map (batch-check-eligibility spec instances)]+ (into {} (map (fn [[id eligible?]] [id (not eligible?)])) eligibility-map)))+ (into {} (map (fn [inst] [(:id inst) true])) instances)))+;;; ---------------------------------------------------- Hydration -----------------------------------------------------(defn hydrate-model-detailsdiff --git a/enterprise/backend/src/metabase_enterprise/snippet_collections/models/native_query_snippet/permissions.clj b/enterprise/backend/src/metabase_enterprise/snippet_collections/models/native_query_snippet/permissions.cljindex 172b7044c79..e2eb2b78f3b 100644--- a/enterprise/backend/src/metabase_enterprise/snippet_collections/models/native_query_snippet/permissions.clj+++ b/enterprise/backend/src/metabase_enterprise/snippet_collections/models/native_query_snippet/permissions.clj@@ -5,6 +5,7 @@[metabase.native-query-snippets.core :as snippets][metabase.permissions.core :as perms][metabase.premium-features.core :refer [defenterprise]]+ [metabase.remote-sync.core :as remote-sync][metabase.util.malli :as mu][metabase.util.malli.schema :as ms][toucan2.core :as t2]))@@ -32,7 +33,8 @@(and(not (perms/sandboxed-user?))(snippets/has-any-native-permissions?)- (has-parent-collection-perms? snippet :write)))+ (has-parent-collection-perms? snippet :write)+ (remote-sync/model-editable? :model/NativeQuerySnippet snippet)))([model id](can-write? (t2/select-one [model :collection_id] :id id))))@@ -43,7 +45,8 @@(and(not (perms/sandboxed-user?))(snippets/has-any-native-permissions?)- (has-parent-collection-perms? m :write)))+ (has-parent-collection-perms? m :write)+ (remote-sync/model-editable? :model/NativeQuerySnippet m)))(defenterprise can-update?"Can the current User apply a map of `changes` to a `snippet`?"@@ -54,4 +57,5 @@(snippets/has-any-native-permissions?)(has-parent-collection-perms? snippet :write)(or (not (contains? changes :collection_id))- (has-parent-collection-perms? changes :write))))+ (has-parent-collection-perms? changes :write))+ (remote-sync/model-editable? :model/NativeQuerySnippet snippet)))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/snippets_api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/snippets_api_test.cljnew file mode 100644index 00000000000..983b9a7c4a9--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/snippets_api_test.clj@@ -0,0 +1,210 @@+(ns metabase-enterprise.remote-sync.snippets-api-test+ "API tests for snippet operations under remote-sync read-only mode.+ Verifies that write operations are blocked when library is synced and mode is read-only."+ (:require+ [clojure.test :refer :all]+ [metabase.collections.test-utils :as collections.tu]+ [metabase.permissions.models.data-permissions :as data-perms]+ [metabase.permissions.models.permissions :as perms]+ [metabase.permissions.models.permissions-group :as perms-group]+ [metabase.test :as mt]+ [metabase.util :as u]+ [toucan2.core :as t2]))++;;; ------------------------------------------- Create Snippet API Tests -------------------------------------------++(deftest create-snippet-blocked-in-read-only-mode-test+ (testing "POST /api/native-query-snippet"+ (testing "Creating a snippet is blocked when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (let [snippet-name (mt/random-name)+ response (mt/user-http-request :rasta :post 403 "native-query-snippet"+ {:name snippet-name+ :content "SELECT 1"+ :collection_id (:id collection)})]+ (is (= "You don't have permissions to do that." response)+ "Should get 403 forbidden when trying to create snippet in read-only mode")+ (is (not (t2/exists? :model/NativeQuerySnippet :name snippet-name))+ "Snippet should not have been created"))))))))))++(deftest create-snippet-allowed-in-read-write-mode-test+ (testing "POST /api/native-query-snippet"+ (testing "Creating a snippet is allowed when mode is read-write even when library is synced"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (let [snippet-name (mt/random-name)]+ (try+ (let [response (mt/user-http-request :rasta :post 200 "native-query-snippet"+ {:name snippet-name+ :content "SELECT 1"+ :collection_id (:id collection)})]+ (is (map? response)+ "Should successfully create snippet in read-write mode")+ (is (= snippet-name (:name response))+ "Response should contain the created snippet"))+ (finally+ (t2/delete! :model/NativeQuerySnippet :name snippet-name))))))))))))++(deftest create-snippet-allowed-when-library-not-synced-test+ (testing "POST /api/native-query-snippet"+ (testing "Creating a snippet is allowed when library is NOT synced even in read-only mode"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-not-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (let [snippet-name (mt/random-name)]+ (try+ (let [response (mt/user-http-request :rasta :post 200 "native-query-snippet"+ {:name snippet-name+ :content "SELECT 1"+ :collection_id (:id collection)})]+ (is (map? response)+ "Should successfully create snippet when library is not synced")+ (is (= snippet-name (:name response))+ "Response should contain the created snippet"))+ (finally+ (t2/delete! :model/NativeQuerySnippet :name snippet-name))))))))))))++;;; ------------------------------------------- Update Snippet API Tests -------------------------------------------++(deftest update-snippet-blocked-in-read-only-mode-test+ (testing "PUT /api/native-query-snippet/:id"+ (testing "Updating a snippet is blocked when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:name "Original Name"+ :content "SELECT 1"+ :collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (let [response (mt/user-http-request :rasta :put 403+ (format "native-query-snippet/%d" (:id snippet))+ {:name "Updated Name"})]+ (is (= "You don't have permissions to do that." response)+ "Should get 403 forbidden when trying to update snippet in read-only mode")+ (is (= "Original Name" (:name (t2/select-one :model/NativeQuerySnippet :id (:id snippet))))+ "Snippet name should not have been updated"))))))))))++(deftest update-snippet-allowed-in-read-write-mode-test+ (testing "PUT /api/native-query-snippet/:id"+ (testing "Updating a snippet is allowed when mode is read-write even when library is synced"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:name "Original Name"+ :content "SELECT 1"+ :collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (let [response (mt/user-http-request :rasta :put 200+ (format "native-query-snippet/%d" (:id snippet))+ {:name "Updated Name"})]+ (is (map? response)+ "Should successfully update snippet in read-write mode")+ (is (= "Updated Name" (:name response))+ "Response should contain the updated name"))))))))))++(deftest update-snippet-allowed-when-library-not-synced-test+ (testing "PUT /api/native-query-snippet/:id"+ (testing "Updating a snippet is allowed when library is NOT synced even in read-only mode"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-not-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:name "Original Name"+ :content "SELECT 1"+ :collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (let [response (mt/user-http-request :rasta :put 200+ (format "native-query-snippet/%d" (:id snippet))+ {:name "Updated Name"})]+ (is (map? response)+ "Should successfully update snippet when library is not synced")+ (is (= "Updated Name" (:name response))+ "Response should contain the updated name"))))))))))++;;; ------------------------------------------- Read Snippet API Tests -------------------------------------------++(deftest read-snippet-allowed-in-read-only-mode-test+ (testing "GET /api/native-query-snippet/:id"+ (testing "Reading a snippet is allowed even when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:name "Test Snippet"+ :content "SELECT 1"+ :collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-read-permissions! (perms-group/all-users) collection)+ (let [response (mt/user-http-request :rasta :get 200+ (format "native-query-snippet/%d" (:id snippet)))]+ (is (map? response)+ "Should successfully read snippet in read-only mode")+ (is (= "Test Snippet" (:name response))+ "Response should contain the snippet name"))))))))))++(deftest list-snippets-allowed-in-read-only-mode-test+ (testing "GET /api/native-query-snippet"+ (testing "Listing snippets is allowed even when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:name "Test Snippet"+ :content "SELECT 1"+ :collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-read-permissions! (perms-group/all-users) collection)+ (let [response (mt/user-http-request :rasta :get 200 "native-query-snippet")]+ (is (sequential? response)+ "Should successfully list snippets in read-only mode")+ (is (some #(= (u/the-id snippet) (:id %)) response)+ "Response should include the test snippet"))))))))))++;;; ------------------------------------------- Archive Snippet API Tests -------------------------------------------++(deftest archive-snippet-blocked-in-read-only-mode-test+ (testing "PUT /api/native-query-snippet/:id"+ (testing "Archiving a snippet (setting archived=true) is blocked when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:name "Test Snippet"+ :content "SELECT 1"+ :collection_id (:id collection)+ :archived false}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (let [response (mt/user-http-request :rasta :put 403+ (format "native-query-snippet/%d" (:id snippet))+ {:archived true})]+ (is (= "You don't have permissions to do that." response)+ "Should get 403 forbidden when trying to archive snippet in read-only mode")+ (is (false? (:archived (t2/select-one :model/NativeQuerySnippet :id (:id snippet))))+ "Snippet should not have been archived"))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.cljindex 4a76b5dfd41..f1a57601c06 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj@@ -303,3 +303,136 @@(is (nil? (spec/query-export-roots field-spec)))(is (nil? (spec/query-export-roots segment-spec)))(is (nil? (spec/query-export-roots measure-spec))))))++;;; -------------------------------------------- Editability Checking Tests ----------------------------------------++(deftest model-editable?-unknown-model-test+ (testing "model-editable? returns true for models not in the spec"+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (is (true? (spec/model-editable? :model/UnknownModel {}))+ "Unknown models should always be editable"))))++(deftest model-editable?-read-write-mode-test+ (testing "model-editable? returns true in read-write mode regardless of eligibility"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ ;; With library synced, snippet would normally not be editable in read-only mode+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced true :location "/"}]+ (is (true? (spec/model-editable? :model/NativeQuerySnippet {}))+ "Snippets should be editable in read-write mode even when library is synced")))))++(deftest model-editable?-library-synced-eligibility-test+ (testing "model-editable? with :library-synced eligibility (NativeQuerySnippet)"+ (testing "returns false when library is synced and mode is read-only"+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced true :location "/"}]+ (is (false? (spec/model-editable? :model/NativeQuerySnippet {}))+ "Snippets should NOT be editable when library is synced and mode is read-only"))))++ (testing "returns true when library is NOT synced even in read-only mode"+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced false :location "/"}]+ (is (true? (spec/model-editable? :model/NativeQuerySnippet {}))+ "Snippets should be editable when library is NOT synced"))))))++(deftest model-editable?-setting-eligibility-test+ (testing "model-editable? with :setting eligibility (Transform)"+ (testing "returns false when setting is enabled and mode is read-only"+ (mt/with-temporary-setting-values [remote-sync-type :read-only+ remote-sync-transforms true]+ (is (false? (spec/model-editable? :model/Transform {}))+ "Transforms should NOT be editable when transforms setting is enabled and mode is read-only")))++ (testing "returns true when setting is disabled even in read-only mode"+ (mt/with-temporary-setting-values [remote-sync-type :read-only+ remote-sync-transforms false]+ (is (true? (spec/model-editable? :model/Transform {}))+ "Transforms should be editable when transforms setting is disabled")))))++(deftest model-editable?-collection-eligibility-test+ (testing "model-editable? with :collection eligibility (Card)"+ (testing "returns false when card is in remote-synced collection and mode is read-only"+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Synced Collection" :is_remote_synced true :location "/"}]+ (is (false? (spec/model-editable? :model/Card {:collection_id coll-id}))+ "Cards in synced collections should NOT be editable in read-only mode"))))++ (testing "returns true when card is in non-synced collection even in read-only mode"+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Normal Collection" :is_remote_synced false :location "/"}]+ (is (true? (spec/model-editable? :model/Card {:collection_id coll-id}))+ "Cards in non-synced collections should be editable"))))))++(deftest model-editable?-nil-instance-test+ (testing "model-editable? works with nil instance for global eligibility models"+ (mt/with-temporary-setting-values [remote-sync-type :read-only+ remote-sync-transforms true]+ (is (false? (spec/model-editable? :model/Transform nil))+ "Transforms with nil instance should check setting-based eligibility"))++ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced true :location "/"}]+ (is (false? (spec/model-editable? :model/NativeQuerySnippet nil))+ "Snippets with nil instance should check library-synced eligibility")))))++;;; ------------------------------------------ Batch Eligibility Checking Tests --------------------------------------++(deftest batch-check-eligibility-library-synced-test+ (testing "batch-check-eligibility with :library-synced eligibility"+ (let [spec (spec/spec-for-model-key :model/NativeQuerySnippet)+ instances [{:id 1} {:id 2} {:id 3}]]++ (testing "returns true for all when library is synced"+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced true :location "/"}]+ (let [result (spec/batch-check-eligibility spec instances)]+ (is (= {1 true, 2 true, 3 true} result)))))++ (testing "returns false for all when library is not synced"+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced false :location "/"}]+ (let [result (spec/batch-check-eligibility spec instances)]+ (is (= {1 false, 2 false, 3 false} result))))))))++(deftest batch-check-eligibility-default-test+ (testing "batch-check-eligibility :default falls back to check-eligibility per instance"+ (mt/with-temp [:model/Collection {synced-id :id} {:name "Synced" :is_remote_synced true :location "/"}+ :model/Collection {not-synced-id :id} {:name "Not Synced" :is_remote_synced false :location "/"}]+ (let [spec (spec/spec-for-model-key :model/Card)+ instances [{:id 1 :collection_id synced-id}+ {:id 2 :collection_id not-synced-id}+ {:id 3 :collection_id synced-id}]+ result (spec/batch-check-eligibility spec instances)]+ (is (true? (get result 1)) "Card in synced collection should be eligible")+ (is (false? (get result 2)) "Card in non-synced collection should not be eligible")+ (is (true? (get result 3)) "Card in synced collection should be eligible")))))++;;; ------------------------------------------ Batch Editability Checking Tests --------------------------------------++(deftest batch-model-editable?-unknown-model-test+ (testing "batch-model-editable? returns true for all instances of unknown models"+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (let [instances [{:id 1} {:id 2} {:id 3}]+ result (spec/batch-model-editable? :model/UnknownModel instances)]+ (is (= {1 true, 2 true, 3 true} result))))))++(deftest batch-model-editable?-read-write-mode-test+ (testing "batch-model-editable? returns true for all in read-write mode"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced true :location "/"}]+ (let [instances [{:id 1} {:id 2} {:id 3}]+ result (spec/batch-model-editable? :model/NativeQuerySnippet instances)]+ (is (= {1 true, 2 true, 3 true} result)))))))++(deftest batch-model-editable?-library-synced-test+ (testing "batch-model-editable? with :library-synced eligibility"+ (testing "returns false for all when library is synced and mode is read-only"+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced true :location "/"}]+ (let [instances [{:id 1} {:id 2} {:id 3}]+ result (spec/batch-model-editable? :model/NativeQuerySnippet instances)]+ (is (= {1 false, 2 false, 3 false} result))))))++ (testing "returns true for all when library is not synced"+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-temp [:model/Collection _ {:name "Library" :type "library" :is_remote_synced false :location "/"}]+ (let [instances [{:id 1} {:id 2} {:id 3}]+ result (spec/batch-model-editable? :model/NativeQuerySnippet instances)]+ (is (= {1 true, 2 true, 3 true} result))))))))diff --git a/enterprise/backend/test/metabase_enterprise/snippet_collections/models/native_query_snippet/permissions_test.clj b/enterprise/backend/test/metabase_enterprise/snippet_collections/models/native_query_snippet/permissions_test.cljindex 338da912b21..37213ad5d53 100644--- a/enterprise/backend/test/metabase_enterprise/snippet_collections/models/native_query_snippet/permissions_test.clj+++ b/enterprise/backend/test/metabase_enterprise/snippet_collections/models/native_query_snippet/permissions_test.clj@@ -3,11 +3,14 @@[clojure.test :refer :all][metabase-enterprise.test :as met][metabase.collections.models.collection :as collection]+ [metabase.collections.test-utils :as collections.tu][metabase.models.interface :as mi][metabase.native-query-snippets.models.native-query-snippet.permissions :as snippet.perms]+ [metabase.permissions.models.data-permissions :as data-perms][metabase.permissions.models.permissions :as perms][metabase.permissions.models.permissions-group :as perms-group]- [metabase.test :as mt]))+ [metabase.test :as mt]+ [toucan2.core :as t2]))(def ^:private root-collection (assoc collection/root-collection :name "Root Collection", :namespace "snippets"))@@ -84,3 +87,148 @@:has-perms-for-id? #(mi/can-write? :model/NativeQuerySnippet (:id snippet)):grant-collection-perms! #(perms/grant-collection-readwrite-permissions! (perms-group/all-users) coll):revoke-collection-perms! #(perms/revoke-collection-permissions! (perms-group/all-users) coll))))))++;;; ------------------------------------------- Remote Sync Read-Only Mode Tests -------------------------------------------++(deftest remote-sync-read-only-write-perms-test+ (testing "Snippets are NOT writable when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (is (false? (mi/can-write? snippet))+ "Snippet should NOT be writable in read-only mode when library is synced")))))))))++(deftest remote-sync-read-only-create-perms-test+ (testing "Snippets cannot be created when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (is (false? (mi/can-create? :model/NativeQuerySnippet {:collection_id (:id collection)}))+ "Snippet should NOT be creatable in read-only mode when library is synced")))))))))++(deftest remote-sync-read-only-update-perms-test+ (testing "Snippets cannot be updated when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (is (false? (mi/can-update? snippet {:name "New Name"}))+ "Snippet should NOT be updatable in read-only mode when library is synced")))))))))++(deftest remote-sync-read-only-read-perms-still-allowed-test+ (testing "Snippets are still readable when library is synced and mode is read-only"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-read-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (is (true? (mi/can-read? snippet))+ "Snippet should still be readable in read-only mode")))))))))++(deftest remote-sync-read-write-mode-allows-edits-test+ (testing "Snippets ARE writable when mode is read-write even when library is synced"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (is (true? (mi/can-write? snippet))+ "Snippet should be writable in read-write mode")))))))))++(deftest remote-sync-library-not-synced-allows-edits-test+ (testing "Snippets ARE writable in read-only mode when library is NOT synced"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-not-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet {:collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (is (true? (mi/can-write? snippet))+ "Snippet should be writable when library is NOT synced")))))))))++;;; ------------------------------------------ Batched Hydration Tests ------------------------------------------++(deftest batched-hydrate-can-write-remote-sync-test+ (testing "batched-hydrate :can_write respects remote-sync read-only mode"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet1 {:name "snippet1" :content "SELECT 1" :collection_id (:id collection)}+ :model/NativeQuerySnippet snippet2 {:name "snippet2" :content "SELECT 2" :collection_id (:id collection)}+ :model/NativeQuerySnippet snippet3 {:name "snippet3" :content "SELECT 3" :collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (let [snippets (mt/with-current-user nil+ (t2/select :model/NativeQuerySnippet :id [:in [(:id snippet1) (:id snippet2) (:id snippet3)]]))+ hydrated (t2/hydrate snippets :can_write)]+ (testing "all snippets should have :can_write = false in read-only mode"+ (is (every? #(false? (:can_write %)) hydrated)+ "Snippets should NOT be writable in read-only mode when library is synced")))))))))))++(deftest batched-hydrate-can-write-read-write-mode-test+ (testing "batched-hydrate :can_write allows writes in read-write mode"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet1 {:name "snippet1" :content "SELECT 1" :collection_id (:id collection)}+ :model/NativeQuerySnippet snippet2 {:name "snippet2" :content "SELECT 2" :collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (let [snippets (mt/with-current-user nil+ (t2/select :model/NativeQuerySnippet :id [:in [(:id snippet1) (:id snippet2)]]))+ hydrated (t2/hydrate snippets :can_write)]+ (testing "all snippets should have :can_write = true in read-write mode"+ (is (every? :can_write hydrated)+ "Snippets should be writable in read-write mode")))))))))))++(deftest batched-hydrate-can-write-library-not-synced-test+ (testing "batched-hydrate :can_write allows writes when library is not synced"+ (mt/with-premium-features #{:snippet-collections}+ (collections.tu/with-library-not-synced+ (mt/with-temporary-setting-values [remote-sync-type :read-only]+ (mt/with-non-admin-groups-no-root-collection-for-namespace-perms "snippets"+ (mt/with-temp [:model/Collection collection {:name "Test Collection", :namespace "snippets"}+ :model/NativeQuerySnippet snippet1 {:name "snippet1" :content "SELECT 1" :collection_id (:id collection)}+ :model/NativeQuerySnippet snippet2 {:name "snippet2" :content "SELECT 2" :collection_id (:id collection)}]+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder-and-native)+ (perms/grant-collection-readwrite-permissions! (perms-group/all-users) collection)+ (mt/with-test-user :rasta+ (let [snippets (mt/with-current-user nil+ (t2/select :model/NativeQuerySnippet :id [:in [(:id snippet1) (:id snippet2)]]))+ hydrated (t2/hydrate snippets :can_write)]+ (testing "all snippets should have :can_write = true when library is not synced"+ (is (every? :can_write hydrated)+ "Snippets should be writable when library is NOT synced")))))))))))diff --git a/src/metabase/native_query_snippets/models/native_query_snippet.clj b/src/metabase/native_query_snippets/models/native_query_snippet.cljindex b5943d94b7e..f8c7a8918ff 100644--- a/src/metabase/native_query_snippets/models/native_query_snippet.clj+++ b/src/metabase/native_query_snippets/models/native_query_snippet.clj@@ -9,6 +9,7 @@[metabase.models.interface :as mi][metabase.models.serialization :as serdes][metabase.native-query-snippets.models.native-query-snippet.permissions :as snippet.perms]+ [metabase.remote-sync.core :as remote-sync][metabase.util :as u][metabase.util.i18n :refer [deferred-tru tru]][metabase.util.malli :as mu]@@ -122,6 +123,22 @@[& args](apply snippet.perms/can-update? args))+(methodical/defmethod t2/batched-hydrate [:model/NativeQuerySnippet :can_write]+ [_model k snippets]+ (let [non-nil-snippets (remove nil? snippets)+ snippets-with-collections (t2/hydrate non-nil-snippets :collection)+ editable-map (remote-sync/batch-model-editable? :model/NativeQuerySnippet non-nil-snippets)]+ (mi/instances-with-hydrated-data+ snippets k+ #(into {}+ (map (fn [snippet]+ [(:id snippet)+ (and (get editable-map (:id snippet) true)+ (mi/can-write? snippet))]))+ snippets-with-collections)+ :id+ {:default false})))+;;; ---------------------------------------------------- Schemas -----------------------------------------------------(def NativeQuerySnippetNamediff --git a/src/metabase/remote_sync/core.clj b/src/metabase/remote_sync/core.cljindex 823a74882e1..28028114e75 100644--- a/src/metabase/remote_sync/core.clj+++ b/src/metabase/remote_sync/core.clj@@ -27,3 +27,23 @@metabase-enterprise.remote-sync.core[]true)++(defenterprise model-editable?+ "Determines if a model instance is editable based on remote sync configuration.++ Returns false if the instance is eligible for remote sync AND remote-sync-type+ is :read-only. Always returns true on OSS.++ For models with global eligibility (e.g., :setting, :library-synced), the instance+ can be nil or empty map."+ metabase-enterprise.remote-sync.core+ [_model-key _instance]+ true)++(defenterprise batch-model-editable?+ "Batch version of model-editable?. Returns a map of instance-id -> editable? boolean.++ OSS always returns true for all instances."+ metabase-enterprise.remote-sync.core+ [_model-key instances]+ (into {} (map (fn [inst] [(:id inst) true])) instances))diff --git a/test/metabase/native_query_snippets/models/native_query_snippet_test.clj b/test/metabase/native_query_snippets/models/native_query_snippet_test.cljindex 97c2d2bc0e0..53269eddf42 100644--- a/test/metabase/native_query_snippets/models/native_query_snippet_test.clj+++ b/test/metabase/native_query_snippets/models/native_query_snippet_test.clj@@ -145,3 +145,36 @@:name "id":display-name "ID"}}(:template_tags extracted)))))))))++;;; ------------------------------------------------ Batched Hydration Tests ------------------------------------------++(deftest ^:parallel batched-hydrate-can-write-test+ (testing "batched-hydrate :can_write returns correct values for multiple snippets"+ (mt/with-temp [:model/Collection {coll-id :id} {:namespace "snippets"}+ :model/NativeQuerySnippet snippet1 {:name "snippet1" :content "SELECT 1" :collection_id coll-id}+ :model/NativeQuerySnippet snippet2 {:name "snippet2" :content "SELECT 2" :collection_id coll-id}+ :model/NativeQuerySnippet snippet3 {:name "snippet3" :content "SELECT 3" :collection_id coll-id}]+ (mt/with-test-user :crowberto+ (let [snippets (t2/select :model/NativeQuerySnippet :id [:in [(:id snippet1) (:id snippet2) (:id snippet3)]])+ hydrated (t2/hydrate snippets :can_write)]+ (testing "all snippets have :can_write hydrated"+ (is (every? #(contains? % :can_write) hydrated)))+ (testing "all snippets are writable in OSS"+ (is (every? :can_write hydrated))))))))++(deftest ^:parallel batched-hydrate-can-write-empty-list-test+ (testing "batched-hydrate :can_write handles empty list"+ (mt/with-test-user :crowberto+ (is (= [] (t2/hydrate [] :can_write))))))++(deftest ^:parallel batched-hydrate-can-write-nil-in-list-test+ (testing "batched-hydrate :can_write handles nil in list"+ (mt/with-temp [:model/Collection {coll-id :id} {:namespace "snippets"}+ :model/NativeQuerySnippet snippet {:name "snippet" :content "SELECT 1" :collection_id coll-id}]+ (mt/with-test-user :crowberto+ (let [snippets [(t2/select-one :model/NativeQuerySnippet :id (:id snippet)) nil]+ hydrated (t2/hydrate snippets :can_write)]+ (is (nil? (second hydrated))+ "nil should remain nil")+ (is (contains? (first hydrated) :can_write)+ "non-nil snippet should be hydrated"))))))