Skip to content

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

perms for moving collections with archived kids (#67728)

John Swanson2393146b8c3f
diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.clj
index 057e5d35a8b..da958bb466d 100644
--- a/src/metabase/collections/models/collection.clj
+++ b/src/metabase/collections/models/collection.clj
@@ -1369,7 +1369,9 @@
(set
(for [collection-or-id (cons
collection
- (t2/select-pks-set :model/Collection :location [:like (str (children-location collection) "%")]))]
+ (t2/select-pks-set :model/Collection
+ :location [:like (str (children-location collection) "%")]
+ :archived false))]
(perms/collection-readwrite-path collection-or-id))))
(mu/defn perms-for-archiving :- [:set perms/PathSchema]
diff --git a/test/metabase/collections/models/collection_test.clj b/test/metabase/collections/models/collection_test.clj
index 9880724ecee..3574e255757 100644
--- a/test/metabase/collections/models/collection_test.clj
+++ b/test/metabase/collections/models/collection_test.clj
@@ -828,6 +828,19 @@
Exception
(collection/perms-for-archiving input)))))))
+(deftest perms-for-archiving-excludes-archived-descendants-test
+ (testing "Archived descendants should be excluded from permission requirements"
+ (with-collection-hierarchy! [{:keys [a], :as collections}]
+ ;; Archive C and all its descendants (D, E, F, G)
+ (t2/update! :model/Collection {:id [:in (map u/the-id [(:c collections) (:d collections) (:e collections)
+ (:f collections) (:g collections)])]}
+ {:archived true})
+ ;; Now archiving A should only require perms for A and B (not C, D, E, F, G which are archived)
+ (is (= #{"/collection/A/"
+ "/collection/B/"}
+ (->> (collection/perms-for-archiving a)
+ (perms-path-ids->names collections)))))))
+
;;; ------------------------------------------------ Perms for Moving ------------------------------------------------
;; `*` marks the things that require permissions in charts below!
@@ -929,6 +942,22 @@
Exception
(collection/perms-for-moving collection new-parent)))))))
+(deftest perms-for-moving-excludes-archived-descendants-test
+ (testing "Archived descendants should be excluded from permission requirements"
+ (with-collection-hierarchy! [{:keys [a b], :as collections}]
+ ;; Archive C and all its descendants (D, E, F, G)
+ (t2/update! :model/Collection {:id [:in (map u/the-id [(:c collections) (:d collections) (:e collections)
+ (:f collections) (:g collections)])]}
+ {:archived true})
+ ;; Create a destination collection outside the hierarchy
+ (mt/with-temp [:model/Collection destination {:name "Destination"}]
+ ;; Now moving A to destination should only require perms for A, B (non-archived descendant), and destination
+ ;; Not C, D, E, F, G which are archived
+ (is (= #{(perms/collection-readwrite-path a)
+ (perms/collection-readwrite-path b)
+ (perms/collection-readwrite-path destination)}
+ (collection/perms-for-moving a destination)))))))
+
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Nested Collections: Moving Collections |
;;; +----------------------------------------------------------------------------------------------------------------+
diff --git a/test/metabase/collections_rest/api_test.clj b/test/metabase/collections_rest/api_test.clj
index dfc0490b3ec..fc50a0b5ccd 100644
--- a/test/metabase/collections_rest/api_test.clj
+++ b/test/metabase/collections_rest/api_test.clj
@@ -2749,6 +2749,23 @@
(mt/user-http-request :rasta :put 403 (str "collection/" (u/the-id collection-a))
{:archived true})))))))))
+(deftest archive-collection-with-archived-descendants-test
+ (testing "PUT /api/collection/:id"
+ (testing "I should be allowed to archive a collection if I have perms on it and all non-archived descendants"
+ ;; Create hierarchy A > B > C where C is already archived
+ ;; Grant perms for A and B only
+ ;; User should be able to archive A because C (which they don't have perms on) is archived
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Collection collection-a {}
+ :model/Collection collection-b {:location (collection/children-location collection-a)}
+ :model/Collection _collection-c {:location (collection/children-location collection-b)
+ :archived true}]
+ (doseq [collection [collection-a collection-b]]
+ (perms/grant-collection-readwrite-permissions! (perms/all-users-group) collection))
+ ;; This should succeed because C is archived and excluded from permission checks
+ (is (some? (mt/user-http-request :rasta :put 200 (str "collection/" (u/the-id collection-a))
+ {:archived true}))))))))
+
(deftest move-collection-test
(testing "PUT /api/collection/:id"
(testing "Can I *change* the `location` of a Collection? (i.e. move it into a different parent Collection)"
@@ -2818,6 +2835,24 @@
(mt/user-http-request :rasta :put 403 (str "collection/" (u/the-id collection-a))
{:parent_id (u/the-id collection-c)})))))))))))
+(deftest move-collection-with-archived-descendants-test
+ (testing "PUT /api/collection/:id"
+ (testing "I should be allowed to move a collection if I have perms on it and all non-archived descendants"
+ ;; Create hierarchy A > B > C, plus destination D, where C is already archived
+ ;; Grant perms for A, B, and D only
+ ;; User should be able to move A into D because C (which they don't have perms on) is archived
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Collection collection-a {}
+ :model/Collection collection-b {:location (collection/children-location collection-a)}
+ :model/Collection _collection-c {:location (collection/children-location collection-b)
+ :archived true}
+ :model/Collection collection-d {}]
+ (doseq [collection [collection-a collection-b collection-d]]
+ (perms/grant-collection-readwrite-permissions! (perms/all-users-group) collection))
+ ;; This should succeed because C is archived and excluded from permission checks
+ (is (some? (mt/user-http-request :rasta :put 200 (str "collection/" (u/the-id collection-a))
+ {:parent_id (u/the-id collection-d)}))))))))
+
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | GET /api/collection/graph and PUT /api/collection/graph |
;;; +----------------------------------------------------------------------------------------------------------------+