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
Check table permissions for data editing (#62385)
Chris Truterf00d64699e54
diff --git a/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj b/enterprise/backend/test/metabase_enterprise/action_v2/api_test.cljindex c58f9370145..cbb878b9292 100644--- a/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj@@ -14,7 +14,6 @@[metabase.driver.sql-jdbc :as sql-jdbc][metabase.driver.sql-jdbc.connection :as sql-jdbc.conn][metabase.query-processor :as qp]- [metabase.sync.core :as sync][metabase.test :as mt][metabase.test.data.sql :as sql.tx][metabase.warehouse-schema.models.field-values :as field-values]@@ -251,6 +250,9 @@:filter [:in (mt/$ids $orders.product_id) 1 2]}}))](zipmap (map first result) (map second result))))]+ ;; TODO waiting on https://github.com/metabase/metabase/pull/62485+ (t2/update! :model/Table {:db_id (mt/id)} {:is_writable true})+(testing "sanity check that we have children rows"(is (= {1 932 98}@@ -312,6 +314,9 @@(is (= 1 (children-count 2)))(is (= 1 (children-count 3))))+ ;; TODO waiting on https://github.com/metabase/metabase/pull/62485+ (t2/update! :model/Table {:db_id (mt/id)} {:is_writable true})+(testing "delete parent with self-referential children should return error without delete-children param"(is (=? {:errors [{:index 0:type "metabase.actions.error/violate-foreign-key-constraint",@@ -361,7 +366,10 @@{:table-name "user"}{:fk :team :field-name "team_id"}){:transaction? false})- (sync/sync-database! (mt/db))++ ;; TODO waiting on https://github.com/metabase/metabase/pull/62485+ (t2/update! :model/Table {:db_id (mt/id)} {:is_writable true})+(let [users-table-id (mt/id :user)#_teams-table-id #_(mt/id :team)delete-user-body {:action "data-grid.row/delete"diff --git a/enterprise/backend/test/metabase_enterprise/action_v2/test_util.clj b/enterprise/backend/test/metabase_enterprise/action_v2/test_util.cljindex 0b91186f145..f0739efa992 100644--- a/enterprise/backend/test/metabase_enterprise/action_v2/test_util.clj+++ b/enterprise/backend/test/metabase_enterprise/action_v2/test_util.clj@@ -53,7 +53,8 @@;; todo figure out how to determine default schema from driver:schema (case (:engine db) :postgres "public" nil):display_name table-name- :field_order :database})]+ :field_order :database+ :is_writable true})](sync/sync-fields-for-table! db table)(:id table)))diff --git a/src/metabase/actions/settings.clj b/src/metabase/actions/settings.cljindex 1ccd9ee647e..5d95b1d4b19 100644--- a/src/metabase/actions/settings.clj+++ b/src/metabase/actions/settings.clj@@ -2,7 +2,8 @@(:require[metabase.settings.core :as setting][metabase.util.i18n :as i18n]- [metabase.warehouses.models.database :as database]))+ [metabase.warehouses.models.database :as database]+ [toucan2.core :as t2]))(setting/defsetting database-enable-actions(i18n/deferred-tru "Whether to enable Actions for a specific Database.")@@ -12,12 +13,46 @@:visibility :public:database-local :only)+;; reasons for disabling table editing++(def ^:private db-routing-reason+ {:key :setting/database-routing+ :message (i18n/deferred-tru "Table editing is not supported with database routing.")})++(def ^:private no-writable-tables-reason+ {:key :permissions/no-writable-table+ :message (i18n/deferred-tru "Table editing requires at least one table with INSERT, UPDATE, and DELETE support.")})++(def ^:private busy-sync-reason+ {:key :database-metadata/sync-in-progress+ :message (i18n/deferred-tru "Unable to determine whether the database connection is readonly, as it is still syncing.")})++(def ^:private missing-permissions-reason+ {:key :database-metadata/not-populated+ :message (i18n/deferred-tru "Unable to determine whether the database connection is readonly, as we are missing metadata from sync.")})++;; Marking reasons as warnings allow the user to proactively set the configuration, but it will only come into+;; effect once the warning is resolved.++(derive :database-metadata/sync-in-progress :setting/disabled-warning)+(derive :database-metadata/not-populated :setting/disabled-warning)+(setting/defsetting database-enable-table-editing(i18n/deferred-tru "Whether to enable table data editing for a specific Database."):default false:feature :table-data-editing:driver-feature :actions/data-editing- :enabled-for-db? (complement database/is-destination?)+ :enabled-for-db? (fn [db]+ (setting/custom-disabled-reasons!+ [(when (database/is-destination? db) db-routing-reason)+ (when (t2/exists? :model/Database :router_database_id (:id db)) db-routing-reason)+ (cond+ ;; TODO we also care about re-sync after connection details are changed+ (= (:initial_sync_status db) "incomplete") busy-sync-reason+ ;; NOTE: we could optimize this into a single query, but the code would be less elegant.+ (t2/exists? :model/Table :db_id (:id db) :is_writable true) nil+ (t2/exists? :model/Table :db_id (:id db) :is_writable nil) missing-permissions-reason+ :else no-writable-tables-reason)])):type :boolean:visibility :public:database-local :onlydiff --git a/src/metabase/settings/models/setting.clj b/src/metabase/settings/models/setting.cljindex 8cd0ccb18cb..a531b7116b2 100644--- a/src/metabase/settings/models/setting.clj+++ b/src/metabase/settings/models/setting.clj@@ -161,16 +161,23 @@(when-let [f (:enabled-for-db? setting-def)](try (when-not (f database)[{:key :disabled-for-db- :message "This database does not support this setting"}])+ :message (tru "This database does not support this setting")}])(catch ExceptionInfo e(or (:setting/disabled-reasons (ex-data e))(throw e))))))+(defn- warning?+ "Warnings don't block you saving a setting, but the value won't be used until the warning is resolved."+ [reason]+ (isa? (:key reason) :setting/disabled-warning))+(defn custom-disabled-reasons!- "Expose custom reasons for a setting being disabled to the admin panel."+ "Expose custom reasons for a setting being disabled to the admin panel.+ For convenience, we strip nil reasons, and no-op with a truthy return if nothing remains."[reasons]- (assert (seq reasons) "At least one reason must be given")- (throw (ex-info "Setting is not enabled for this database" {:setting/disabled-reasons reasons})))+ (if-let [reasons (seq (remove nil? reasons))]+ (throw (ex-info "Setting is not enabled for this database" {:setting/disabled-reasons reasons}))+ true));; This is called `LocalOption` rather than `DatabaseLocalOption` or something like that because we intend to also add;; User-Local Settings at some point in the future. The will use the same options@@ -946,7 +953,9 @@{:setting s-name:required-feature driver-feature:database-id (:id database)})))- (when-let [reasons (disabled-for-db-reasons setting database)]+ ;; Warnings are only for display and shouldn't block writing the setting.+ ;; They imply that the setting won't work until the issue is resolved, but often it is a transient problem.+ (when-let [reasons (seq (remove warning? (disabled-for-db-reasons setting database)))](throw (ex-info (tru "Setting {0} is not enabled for this database" s-name){:setting s-name:database-id (:id database)diff --git a/src/metabase/warehouses/api.clj b/src/metabase/warehouses/api.cljindex a34c467c69f..8b650647e91 100644--- a/src/metabase/warehouses/api.clj+++ b/src/metabase/warehouses/api.clj@@ -33,7 +33,7 @@[metabase.util :as u][metabase.util.cron :as u.cron][metabase.util.honey-sql-2 :as h2x]- [metabase.util.i18n :refer [deferred-tru trs tru]]+ [metabase.util.i18n :as i18n :refer [deferred-tru trs tru]][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]@@ -1326,7 +1326,7 @@[:reasons[:sequential [:map[:key :keyword]- [:message :string]]]]]]]])+ [:message [:or :string [:fn i18n/localized-string?]]]]]]]]]])(api.macros/defendpoint :get "/:id/settings-available" :- [:map [:settings ::available-settings]]"Get all database-local settings and their availability for the given database."diff --git a/test/metabase/actions/settings_test.clj b/test/metabase/actions/settings_test.cljnew file mode 100644index 00000000000..845867d8d3f--- /dev/null+++ b/test/metabase/actions/settings_test.clj@@ -0,0 +1,71 @@+(ns metabase.actions.settings-test+ (:require+ [clojure.test :refer :all]+ [metabase.driver.util :as driver.u]+ [metabase.settings.core :as setting]+ [metabase.test :as mt]))++(defn- extract-disabled-reasons*+ "Extract disabled reasons from enabled-for-db? call. If no reasons, rethrow the exception."+ [enabled-for-db? db]+ (try+ (when-not (enabled-for-db? db)+ [:no-explicit-reason])+ (catch Exception e+ (or (some->> (:setting/disabled-reasons (ex-data e)) (map :key))+ (throw e)))))++#_{:clj-kondo/ignore [:metabase/test-helpers-use-non-thread-safe-functions]}+(defn- can-configure-data-editing-for-db? [db]+ (try+ (let [driver-support? (fn [db feature] (driver.u/supports? (driver.u/database->driver db) feature db))]+ (setting/validate-settable-for-db! :database-enable-table-editing db driver-support?))+ true+ (catch Exception _+ false)))++(deftest database-enable-table-editing-enabled-for-db?-test+ (testing "enabled-for-db? returns appropriate disabled reasons"+ (mt/with-premium-features #{:table-data-editing}+ (let [enabled-for-db? (:enabled-for-db? (#'setting/resolve-setting :database-enable-table-editing))+ disabled-reasons (partial extract-disabled-reasons* enabled-for-db?)]++ (testing "returns database routing reason for destination databases"+ (mt/with-temp [:model/Database router-db {:initial_sync_status "complete"}+ :model/Database target-db {:initial_sync_status "complete", :router_database_id (:id router-db)}+ :model/Table _ {:db_id (:id router-db) :is_writable true}+ :model/Table _ {:db_id (:id target-db) :is_writable true}]+ (is (= [:setting/database-routing]+ (disabled-reasons router-db)))+ (is (= [:setting/database-routing]+ (disabled-reasons target-db)))+ (is (false? (can-configure-data-editing-for-db? router-db)))+ (is (false? (can-configure-data-editing-for-db? target-db)))))++ (testing "returns sync in progress reason when sync is incomplete"+ (mt/with-temp [:model/Database db {:initial_sync_status "incomplete"}]+ (is (= [:database-metadata/sync-in-progress] (disabled-reasons db)))+ (is (can-configure-data-editing-for-db? db))))++ (testing "succeeds when database has writable tables"+ (mt/with-temp [:model/Database db {:initial_sync_status "complete"}+ :model/Table _table {:db_id (:id db) :is_writable true}]+ (is (= nil (disabled-reasons db)))+ (is (can-configure-data-editing-for-db? db))))++ (testing "returns missing permissions reason when tables have unknown write-ability"+ (mt/with-temp [:model/Database db {:initial_sync_status "complete"}+ :model/Table _table {:db_id (:id db) :is_writable nil}]+ (is (= [:database-metadata/not-populated] (disabled-reasons db)))+ (is (can-configure-data-editing-for-db? db))))++ (testing "returns no writable tables reason when all tables are not writable"+ (mt/with-temp [:model/Database db {:initial_sync_status "complete"}+ :model/Table _table {:db_id (:id db) :is_writable false}]+ (is (= [:permissions/no-writable-table] (disabled-reasons db)))+ (is (false? (can-configure-data-editing-for-db? db)))))++ (testing "returns no writable tables reason when database has no tables"+ (mt/with-temp [:model/Database db {:initial_sync_status "complete"}]+ (is (= [:permissions/no-writable-table] (disabled-reasons db)))+ (is (false? (can-configure-data-editing-for-db? db)))))))))diff --git a/test/metabase/warehouses/api_test.clj b/test/metabase/warehouses/api_test.cljindex ac3de0fef51..5326de9d6bf 100644--- a/test/metabase/warehouses/api_test.clj+++ b/test/metabase/warehouses/api_test.clj@@ -2417,9 +2417,9 @@:api-test-disabled-for-multiple-reasons{:enabled false- :reasons [{:key "driver-feature-missing"+ :reasons [{:key "driver-feature-missing":message "The H2 driver does not support the `jvm-timezone-setting` feature"}- {:key "custom/three"+ {:key "custom/three":message "Never"}]}}(select-keys settings [:unaggregated-query-row-limit