Skip to content

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

Don't reject edits to a dashboard question that sits on other dashboards (#83277)

John Swanson3951ceeda113
diff --git a/src/metabase/queries/db.clj b/src/metabase/queries/db.clj
index 45d31b645c5..fd2943258cb 100644
--- a/src/metabase/queries/db.clj
+++ b/src/metabase/queries/db.clj
@@ -152,11 +152,16 @@
;;; ------------------------------------------- Card statistics -------------------------------------------
(mu/defn dashcard-counts-by-card
- "Rows of `:card_id` and `:count` of DashboardCards for each of `card-ids`."
+ "Rows of `:card_id` and `:count` of DashboardCards each of `card-ids` appears on, directly or as a series."
[card-ids :- [:sequential ::lib.schema.id/card]]
(t2/query {:select [[:%count.* :count] :card_id]
- :from [:report_dashboardcard]
- :where [:in :card_id card-ids]
+ :from [[^:allow-subquery {:union-all [^:allow-subquery {:select [:card_id]
+ :from [:report_dashboardcard]
+ :where [:in :card_id card-ids]}
+ ^:allow-subquery {:select [:card_id]
+ :from [:dashboardcard_series]
+ :where [:in :card_id card-ids]}]}
+ :placements]]
:group-by [:card_id]}))
(mu/defn parameter-card-counts-by-card
diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.clj
index eb0795dca8a..a6a18d92ab1 100644
--- a/src/metabase/queries/models/card.clj
+++ b/src/metabase/queries/models/card.clj
@@ -408,20 +408,27 @@
(not (contains? #{"question" :question} (:type changes))))
(tru "Invalid Dashboard Question: Cannot set `type` on a Dashboard Question")))))
+(defn- other-dashboard-id->name
+ "Map of id to name for the dashboards `card` appears on, excluding its current dashboard and the one `changes`
+ targets."
+ [card changes]
+ (->> (t2/hydrate card :in_dashboards)
+ :in_dashboards
+ (remove #(contains? #{(:dashboard_id changes) (:dashboard_id card)} (:id %)))
+ (map (juxt :id :name))
+ (into {})))
+
(defn- assert-is-valid-dashboard-internal-update [changes card]
- (let [dashboard-id->name (->> (t2/hydrate card :in_dashboards)
- :in_dashboards
- (remove #(or (= (:id %)
- (:dashboard_id changes))
- (= (:id %)
- (:dashboard_id card))))
- (map (juxt :id :name))
- (into {}))]
- (when (and (:dashboard_id changes) (seq dashboard-id->name))
- (throw (ex-info
- (tru "Can''t move question into dashboard. Questions saved in dashboards can''t appear in other dashboards.")
- {:status-code 400
- :other-dashboards dashboard-id->name}))))
+ ;; Clients re-send the current `dashboard_id` on every save, so only a genuine move into a dashboard is checked
+ ;; (#82237).
+ (when (and (api/column-will-change? (:dashboard_id card) (get changes :dashboard_id ::api/not-provided))
+ (:dashboard_id changes))
+ (let [dashboard-id->name (other-dashboard-id->name card changes)]
+ (when (seq dashboard-id->name)
+ (throw (ex-info
+ (tru "Can''t move question into dashboard. Questions saved in dashboards can''t appear in other dashboards.")
+ {:status-code 400
+ :other-dashboards dashboard-id->name})))))
(when-let [reason (invalid-dashboard-internal-card-update-reason? card changes)]
(throw (ex-info reason {:status-code 400
:changes changes
diff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.clj
index 1c6b126804e..0be84711f1c 100644
--- a/test/metabase/queries/models/card_test.clj
+++ b/test/metabase/queries/models/card_test.clj
@@ -59,7 +59,16 @@
(testing "add to a second Dashboard"
(add-card-to-dash! dash-2)
(is (= 2
- (get-dashboard-count))))))))
+ (get-dashboard-count))))
+ (testing "a series placement on another card's dashcard counts too"
+ (mt/with-temp [:model/Card {other-card-id :id} {}
+ :model/DashboardCard {dashcard-id :id} {:card_id other-card-id
+ :dashboard_id (u/the-id dash-1)}
+ :model/DashboardCardSeries _ {:dashboardcard_id dashcard-id
+ :card_id card-id
+ :position 0}]
+ (is (= 3
+ (get-dashboard-count)))))))))
(deftest dropdown-widget-values-usage-count-test
(let [hydrated-count (fn [card] (-> card
@@ -1164,6 +1173,42 @@
(is (card/update-card! {:card-before-update card
:card-updates {:type :question}}))))))
+(deftest updating-dashboard-internal-card-used-on-other-dashboards-test
+ ;; #82237
+ (mt/with-temp [:model/Dashboard {home-dash-id :id} {}
+ :model/Dashboard {other-dash-id :id} {}
+ :model/Dashboard {third-dash-id :id} {}
+ :model/Card card {:dashboard_id home-dash-id}
+ :model/DashboardCard _ {:card_id (:id card) :dashboard_id other-dash-id}]
+ (mt/with-test-user :rasta
+ (testing "re-sending the unchanged dashboard_id alongside an edit succeeds"
+ (is (= "edited"
+ (:name (card/update-card! {:card-before-update card
+ :card-updates {:name "edited" :dashboard_id home-dash-id}})))))
+ (testing "moving it into a different dashboard is still rejected"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Can't move question into dashboard"
+ (card/update-card! {:card-before-update card
+ :card-updates {:dashboard_id third-dash-id}})))))))
+
+(deftest moving-collection-card-into-dashboard-while-on-other-dashboards-test
+ ;; Model-level guard for the narrowed check above; the API layer covers the same moves in
+ ;; `metabase.queries-rest.api.card-test`.
+ (mt/with-temp [:model/Dashboard {dash-id :id} {}
+ :model/Dashboard {other-dash-id :id} {}
+ :model/Card card {}
+ :model/DashboardCard _ {:card_id (:id card) :dashboard_id other-dash-id}]
+ (mt/with-test-user :rasta
+ (testing "without delete-old-dashcards? the move is rejected"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Can't move question into dashboard"
+ (card/update-card! {:card-before-update card
+ :card-updates {:dashboard_id dash-id}}))))
+ (testing "with delete-old-dashcards? the other dashcards are removed and the move succeeds"
+ (is (= dash-id
+ (:dashboard_id (card/update-card! {:card-before-update card
+ :card-updates {:dashboard_id dash-id}
+ :delete-old-dashcards? true}))))
+ (is (not (t2/exists? :model/DashboardCard :card_id (:id card) :dashboard_id other-dash-id)))))))
+
(deftest update-does-not-break
;; There's currently a footgun in Toucan2 - if 1) the result of `before-update` doesn't have an ID, 2) part of your
;; `update` would change a subset of selected rows, and 3) part of your `update` would change *every* selected row
diff --git a/test/metabase/queries_rest/api/card_test.clj b/test/metabase/queries_rest/api/card_test.clj
index a6bfafca9c6..befc69ed3d9 100644
--- a/test/metabase/queries_rest/api/card_test.clj
+++ b/test/metabase/queries_rest/api/card_test.clj
@@ -4563,6 +4563,38 @@
(testing "We can't set the `type`"
(is (mt/user-http-request :crowberto :put 400 (str "card/" card-id) {:type "model"})))))
+(deftest full-card-put-on-dashboard-internal-card-used-on-other-dashboards-test
+ ;; #82237
+ (mt/with-temp [:model/Collection {coll-id :id} {}
+ :model/Dashboard {home-dash-id :id} {:collection_id coll-id}
+ :model/Dashboard {other-dash-id :id} {}
+ :model/Card {card-id :id} {:dashboard_id home-dash-id
+ :dataset_query (mt/mbql-query venues)}
+ :model/DashboardCard _ {:card_id card-id :dashboard_id other-dash-id}]
+ (let [card (t2/select-one :model/Card :id card-id)]
+ (testing "the whole writable card, current dashboard_id included, is accepted the way the FE sends it"
+ (is (=? {:name "edited" :dashboard_id home-dash-id}
+ (mt/user-http-request :crowberto :put 200 (str "card/" card-id)
+ {:name "edited"
+ :cache_ttl nil
+ :type "question"
+ :dataset_query (:dataset_query card)
+ :display "table"
+ :description nil
+ :visualization_settings {}
+ :parameters []
+ :parameter_mappings []
+ :archived false
+ :enable_embedding false
+ :embedding_params nil
+ :collection_id coll-id
+ :dashboard_id home-dash-id
+ :collection_position nil
+ :collection_preview true
+ :result_metadata (:result_metadata card)}))))
+ (testing "the other dashboard still has its dashcard"
+ (is (t2/exists? :model/DashboardCard :card_id card-id :dashboard_id other-dash-id))))))
+
(deftest dashboard-questions-get-autoplaced-on-unarchive-or-placement
(mt/with-temp [:model/Collection {coll-id :id} {}
:model/Dashboard {dash-id :id} {:collection_id coll-id}