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
UXW-2578 - Prevent renaming of tenant collection (#67086)
John Swanson68fb0611069b
diff --git a/enterprise/backend/src/metabase_enterprise/tenants/model.clj b/enterprise/backend/src/metabase_enterprise/tenants/model.cljindex 383c78a4094..728a0b0c602 100644--- a/enterprise/backend/src/metabase_enterprise/tenants/model.clj+++ b/enterprise/backend/src/metabase_enterprise/tenants/model.clj@@ -1,10 +1,11 @@(ns metabase-enterprise.tenants.model(:require+ [metabase.api.common :as api][metabase.audit-app.core :as audit-app][metabase.models.interface :as mi][metabase.premium-features.core :refer [defenterprise]][metabase.util :as u]- [metabase.util.i18n :refer [deferred-tru]]+ [metabase.util.i18n :refer [deferred-tru tru]][metabase.util.malli :as mu][metabase.util.malli.schema :as ms][methodical.core :as methodical]@@ -85,3 +86,28 @@:location[:like (str "/" tenant-collection-id "/%")])](conj descendant-ids tenant-collection-id))))))++(defenterprise maybe-localize-tenant-collection-names+ "EE implementation: localizes tenant root collection names."+ :feature :tenants+ [collections]+ (let [tenant-collection-ids (keep (fn [c]+ (when (= (:type c) "tenant-specific-root-collection")+ (:id c)))+ collections)+ collection-id->tenant-name-and-id+ (when (seq tenant-collection-ids)+ (t2/select-fn->fn :tenant_collection_id (juxt :name :id)+ :model/Tenant+ :tenant_collection_id [:in tenant-collection-ids]))]+ (mapv (fn [{ttype :type id :id :as coll}]+ (cond-> coll+ (= ttype "tenant-specific-root-collection")+ (assoc :name (if-let [user-tenant-id (:tenant_id @api/*current-user*)]+ (let [[_tenant-name tenant-id] (collection-id->tenant-name-and-id id)]+ ;; this should never happen, but juuuuuust in case+ (api/check-403 (= user-tenant-id tenant-id))+ (tru "Our data"))+ (let [[tenant-name _tenant-id] (collection-id->tenant-name-and-id id)]+ (tru "Tenant collection: {0}" tenant-name))))))+ collections)))diff --git a/enterprise/backend/test/metabase_enterprise/tenants/model_test.clj b/enterprise/backend/test/metabase_enterprise/tenants/model_test.cljindex 1bea44e1f6d..5367170e7fb 100644--- a/enterprise/backend/test/metabase_enterprise/tenants/model_test.clj+++ b/enterprise/backend/test/metabase_enterprise/tenants/model_test.clj@@ -147,3 +147,49 @@#"cannot move a Collection to a different namespace"(t2/update! :model/Collection tenant-collection-id{:namespace nil})))))))))++;;; +----------------------------------------------------------------------------------------------------------------++;;; | Tenant Collection Name Localization |+;;; +----------------------------------------------------------------------------------------------------------------+++(deftest maybe-localize-tenant-collection-names-turns-all-dtcs-into-our-data+ (testing "maybe-localize-tenant-collection-names handles multiple collections efficiently"+ (mt/with-premium-features #{:tenants}+ (mt/with-temporary-setting-values [use-tenants true]+ (mt/with-temp [:model/Tenant {tenant1-coll-id :tenant_collection_id tenant1-id :id} {:name "Tenant One" :slug "tenant1"}+ :model/Tenant {tenant2-coll-id :tenant_collection_id} {:name "Tenant Two" :slug "tenant2"}+ :model/Collection regular-coll {:name "Regular Collection" :location "/"}+ :model/User {tenant-user-id :id} {:tenant_id tenant1-id}]+ (let [colls [(t2/select-one :model/Collection :id tenant1-coll-id)+ (t2/select-one :model/Collection :id tenant2-coll-id)+ regular-coll]+ localized-colls (collection/maybe-localize-tenant-collection-names colls)]+ (is (= ["Tenant collection: Tenant One"+ "Tenant collection: Tenant Two"+ "Regular Collection"]+ (map :name localized-colls))))+ (mt/with-current-user tenant-user-id+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"You don't have permissions to do that"+ (collection/maybe-localize-tenant-collection-names+ [(t2/select-one :model/Collection tenant2-coll-id)])))+ (is (= ["Our data"+ "Regular Collection"]+ (map :name (collection/maybe-localize-tenant-collection-names [(t2/select-one :model/Collection tenant1-coll-id)+ regular-coll]))))))))))++(deftest maybe-localize-tenant-collection-name-single-test+ (testing "maybe-localize-tenant-collection-name works for single collection"+ (mt/with-premium-features #{:tenants}+ (mt/with-temporary-setting-values [use-tenants true]+ (mt/with-temp [:model/Tenant {tenant-collection-id :tenant_collection_id+ tenant-id :id} {:name "My Tenant" :slug "mytenant"}+ :model/User {tenant-user :id} {:tenant_id tenant-id}]+ (let [coll (t2/select-one :model/Collection :id tenant-collection-id)+ localized-coll (collection/maybe-localize-tenant-collection-name coll)]+ (is (= "Tenant collection: My Tenant" (:name localized-coll))))++ (mt/with-current-user tenant-user+ (let [coll (t2/select-one :model/Collection :id tenant-collection-id)+ localized-coll (collection/maybe-localize-tenant-collection-name coll)]+ (is (= "Our data" (:name localized-coll))))))))))diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.cljindex 68598bca4ae..cf091b8c953 100644--- a/src/metabase/collections/models/collection.clj+++ b/src/metabase/collections/models/collection.clj@@ -513,6 +513,19 @@[collection](first (personal-collections-with-ui-details [collection])))+(defenterprise maybe-localize-tenant-collection-names+ "For tenant root collections, localize the name to the user's locale.++ OSS version: returns collections unchanged."+ metabase-enterprise.tenants.model+ [collections]+ collections)++(defn maybe-localize-tenant-collection-name+ "Single-collection version of [[maybe-localize-tenant-collection-names]]."+ [collection]+ (first (maybe-localize-tenant-collection-names [collection])))+(def ^:private CollectionWithLocationAndPersonalOwnerID"Schema for a Collection instance that has a valid `:location`, and a `:personal_owner_id` key *present* (but notneccesarily non-nil)."diff --git a/src/metabase/collections_rest/api.clj b/src/metabase/collections_rest/api.cljindex e356d57b809..ed5fb72f2af 100644--- a/src/metabase/collections_rest/api.clj+++ b/src/metabase/collections_rest/api.clj@@ -167,9 +167,11 @@(cons root))))(t2/hydrate collections :can_write :is_personal :can_delete :is_remote_synced :parent_id);; remove the :metabase.collection.models.collection.root/is-root? tag since FE doesn't need it- ;; and for personal collections we translate the name to user's locale- (collection/personal-collections-with-ui-details (for [collection collections]- (dissoc collection ::collection.root/is-root?)))))+ ;; and for personal/tenant collections we translate the name to user's locale+ (->> (for [collection collections]+ (dissoc collection ::collection.root/is-root?))+ collection/personal-collections-with-ui-details+ collection/maybe-localize-tenant-collection-names)))(defn- shallow-tree-from-collection-id"Returns only a shallow Collection in the provided collection-id, e.g.@@ -187,7 +189,8 @@```"[colls](->> colls- (map collection/personal-collection-with-ui-details)+ (map (comp collection/maybe-localize-tenant-collection-name+ collection/personal-collection-with-ui-details))(collection/collections->tree nil)(map (fn [coll] (update coll :children #(boolean (seq %)))))))@@ -274,7 +277,9 @@[:= :archived_at nil]]})(map :collection_id)(into #{}))}))- collections-with-details (map collection/personal-collection-with-ui-details collections)]+ collections-with-details (map (comp collection/maybe-localize-tenant-collection-name+ collection/personal-collection-with-ui-details)+ collections)](collection/collections->tree collection-type-ids collections-with-details)))));;; --------------------------------- Fetching a single Collection & its 'children' ----------------------------------@@ -847,6 +852,7 @@(let [type-value (:type row)](-> (t2/instance :model/Collection row)collection/maybe-localize-system-collection-name+ collection/maybe-localize-tenant-collection-name(update :archived api/bit->boolean)(update :is_remote_synced api/bit->boolean)(t2/hydrate :can_write :effective_location :can_restore :can_delete :is_shared_tenant_collection)@@ -1105,6 +1111,7 @@[collection :- collection/CollectionWithLocationAndIDOrRoot](-> collectioncollection/personal-collection-with-ui-details+ collection/maybe-localize-tenant-collection-name(t2/hydrate :parent_id:effective_location[:effective_ancestors :can_write]@@ -1591,6 +1598,9 @@[:authority_level {:optional true} [:maybe collection/AuthorityLevel]]]];; do we have perms to edit this Collection?(let [collection-before-update (t2/hydrate (api/write-check :model/Collection id) :parent_id)]+ ;; tenant-specific-root-collection collections cannot be updated+ (api/check-400+ (not= (:type collection-before-update) "tenant-specific-root-collection"));; if authority_level is changing, make sure we're allowed to do that(when (and (contains? collection-updates :authority_level)(not= (keyword authority-level) (:authority_level collection-before-update)))diff --git a/src/metabase/search/impl.clj b/src/metabase/search/impl.cljindex a4b8038d7cc..f10546171c1 100644--- a/src/metabase/search/impl.clj+++ b/src/metabase/search/impl.clj@@ -370,7 +370,9 @@:always(assoc :type (:collection_type collection)):always- collection/maybe-localize-system-collection-name))+ collection/maybe-localize-system-collection-name+ :always+ collection/maybe-localize-tenant-collection-name))(defn- normalize-result [result](let [instance (to-toucan-instance (t2.realize/realize result))]