Skip to content

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

Made a data app's collection, its cards and its actions read-only for everyone through permissions (#84153)

Timofey Kachalov3f782175f099
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/README.md b/enterprise/backend/src/metabase_enterprise/data_apps/README.md
index 76b86cadcfb..b0de8e2a916 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/README.md
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/README.md
@@ -71,7 +71,8 @@ An author deletes an app by deleting its directory and its collection's files un
`collections/data_apps/` in one commit: the pull deletes the app, and the app's `before-delete`
hook deletes the collection with what it holds. A commit that deletes the directory but keeps the
collection's files still deletes the app and its collection; the next export removes the files,
-and until then a full pull loads them back as a collection no app owns.
+and until then a full pull loads them back as a collection no app owns. That collection is read-only
+like an app's, so only deleting its files from the repository removes it.
## Serving
@@ -124,6 +125,10 @@ Collection access alone does not grant app access.
Every app owns a collection from insertion. If its collection is deleted separately, resource reconciliation recreates it.
+Nobody can write a collection of the `data-apps` namespace, or what it holds, administrators included, with or
+without remote sync: the collection's `can-write?` refuses it, the write permissions of everything that takes them
+from its collection are a path no user holds, and a pull is what changes them.
+
Assignments grant read-only access to the resource collection. Sync restores these grants and removes collection
access from unassigned groups. Assignment changes never change data permissions. Deleting an app deletes its
collection and assignments, but preserves the assigned groups.
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj
index 48f75e9b94b..0cdb88cae38 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/core.clj
@@ -36,14 +36,7 @@
:feature :none
[collection]
(or (= (settings/remote-sync-type) :read-write)
- (not (or (collections/remote-synced-collection? collection)
- ;; a data app's collection is synced with the app; without remote sync nothing writes its files
- (and (settings/remote-sync-enabled)
- (spec/data-apps-namespace-collection?
- {:namespace (if (and (map? collection) (contains? collection :namespace))
- (:namespace collection)
- ;; the root collection has no ID, and nothing in it is synced
- (some-> (u/id collection) remote-sync.db/collection-namespace))}))))))
+ (not (collections/remote-synced-collection? collection))))
(defenterprise table-editable?
"Determines if a table's metadata should be editable.
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
index 8ea85a507cf..cfeedb6e517 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
@@ -580,8 +580,8 @@
(remote-sync.db/insert-rso! (-> row (dissoc :id) (assoc :status_changed_at timestamp))))))
(defn- record-data-app-tables!
- "Record the tables each data app's resources read, after a load or an export, logging a failure: the load's
- transaction has committed and the export's commit is pushed by then, so a failure must not undo their record."
+ "Record the tables each data app's resources read, after a load, logging a failure: the load's transaction has
+ committed by then, so a failure must not undo its record."
[]
(try
(data-apps/record-table-dependencies!)
@@ -1225,7 +1225,6 @@
(doseq [removed-ids (partition-all 500 (find-departed-entities export-rows))]
(remote-sync.db/delete-rsos! removed-ids))
(mark-rows-synced! (remote-sync.db/all-rso-ids) synced sync-timestamp))
- (record-data-app-tables!)
(if (= version :remote-sync/empty-commit)
(do
(log/info "Remote sync full export: re-serialized content matches remote; skipped empty commit")
@@ -1259,7 +1258,6 @@
(doseq [removed-ids (partition-all 500 removed-ids)]
(remote-sync.db/delete-rsos! removed-ids))
(mark-rows-synced! (map :id synced) synced sync-timestamp))
- (record-data-app-tables!)
(if (= version :remote-sync/empty-commit)
(do (log/info "Remote sync incremental export: nothing changed; skipped empty commit")
{:status :success :outcome {:kind "push-skipped"}})
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/data_apps_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/data_apps_test.clj
index 88f112a5c70..d7067c66e16 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/data_apps_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/data_apps_test.clj
@@ -8,7 +8,6 @@
[metabase-enterprise.data-apps.core :as data-apps]
[metabase-enterprise.data-apps.test-util :as data-apps.tu]
[metabase-enterprise.remote-sync.impl :as impl]
- [metabase-enterprise.remote-sync.models.remote-sync-object :as remote-sync.object]
[metabase-enterprise.remote-sync.models.remote-sync-task :as remote-sync.task]
[metabase-enterprise.remote-sync.source :as source]
[metabase-enterprise.remote-sync.source.protocol :as source.p]
@@ -17,6 +16,7 @@
[metabase.actions.core :as actions]
[metabase.collections.models.collection :as collection]
[metabase.driver :as driver]
+ [metabase.events.core :as events]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.search.test-util :as search.tu]
@@ -329,8 +329,8 @@
[mock stem]
(some #(str/starts-with? (last (str/split % #"/")) stem) (resource-files mock)))
-(deftest export-keeps-the-file-of-a-resource-edited-here-test
- (testing "a card and an action edited in Metabase are exported as changed, not removed from the repository"
+(deftest an-apps-collection-is-read-only-test
+ (testing "an admin can't write an app's collection, its cards or its actions, whatever the sync mode"
(with-data-apps-sync
(data-apps.tu/do-with-sources!
(fn [{:keys [action-id]}]
@@ -338,68 +338,113 @@
shop-collection-name shop-collection-eid
[{:entity_id question-eid :name "VenuesList" :query (venues-query)}]
[action-id])
- mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree resources)})]
- (is (= :success (:status (import-at! mock "main" :force? true))))
- (let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)
- copy-id (t2/select-one-pk :model/Action :collection_id (shop-collection-id))]
- (mt/user-http-request :crowberto :put 200 (str "card/" card-id) {:description "edited here"})
- (mt/with-actions-enabled
- (mt/user-http-request :crowberto :put 200 (str "action/" copy-id) {:name "Renamed venue"}))
- (is (= :success (:status (export! mock))))
- (is (file-named mock "venueslist"))
- (is (file-named mock "renamed_venue"))
- (is (some #(str/includes? (get-in @(:files-atom mock) ["main" %]) "edited here") (resource-files mock)))
- (testing "and a pull of what was exported keeps both"
- (is (= :success (:status (import-at! mock "main" :force? true))))
- (is (t2/exists? :model/Card :id card-id))
- (is (t2/exists? :model/Action :id copy-id))))))))))
-
-(deftest a-read-only-instance-refuses-an-edit-to-a-resource-test
- (testing "with remote sync read-only, a card in an app's collection can't be edited here, like any synced card"
+ src (test-helpers/versioned-source :trees {"v0" (shop-tree resources)} :current "v0")
+ mp (mt/metadata-provider)
+ venues-q (lib/->legacy-MBQL (lib/query mp (lib.metadata/table mp (mt/id :venues))))]
+ (is (= :success (:status (import-at! src "v0" :force? true))))
+ (let [collection-id (shop-collection-id)
+ card-id (t2/select-one-pk :model/Card :entity_id question-eid)
+ copy-id (t2/select-one-pk :model/Action :collection_id collection-id)
+ read-only! (fn []
+ (is (=? {:can_write false}
+ (mt/user-http-request :crowberto :get 200 (str "collection/" collection-id))))
+ (is (=? {:can_write false}
+ (mt/user-http-request :crowberto :get 200 (str "card/" card-id))))
+ (mt/user-http-request :crowberto :put 403 (str "collection/" collection-id) {:name "Renamed"})
+ (mt/user-http-request :crowberto :put 403 (str "card/" card-id) {:description "edited here"})
+ (mt/user-http-request :crowberto :put 403 (str "card/" card-id) {:collection_id nil})
+ (mt/user-http-request :crowberto :post 403 "card/collections" {:card_ids [card-id] :collection_id nil})
+ (mt/with-temp [:model/Dashboard {dashboard-id :id} {:name "Elsewhere"}
+ :model/Card {mover-id :id} {:name "Mover" :dataset_query venues-q}]
+ (mt/user-http-request :crowberto :put 403 (str "card/" card-id) {:dashboard_id dashboard-id})
+ (mt/user-http-request :crowberto :post 403 "card/collections" {:card_ids [mover-id] :collection_id collection-id}))
+ (mt/user-http-request :crowberto :delete 403 (str "card/" card-id))
+ (mt/user-http-request :crowberto :post 403 "card"
+ {:name "New"
+ :display "table"
+ :visualization_settings {}
+ :collection_id collection-id
+ :dataset_query venues-q})
+ (mt/with-actions-enabled
+ (mt/user-http-request :crowberto :put 403 (str "action/" copy-id) {:name "Renamed"})))]
+ (testing "on a read-write instance"
+ (mt/with-temporary-setting-values [remote-sync-type :read-write remote-sync-url "https://example.com/repo.git"]
+ (read-only!)))
+ (testing "on a read-only instance"
+ (mt/with-temporary-setting-values [remote-sync-type :read-only remote-sync-url "https://example.com/repo.git"]
+ (read-only!)))
+ (testing "without remote sync"
+ (mt/with-temporary-setting-values [remote-sync-url nil]
+ (read-only!)))
+ (is (=? {:collection_id collection-id :name "VenuesList" :description nil}
+ (t2/select-one :model/Card :id card-id))))))))))
+
+(deftest a-copy-of-an-apps-card-elsewhere-is-a-card-like-any-other-test
+ (testing "copying a card out of an app's collection needs only read access to it"
(with-data-apps-sync
- (let [src (test-helpers/versioned-source :trees {"v0" (shop-tree (question-resources))} :current "v0")]
+ (let [src (test-helpers/versioned-source :trees {"v0" (shop-tree (question-resources))} :current "v0")
+ mp (mt/metadata-provider)]
(is (= :success (:status (import-at! src "v0" :force? true))))
- (let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)]
- (testing "without remote sync the card is not synced, so it can be edited"
- (mt/with-temporary-setting-values [remote-sync-type :read-only remote-sync-url nil]
- (mt/user-http-request :crowberto :put 200 (str "card/" card-id) {:description "edited without sync"})))
+ (is (=? {:collection_id nil}
+ (mt/user-http-request :crowberto :post 200 "card"
+ {:name "Copy"
+ :display "table"
+ :visualization_settings {}
+ :dataset_query (lib/->legacy-MBQL
+ (lib/query mp (lib.metadata/table mp (mt/id :venues))))
+ :source_card_id (t2/select-one-pk :model/Card :entity_id question-eid)})))))))
+
+(deftest a-pull-changes-a-card-in-an-apps-collection-test
+ (testing "the permissions that refuse every write to an app's collection don't stop a pull"
+ (with-data-apps-sync
+ (let [resources (fn [card-name]
+ (data-apps.tu/build-resources shop-collection-name shop-collection-eid
+ [{:entity_id question-eid :name card-name :query (venues-query)}]
+ []))
+ src (test-helpers/versioned-source :trees {"v0" (shop-tree (resources "VenuesList"))
+ "v1" (shop-tree (resources "Renamed on read-write"))
+ "v2" (shop-tree (resources "Renamed on read-only"))}
+ :current "v0")
+ card-name #(t2/select-one-fn :name :model/Card :entity_id question-eid)]
+ (is (= :success (:status (import-at! src "v0" :force? true))))
+ (testing "on a read-write instance"
+ (is (= :success (:status (import-at! src "v1"))))
+ (is (= "Renamed on read-write" (card-name))))
+ (testing "on a read-only instance"
(mt/with-temporary-setting-values [remote-sync-type :read-only remote-sync-url "https://example.com/repo.git"]
- (mt/user-http-request :crowberto :put 403 (str "card/" card-id) {:description "edited here"})
- (testing "and the instance still pulls"
- (is (= :success (:status (import-at! src "v0" :force? true)))))))))))
+ (is (= :success (:status (import-at! src "v2"))))
+ (is (= "Renamed on read-only" (card-name)))))))))
(deftest an-apps-collection-holds-only-what-a-pull-accepts-test
- (testing "what the resource validator would refuse on a pull can't be saved into an app's collection"
+ (testing "what the resource validator would refuse on a pull can't be saved into an app's collection, by a write the
+ permissions don't cover"
(with-data-apps-sync
- (let [src (test-helpers/versioned-source :trees {"v0" (shop-tree (question-resources))} :current "v0")
- mp (mt/metadata-provider)
- q (lib/->legacy-MBQL (lib/query mp (lib.metadata/table mp (mt/id :venues))))]
+ (let [src (test-helpers/versioned-source :trees {"v0" (shop-tree (question-resources))} :current "v0")]
(is (= :success (:status (import-at! src "v0" :force? true))))
(let [collection-id (shop-collection-id)
- card (fn [status card-type]
- (mt/user-http-request :crowberto :post status "card"
- {:name "Saved here" :type card-type :display "table"
- :visualization_settings {} :collection_id collection-id
- :dataset_query q}))]
- (mt/user-http-request :crowberto :post 400 "dashboard" {:name "Saved here" :collection_id collection-id})
- (is (not (t2/exists? :model/Dashboard :name "Saved here")))
- (is (= "A data app's collection can hold only questions, metrics, and query actions"
- (mt/user-http-request :crowberto :post 400 "collection" {:name "Inside" :parent_id collection-id :namespace "data-apps"})))
- (is (not (t2/exists? :model/Collection :name "Inside")))
- (card 400 "model")
- (is (not (t2/exists? :model/Card :collection_id collection-id :type :model)))
- (is (=? {:collection_id collection-id :type :question}
- (t2/select-one :model/Card :id (:id (card 200 "question")))))
+ card! (fn [card-type]
+ (mt/with-temp [:model/Card {card-id :id} {:name "Saved here" :type card-type :collection_id collection-id}]
+ (t2/select-one :model/Card :id card-id)))]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"can hold only questions, metrics, and query actions"
+ (mt/with-temp [:model/Dashboard _ {:name "Saved here" :collection_id collection-id}])))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"can hold only questions, metrics, and query actions"
+ (mt/with-temp [:model/Collection _ {:name "Inside"
+ :location (collection/children-location
+ (t2/select-one :model/Collection :id collection-id))
+ :namespace "data-apps"}])))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"data app's collection" (card! :model)))
+ (is (=? {:collection_id collection-id :type :question} (card! :question)))
(testing "a bookmark is not content"
(mt/user-http-request :crowberto :post 200 (str "bookmark/collection/" collection-id)))
(testing "with the library feature too, whose check runs in the same place"
(mt/with-additional-premium-features #{:library}
- (card 400 "model")))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"data app's collection" (card! :model)))))
(testing "a question a load checks as a possible dashboard question, before its dashboard is known, is a question here"
(is (= (collection/check-allowed-content :question collection-id)
(collection/check-allowed-content :dashboard-question collection-id))))
(mt/with-temp [:model/Dashboard {dashboard-id :id} {:name "Elsewhere"}]
- (mt/user-http-request :crowberto :put 400 (str "dashboard/" dashboard-id) {:collection_id collection-id})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"can hold only questions, metrics, and query actions"
+ (t2/update! :model/Dashboard dashboard-id {:collection_id collection-id})))
(is (nil? (t2/select-one-fn :collection_id :model/Dashboard :id dashboard-id)))))))))
(deftest a-card-in-an-apps-collection-stays-what-a-pull-accepts-test
@@ -414,18 +459,20 @@
(testing "archived, public, or a model"
(mt/with-temporary-setting-values [enable-public-sharing true]
(mt/user-http-request :crowberto :post 400 (str "card/" card-id "/public_link")))
- (mt/user-http-request :crowberto :put 400 (str "card/" card-id) {:archived true})
- (mt/user-http-request :crowberto :put 400 (str "card/" card-id) {:type "model"})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"must be a question or metric that is not archived"
+ (t2/update! :model/Card card-id {:archived true})))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"must be a question or metric that is not archived"
+ (t2/update! :model/Card card-id {:type :model})))
(is (=? {:archived false :public_uuid nil :type :question} (t2/select-one :model/Card :id card-id))))
(testing "a card that is already public can't be moved in"
(mt/with-temp [:model/Card {public-id :id} {:name "Already public"
:dataset_query q
:public_uuid (str (random-uuid))
:made_public_by_id (mt/user->id :crowberto)}]
- (mt/user-http-request :crowberto :put 400 (str "card/" public-id) {:collection_id collection-id})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"must be a question or metric that is not archived"
+ (t2/update! :model/Card public-id {:collection_id collection-id})))
(is (nil? (t2/select-one-fn :collection_id :model/Card :id public-id)))))
- (testing "it can still be edited, and the export keeps its file"
- (mt/user-http-request :crowberto :put 200 (str "card/" card-id) {:description "still editable"})
+ (testing "the export keeps its file"
(is (= :success (:status (export! mock))))
(is (file-named mock "venueslist"))
(is (= :success (:status (import-at! mock "main" :force? true)))))
@@ -433,60 +480,62 @@
(mt/with-temp [:model/Card {other-id :id} {:name "Elsewhere"}]
(mt/user-http-request :crowberto :put 200 (str "card/" other-id) {:archived true}))))))))
-(deftest a-card-another-app-card-reads-stays-in-the-apps-collection-test
- (testing "a card that another card of the app reads can't leave the collection or be deleted: the export would write
- the reader into a file every pull refuses"
+(deftest a-pull-moves-cards-out-of-an-apps-collection-test
+ (testing "a pull whose commit moves a card and the card that reads it out of an app's collection succeeds: the load
+ moves the card read first, while its reader is still in the collection"
(with-data-apps-sync
- (let [mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree (question-resources))})
- mp (mt/metadata-provider)
- venues-q (lib/->legacy-MBQL (lib/query mp (lib.metadata/table mp (mt/id :venues))))]
+ (let [mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree (question-resources))})
+ mp (mt/metadata-provider)]
(is (= :success (:status (import-at! mock "main" :force? true))))
- (let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)
- collection-id (shop-collection-id)
- reads-card (fn [id] (lib/->legacy-MBQL (lib/query mp (lib.metadata/card mp id))))
- reader-id (:id (mt/user-http-request :crowberto :post 200 "card"
- {:name "Reads VenuesList"
- :display "table"
- :visualization_settings {}
- :collection_id collection-id
- :dataset_query (reads-card card-id)}))]
- (testing "the card it reads can't be moved out"
- (mt/user-http-request :crowberto :put 400 (str "card/" card-id) {:collection_id nil})
- (is (= collection-id (t2/select-one-fn :collection_id :model/Card :id card-id))))
- (testing "nor as a dashboard question, which a card other cards read can't become"
- (mt/with-temp [:model/Dashboard {dashboard-id :id} {:name "Elsewhere"}]
- (mt/user-http-request :crowberto :put 400 (str "card/" card-id) {:dashboard_id dashboard-id})
- (is (= collection-id (t2/select-one-fn :collection_id :model/Card :id card-id)))))
- (testing "or deleted"
- (mt/user-http-request :crowberto :delete 400 (str "card/" card-id))
- (is (t2/exists? :model/Card :id card-id)))
- (testing "one request that moves a card in and changes its query from a card outside to a table is fine"
- (mt/with-temp [:model/Card {outside-id :id} {:name "Outside" :dataset_query venues-q}
- :model/Card {mover-id :id} {:name "Mover" :dataset_query (reads-card outside-id)}]
- (mt/user-http-request :crowberto :put 200 (str "card/" mover-id)
- {:collection_id collection-id :dataset_query venues-q})
- (is (= collection-id (t2/select-one-fn :collection_id :model/Card :id mover-id)))))
- (testing "once the reader has left, the card can leave too"
- (mt/user-http-request :crowberto :put 200 (str "card/" reader-id) {:collection_id nil})
- (is (nil? (t2/select-one-fn :collection_id :model/Card :id reader-id)))
- (mt/user-http-request :crowberto :put 200 (str "card/" card-id) {:collection_id nil})
- (is (nil? (t2/select-one-fn :collection_id :model/Card :id card-id)))))))))
+ (let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)
+ reader (first (t2/insert-returning-instances!
+ :model/Card
+ {:name "Reads VenuesList"
+ :display "table"
+ :visualization_settings {}
+ :creator_id (mt/user->id :crowberto)
+ :database_id (mt/id)
+ :collection_id (shop-collection-id)
+ :dataset_query (lib/query mp (lib.metadata/card mp card-id))}))
+ _ (events/publish-event! :event/card-create {:object reader :user-id (mt/user->id :crowberto)})
+ _ (is (= :success (:status (export! mock))))
+ synced-eid "shopCardsMovedOutColl"
+ synced-dir (str "collections/" synced-eid "_moved_out/")
+ moved-eids #{question-eid (:entity_id reader)}
+ ;; the commit an author pushes: both cards' files in an ordinary synced collection
+ moved-out (reduce-kv (fn [tree path content]
+ (let [entity (yaml/parse-string content)]
+ (if (contains? moved-eids (:entity_id entity))
+ (assoc tree
+ (str synced-dir "cards/" (last (str/split path #"/")))
+ (yaml/generate-string (assoc entity :collection_id synced-eid)))
+ (assoc tree path content))))
+ {(str synced-dir synced-eid "_moved_out.yaml")
+ (test-helpers/generate-collection-yaml synced-eid "Moved out" :is-remote-synced true)}
+ (get @(:files-atom mock) "main"))
+ src (test-helpers/versioned-source :trees {"v1" moved-out} :current "v1")]
+ (is (= 2 (count (filter #(str/starts-with? % (str synced-dir "cards/")) (keys moved-out)))))
+ (let [result (import-at! src "v1" :force? true)]
+ (is (= :success (:status result)) (:message result)))
+ (is (= #{(t2/select-one-pk :model/Collection :entity_id synced-eid)}
+ (t2/select-fn-set :collection_id :model/Card :entity_id [:in moved-eids]))))))))
(deftest deleting-an-app-deletes-cards-that-read-each-other-test
- (testing "the refusal to delete a card another app card reads is the endpoint's: deleting the app deletes its
- collection with every card in it, readers and read"
+ (testing "deleting the app deletes its collection with every card in it, readers and read"
(with-data-apps-sync
(let [mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree (question-resources))})
mp (mt/metadata-provider)]
(is (= :success (:status (import-at! mock "main" :force? true))))
(let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)
collection-id (shop-collection-id)
- reader-id (:id (mt/user-http-request :crowberto :post 200 "card"
- {:name "Reads VenuesList"
- :display "table"
- :visualization_settings {}
- :collection_id collection-id
- :dataset_query (lib/->legacy-MBQL (lib/query mp (lib.metadata/card mp card-id)))}))]
+ reader-id (t2/insert-returning-pk! :model/Card
+ {:name "Reads VenuesList"
+ :display "table"
+ :visualization_settings {}
+ :creator_id (mt/user->id :crowberto)
+ :database_id (mt/id)
+ :collection_id collection-id
+ :dataset_query (lib/query mp (lib.metadata/card mp card-id))})]
(mt/user-http-request :crowberto :delete 204 "apps/shop")
(is (not (t2/exists? :model/Collection :id collection-id)))
(is (not (t2/exists? :model/Card :id [:in [card-id reader-id]]))))))))
@@ -739,7 +788,8 @@
(let [mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree (question-resources))})]
(is (= :success (:status (import-at! mock "main" :force? true))))
(let [before (resource-files mock)]
- (mt/user-http-request :crowberto :put 400 (str "collection/" (shop-collection-id)) {:archived true})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"cannot move a data app's collection to the trash"
+ (t2/update! :model/Collection (shop-collection-id) {:archived true})))
(is (= :success (:status (export! mock))))
(is (= before (resource-files mock)))
(testing "deleting the app still deletes its collection"
@@ -835,22 +885,22 @@
:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :venues)))}]
(let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)
collection-id (shop-collection-id)
- reading (fn [id] (lib/->legacy-MBQL (lib/query mp (lib.metadata/card mp id))))
- save (fn [status collection-id query]
- (mt/user-http-request :crowberto :post status "card"
- {:name "Saved" :type "question" :display "table"
- :visualization_settings {} :collection_id collection-id
- :dataset_query query}))]
- (save 400 collection-id (reading other-id))
- (is (not (t2/exists? :model/Card :name "Saved")))
+ reading (fn [id] (lib/query mp (lib.metadata/card mp id)))
+ save! (fn [query]
+ (mt/with-temp [:model/Card _ {:name "Saved" :collection_id collection-id :dataset_query query}]))]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"can read only the app's own cards"
+ (save! (reading other-id))))
(let [query-before (t2/select-one-fn :dataset_query :model/Card :id card-id)]
- (mt/user-http-request :crowberto :put 400 (str "card/" card-id) {:dataset_query (reading other-id)})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"can read only the app's own cards"
+ (t2/update! :model/Card card-id {:dataset_query (reading other-id)})))
(is (= query-before (t2/select-one-fn :dataset_query :model/Card :id card-id))))
(testing "one of the app's own cards is fine"
- (save 200 collection-id (reading own-id))
- (mt/user-http-request :crowberto :put 200 (str "card/" card-id) {:dataset_query (reading own-id)}))
+ (save! (reading own-id))
+ (t2/update! :model/Card card-id {:dataset_query (reading own-id)}))
(testing "as is a card elsewhere reading the card outside"
- (save 200 nil (reading other-id)))))))))
+ (mt/user-http-request :crowberto :post 200 "card"
+ {:name "Saved" :type "question" :display "table" :visualization_settings {}
+ :dataset_query (lib/->legacy-MBQL (reading other-id))}))))))))
(deftest a-card-in-an-apps-collection-cant-take-embedding-settings-test
(testing "embedding parameters would make the export write a file every pull refuses, with embedding itself left off"
@@ -859,7 +909,8 @@
(is (= :success (:status (import-at! mock "main" :force? true))))
(let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)]
(mt/with-temporary-setting-values [enable-embedding-modular true]
- (mt/user-http-request :crowberto :put 400 (str "card/" card-id) {:embedding_params {}})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"must be a question or metric that is not archived"
+ (t2/update! :model/Card card-id {:embedding_params {}})))
(testing "a card elsewhere takes them"
(mt/with-temp [:model/Card {other-id :id} {:name "Elsewhere"}]
(mt/user-http-request :crowberto :put 200 (str "card/" other-id) {:embedding_params {}}))))
@@ -873,17 +924,16 @@
(is (= :success (:status (import-at! mock "main" :force? true))))
(let [collection-id (shop-collection-id)
before (resource-files mock)]
- (mt/with-additional-premium-features #{:official-collections}
- (mt/user-http-request :crowberto :put 400 (str "collection/" collection-id) {:authority_level "official"}))
- (mt/with-temp [:model/Collection {other-id :id} {:name "Other root" :namespace :data-apps}]
- (mt/user-http-request :crowberto :put 400 (str "collection/" collection-id) {:parent_id other-id}))
- (testing "a rename still works"
- (mt/user-http-request :crowberto :put 200 (str "collection/" collection-id) {:name "Renamed"}))
- (is (=? {:authority_level nil :location "/" :name "Renamed"} (t2/select-one :model/Collection :id collection-id)))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"cannot move a data app's collection to the trash or make it official"
+ (t2/update! :model/Collection collection-id {:authority_level "official"})))
+ (mt/with-temp [:model/Collection other {:name "Other root" :namespace :data-apps}]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"You cannot move a data app's collection"
+ (collection/move-collection! (t2/select-one :model/Collection :id collection-id)
+ (collection/children-location other)))))
+ (is (=? {:authority_level nil :location "/"} (t2/select-one :model/Collection :id collection-id)))
(is (= :success (:status (export! mock))))
- (testing "and the export writes the collection's files, under the new name"
- (is (= (count before) (count (resource-files mock))))
- (is (file-named mock "venueslist")))
+ (testing "and the export keeps the collection's files"
+ (is (= before (resource-files mock))))
(is (= :success (:status (import-at! mock "main" :force? true)))))))))
(deftest an-action-in-an-apps-collection-cant-be-archived-test
@@ -898,17 +948,15 @@
(is (= :success (:status (import-at! mock "main" :force? true))))
(let [copy-id (t2/select-one-pk :model/Action :collection_id (shop-collection-id))
before (resource-files mock)]
- (mt/with-actions-enabled
- (mt/user-http-request :crowberto :put 400 (str "action/" copy-id) {:archived true})
- (testing "it can still be renamed"
- (mt/user-http-request :crowberto :put 200 (str "action/" copy-id) {:name "Renamed"}))
- (testing "and the source can still be archived"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"can't be archived"
+ (t2/update! :model/Action copy-id {:archived true})))
+ (testing "the source can still be archived"
+ (mt/with-actions-enabled
(mt/user-http-request :crowberto :put 200 (str "action/" action-id) {:archived true})))
(is (false? (t2/select-one-fn :archived :model/Action :id copy-id)))
(is (= :success (:status (export! mock))))
- (testing "and the export keeps the action's file, under its new name"
- (is (= (count before) (count (resource-files mock))))
- (is (file-named mock "renamed"))))))))))
+ (testing "and the export keeps the action's file"
+ (is (= before (resource-files mock)))))))))))
(deftest an-apps-collection-cant-be-marked-remote-synced-test
(testing "an app's collection is synced with its app, and a pull refuses its file once it says remote-synced"
@@ -940,14 +988,15 @@
card-id (t2/select-one-pk :model/Card :entity_id question-eid)
reads (fn [id]
(let [tag (str "#" id)]
- {:database (mt/id)
- :type :native
- :native {:query (str "SELECT * FROM {{" tag "}}")
- :template-tags {tag {:type :card
- :card-id id
- :name tag
- :display-name tag
- :id (str (random-uuid))}}}}))]
+ (lib/query (mt/metadata-provider)
+ {:database (mt/id)
+ :type :native
+ :native {:query (str "SELECT * FROM {{" tag "}}")
+ :template-tags {tag {:type :card
+ :card-id id
+ :name tag
+ :display-name tag
+ :id (str (random-uuid))}}}})))]
(mt/with-actions-enabled
(mt/with-temporary-setting-values [enable-public-sharing true]
(mt/user-http-request :crowberto :post 400 (str "action/" copy-id "/public_link"))
@@ -956,10 +1005,12 @@
(mt/user-http-request :crowberto :post 200 (str "action/" action-id "/public_link"))))
(mt/with-temp [:model/Card {outside-id :id} {:name "Outside"}]
(let [query-before (t2/select-one-fn :dataset_query :model/QueryAction :action_id copy-id)]
- (mt/user-http-request :crowberto :put 400 (str "action/" copy-id) {:dataset_query (reads outside-id)})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"can read only the app's own cards"
+ (actions/update! {:id copy-id :dataset_query (reads outside-id)}
+ (actions/select-action :id copy-id))))
(is (= query-before (t2/select-one-fn :dataset_query :model/QueryAction :action_id copy-id)))))
(testing "it can read the app's own card"
- (mt/user-http-request :crowberto :put 200 (str "action/" copy-id) {:dataset_query (reads card-id)})
+ (actions/update! {:id copy-id :dataset_query (reads card-id)} (actions/select-action :id copy-id))
(is (= #{card-id}
(into #{} (keep :card-id)
(filter map? (tree-seq coll? seq (t2/select-one-fn :dataset_query :model/QueryAction :action_id copy-id))))))))
@@ -976,26 +1027,6 @@
(is (t2/exists? :model/DataApp :name "shop"))
(is (= "v0" (remote-sync.task/last-version)))))))
-(deftest the-bulk-move-endpoint-moves-app-cards-through-the-model-test
- (testing "POST /api/card/collections runs the checks a PUT runs, and the export sees the move"
- (with-data-apps-sync
- (let [mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree (question-resources))})
- mp (mt/metadata-provider)
- q (lib/->legacy-MBQL (lib/query mp (lib.metadata/table mp (mt/id :venues))))]
- (is (= :success (:status (import-at! mock "main" :force? true))))
- (let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)
- collection-id (shop-collection-id)]
- (testing "a model can't be moved in"
- (mt/with-temp [:model/Card {model-id :id} {:name "A model" :type :model :dataset_query q}]
- (mt/user-http-request :crowberto :post 400 "card/collections" {:card_ids [model-id] :collection_id collection-id})
- (is (nil? (t2/select-one-fn :collection_id :model/Card :id model-id)))))
- (testing "the app's card moved out is gone from the app's files on the next export"
- (mt/user-http-request :crowberto :post 200 "card/collections" {:card_ids [card-id] :collection_id nil})
- (is (nil? (t2/select-one-fn :collection_id :model/Card :id card-id)))
- (is (= :success (:status (export! mock))))
- (is (not (file-named mock "venueslist")))
- (is (= :success (:status (import-at! mock "main" :force? true))))))))))
-
(deftest a-refused-pull-names-the-file-whatever-its-name-test
(testing "a validator failure is reported as what it is when a path has \"branch\" in it, not as a branch error"
(with-data-apps-sync
@@ -1014,35 +1045,16 @@
(is (str/includes? (:message result) "must be a question or metric"))
(is (str/includes? (:message result) card-file))))))
-(deftest an-export-records-the-tables-test
- (testing "editing a resource card and exporting is how an app changes on a read-write instance, and the permission
- warnings follow the export rather than the next pull"
+(deftest an-export-leaves-the-tables-an-app-reads-alone-test
+ (testing "only a pull changes an app's cards, so a pull records the tables they read and an export leaves the record
+ as it is"
(with-data-apps-sync
- (let [mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree (question-resources))})
- mp (mt/metadata-provider)]
- (is (= :success (:status (import-at! mock "main" :force? true))))
- (is (= [(mt/id :venues)] (t2/select-one-fn :table_ids :model/DataApp :name "shop")))
- (let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)]
- (mt/user-http-request :crowberto :put 200 (str "card/" card-id)
- {:dataset_query (lib/->legacy-MBQL (lib/query mp (lib.metadata/table mp (mt/id :checkins))))})
- (is (= :success (:status (export! mock))))
- (is (= [(mt/id :checkins)] (t2/select-one-fn :table_ids :model/DataApp :name "shop"))))))))
-
-(deftest an-export-whose-table-record-fails-still-records-the-export-test
- (testing "the commit is pushed before the tables are recorded, so a failure there must not undo the record of it"
- (with-data-apps-sync
- (let [mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree (question-resources))})
- mp (mt/metadata-provider)]
+ (let [mock (test-helpers/create-mock-source :initial-files {"main" (shop-tree (question-resources))})]
(is (= :success (:status (import-at! mock "main" :force? true))))
- (let [card-id (t2/select-one-pk :model/Card :entity_id question-eid)]
- (mt/user-http-request :crowberto :put 200 (str "card/" card-id)
- {:dataset_query (lib/->legacy-MBQL (lib/query mp (lib.metadata/table mp (mt/id :checkins))))})
- (is (seq (remote-sync.object/dirty-rows)))
- (mt/with-dynamic-fn-redefs [data-apps/record-table-dependencies! (fn [] (throw (ex-info "the tables can't be recorded" {})))]
- (let [result (export! mock)]
- (is (= :success (:status result)) (:message result))))
- (testing "the export is recorded: the rows are synced"
- (is (empty? (remote-sync.object/dirty-rows)))))))))
+ (mt/user-http-request :crowberto :put 200 "apps/shop" {:bundle "B2"})
+ (t2/update! :model/DataApp :name "shop" {:table_ids [(mt/id :checkins)]})
+ (is (= :success (:status (export! mock))))
+ (is (= [(mt/id :checkins)] (t2/select-one-fn :table_ids :model/DataApp :name "shop")))))))
(defn- generated-app-tree
"The repo files of the `shop` app as `/api/apps/generate` writes them: its manifest and collection, a question
diff --git a/frontend/src/metabase-types/api/collection.ts b/frontend/src/metabase-types/api/collection.ts
index 3b5885366fa..89225e438a0 100644
--- a/frontend/src/metabase-types/api/collection.ts
+++ b/frontend/src/metabase-types/api/collection.ts
@@ -17,6 +17,7 @@ export type CollectionNamespace =
| "snippets"
| "transforms"
| "data-actions"
+ | "data-apps"
| "analytics"
| "tenant-specific"
| "shared-tenant-collection";
diff --git a/frontend/src/metabase/collections/components/CollectionMenu/CollectionMenu.tsx b/frontend/src/metabase/collections/components/CollectionMenu/CollectionMenu.tsx
index 23db6ba4f1e..1767db417a3 100644
--- a/frontend/src/metabase/collections/components/CollectionMenu/CollectionMenu.tsx
+++ b/frontend/src/metabase/collections/components/CollectionMenu/CollectionMenu.tsx
@@ -2,6 +2,7 @@ import { type ReactNode, useState } from "react";
import { t } from "ttag";
import {
+ isDataAppCollection,
isDedicatedTenantCollectionRoot,
isInstanceAnalyticsCustomCollection,
isPersonalCollection,
@@ -85,7 +86,9 @@ export const CollectionMenu = ({
isAdmin &&
!isPersonal &&
!isDedicatedTenantCollectionRoot(collection) &&
- !isSharedTenantCollection
+ !isSharedTenantCollection &&
+ // a data app's collection takes its permissions from the groups assigned to the app
+ !isDataAppCollection(collection)
) {
editItems.push(
<Menu.Item
diff --git a/frontend/src/metabase/collections/components/CollectionMenu/tests/common.unit.spec.tsx b/frontend/src/metabase/collections/components/CollectionMenu/tests/common.unit.spec.tsx
index 3fd54332b18..07559be20b2 100644
--- a/frontend/src/metabase/collections/components/CollectionMenu/tests/common.unit.spec.tsx
+++ b/frontend/src/metabase/collections/components/CollectionMenu/tests/common.unit.spec.tsx
@@ -43,6 +43,18 @@ describe("CollectionMenu", () => {
expect(queryIcon("ellipsis")).not.toBeInTheDocument();
});
+ it("should not be able to edit permissions for a data app's collection", () => {
+ setup({
+ collection: createMockCollection({
+ namespace: "data-apps",
+ can_write: false,
+ }),
+ isAdmin: true,
+ });
+
+ expect(queryIcon("ellipsis")).not.toBeInTheDocument();
+ });
+
it("should not be able to edit permissions for personal subcollections", async () => {
setup({
collection: createMockCollection({
diff --git a/frontend/src/metabase/common/collections/utils.ts b/frontend/src/metabase/common/collections/utils.ts
index fea2c6d587f..6c2a97a8507 100644
--- a/frontend/src/metabase/common/collections/utils.ts
+++ b/frontend/src/metabase/common/collections/utils.ts
@@ -131,6 +131,12 @@ export function isInstanceAnalyticsCustomCollection(
);
}
+export function isDataAppCollection(
+ collection: Pick<Collection, "namespace">,
+): boolean {
+ return collection.namespace === "data-apps";
+}
+
export function isSyncedCollection(collection: Partial<Collection>): boolean {
return PLUGIN_COLLECTIONS.isSyncedCollection(collection);
}
diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.clj
index 5593a8dca28..6f4299bbe9d 100644
--- a/src/metabase/collections/models/collection.clj
+++ b/src/metabase/collections/models/collection.clj
@@ -376,6 +376,8 @@
([instance]
(and (not (default-audit-collection? instance))
(not (is-trash-or-descendant? instance))
+ ;; not even an administrator edits a data app's collection: only a pull of its app changes it
+ (not= (keyword (:namespace instance)) data-apps-ns)
(mi/current-user-has-full-permissions? :write instance)
(remote-sync/collection-editable? instance)))
([_model pk]
diff --git a/src/metabase/permissions/db.clj b/src/metabase/permissions/db.clj
index cf761ffa325..3a69eef497d 100644
--- a/src/metabase/permissions/db.clj
+++ b/src/metabase/permissions/db.clj
@@ -549,6 +549,11 @@
[collection-id :- ::lib.schema.id/collection]
(t2/select-one :model/Collection :id collection-id))
+(mu/defn collection-namespace
+ "The namespace of the Collection with `collection-id`."
+ [collection-id :- ::lib.schema.id/collection]
+ (t2/select-one-fn :namespace :model/Collection :id collection-id))
+
(mu/defn personal-or-descendant-collection-ids
"The IDs among `collection-ids` that are personal Collections, or descendants of one."
[collection-ids :- [:set ::lib.schema.id/collection]]
diff --git a/src/metabase/permissions/models/permissions.clj b/src/metabase/permissions/models/permissions.clj
index b276116d399..ee4c19501f0 100644
--- a/src/metabase/permissions/models/permissions.clj
+++ b/src/metabase/permissions/models/permissions.clj
@@ -307,14 +307,27 @@
(derive ::mi/read-policy.full-perms-for-perms-set)
(derive ::mi/write-policy.full-perms-for-perms-set))
+(defn data-apps-namespace-collection?
+ "Is `collection-or-id` a collection of the data apps namespace? Reads the namespace from the app DB unless given a
+ collection with `:namespace`."
+ [collection-or-id]
+ (= "data-apps"
+ (some-> (if (and (map? collection-or-id) (contains? collection-or-id :namespace))
+ (:namespace collection-or-id)
+ ;; the root collection has no ID, and is the default namespace's
+ (some-> (u/id collection-or-id) permissions.db/collection-namespace))
+ name)))
+
(defmethod mi/perms-objects-set :perms/use-parent-collection-perms
[instance read-or-write]
- (if (or (= read-or-write :read)
- (remote-sync/collection-editable? (or (:collection instance) (:collection_id instance))))
- (perms-objects-set-for-parent-collection (:collection_id instance) read-or-write)
- ;; We need to return a dummy permissions string that cannot possibly belong to a user in
- ;; the case where an instance is not syncable due to remote-sync being in ':production' mode
- #{"___no-remote-sync-access"}))
+ (let [collection (or (:collection instance) (:collection_id instance))]
+ (if (or (= read-or-write :read)
+ ;; not even an administrator edits what a data app's collection holds: only a pull of its app changes it
+ (and (not (data-apps-namespace-collection? collection))
+ (remote-sync/collection-editable? collection)))
+ (perms-objects-set-for-parent-collection (:collection_id instance) read-or-write)
+ ;; a path that no permissions set holds, not even an administrator's `/`
+ #{"___no-write-access"})))
(methodical/defmethod t2/batched-hydrate [:perms/use-parent-collection-perms :can_write]
[_model k models]
diff --git a/src/metabase/queries/card_write_checks.clj b/src/metabase/queries/card_write_checks.clj
index 144b26b55e0..980f264e63c 100644
--- a/src/metabase/queries/card_write_checks.clj
+++ b/src/metabase/queries/card_write_checks.clj
@@ -120,14 +120,6 @@
(api/create-check :model/Card {:collection_id (actual-collection-id card)})
(check-no-save-cycle! ::no-id query)))
-(defn check-allowed-to-delete-card!
- "Throw a 400 when `card` is in a data app's collection and another card there reads it. For `DELETE /api/card/:id`
- rather than the delete hook: deleting the collection deletes its cards together, readers and read."
- [{:keys [id collection_id]}]
- (when-let [reader (card/data-app-card-reader collection_id id)]
- (throw (ex-info (tru "Card {0} in the data app''s collection reads this card, so it can''t be deleted." reader)
- {:status-code 400}))))
-
(mu/defn check-allowed-to-update-card!
"The post-write permission/validation stack for updating a card, mirroring `PUT /api/card/:id`.
Run *after* the card's write-check: `card-before-update` is the existing card, `card-updates`
diff --git a/src/metabase/queries/core.clj b/src/metabase/queries/core.clj
index 807ca23f801..112ea8a0009 100644
--- a/src/metabase/queries/core.clj
+++ b/src/metabase/queries/core.clj
@@ -45,7 +45,6 @@
[metabase.queries.card-write-checks
actual-collection-id
check-allowed-to-create-card!
- check-allowed-to-delete-card!
check-allowed-to-run-query!
check-allowed-to-update-card!
check-card-can-be-saved!
diff --git a/src/metabase/queries/db.clj b/src/metabase/queries/db.clj
index 66d6b744518..bc581e8891e 100644
--- a/src/metabase/queries/db.clj
+++ b/src/metabase/queries/db.clj
@@ -596,8 +596,3 @@
(t2/select-pks-set :model/Card {:where [:and
[:in :id card-ids]
[:or [:= :collection_id nil] [:not= :collection_id collection-id]]]}))
-
-(defn other-cards-in-collection
- "The cards in the collection with `collection-id`, leaving out the card with `card-id`."
- [collection-id card-id]
- (t2/select :model/Card :collection_id collection-id :id [:not= card-id]))
diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.clj
index eb077297d1f..439836d5269 100644
--- a/src/metabase/queries/models/card.clj
+++ b/src/metabase/queries/models/card.clj
@@ -1022,25 +1022,6 @@
(str/join ", " (sort outside)))
{:status-code 400}))))))
-(defn data-app-card-reader
- "The ID of a card in the data app's collection `collection-id` that reads the card with `card-id`, if one does."
- [collection-id card-id]
- (when (and (some? collection-id)
- (perms/data-app-collection? collection-id))
- (some (fn [other]
- (when (some #(= {:model "Card" :id card-id} (select-keys (last %) [:model :id]))
- (serdes/serialization-dependencies "Card" other))
- (:id other)))
- (queries.db/other-cards-in-collection collection-id card-id))))
-
-(defn- check-data-app-card-stays
- "Throws when the card with `id` leaves the data app's collection `from` while another card there reads it."
- [id from]
- ;; the next export would write the reader into a file every pull refuses
- (when-let [reader (data-app-card-reader from id)]
- (throw (ex-info (tru "Card {0} in the data app''s collection reads this card, so it can''t leave the collection." reader)
- {:status-code 400}))))
-
(t2/define-before-insert :model/Card
[card]
(check-timeline-visibility-permissions! card *copy-source-card*)
@@ -1129,8 +1110,6 @@
(when (or (contains? changes :visualization_settings) (contains? changes :display))
(check-timeline-visibility-permissions! card original))
(check-allowed-content card changes)
- (when (contains? changes :collection_id)
- (check-data-app-card-stays (:id card) (:collection_id original)))
(-> card
(dissoc :verified-result-metadata?)
(migrate-schema-governed-columns original changes)
diff --git a/src/metabase/queries_rest/api/card.clj b/src/metabase/queries_rest/api/card.clj
index a5323d76c46..71ae3d9774d 100644
--- a/src/metabase/queries_rest/api/card.clj
+++ b/src/metabase/queries_rest/api/card.clj
@@ -673,7 +673,6 @@
(let [card (api/write-check :model/Card id)
;; the database deletes the card's actions through the foreign key, with no events of their own
actions (queries-rest.db/actions-for-model id)]
- (queries/check-allowed-to-delete-card! card)
(queries-rest.db/delete-card! id)
(events/publish-event! :event/card-delete {:object card :user-id api/*current-user-id*})
(doseq [action actions]
diff --git a/test/metabase/actions/models_test.clj b/test/metabase/actions/models_test.clj
index f66c2029bfc..fc462b71e73 100644
--- a/test/metabase/actions/models_test.clj
+++ b/test/metabase/actions/models_test.clj
@@ -6,9 +6,11 @@
[clojure.test :refer :all]
[metabase.actions.models :as action]
[metabase.actions.schema :as actions.schema]
+ [metabase.api.common :as api]
[metabase.driver :as driver]
[metabase.driver.mysql :as mysql]
[metabase.lib.core :as lib]
+ [metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
[metabase.query-processor.preprocess :as qp.preprocess]
[metabase.sync.core :as sync]
@@ -230,19 +232,49 @@
(deftest query-action-without-model-test
(mt/test-drivers (mt/normal-drivers-with-feature :actions/custom)
(testing "a query action can be inserted and updated without a model, and keeps its own collection"
- (mt/with-temp [:model/Collection {coll-id :id} {:namespace "data-actions"}]
+ (mt/with-temp [:model/Collection {coll-id :id} {:namespace "data-actions"}
+ :model/Collection {app-id :id} {:namespace "data-apps"}]
(mt/with-model-cleanup [:model/Action]
- (let [action-id (action/insert! (lib/normalize ::actions.schema/action.for-insert
- {:type :query
- :name "No model"
- :collection_id coll-id
- :database_id (mt/id)
- :dataset_query (mt/native-query {:query "update categories set name = 'x' where id = 1"})}))]
+ (let [insert! (fn [collection-id]
+ (action/insert! (lib/normalize ::actions.schema/action.for-insert
+ {:type :query
+ :name "No model"
+ :collection_id collection-id
+ :database_id (mt/id)
+ :dataset_query (mt/native-query {:query "update categories set name = 'x' where id = 1"})})))
+ action-id (insert! coll-id)]
(is (=? {:model_id nil :collection_id coll-id :database_id (mt/id)}
(action/select-action :id action-id)))
(action/update! {:id action-id :name "Renamed"} (action/select-action :id action-id))
(is (=? {:name "Renamed" :model_id nil :collection_id coll-id}
- (action/select-action :id action-id)))))))))
+ (action/select-action :id action-id)))
+ (testing "a data app collection takes one too"
+ (is (=? {:model_id nil :collection_id app-id}
+ (action/select-action :id (insert! app-id)))))))))))
+
+(deftest action-in-a-data-apps-collection-is-read-only-test
+ (testing "Nobody writes an action in a collection of the data apps namespace, administrators included"
+ (mt/with-temp [:model/Collection {app-id :id} {:namespace "data-apps"}
+ :model/Collection {coll-id :id} {:namespace "data-actions"}]
+ (mt/with-model-cleanup [:model/Action]
+ (let [action-in (fn [collection-id]
+ (lib/normalize ::actions.schema/action.for-insert
+ {:type :query
+ :name "No model"
+ :collection_id collection-id
+ :database_id (mt/id)
+ :dataset_query (mt/native-query {:query "update categories set name = 'x' where id = 1"})}))
+ action (action/select-action :id (action/insert! (action-in app-id)))
+ elsewhere (action/select-action :id (action/insert! (action-in coll-id)))]
+ (mt/with-test-user :crowberto
+ (is (false? (mi/can-write? action)))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"You don't have permissions to do that."
+ (api/write-check :model/Action (:id action))))
+ (is (false? (:can_write (t2/hydrate action :can_write))))
+ (testing "nor creates one there, or moves one in"
+ (is (false? (mi/can-create? :model/Action (action-in app-id))))
+ (is (true? (mi/can-write? elsewhere)))
+ (is (false? (mi/can-update? elsewhere {:collection_id app-id}))))))))))
(deftest implicit-action-requires-model-test
(mt/test-drivers (mt/normal-drivers-with-feature :actions/custom)
diff --git a/test/metabase/actions_rest/api_test.clj b/test/metabase/actions_rest/api_test.clj
index 17ce8317c9d..0259ba1a39a 100644
--- a/test/metabase/actions_rest/api_test.clj
+++ b/test/metabase/actions_rest/api_test.clj
@@ -629,11 +629,10 @@
(is (= model-collection (t2/select-one-fn :collection_id :model/Action :id action-id)))))))))
(deftest action-without-model-collection-guards-test
- (testing "an action without a model can only go in the data actions root or an existing, unarchived data actions or data app collection"
+ (testing "an action without a model can only go in the data actions root or an existing, unarchived data actions collection"
(mt/with-actions-test-data-and-actions-enabled
(mt/with-model-cleanup [:model/Action]
(mt/with-temp [:model/Collection {coll-id :id} {:namespace "data-actions"}
- :model/Collection {app-id :id} {:namespace "data-apps"}
:model/Collection {default-id :id} {}
:model/Collection {snippets-id :id} {:namespace "snippets"}
:model/Collection {archived-id :id} {:namespace "data-actions" :archived true}]
@@ -643,8 +642,6 @@
(str (mt/user-http-request :crowberto :post 400 "action" (model-less-query-action default-id)))))
(is (= "Not found."
(mt/user-http-request :crowberto :post 404 "action" (model-less-query-action Integer/MAX_VALUE))))
- (is (=? {:collection_id app-id}
- (mt/user-http-request :crowberto :post 200 "action" (model-less-query-action app-id))))
(let [path (str "action/" (:id (mt/user-http-request :crowberto :post 200 "action" (model-less-query-action coll-id))))]
(mt/user-http-request :crowberto :put 400 path {:collection_id archived-id})
(mt/user-http-request :crowberto :put 400 path {:collection_id default-id})
diff --git a/test/metabase/collections/models/collection_test.clj b/test/metabase/collections/models/collection_test.clj
index fa7865ea3b5..de89c1f8a86 100644
--- a/test/metabase/collections/models/collection_test.clj
+++ b/test/metabase/collections/models/collection_test.clj
@@ -3346,6 +3346,13 @@
(is (true? (mi/can-read? dashboards)))
(is (false? (mi/can-write? dashboards)))))))))
+(deftest data-apps-collection-is-read-only-test
+ (testing "Nobody writes a collection of the data apps namespace, administrators included"
+ (mt/with-temp [:model/Collection collection {:namespace "data-apps"}]
+ (mt/with-test-user :crowberto
+ (is (false? (mi/can-write? collection)))
+ (is (false? (mi/can-write? :model/Collection (:id collection))))))))
+
(deftest create-library
(mt/with-empty-h2-app-db!
(testing "Can create a library if none exist"
diff --git a/test/metabase/permissions/models/permissions_test.clj b/test/metabase/permissions/models/permissions_test.clj
index 7a29e7f1a32..da78f4d9295 100644
--- a/test/metabase/permissions/models/permissions_test.clj
+++ b/test/metabase/permissions/models/permissions_test.clj
@@ -308,6 +308,18 @@
(testing "admin can create dashboard in root collection"
(is (true? (mi/can-create? :model/Dashboard {}))))))))
+(deftest content-of-a-data-apps-collection-is-read-only-test
+ (testing "Nobody writes what takes its permissions from a collection of the data apps namespace, administrators included"
+ (mt/with-temp [:model/Collection {collection-id :id} {:namespace "data-apps"}]
+ (mt/with-test-user :crowberto
+ ;; unsaved, since the namespace refuses both
+ (doseq [model [:model/Dashboard :model/Timeline]
+ :let [instance (t2/instance model {:collection_id collection-id})]]
+ (testing model
+ (is (false? (mi/can-write? instance)))
+ (testing "while reading still follows the collection's permissions"
+ (is (true? (mi/can-read? instance))))))))))
+
(deftest ^:parallel can-read-via-parent-collection?-test
(testing "read perm is true iff the current user has read on the parent collection"
(binding [api/*current-user-permissions-set* (atom #{"/"})]
diff --git a/test/metabase/queries/models/card_test.clj b/test/metabase/queries/models/card_test.clj
index b646eba4f50..d1caead3b34 100644
--- a/test/metabase/queries/models/card_test.clj
+++ b/test/metabase/queries/models/card_test.clj
@@ -1177,6 +1177,17 @@
(is (false? (mi/can-read? card)))
(is (false? (mi/can-write? card)))))))))
+(deftest card-in-a-data-apps-collection-is-read-only-test
+ (testing "Nobody writes a card in a collection of the data apps namespace, administrators included"
+ (mt/with-temp [:model/Collection {collection-id :id} {:namespace "data-apps"}
+ :model/Card card {:collection_id collection-id}]
+ (mt/with-test-user :crowberto
+ (is (false? (mi/can-write? card)))
+ (is (false? (mi/can-write? :model/Card (:id card))))
+ (is (false? (:can_write (t2/hydrate card :can_write))))
+ (testing "nor creates one there"
+ (is (false? (mi/can-create? :model/Card {:collection_id collection-id}))))))))
+
(deftest ^:parallel breakouts->identifier->action-fn-test
(are [b1 b2 expected-identifier->action] (=? expected-identifier->action
(#'card/breakouts->identifier->action