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
Use the resource collection's grants as a data app's access (#85443)
Alexander Polyankin2393c4ab9ae0
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 0768922d880..9e92cc7aad4 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -2631,7 +2631,6 @@app-dbappearancecore.initialization-status- data-appsdriverembeddinglib.be@@ -3855,7 +3854,6 @@:model/Card:model/Collection:model/DataPermissions- :model/Permissions:model/Table}:model-exports #{:model/DataApp}}diff --git a/e2e/test/scenarios/data-apps/data-studio-permissions.cy.spec.ts b/e2e/test/scenarios/data-apps/data-studio-permissions.cy.spec.tsindex 222127b7a93..4521f9c2d4a 100644--- a/e2e/test/scenarios/data-apps/data-studio-permissions.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/data-studio-permissions.cy.spec.ts@@ -131,9 +131,9 @@ describe("scenarios > data apps > data studio permissions", () => {});function visitHome() {- cy.intercept("GET", "/api/ee/apps?available=true").as("availableApps");+ cy.intercept("GET", "/api/ee/apps").as("dataApps");cy.visit("/");- cy.wait("@availableApps");+ cy.wait("@dataApps");H.navigationSidebar().findByRole("listitem", { name: "Home" }).should("be.visible");diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj b/enterprise/backend/src/metabase_enterprise/data_apps/api.cljindex 0ca0c51ee4b..8885bed6372 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj@@ -1,6 +1,6 @@(ns metabase-enterprise.data-apps.api- "Data-app endpoints, mounted at `/api/ee/apps`: manage data apps and their groups, serve their bundles, and generate- their typed schema."+ "Data-app endpoints, mounted at `/api/ee/apps`: manage data apps and the groups that can read their collections, serve+ their bundles, and generate their typed schema."(:require[clojure.string :as str][metabase-enterprise.data-apps.apps :as data-apps.apps]@@ -15,7 +15,6 @@[metabase.api.macros :as api.macros][metabase.api.routes.common :refer [+auth]][metabase.events.core :as events]- [metabase.models.interface :as mi][metabase.remote-sync.core :as remote-sync][metabase.util.i18n :refer [tru]][metabase.util.json :as json]@@ -101,7 +100,9 @@[:map {:closed true}[:id ms/PositiveInt][:name ms/NonBlankString]- [:display_name ms/NonBlankString]])+ [:display_name ms/NonBlankString]+ [:enabled :boolean]+ [:outdated :boolean]])(def ^:private RepoStatusResponse[:map@@ -163,17 +164,8 @@"Return full data-app metadata, with whether the app is outdated, to superusers and onlynavigational fields to other users."[app]- (if api/*is-superuser?*- (assoc app :outdated (data-app.config/outdated? app))- (select-keys app [:id :name :display_name])))--(defn- read-check-data-app- "Check whether the current user can access a data app. Viewing requires read access to the app's- resource collection, which every app has."- [app]- (api/read-check app)- (api/read-check :model/Collection (:resource_collection_id app))- app)+ (cond-> (assoc app :outdated (data-app.config/outdated? app))+ (not api/*is-superuser?*) (select-keys [:id :name :display_name :enabled :outdated])))(defn- check-not-outdated"Refuse an app built for an older contract than this Metabase serves with a 409 carrying@@ -189,14 +181,11 @@app)(api.macros/defendpoint :get "/" :- [:sequential [:or DataAppResponse PublicDataAppResponse]]- "List the data apps. Pass `available=true` to return only enabled apps. Disabled and outdated apps are listed only- to superusers, who see them badged, and never as available."- [_route-params- {:keys [available]} :- [:map {:closed true} [:available {:optional true} [:maybe :boolean]]]]- (->> (data-apps.db/data-apps available)- (filter mi/can-read?)- (remove #(and (or available (not api/*is-superuser?*))- (data-app.config/outdated? %)))+ "List the data apps the user can read. Disabled and outdated apps are listed only to superusers, who see them+ badged."+ []+ (->> (data-apps.db/readable-data-apps api/*current-user-id* (boolean api/*is-superuser?*))+ (remove #(and (not api/*is-superuser?*) (data-app.config/outdated? %)))(mapv data-app-response)))(defn- with-bundle@@ -259,7 +248,7 @@(update-app! slug {:bundle bundle}))(api.macros/defendpoint :delete ["/:slug" :slug slug-regex] :- :nil- "Delete a data app, its bundle, its collection, and its group assignments."+ "Delete a data app, its bundle, and its collection with the grants on it."[{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]](let [app (write-check-data-app slug)](check-editable! app)@@ -298,7 +287,7 @@(api.macros/defendpoint :get ["/:slug/group-permission-warnings" :slug slug-regex]:- [:sequential GroupPermissionWarning]- "Return missing table access for groups already assigned to this app."+ "Return missing table access for the groups listed for this app."[{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]](api/check-superuser)(data-app.group-access/permission-warnings (api/check-404 (data-apps.db/data-app-by-slug slug))))@@ -310,13 +299,13 @@[:member_count ms/IntGreaterThanOrEqualToZero]])(api.macros/defendpoint :get ["/:slug/groups" :slug slug-regex] :- [:sequential AssignedGroup]- "List the groups assigned to a data app."+ "List the groups other than Administrators that can read a data app's resource collection."[{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]](api/check-superuser)(data-app.group-access/assigned-groups (api/check-404 (data-apps.db/data-app-by-slug slug))))(api.macros/defendpoint :post ["/:slug/groups" :slug slug-regex] :- [:sequential AssignedGroup]- "Assign a batch of internal groups atomically."+ "Grant a batch of internal groups read access to a data app's resource collection atomically."[{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]_query-params{group-ids :group_ids} :- AddGroupsRequest]@@ -324,7 +313,7 @@(data-app.group-access/add-groups! (api/check-404 (data-apps.db/data-app-by-slug slug)) group-ids))(api.macros/defendpoint :delete ["/:slug/groups/:group-id" :slug slug-regex] :- :nil- "Remove a group's assignment and collection access."+ "Revoke a group's access to a data app's resource collection."[{:keys [slug group-id]} :- [:map {:closed true} [:slug ms/NonBlankString] [:group-id ms/PositiveInt]]](api/check-superuser)(data-app.group-access/remove-group! (api/check-404 (data-apps.db/data-app-by-slug slug)) group-id))@@ -336,7 +325,7 @@(api.macros/defendpoint :get ["/:slug" :slug slug-regex] :- [:or DataAppResponse PublicDataAppResponse]"Fetch metadata for a single data app by its slug, disabled ones to superusers only."[{:keys [slug]} :- [:map {:closed true} [:slug ms/NonBlankString]]]- (let [app (read-check-data-app (data-apps.db/data-app-by-slug slug))]+ (let [app (api/read-check (data-apps.db/data-app-by-slug slug))](data-app-response (cond-> app (not api/*is-superuser?*) check-not-outdated))))(api.macros/defendpoint :get ["/:slug/bundle" :slug slug-regex] :- :any@@ -350,7 +339,7 @@respondraise](try- (let [row (check-not-outdated (read-check-data-app (data-apps.db/data-app-by-slug slug)))+ (let [row (check-not-outdated (api/read-check (data-apps.db/data-app-by-slug slug)))hash (:bundle_hash row)etag (some->> hash (format "\"%s\""))](conddiff --git a/enterprise/backend/src/metabase_enterprise/data_apps/core.clj b/enterprise/backend/src/metabase_enterprise/data_apps/core.cljindex df0d77f6712..7eaed402c40 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/core.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/core.clj@@ -1,18 +1,9 @@(ns metabase-enterprise.data-apps.core- "What other modules may ask the data-apps module: whether a user can open an app, and which apps a user may read."+ "What other modules may ask the data-apps module: which apps a user may read."(:require[metabase-enterprise.data-apps.db :as data-apps.db]- [metabase.api.common :as api]- [metabase.models.interface :as mi][metabase.premium-features.core :refer [defenterprise]]))-(defenterprise check-data-app-access!- "Checks that the user can read the app: a member of an assigned group while it is enabled, or a superuser."- :feature :data-apps- [request]- (let [app (api/check-404 (data-apps.db/data-app-by-slug (get-in request [:route-params :name])))]- (api/check-403 (mi/can-read? app))))-(defenterprise visible-data-app-filter-select"A HoneySQL query of the ids of the data apps that the user with `:user-id` and `:is-superuser?` can read.":feature :data-appsdiff --git a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj b/enterprise/backend/src/metabase_enterprise/data_apps/db.cljindex 6f857dbc464..9659e39cdb0 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj@@ -3,6 +3,7 @@additional logic, so the rest of the module only touches `toucan2.core` for model definitions and hydration methods."(:require[metabase-enterprise.data-apps.schema :as data-apps.schema]+ [metabase.collections.models.collection :as collection][metabase.lib.schema.id :as lib.schema.id][metabase.util.malli :as mu][metabase.util.malli.schema :as ms]@@ -27,13 +28,6 @@[slug :- :string](t2/select-one non-blob-model :name slug))-(mu/defn data-apps- "Every DataApp without its bundle, ordered by display name; only the enabled ones when `available?`."- [available? :- [:maybe :boolean]]- (t2/select non-blob-model- (cond-> {:order-by [[:display_name :asc]]}- available? (assoc :where [:= :enabled true]))))-(mu/defn data-app-bundle"The bundle bytes of the DataApp with `data-app-id`."[data-app-id :- ms/PositiveInt]@@ -122,62 +116,29 @@[collection-id](t2/delete! :model/Collection :id collection-id))-(defn permissions-for-paths-excluding-group- "Permission grants for `paths`, excluding `group-id`."- [paths group-id]- (t2/select [:model/Permissions :group_id :object]- :object [:in paths]- :group_id [:not= group-id]))--(defn app-assignments- "Assignments for the requested apps."- [app-ids]- (if (seq app-ids)- (t2/select :model/DataAppGroupAssignment :data_app_id [:in app-ids])- []))-(mu/defn readable-data-app-ids-select"A HoneySQL query of the ids of the DataApps that the User with `user-id` can read: every app for a superuser, else- the enabled apps assigned to one of their groups, and none for a tenant user."- [user-id :- [:maybe ms/PositiveInt]+ the enabled apps whose resource collection they can read."+ [user-id :- ms/PositiveIntsuperuser? :- :boolean](if superuser?^:allow-subquery {:select [:id] :from [(t2/table-name :model/DataApp)]}- ^:allow-subquery {:select [:assignment.data_app_id]- :from [[(t2/table-name :model/DataAppGroupAssignment) :assignment]]- :join [[(t2/table-name :model/DataApp) :app]- [:= :app.id :assignment.data_app_id]- [:permissions_group_membership :membership]- [:= :membership.group_id :assignment.permission_group_id]- [:core_user :member]- [:= :member.id :membership.user_id]]- ;; No `[:auto/param]` marker: this query also runs inside queries compiled outside Toucan, such- ;; as the search index's, which reject it. Both compilers bind the integer id as a parameter.+ ^:allow-subquery {:select [:id]+ :from [(t2/table-name :model/DataApp)]:where [:and- [:= :app.enabled true]- [:= :membership.user_id user-id]- [:= :member.tenant_id nil]]}))--(mu/defn readable-data-app?- "Whether the non-superuser User with `user-id` can read the DataApp with `app-id`."- [user-id :- ms/PositiveInt- app-id :- ms/PositiveInt]- (t2/exists? :model/DataApp {:where [:and- [:= :id [:auto/param app-id]]- [:in :id (readable-data-app-ids-select user-id false)]]}))--(defn insert-assignments!- "Assign groups to an app. The unique constraint rejects concurrent duplicates."- [app-id group-ids]- (t2/insert! :model/DataAppGroupAssignment- (mapv (fn [group-id] {:data_app_id app-id :permission_group_id group-id}) group-ids)))--(defn delete-assignments!- "Remove the requested group assignments from an app."- [app-id group-ids]- (if (seq group-ids)- (t2/delete! :model/DataAppGroupAssignment :data_app_id app-id :permission_group_id [:in group-ids])- 0))+ [:= :enabled true]+ [:in :resource_collection_id+ (collection/visible-collection-query+ {:permission-level :read :include-archived-items :all}+ {:current-user-id user-id :is-superuser? false})]]}))++(mu/defn readable-data-apps+ "The DataApps that the User with `user-id` can read, without their bundles, ordered by display name."+ [user-id :- ms/PositiveInt+ superuser? :- :boolean]+ (t2/select non-blob-model+ {:where [:in :id (readable-data-app-ids-select user-id superuser?)]+ :order-by [[:display_name :asc]]}))(defn permissions-for-warnings"View-data permission rows for the warning tables and their databases."diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/group_access.clj b/enterprise/backend/src/metabase_enterprise/data_apps/group_access.cljindex 458f58d87fb..c891d0d9490 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/group_access.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/group_access.clj@@ -1,7 +1,6 @@(ns metabase-enterprise.data-apps.group-access(:require[metabase-enterprise.data-apps.db :as data-apps.db]- [metabase-enterprise.data-apps.resources :as resources][metabase-enterprise.dependencies.models.dependency :as models.dependency][metabase.api.common :as api][metabase.graph.core :as graph]@@ -10,37 +9,56 @@[metabase.util.i18n :refer [tru]][toucan2.core :as t2]))+(defn- access-group-ids+ "The ids of the groups other than Administrators that can read the resource collection of `app`."+ [app]+ (if-let [collection-id (:resource_collection_id app)]+ (disj (perms/collection-read-access-group-ids collection-id) (:id (perms/admin-group)))+ #{}))+(defn assigned-groups- "Assigned groups with their member counts."+ "The groups other than Administrators that can read the resource collection of `app`, with their member counts."[app]- (let [using-tenants? (setting/get :use-tenants)- group-ids (into #{} (map :permission_group_id) (data-apps.db/app-assignments [(:id app)]))]+ (let [using-tenants? (setting/get :use-tenants)](mapv (fn [group](assoc group :name (perms/group-display-name group using-tenants?)))- (t2/hydrate (perms/groups-by-ids group-ids) :member_count))))+ (t2/hydrate (perms/groups-by-ids (access-group-ids app)) :member_count))))++(defn- check-assignable-groups+ "Throws unless every group with an id in `group-ids` exists and is neither a tenant group nor Administrators."+ [group-ids]+ (let [group-tenant-flags (perms/group-tenant-flags (set group-ids))]+ (api/check-404 (every? #(contains? group-tenant-flags %) group-ids))+ (api/check-400 (not-any? #(or (get group-tenant-flags %) (= % (:id (perms/admin-group)))) group-ids)+ (tru "Tenant groups and admins cannot be assigned to data apps."))))++(defn- app-collection+ "The resource collection of `app`, or a 404."+ [app]+ (api/check-404 (some-> (:resource_collection_id app) data-apps.db/resource-collection)))(defn add-groups!- "Assign groups to app and update app collection grants."+ "Grant groups read access to the resource collection of `app`."[app group-ids]- (let [group-ids (vec (distinct group-ids))]+ (let [group-ids (vec (distinct group-ids))+ collection (app-collection app)]+ (check-assignable-groups group-ids)(perms/with-global-permissions-lock(t2/with-transaction [_conn]- (let [assigned (into #{} (map :permission_group_id) (data-apps.db/app-assignments [(:id app)]))]- (api/check-400 (not-any? assigned group-ids) (tru "One or more groups already have access to this data app.")))- (data-apps.db/insert-assignments! (:id app) group-ids)- (resources/reconcile-existing-collection-permissions! app))))+ (api/check-400 (not-any? (access-group-ids app) group-ids)+ (tru "One or more groups already have access to this data app."))+ (doseq [group-id group-ids]+ (perms/grant-collection-read-permissions! group-id collection)))))(assigned-groups app))(defn remove-group!- "Remove an assignment and its collection grants in one transaction."+ "Revoke a group's access to the resource collection of `app`."[app group-id]- (perms/with-global-permissions-lock- (t2/with-transaction [_conn]- (api/check-404 (pos? (data-apps.db/delete-assignments! (:id app) [group-id])))- (when-not (= group-id (:id (perms/admin-group)))- (when-let [collection (some-> (:id app) data-apps.db/data-app :resource_collection_id- data-apps.db/resource-collection)]- (perms/revoke-collection-permissions! group-id collection)))))+ (let [collection (app-collection app)]+ (perms/with-global-permissions-lock+ (t2/with-transaction [_conn]+ (api/check-404 (contains? (access-group-ids app) group-id))+ (perms/revoke-collection-permissions! group-id collection))))nil)(defn- group-has-table-access?@@ -87,12 +105,12 @@(vec (into (set table) outputs))))(defn permission-warnings- "Missing table access for assigned groups, including access inherited from All Users."- [{app-id :id collection-id :resource_collection_id}]- (let [assigned-ids (mapv :permission_group_id (data-apps.db/app-assignments [app-id]))- table-ids (when (and (seq assigned-ids) collection-id)+ "Missing table access for the groups with access to `app`, including access inherited from All Users."+ [{collection-id :resource_collection_id :as app}]+ (let [assigned-ids (sort (access-group-ids app))+ table-ids (when (seq assigned-ids)(resource-table-ids collection-id))]- (if (and (seq assigned-ids) (seq table-ids))+ (if (seq table-ids)(let [tables (table-details table-ids)all-users-id (:id (perms/all-users-group))access (group-table-access (conj (set assigned-ids) all-users-id) tables)]diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/init.clj b/enterprise/backend/src/metabase_enterprise/data_apps/init.cljindex 4b9b3b50cd1..2a02e6ec974 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/init.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/init.clj@@ -4,5 +4,4 @@[metabase-enterprise.data-apps.apps][metabase-enterprise.data-apps.config][metabase-enterprise.data-apps.csp]- [metabase-enterprise.data-apps.models.data-app]- [metabase-enterprise.data-apps.models.data-app-group-assignment]))+ [metabase-enterprise.data-apps.models.data-app]))diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.cljindex ec4a608662e..f82141e0aee 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj@@ -126,8 +126,10 @@([_model pk](and (premium-features/has-feature? :data-apps)(or api/*is-superuser?*- (and (some? api/*current-user-id*)- (data-apps.db/readable-data-app? api/*current-user-id* pk))))))+ (let [{:keys [enabled resource_collection_id]} (data-apps.db/data-app pk)]+ (and (true? enabled)+ (some? resource_collection_id)+ (mi/can-read? :model/Collection resource_collection_id)))))))(defmethod mi/can-write? :model/DataApp([_instance] api/*is-superuser?*)diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app_group_assignment.clj b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app_group_assignment.cljdeleted file mode 100644index 1a5bde10f1a..00000000000--- a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app_group_assignment.clj+++ /dev/null@@ -1,41 +0,0 @@-(ns metabase-enterprise.data-apps.models.data-app-group-assignment- (:require- [metabase.api.common :as api]- [metabase.models.interface :as mi]- [metabase.permissions.core :as perms]- [metabase.util.i18n :refer [tru]]- [methodical.core :as methodical]- [toucan2.core :as t2]))--(methodical/defmethod t2/table-name :model/DataAppGroupAssignment [_model] :data_app_group_assignment)--(derive :model/DataAppGroupAssignment :metabase/model)--(defn- check-group-for-app-assignment- [{group-id :permission_group_id :as assignment}]- (let [group-tenant-flags (perms/group-tenant-flags #{group-id})]- (api/check-404 (contains? group-tenant-flags group-id))- (api/check-400 (and (not (get group-tenant-flags group-id))- (not= group-id (:id (perms/admin-group))))- (tru "Tenant groups and admins cannot be assigned to data apps.")))- assignment)--(t2/define-before-insert :model/DataAppGroupAssignment- [assignment]- (check-group-for-app-assignment assignment))--(t2/define-before-update :model/DataAppGroupAssignment- [assignment]- (check-group-for-app-assignment assignment))--(defmethod mi/can-read? :model/DataAppGroupAssignment- ([_instance] (mi/superuser?))- ([_model _pk] (mi/superuser?)))--(defmethod mi/can-write? :model/DataAppGroupAssignment- ([_instance] (mi/superuser?))- ([_model _pk] (mi/superuser?)))--(defmethod mi/can-create? :model/DataAppGroupAssignment- [_model _instance]- (mi/superuser?))diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/resources.clj b/enterprise/backend/src/metabase_enterprise/data_apps/resources.cljindex 4cfcc4d6b66..25b4923cc57 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/resources.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/resources.clj@@ -3,7 +3,6 @@(:require[metabase-enterprise.data-apps.db :as data-apps.db][metabase.collections.core :as collection]- [metabase.permissions.core :as perms][metabase.request.core :as request]))(set! *warn-on-reflection* true)@@ -36,28 +35,6 @@(collection/archive-or-unarchive-collection! collection{:archived false, :parent_id nil}))))-(defn- reconcile-collection-permissions!- "Restore read-only collection grants from the app assignments. Preserve correct rows."- [app collection]- (let [group-ids (into #{} (map :permission_group_id) (data-apps.db/app-assignments [(:id app)]))- read-path (perms/collection-read-path collection)- write-path (perms/collection-readwrite-path collection)-- ;; existing permissions for the app collection for each groups- permissions-by-group (group-by :group_id- (data-apps.db/permissions-for-paths-excluding-group- ["/" read-path write-path] (:id (perms/admin-group))))- read-only? (fn [group-id]- (= #{read-path} (set (map :object (get permissions-by-group group-id)))))]- ;; revoke app collection access from unassigned groups- (doseq [group-id (keys permissions-by-group)- :when (not (and (group-ids group-id) (read-only? group-id)))]- (perms/revoke-collection-permissions! group-id collection))- ;; grant app collection access to assigned groups- (doseq [group-id group-ids- :when (not (read-only? group-id))]- (perms/grant-collection-read-permissions! group-id collection))))-(defn- resource-collection!"The resource collection of `app`, created again if the app's was deleted out from under it: the reference isnullable so that deleting the app can delete the collection first, which also lets the collection go alone."@@ -68,25 +45,15 @@(data-apps.db/update-data-app! (:id app) {:resource_collection_id (:id collection)})collection)))-(defn reconcile-existing-collection-permissions!- "Reconcile grants for an existing resource collection without creating or restoring it."- [app]- (perms/with-global-permissions-lock- (when-let [collection (some-> (:id app) data-apps.db/data-app :resource_collection_id- data-apps.db/resource-collection)]- (reconcile-collection-permissions! app collection))))-(defn ensure-resources!- "Create or restore the server-owned permission resources for `app` and return their IDs."+ "Create, rename, or restore the resource collection of `app`, and return `{:resource_collection_id id}`."[app]- (perms/with-global-permissions-lock- (let [app (data-apps.db/data-app (:id app))- collection (resource-collection! app)]- (data-apps.db/update-resource-collection! (:id collection)- {:name (resource-name app)})- (restore-trashed-collection! collection)- (reconcile-collection-permissions! app collection)- {:resource_collection_id (:id collection)})))+ (let [app (data-apps.db/data-app (:id app))+ collection (resource-collection! app)]+ (data-apps.db/update-resource-collection! (:id collection)+ {:name (resource-name app)})+ (restore-trashed-collection! collection)+ {:resource_collection_id (:id collection)}))(defn delete-resources!"Delete the app collection."diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.cljindex ca6dd888b74..dea7f19b834 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj@@ -95,7 +95,6 @@"ContentDiagnosticsFinding""ContentTranslation""DashboardBookmark"- "DataAppGroupAssignment""DataComplexityScore""DataPermissions""DatabaseRouter"diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.cljindex f0519ce3383..07ef654bb96 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj@@ -35,7 +35,7 @@;;; ---------------------------------------------- Permissions -----------------------------------------------(deftest assigned-users-can-load-bundles-but-cannot-manage-apps-test+(deftest users-with-access-can-load-bundles-but-cannot-manage-apps-test;; global mode so the `:data-apps` premium feature is visible to the real-HTTP;; `user-real-request` calls below (which run on Jetty threads that don't inherit;; a thread-local `binding`).@@ -44,10 +44,8 @@(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup](create-app!)(let [app (t2/select-one :model/DataApp :name "demo")- {:keys [resource_collection_id]} (data-app.resources/ensure-resources! app)permission_group_id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Finches"}))]- (testing "collection access alone does not assign an app"- (perms/grant-collection-read-permissions! (perms/all-users-group) resource_collection_id)+ (testing "a user who cannot read the app's collection cannot open the app"(mt/user-http-request :rasta :get 403 "ee/apps/demo"))(testing "a member can open a data app"(group-access/add-groups! app [permission_group_id])@@ -67,7 +65,7 @@(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup](create-app!)(let [app (t2/select-one :model/DataApp :name "demo")- {:keys [resource_collection_id]} (data-app.resources/ensure-resources! app)]+ {:keys [resource_collection_id]} app](is (not (some #(= resource_collection_id (:id %))(mt/user-http-request :rasta :get 200 "collection")))))(testing "a superuser can list, read metadata, and serve the bundle"@@ -117,7 +115,7 @@(mt/with-premium-features #{}(mt/user-http-request :crowberto :post 402 "ee/apps" {:name "demo"})))-(deftest assigned-group-reaches-only-copied-actions-test+(deftest group-with-access-reaches-only-copied-actions-test(testing "an action is reachable exactly when it lives in the data app collection"(mt/with-premium-features #{:data-apps}(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]@@ -139,7 +137,7 @@:dataset_query (lib/native-query (mt/metadata-provider) "UPDATE venues SET name = 'x'")}source-id (actions/insert! (assoc action :collection_id source-collection-id))copied-id (actions/insert! (assoc action :collection_id resource_collection_id))]- (testing "the assigned group reads the action copied into the app collection"+ (testing "the group with access reads the action copied into the app collection"(is (=? {:id copied-id}(mt/user-http-request :rasta :get 200 (str "action/" copied-id)))))(testing "the same group cannot read the source action it was copied from"@@ -149,15 +147,6 @@(is (=? {:id source-id}(mt/user-http-request :crowberto :get 200 (str "action/" source-id)))))))))))))-(deftest list-available-apps-test- (mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (t2/insert! :model/DataApp :name "ready" :display_name "Ready" :bundle_path "data_apps/ready/index.js")- (t2/insert! :model/DataApp :name "disabled" :display_name "Disabled" :bundle_path "data_apps/disabled/index.js"- :enabled false)- (is (=? [{:name "ready" :display_name "Ready"}]- (mt/user-http-request :crowberto :get 200 "ee/apps?available=true"))))))-(deftest outdated-apps-are-hidden-from-users-and-badged-for-admins-test(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}@@ -170,20 +159,20 @@(perms/add-user-to-group! (mt/user->id :rasta) group-id)(doseq [slug ["old" "current"]:let [app (t2/select-one :model/DataApp :name slug)]]- (data-app.resources/ensure-resources! app)(group-access/add-groups! app [group-id])))(with-redefs [data-app.config/supported-app-version 2](testing "a regular user is never told about the outdated app in a list"- (doseq [url ["ee/apps" "ee/apps?available=true"]]- (is (= [{:id (t2/select-one-pk :model/DataApp :name "current") :name "current" :display_name "Current"}]- (mt/user-http-request :rasta :get 200 url)))))+ (is (= [{:id (t2/select-one-pk :model/DataApp :name "current") :name "current" :display_name "Current"+ :enabled true :outdated false}]+ (mt/user-http-request :rasta :get 200 "ee/apps"))))(testing "for a regular user, opening an outdated app is a 409 that says what to do"(doseq [url ["ee/apps/old" "ee/apps/old/bundle"]](is (=? {:error-code "data-app-outdated":message #"This app was built for version 1 of data apps.*"}(mt/user-http-request :rasta :get 409 url)))))(testing "the current app still opens"- (is (= {:id (t2/select-one-pk :model/DataApp :name "current") :name "current" :display_name "Current"}+ (is (= {:id (t2/select-one-pk :model/DataApp :name "current") :name "current" :display_name "Current"+ :enabled true :outdated false}(mt/user-http-request :rasta :get 200 "ee/apps/current")))(is (str/includes?(str (mt/user-real-request :crowberto :get 200 "ee/apps/current/bundle"))@@ -192,9 +181,6 @@(is (=? [{:name "current" :version 2 :outdated false}{:name "old" :version 1 :outdated true}](mt/user-http-request :crowberto :get 200 "ee/apps")))- (is (=? [{:name "current"}]- (mt/user-http-request :crowberto :get 200 "ee/apps?available=true"))- "but the navbar's available list leaves it out for admins too")(is (=? {:name "old" :version 1 :outdated true}(mt/user-http-request :crowberto :get 200 "ee/apps/old"))))(testing "nobody gets an outdated bundle"@@ -304,11 +290,10 @@(mt/with-temp [:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta), :group_id group-id}](let [app (t2/select-one :model/DataApp :name "demo")]- (data-app.resources/ensure-resources! app)(group-access/add-groups! app [group-id]))- (testing "an assigned user can open the app"+ (testing "a user with access can open the app"(mt/user-http-request :rasta :get 200 "ee/apps/demo"))- (testing "an assigned user cannot delete the app"+ (testing "a user with access cannot delete the app"(is (= "You don't have permissions to do that."(mt/user-http-request :rasta :delete 403 "ee/apps/demo")))(is (t2/exists? :model/DataApp :name "demo")))))))@@ -317,13 +302,13 @@(mt/with-premium-features #{:data-apps}(mt/user-http-request :crowberto :delete 404 "ee/apps/missing")))-(deftest delete-endpoint-preserves-assigned-groups-test+(deftest delete-endpoint-preserves-groups-test(mt/with-premium-features #{:data-apps}(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup](create-app!)(mt/with-temp [:model/PermissionsGroup group {}](let [app (t2/select-one :model/DataApp :name "demo")- {collection-id :resource_collection_id} (data-app.resources/ensure-resources! app)]+ {collection-id :resource_collection_id} app](data-apps.tu/with-published-tables! [(mt/id :venues)](mt/with-temp [:model/Card {card-id :id} {:collection_id collection-id :dataset_query (data-apps.tu/venues-query)}](group-access/add-groups! app [(:id group)])@@ -331,7 +316,8 @@(is (not (t2/exists? :model/DataApp :id (:id app))))(is (not (t2/exists? :model/Collection :id collection-id)))(is (not (t2/exists? :model/Card :id card-id)))- (is (empty? (t2/select :model/DataAppGroupAssignment :data_app_id (:id app))))+ (is (not (t2/exists? :model/Permissions :group_id (:id group)+ :object (perms/collection-read-path collection-id))))(is (t2/exists? :model/PermissionsGroup :id (:id group))))))))));;; ----------------------------------------------------- API -----------------------------------------------------@@ -371,7 +357,7 @@(testing "PUT /:slug can disable an app"(is (=? {:name "demo" :enabled false}(mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled false}))))- (testing "a disabled app is not served to an assigned user"+ (testing "a disabled app is not served to a user with access"(group-access/add-groups! (t2/select-one :model/DataApp :name "demo") [(:id (perms/all-users-group))])(mt/user-http-request :rasta :get 403 "ee/apps/demo")(mt/user-real-request :rasta :get 403 "ee/apps/demo/bundle"))@@ -539,14 +525,24 @@(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup](let [{:keys [id]} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo" :enabled false})](group-access/add-groups! (t2/select-one :model/DataApp :id id) [(:id (perms/all-users-group))])- (testing "an assigned user doesn't see the disabled app"+ (testing "a user with access doesn't see the disabled app"(is (not-any? #(= "demo" (:name %)) (mt/user-http-request :rasta :get 200 "ee/apps"))))(testing "a superuser does"(is (some #(= "demo" (:name %)) (mt/user-http-request :crowberto :get 200 "ee/apps"))))- (testing "an assigned user sees it once it's enabled"+ (testing "a user with access sees it once it's enabled"(mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled true})(is (some #(= "demo" (:name %)) (mt/user-http-request :rasta :get 200 "ee/apps"))))))))+(deftest apps-are-listed-only-to-users-who-can-read-their-collection-test+ (mt/with-premium-features #{:data-apps}+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})+ (testing "a user without a grant on the app's collection doesn't see the app"+ (is (not-any? #(= "demo" (:name %)) (mt/user-http-request :rasta :get 200 "ee/apps"))))+ (testing "a user sees it once their group can read the collection"+ (group-access/add-groups! (t2/select-one :model/DataApp :name "demo") [(:id (perms/all-users-group))])+ (is (some #(= "demo" (:name %)) (mt/user-http-request :rasta :get 200 "ee/apps")))))))+(deftest typed-schema-keys-are-the-same-on-every-instance-test(mt/with-premium-features #{:data-apps}(data-apps.tu/with-published-tables! [(mt/id :venues)]diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/db_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/db_test.cljindex dbff5a3e081..0140a48db9a 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/db_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/db_test.clj@@ -5,28 +5,6 @@[metabase.test :as mt][toucan2.core :as t2]))-(deftest delete-assignments-test- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/DataApp other-app {:name "owls" :display_name "Owls" :bundle_path "owls.js"}- :model/PermissionsGroup finches {}- :model/PermissionsGroup robins {}- :model/PermissionsGroup sparrows {}]- (data-apps.db/insert-assignments! (:id app) [(:id finches) (:id robins) (:id sparrows)])- (data-apps.db/insert-assignments! (:id other-app) [(:id finches)])- (let [assignments (fn []- (into #{}- (map (juxt :data_app_id :permission_group_id))- (data-apps.db/app-assignments [(:id app) (:id other-app)])))- before (assignments)]- (testing "empty input preserves all assignments"- (is (= 0 (data-apps.db/delete-assignments! (:id app) [])))- (is (= before (assignments))))- (testing "bulk deletion retains other groups and other apps' assignments"- (data-apps.db/delete-assignments! (:id app) [(:id finches) (:id robins)])- (is (= #{[(:id app) (:id sparrows)]- [(:id other-app) (:id finches)]}- (assignments)))))))-(deftest warning-queries-with-empty-ids-test(mt/with-temp [:model/PermissionsGroup group {}](doseq [[group-ids database-ids table-ids] [[[] [1] [1]] [[(:id group)] [] [1]] [[(:id group)] [1] []]]]diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.cljindex b00171fabad..f529e937cfd 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj@@ -2,7 +2,6 @@(:require[clojure.test :refer :all][metabase-enterprise.data-apps.group-access :as group-access]- [metabase-enterprise.data-apps.resources :as resources][metabase.api.macros :as api.macros][metabase.app-db.core :as mdb][metabase.collections.core :as collection]@@ -15,25 +14,27 @@(set! *warn-on-reflection* true)+(defn- access-group-ids [app]+ (into #{} (map :id) (group-access/assigned-groups app)))+(deftest visible-data-app-filter-select-outside-toucan-test(testing "the read rule query works in an app-DB query compiled outside Toucan, as the search index's queries are"(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp {app-id :id} {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/DataApp _ {:name "owls" :display_name "Owls" :bundle_path "owls.js"}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id app-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (group-access/add-groups! (t2/select-one :model/DataApp :id app-id) [group-id])(is (= [app-id](map :id (mdb/query {:select [:id]:from [:data_app]:where [:in :id (data-apps/visible-data-app-filter-select{:user-id (mt/user->id :rasta) :is-superuser? false})]}))))))))-(deftest assignments-authorize-access-test+(deftest added-groups-authorize-access-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/PermissionsGroup group {}]- (resources/ensure-resources! app)(mt/with-current-user (mt/user->id :rasta)(is (not (mi/can-read? app))))(group-access/add-groups! app [(:id group)])@@ -64,9 +65,9 @@(let [list-apps (api.macros/find-route-fn 'metabase-enterprise.data-apps.api :get "/")](mt/with-current-user (mt/user->id :rasta)(list-apps {} {})- (testing "a disabled app isn't listed to a non-superuser, available or not"- (doseq [query [{} {:available true}]]- (is (= [{:id (:id app) :name "birds" :display_name "Birds"}] (list-apps {} query))))))))))+ (testing "a disabled app isn't listed to a non-superuser"+ (is (= [{:id (:id app) :name "birds" :display_name "Birds" :enabled true :outdated false}]+ (list-apps {} {})))))))))(deftest read-access-agrees-across-entry-points-test(mt/with-premium-features #{:data-apps :tenants}@@ -75,15 +76,9 @@:model/User tenant-user {:tenant_id (:id tenant)}:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js":bundle (.getBytes "BUNDLE" "UTF-8")}- :model/PermissionsGroup tenant-group {}:model/PermissionsGroup group {}]- (resources/ensure-resources! app)(group-access/add-groups! app [(:id group)])(perms/add-user-to-group! (mt/user->id :rasta) (:id group))- ;; A stale assignment must not admit tenant users.- (t2/insert! :model/DataAppGroupAssignment {:data_app_id (:id app) :permission_group_id (:id tenant-group)})- (t2/update! :model/PermissionsGroup (:id tenant-group) {:is_tenant_group true})- (perms/add-user-to-group! (:id tenant-user) (:id tenant-group))(doseq [[user-id admin? allowed?] [[(mt/user->id :crowberto) true true][(mt/user->id :rasta) false true][(mt/user->id :lucky) false false]@@ -98,13 +93,7 @@(is (= allowed? (contains? (t2/select-pks-set :model/DataApp{:where [:in :id (data-apps/visible-data-app-filter-select{:user-id user-id :is-superuser? admin?})]})- (:id app))))- (mt/with-current-user user-id- (let [request {:route-params {:name "birds"} :metabase-user-id user-id :is-superuser? admin?}]- (if allowed?- (is (true? (data-apps/check-data-app-access! request)))- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"permissions"- (data-apps/check-data-app-access! request))))))))))))+ (:id app))))))))))(deftest group-api-test(mt/with-premium-features #{:data-apps}@@ -118,14 +107,15 @@[](vec (range 1 102))]](mt/user-http-request :crowberto :post 400 "ee/apps/birds/groups" {:group_ids ids})- (is (empty? (t2/select :model/DataAppGroupAssignment :data_app_id (:id app)))))+ (is (empty? (access-group-ids app))))(mt/user-http-request :crowberto :post 404 "ee/apps/birds/groups" {:group_ids [(:id finches) Integer/MAX_VALUE]})(is (=? [{:id (:id finches) :name "Finches" :member_count 0}{:id (:id owls) :name "Owls" :member_count 0}](mt/user-http-request :crowberto :post 200 "ee/apps/birds/groups"{:group_ids [(:id finches) (:id finches) (:id owls)]})))- (is (= #{(:id finches) (:id owls)}- (t2/select-fn-set :permission_group_id :model/DataAppGroupAssignment :data_app_id (:id app))))+ (is (= #{(:id finches) (:id owls)} (access-group-ids app)))+ (is (= #{(perms/collection-read-path (:resource_collection_id app))}+ (t2/select-fn-set :object :model/Permissions :group_id (:id finches))))(mt/user-http-request :crowberto :post 400 "ee/apps/birds/groups" {:group_ids [(:id finches)]})(mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" (:id finches)))(is (= [(:id owls)] (mapv :id (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups"))))@@ -144,21 +134,22 @@{:group_ids [(:id group)]:user_ids [(mt/user->id :rasta)]:permission_group_ids [(:id ignored-group)]})))- (is (= [(:id group)]- (t2/select-fn-vec :permission_group_id :model/DataAppGroupAssignment :data_app_id (:id app))))+ (is (= #{(:id group)} (access-group-ids app)))(mt/with-current-user (mt/user->id :rasta)(is (not (mi/can-read? app)))))))-(deftest assignment-does-not-recreate-deleted-collection-test+(deftest group-access-without-collection-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/PermissionsGroup group {}](t2/delete! :model/Collection :id (:resource_collection_id app))- (mt/user-http-request :crowberto :post 200 "ee/apps/birds/groups" {:group_ids [(:id group)]})- (is (t2/exists? :model/DataAppGroupAssignment :data_app_id (:id app) :permission_group_id (:id group)))- (is (nil? (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app)))))))+ (testing "an app without a collection lists no groups, and grants none without creating one"+ (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))+ (mt/user-http-request :crowberto :post 404 "ee/apps/birds/groups" {:group_ids [(:id group)]})+ (mt/user-http-request :crowberto :delete 404 (str "ee/apps/birds/groups/" (:id group)))+ (is (nil? (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))))))))-(deftest assigned-group-display-name-test+(deftest group-with-access-display-name-test(mt/with-premium-features #{:data-apps :tenants}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}](group-access/add-groups! app [(:id (perms/all-users-group))])@@ -167,11 +158,10 @@(is (= [expected-name](mapv :name (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))))))))-(deftest revoke-assignment-with-trashed-collection-test+(deftest remove-group-with-trashed-collection-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/PermissionsGroup group {}]- (resources/ensure-resources! app)(group-access/add-groups! app [(:id group)])(perms/add-user-to-group! (mt/user->id :rasta) (:id group))(let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))]@@ -180,7 +170,7 @@(collection/archive-or-unarchive-collection!(t2/select-one :model/Collection :id collection-id) {:archived true})))(mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" (:id group)))- (is (empty? (t2/select :model/DataAppGroupAssignment :data_app_id (:id app))))+ (is (empty? (access-group-ids app)))(is (empty? (t2/select :model/Permissions :group_id (:id group):object [:in [(perms/collection-read-path collection-id)(perms/collection-readwrite-path collection-id)]])))@@ -188,32 +178,28 @@(mt/with-current-user (mt/user->id :rasta)(is (not (mi/can-read? app))))))))-(deftest assignment-transaction-rolls-back-grant-failure-test+(deftest add-groups-rolls-back-grant-failure-test(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/PermissionsGroup group {}]- (let [{collection-id :resource_collection_id} (resources/ensure-resources! app)- reconcile-permissions! (mt/original-fn #'resources/reconcile-existing-collection-permissions!)]- (mt/with-dynamic-fn-redefs [resources/reconcile-existing-collection-permissions!- (fn [app]- (reconcile-permissions! app)- (throw (ex-info "Failed after granting collection access" {})))]+ :model/PermissionsGroup group {}+ :model/PermissionsGroup failing-group {}]+ (let [grant! (mt/original-fn #'perms/grant-collection-read-permissions!)]+ (mt/with-dynamic-fn-redefs [perms/grant-collection-read-permissions!+ (fn [group-id collection]+ (when (= group-id (:id failing-group))+ (throw (ex-info "Failed after granting collection access" {})))+ (grant! group-id collection))](is (thrown-with-msg? clojure.lang.ExceptionInfo #"Failed after granting collection access"- (group-access/add-groups! app [(:id group)]))))- (is (empty? (t2/select :model/DataAppGroupAssignment :data_app_id (:id app))))- (is (not (t2/exists? :model/Permissions :group_id (:id group)- :object (perms/collection-read-path collection-id)))))))+ (group-access/add-groups! app [(:id group) (:id failing-group)]))))+ (is (empty? (access-group-ids app))))))-(deftest assignments-preserve-data-permissions-test+(deftest group-access-preserves-data-permissions-test(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/PermissionsGroup group {}](perms/set-database-permission! (:id group) (mt/id) :perms/create-queries :query-builder-and-native)(let [before (t2/select :model/DataPermissions :group_id (:id group))]- (testing "assignment preserves existing data permissions"+ (testing "adding a group preserves existing data permissions"(group-access/add-groups! app [(:id group)])(is (= before (t2/select :model/DataPermissions :group_id (:id group)))))- (testing "reconciliation preserves existing data permissions"- (resources/ensure-resources! app)- (is (= before (t2/select :model/DataPermissions :group_id (:id group)))))(testing "removal preserves existing data permissions"(group-access/remove-group! app (:id group))(is (= before (t2/select :model/DataPermissions :group_id (:id group))))))))@@ -223,18 +209,15 @@:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])(t2/delete! :model/PermissionsGroup :id (:id group))- (is (empty? (t2/select :model/DataAppGroupAssignment :data_app_id (:id app))))+ (is (empty? (access-group-ids app)))(is (t2/exists? :model/DataApp :id (:id app)))))-(deftest all-users-assignment-test+(deftest all-users-access-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}](group-access/add-groups! app [(:id (perms/all-users-group))])(mt/with-current-user (mt/user->id :rasta)- (is (mi/can-read? app)))- (mt/with-current-user (mt/user->id :rasta)- (is (true? (data-apps/check-data-app-access!- {:route-params {:name "birds"} :metabase-user-id (mt/user->id :rasta)})))))))+ (is (mi/can-read? app))))))(deftest disabled-app-access-test(mt/with-premium-features #{:data-apps}@@ -242,19 +225,15 @@(group-access/add-groups! app [(:id (perms/all-users-group))])(testing "a superuser reaches a disabled app"(mt/with-current-user (mt/user->id :crowberto)- (is (true? (data-apps/check-data-app-access!- {:route-params {:name "birds"} :metabase-user-id (mt/user->id :crowberto) :is-superuser? true})))))+ (is (mi/can-read? app))))(testing "a user with group access does not reach it"(mt/with-current-user (mt/user->id :rasta)- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"You don't have permissions to do that."- (data-apps/check-data-app-access!- {:route-params {:name "birds"} :metabase-user-id (mt/user->id :rasta)}))))))))+ (is (not (mi/can-read? app))))))))(deftest group-endpoints-require-feature-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/PermissionsGroup group {}]- (resources/ensure-resources! app)(group-access/add-groups! app [(:id group)])(let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))](mt/with-premium-features #{}@@ -262,26 +241,14 @@(mt/user-http-request :crowberto :get 402 "ee/apps/birds/groups")(mt/user-http-request :crowberto :post 402 "ee/apps/birds/groups" {:group_ids [(:id group)]})(mt/user-http-request :crowberto :delete 402 (str "ee/apps/birds/groups/" (:id group)))- (is (t2/exists? :model/DataAppGroupAssignment :data_app_id (:id app) :permission_group_id (:id group)))+ (is (= #{(:id group)} (access-group-ids app)))(is (t2/exists? :model/Permissions :group_id (:id group):object (perms/collection-read-path collection-id))))))))-(deftest assignment-uniqueness-test- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/PermissionsGroup group {}]- (let [assignment {:data_app_id (:id app) :permission_group_id (:id group)}]- (t2/insert! :model/DataAppGroupAssignment assignment)- (is (thrown? Exception- (t2/with-transaction [_conn]- (t2/insert! :model/DataAppGroupAssignment assignment))))- (is (= 1 (t2/count :model/DataAppGroupAssignment- :data_app_id (:id app) :permission_group_id (:id group)))))))--(deftest revoke-assignment-after-group-becomes-ineligible-test+(deftest remove-group-after-it-becomes-ineligible-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/PermissionsGroup group {}]- (resources/ensure-resources! app)(group-access/add-groups! app [(:id group)])(t2/update! :model/PermissionsGroup (:id group) {:is_tenant_group true})(mt/user-http-request :rasta :delete 403 (str "ee/apps/birds/groups/" (:id group)))@@ -292,20 +259,18 @@(is (not (t2/exists? :model/Permissions :group_id (:id group):object (perms/collection-read-path collection-id))))))))-(deftest revoke-invalid-admin-assignment-preserves-admin-permissions-test+(deftest admin-group-is-never-listed-or-revoked-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}]- (resources/ensure-resources! app)- (let [admin-id (:id (perms/admin-group))+ (let [admin-id (:id (perms/admin-group))permissions (t2/select :model/Permissions :group_id admin-id)]- ;; Insert a legacy assignment without the model hooks that now reject it.- (t2/query {:insert-into (t2/table-name :model/DataAppGroupAssignment)- :values [{:data_app_id (:id app) :permission_group_id admin-id}]})- (mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" admin-id))(is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))- (is (= permissions (t2/select :model/Permissions :group_id admin-id)))))))+ (mt/user-http-request :crowberto :delete 404 (str "ee/apps/birds/groups/" admin-id))+ (is (= permissions (t2/select :model/Permissions :group_id admin-id)))+ (mt/with-current-user (mt/user->id :crowberto)+ (is (mi/can-read? app)))))))-(deftest revoke-assignment-requires-matching-app-and-group-test+(deftest remove-group-requires-matching-app-and-group-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/DataApp _ {:name "wrens" :display_name "Wrens" :bundle_path "wrens.js"}@@ -320,17 +285,15 @@(is (= [(:id group)](mapv :id (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))))))-(deftest collection-drift-denies-access-until-grants-are-reconciled-test+(deftest collection-grants-are-the-app-access-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}]- (resources/ensure-resources! app)- (let [group-id (:id (perms/all-users-group))]- (group-access/add-groups! app [group-id])- (let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))]- (perms/revoke-collection-permissions! group-id collection-id)- (mt/user-http-request :rasta :get 403 "ee/apps/birds")- (resources/ensure-resources! app)- (is (= {:id (:id app) :name "birds" :display_name "Birds"}+ (let [group-id (:id (perms/all-users-group))+ collection-id (:resource_collection_id app)]+ (testing "a grant made on the collection is listed and admits the group's members"+ (perms/grant-collection-read-permissions! group-id collection-id)+ (is (= #{group-id} (access-group-ids app)))+ (is (= {:id (:id app) :name "birds" :display_name "Birds" :enabled true :outdated false}(mt/user-http-request :rasta :get 200 "ee/apps/birds"))))))))(deftest tenant-users-cannot-inherit-app-access-test@@ -342,7 +305,7 @@(mt/with-current-user (:id user)(is (not (mi/can-read? app)))))))-(deftest sso-membership-controls-assigned-app-access-test+(deftest sso-membership-controls-app-access-test(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/PermissionsGroup group {}diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.cljindex 1a4dd5d4eaf..5f5765b7d0d 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj@@ -146,7 +146,7 @@(mt/with-temp-vals-in-db :model/Table (mt/id :venues) {:active false}(is (= [] (group-access/permission-warnings app)))))))))-(deftest group-permission-warnings-ignore-unassigned-groups-test+(deftest group-permission-warnings-ignore-groups-without-access-test(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}(mt/with-no-data-perms-for-all-users!(with-app-reading! [_app [(mt/id :venues) (mt/id :orders)]]diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_group_assignment_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_group_assignment_test.cljdeleted file mode 100644index f5dcb5c6380..00000000000--- a/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_group_assignment_test.clj+++ /dev/null@@ -1,56 +0,0 @@-(ns metabase-enterprise.data-apps.models.data-app-group-assignment-test- (:require- [clojure.test :refer :all]- [metabase-enterprise.data-apps.group-access :as group-access]- [metabase.models.interface :as mi]- [metabase.permissions.core :as perms]- [metabase.test :as mt]- [toucan2.core :as t2]))--(deftest permissions-test- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/PermissionsGroup group {}]- (group-access/add-groups! app [(:id group)])- (perms/add-user-to-group! (mt/user->id :rasta) (:id group))- (let [assignment (t2/select-one :model/DataAppGroupAssignment :data_app_id (:id app))]- ; crowberto is an admin. rasta is a member of the group. lucky is neither.- (doseq [[user allowed?] [[:crowberto true] [:rasta false] [:lucky false]]]- (testing (str "assignment permissions for " user)- (mt/with-current-user (mt/user->id user)- (is (= allowed? (mi/can-read? assignment)))- (is (= allowed? (mi/can-read? :model/DataAppGroupAssignment (:id assignment))))- (is (= allowed? (mi/can-write? assignment)))- (is (= allowed? (mi/can-write? :model/DataAppGroupAssignment (:id assignment))))- (is (= allowed? (mi/can-create? :model/DataAppGroupAssignment- {:data_app_id (:id app) :permission_group_id (:id group)})))))))))--(deftest insert-validates-group-test- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/PermissionsGroup tenant-group {:is_tenant_group true}]- ; admins, tenant groups and non-existent groups can't be assigned- (doseq [group-id [(:id (perms/admin-group)) (:id tenant-group) Integer/MAX_VALUE]]- (testing (str "cannot assign group " group-id)- (is (thrown? clojure.lang.ExceptionInfo- (t2/with-transaction [_conn]- (t2/insert! :model/DataAppGroupAssignment- {:data_app_id (:id app) :permission_group_id group-id}))))- (is (empty? (t2/select :model/DataAppGroupAssignment :data_app_id (:id app))))))))--(deftest update-validates-group-test- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/PermissionsGroup group {}- :model/PermissionsGroup replacement-group {}- :model/PermissionsGroup tenant-group {:is_tenant_group true}- :model/DataAppGroupAssignment assignment {:data_app_id (:id app) :permission_group_id (:id group)}]- ; admins, tenant groups and non-existent groups can't be assigned- (doseq [group-id [(:id (perms/admin-group)) (:id tenant-group) Integer/MAX_VALUE]]- (testing (str "cannot change an assignment to group " group-id)- (is (thrown? clojure.lang.ExceptionInfo- (t2/with-transaction [_conn]- (t2/update! :model/DataAppGroupAssignment (:id assignment) {:permission_group_id group-id}))))- (is (= (:id group)- (t2/select-one-fn :permission_group_id :model/DataAppGroupAssignment :id (:id assignment))))))- (testing "an assignment can move to another eligible group"- (t2/update! :model/DataAppGroupAssignment (:id assignment) {:permission_group_id (:id replacement-group)})- (is (= (:id replacement-group)- (t2/select-one-fn :permission_group_id :model/DataAppGroupAssignment :id (:id assignment)))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_test.cljindex 1cc609c3a6d..c05c867dee6 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_test.clj@@ -8,6 +8,7 @@[metabase.api.common :as api][metabase.events.core :as events][metabase.models.interface :as mi]+ [metabase.permissions.core :as perms][metabase.test :as mt][metabase.util.json :as json][toucan2.core :as t2]))@@ -40,11 +41,12 @@(deftest insert-creates-the-resources-the-app-owns-test(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [{:keys [id resource_collection_id]} (t2/select-one :model/DataApp (insert-app!))]+ (let [{:keys [resource_collection_id]} (t2/select-one :model/DataApp (insert-app!))](is (=? {:name "Data App: m" :namespace :data-apps :location "/"}(t2/select-one :model/Collection :id resource_collection_id))"the collection is the app's own, in the data-apps namespace")- (is (not (t2/exists? :model/DataAppGroupAssignment :data_app_id id))))))+ (is (= #{} (perms/collection-read-access-group-ids resource_collection_id))+ "no group but Administrators can read the new collection"))))(deftest insert-publishes-the-creation-of-the-collection-test(testing "the collection is created the way any collection is, so what listens to collection events (remote sync,@@ -142,18 +144,20 @@(:allowed_hosts (t2/select-one :model/DataApp :name "n")))))))(deftest permissions-test- (testing "a superuser can read, write, and create"- (mt/with-premium-features #{:data-apps}- (binding [api/*is-superuser?* true]- (is (mi/can-read? :model/DataApp 1))- (is (mi/can-write? :model/DataApp 1))- (is (mi/can-create? :model/DataApp {})))))- (testing "without the data apps feature, a superuser cannot read"- (mt/with-premium-features #{}- (binding [api/*is-superuser?* true]- (is (not (mi/can-read? :model/DataApp 1))))))- (testing "an unassigned user cannot read, write, or create"- (binding [api/*is-superuser?* false]- (is (not (mi/can-read? :model/DataApp 1)))- (is (not (mi/can-write? :model/DataApp 1)))- (is (not (mi/can-create? :model/DataApp {}))))))+ (mt/with-model-cleanup [:model/DataApp :model/Collection]+ (let [id (insert-app!)]+ (testing "a superuser can read, write, and create"+ (mt/with-premium-features #{:data-apps}+ (binding [api/*is-superuser?* true]+ (is (mi/can-read? :model/DataApp id))+ (is (mi/can-write? :model/DataApp id))+ (is (mi/can-create? :model/DataApp {})))))+ (testing "without the data apps feature, a superuser cannot read"+ (mt/with-premium-features #{}+ (binding [api/*is-superuser?* true]+ (is (not (mi/can-read? :model/DataApp id))))))+ (testing "a user without access to the app's collection cannot read, write, or create"+ (binding [api/*is-superuser?* false]+ (is (not (mi/can-read? :model/DataApp id)))+ (is (not (mi/can-write? :model/DataApp id)))+ (is (not (mi/can-create? :model/DataApp {}))))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/resources_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/resources_test.cljindex f37a3067107..5ce2d89758f 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/resources_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/resources_test.clj@@ -71,46 +71,19 @@(t2/select-one :model/Collection :id resource_collection_id)))(is (= resource_collection_id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app)))))))))-(deftest ensure-resources-preserves-collection-grants-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [app (create-data-app! "birds")- {collection-id :resource_collection_id} (data-app.resources/ensure-resources! app)- group-id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Finches"}))- _ (group-access/add-groups! app [group-id])- read-path (perms/collection-read-path collection-id)- grant (t2/select-one :model/Permissions :group_id group-id :object read-path)]- (is (some? grant))- (data-app.resources/ensure-resources! app)- ; correct grants should not be changed after sync- (is (= grant (t2/select-one :model/Permissions :group_id group-id :object read-path))))))--(deftest ensure-resources-repairs-collection-grants-test+(deftest ensure-resources-leaves-collection-grants-alone-test(mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup](mt/with-temp [:model/PermissionsGroup {other-group-id :id} {}](let [app (create-data-app! "birds"){collection-id :resource_collection_id} (data-app.resources/ensure-resources! app)group-id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Finches"}))_ (group-access/add-groups! app [group-id])- read-path (perms/collection-read-path collection-id)- write-path (perms/collection-readwrite-path collection-id)]- ; grant incorrect permission: write access to the data app collection- (perms/grant-collection-readwrite-permissions! group-id collection-id)- ; grant incorrect permission: let other groups read the data app collection+ grants (fn [] (t2/select-fn-set (juxt :group_id :object) :model/Permissions+ :object [:in [(perms/collection-read-path collection-id)+ (perms/collection-readwrite-path collection-id)]]))]+ (perms/revoke-collection-permissions! group-id collection-id)(perms/grant-collection-read-permissions! other-group-id collection-id)- (data-app.resources/ensure-resources! app)- ; the two bad grants should be removed after sync.- ; only the correct grants should remain.- (is (= [{:group_id group-id :object read-path}]- (t2/select [:model/Permissions :group_id :object]- :object [:in [read-path write-path]])))))))--(deftest ensure-resources-restores-missing-collection-grant-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [app (create-data-app! "birds")- {collection-id :resource_collection_id} (data-app.resources/ensure-resources! app)- group-id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Finches"}))- _ (group-access/add-groups! app [group-id])]- (perms/revoke-collection-permissions! group-id collection-id)- (data-app.resources/ensure-resources! app)- ; read access should be restored after sync- (is (t2/exists? :model/Permissions :group_id group-id :object (perms/collection-read-path collection-id))))))+ (let [before (grants)]+ (data-app.resources/ensure-resources! app)+ (is (= #{[other-group-id (perms/collection-read-path collection-id)]} before))+ (is (= before (grants))))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/search_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/search_test.cljindex 978520248dd..c1eb0932787 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/search_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/search_test.clj@@ -52,6 +52,11 @@search/model-set(contains? "data-app"))))+(defn- grant-app-access!+ "Grant the group with `group-id` read access to the resource collection of the data app with `app-id`."+ [group-id app-id]+ (perms/grant-collection-read-permissions! group-id (t2/select-one-fn :resource_collection_id :model/DataApp :id app-id)))+(defn- with-each-engine"Run `check!` with the index engine when the app DB supports it, then with the legacy engine."[check!]@@ -62,23 +67,23 @@(mt/with-premium-features #{:data-apps}(mt/with-temp [:model/DataApp {slug-match-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds":bundle_path "sightings.js"}- :model/DataApp {assigned-id :id} {:name "plovers" :display_name "Sandpiper counts"- :bundle_path "counts.js"}+ :model/DataApp {granted-id :id} {:name "plovers" :display_name "Sandpiper counts"+ :bundle_path "counts.js"}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id assigned-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (grant-app-access! group-id granted-id)(with-each-engine(fn [](testing "an admin finds every app, by its slug or its display name"- (is (= #{slug-match-id assigned-id}+ (is (= #{slug-match-id granted-id}(set (keys (found-data-apps :crowberto :models "data-app"))))))- (testing "another user finds the apps assigned to one of their groups, with display name and slug"- (is (= {assigned-id {:name "Sandpiper counts" :data_app_slug "plovers"}}+ (testing "another user finds the apps whose collection one of their groups can read, with display name and slug"+ (is (= {granted-id {:name "Sandpiper counts" :data_app_slug "plovers"}}(found-data-apps :rasta :models "data-app"))))- (testing "a user in no assigned group finds none"+ (testing "a user in no group with access finds none"(is (= {} (found-data-apps :lucky :models "data-app"))))(testing "a search that names no models, as global search does, finds them too"- (is (= #{slug-match-id assigned-id}+ (is (= #{slug-match-id granted-id}(set (keys (found-data-apps :crowberto)))))))))))(deftest search-finds-disabled-and-outdated-data-apps-the-user-can-read-test@@ -90,10 +95,10 @@:model/DataApp {disabled-id :id} {:name "sandpiper-sanderling" :display_name "Sanderling":bundle_path "sanderling.js" :version 2 :enabled false}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id current-id :permission_group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id outdated-id :permission_group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id disabled-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (grant-app-access! group-id current-id)+ (grant-app-access! group-id outdated-id)+ (grant-app-access! group-id disabled-id)(with-redefs [data-apps.config/supported-app-version 2](with-each-engine(fn []@@ -114,26 +119,21 @@:bundle_path "counts.js"}:model/DataApp {disabled-id :id} {:name "sandpiper-drafts" :display_name "Drafts":enabled false :bundle_path "drafts.js"}- :model/DataApp {unassigned-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"- :bundle_path "sightings.js"}+ :model/DataApp {ungranted-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"+ :bundle_path "sightings.js"}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroup {tenant-group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id enabled-id :permission_group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id disabled-id :permission_group_id group-id}]- ;; Assigned before the group becomes a tenant group.- (t2/insert! :model/DataAppGroupAssignment {:data_app_id enabled-id :permission_group_id tenant-group-id})- (t2/update! :model/PermissionsGroup tenant-group-id {:is_tenant_group true})- (perms/add-user-to-group! (:id tenant-user) tenant-group-id)+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (grant-app-access! group-id enabled-id)+ (grant-app-access! group-id disabled-id)(with-each-engine(fn [](testing "an admin finds every app"- (is (= #{enabled-id disabled-id unassigned-id} (set (keys (found-data-apps :crowberto))))))- (testing "another user finds only the enabled apps assigned to one of their groups"+ (is (= #{enabled-id disabled-id ungranted-id} (set (keys (found-data-apps :crowberto))))))+ (testing "another user finds only the enabled apps whose collection one of their groups can read"(is (= #{enabled-id} (set (keys (found-data-apps :rasta))))))- (testing "a user in no assigned group finds none"+ (testing "a user in no group with access finds none"(is (= {} (found-data-apps :lucky))))- (testing "a tenant user finds none, even through an assigned tenant group"+ (testing "a tenant user finds none"(is (= {} (found-data-apps (:id tenant-user)))))))))))(deftest type-filter-offers-data-apps-only-to-users-who-can-read-one-test@@ -144,16 +144,16 @@:enabled false :bundle_path "drafts.js"}:model/PermissionsGroup {other-group-id :id} {}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id enabled-id :permission_group_id other-group-id}- :model/DataAppGroupAssignment _ {:data_app_id disabled-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (grant-app-access! other-group-id enabled-id)+ (grant-app-access! group-id disabled-id)(with-each-engine(fn [](testing "an admin is offered data apps"(is (offers-data-app-type? :crowberto)))- (testing "a user in no assigned group is not offered data apps"+ (testing "a user in no group with access is not offered data apps"(is (not (offers-data-app-type? :lucky))))- (testing "a user whose groups are assigned only a disabled app is not offered data apps"+ (testing "a user whose groups can read only a disabled app's collection is not offered data apps"(is (not (offers-data-app-type? :rasta)))))))))(deftest type-filter-offers-no-data-apps-without-the-feature-test@@ -161,8 +161,8 @@(mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds":bundle_path "s.js"}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id app-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (grant-app-access! group-id app-id)(with-each-engine(fn [](doseq [user [:crowberto :rasta :lucky]]@@ -181,8 +181,8 @@(mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds":bundle_path "s.js"}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id app-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (grant-app-access! group-id app-id)(doseq [[app-db with-app-db] [["this app DB" (fn [thunk] (thunk))]["an app DB without the search index" without-app-db-index]]](with-app-db@@ -190,9 +190,9 @@(testing app-db(testing "an admin is offered data apps"(is (semantic-engine-offers-data-app-type? :crowberto)))- (testing "a user in an assigned group is offered data apps"+ (testing "a user in a group with access is offered data apps"(is (semantic-engine-offers-data-app-type? :rasta)))- (testing "a user in no assigned group is not offered data apps"+ (testing "a user in no group with access is not offered data apps"(is (not (semantic-engine-offers-data-app-type? :lucky)))))))))))(deftest search-by-ids-finds-the-requested-data-app-test@@ -202,9 +202,9 @@:model/DataApp {other-id :id} {:name "plovers" :display_name "Sandpiper counts":bundle_path "counts.js"}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id app-id :permission_group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id other-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (grant-app-access! group-id app-id)+ (grant-app-access! group-id other-id)(with-each-engine(fn [](doseq [user [:crowberto :rasta]]diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/serialization_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/serialization_test.cljindex f6086b5d187..911985cec52 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/serialization_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/serialization_test.clj@@ -107,10 +107,10 @@(is (= "console.log(1)" (bundle-text imported)))(testing "new app starts enabled, like one created through the API"(is (true? (:enabled imported))))- (testing "the import links the collection the manifest names without importing group assignments"+ (testing "the import links the collection the manifest names without importing group access"(is (=? {:entity_id collection-entity-id :namespace :data-apps}(t2/select-one :model/Collection :id (:resource_collection_id imported))))- (is (not (t2/exists? :model/DataAppGroupAssignment :data_app_id (:id imported)))))))))))+ (is (empty? (group-access/assigned-groups imported))))))))))(deftest import-refuses-a-manifest-that-names-another-collection-test(mt/with-premium-features #{:data-apps}@@ -150,8 +150,7 @@:enabled false:resource_collection_id (:resource_collection_id app)}updated))- (is (t2/exists? :model/DataAppGroupAssignment- :data_app_id (:id app) :permission_group_id (:id group)))+ (is (= [(:id group)] (mapv :id (group-access/assigned-groups updated))))(is (= "console.log(2)" (bundle-text updated)))(is (not= (:bundle_hash app) (:bundle_hash updated)))))))))diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.cljindex c4b764d94d8..66fa76d41f8 100644--- a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj@@ -3175,8 +3175,7 @@(data-apps.tu/with-published-tables! [(mt/id :venues)](mt/with-temp [:model/DataApp {app-id :id collection-id :resource_collection_id}{:name "birds" :display_name "Birds" :bundle_path "birds.js" :enabled false}- :model/Card {card-id :id} (data-app-card collection-id)- :model/PermissionsGroup {group-id :id} {}]+ :model/Card {card-id :id} (data-app-card collection-id)](mt/with-model-cleanup [:model/Dependency :model/DependencyStatus](deps.test/synchronously-run-dependency-job!)(perms/grant-collection-read-permissions! (perms/all-users-group) collection-id)@@ -3189,11 +3188,7 @@(mt/with-premium-features #{:dependencies :data-apps}(testing "an admin sees every app, disabled ones too"(is (= #{app-id} (dependent-ids :crowberto))))- (testing "a user sees the enabled apps assigned to one of their groups, not the apps whose collection they- can read"- (is (= #{} (dependent-ids :rasta)))- (perms/add-user-to-group! (mt/user->id :rasta) group-id)- (t2/insert! :model/DataAppGroupAssignment {:data_app_id app-id :permission_group_id group-id})+ (testing "a user sees the enabled apps whose collection they can read"(is (= #{} (dependent-ids :rasta)))(t2/update! :model/DataApp app-id {:enabled true})(is (= #{app-id} (dependent-ids :rasta)))))@@ -3204,11 +3199,11 @@(deftest graph-data-app-names-only-for-non-admins-test(testing "GET /api/ee/dependencies/graph gives a non-admin only a data app's names, as the data-app API does"(mt/with-premium-features #{:dependencies :data-apps}- (mt/with-temp [:model/DataApp {app-id :id} {:name "birds" :display_name "Birds" :description "Sightings"- :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp {app-id :id collection-id :resource_collection_id}+ {:name "birds" :display_name "Birds" :description "Sightings" :bundle_path "birds.js"}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id app-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (perms/grant-collection-read-permissions! group-id collection-id)(let [app-data (fn [user](->> (mt/user-http-request user :get 200 "ee/dependencies/graph" :id app-id :type "data-app"):nodes@@ -3223,12 +3218,13 @@(deftest graph-data-app-without-data-apps-feature-test(testing "GET /api/ee/dependencies/graph refuses a data app start node without the data apps feature"(mt/with-premium-features #{:dependencies}- (mt/with-temp [:model/DataApp {app-id :id} {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp {app-id :id collection-id :resource_collection_id}+ {:name "birds" :display_name "Birds" :bundle_path "birds.js"}:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}- :model/DataAppGroupAssignment _ {:data_app_id app-id :permission_group_id group-id}]+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]+ (perms/grant-collection-read-permissions! group-id collection-id)(doseq [[description user] [["to an admin" :crowberto]- ["to a user in a group assigned the app" :rasta]]]+ ["to a user in a group that can read the app's collection" :rasta]]](testing description(is (= "You don't have permissions to do that."(mt/user-http-request user :get 403 "ee/dependencies/graph" :id app-id :type "data-app")))))))))diff --git a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.cljindex 6be8f4f449d..0d35cd6f241 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj@@ -55,7 +55,6 @@:model/ContentDiagnosticsFinding:model/ContentTranslation:model/DashboardBookmark- :model/DataAppGroupAssignment:model/DataComplexityScore:model/DataPermissions:model/DatabaseRouterdiff --git a/enterprise/frontend/src/metabase-enterprise/api/data-app.ts b/enterprise/frontend/src/metabase-enterprise/api/data-app.tsindex 29764af0c5a..21cff44ca5d 100644--- a/enterprise/frontend/src/metabase-enterprise/api/data-app.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/data-app.ts@@ -15,15 +15,12 @@ import { idTag, invalidateTags, listTag } from "./tags";// when a sync changes things.const REPO_STATUS_TAG = idTag("data-app", "REPO-STATUS");-type ListDataAppsOptions = { available?: boolean };-export const dataAppApi = EnterpriseApi.injectEndpoints({endpoints: (builder) => ({- listDataApps: builder.query<DataApp[], ListDataAppsOptions | void>({- query: (options) => ({+ listDataApps: builder.query<DataApp[], void>({+ query: () => ({method: "GET",url: "/api/ee/apps",- params: options,}),providesTags: (apps = []) => [listTag("data-app"),diff --git a/enterprise/frontend/src/metabase-enterprise/data-apps/navbar/DataAppsNavbarSection.tsx b/enterprise/frontend/src/metabase-enterprise/data-apps/navbar/DataAppsNavbarSection.tsxindex e3261709dff..bdf919b6997 100644--- a/enterprise/frontend/src/metabase-enterprise/data-apps/navbar/DataAppsNavbarSection.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-apps/navbar/DataAppsNavbarSection.tsx@@ -14,7 +14,10 @@ export function DataAppsNavbarSection({}: {onItemSelect: () => void;}) {- const { data: dataApps = [] } = useListDataAppsQuery({ available: true });+ const { data: allDataApps = [] } = useListDataAppsQuery();+ const dataApps = allDataApps.filter(+ (dataApp) => dataApp.enabled && !dataApp.outdated,+ );if (dataApps.length === 0) {return null;diff --git a/enterprise/frontend/src/metabase-enterprise/data-apps/navbar/DataAppsNavbarSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-apps/navbar/DataAppsNavbarSection.unit.spec.tsxindex 0a75a582c13..dbf9932a41f 100644--- a/enterprise/frontend/src/metabase-enterprise/data-apps/navbar/DataAppsNavbarSection.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/data-apps/navbar/DataAppsNavbarSection.unit.spec.tsx@@ -3,12 +3,13 @@ import fetchMock from "fetch-mock";import { renderWithProviders, screen, waitFor, within } from "__support__/ui";import { Route } from "metabase/router";+import { createMockDataApp } from "metabase-types/api/mocks";import { DataAppsNavbarSection } from "./DataAppsNavbarSection";describe("DataAppsNavbarSection", () => {it("does not show apps section when there are no data apps", async () => {- fetchMock.get("path:/api/ee/apps?available=true", []);+ fetchMock.get("path:/api/ee/apps", []);renderWithProviders(<DataAppsNavbarSection onItemSelect={jest.fn()} />);@@ -19,10 +20,20 @@ describe("DataAppsNavbarSection", () => {);});- it("shows every data app as a link", async () => {- fetchMock.get("path:/api/ee/apps?available=true", [- { name: "gizmo", display_name: "Gizmo" },- { name: "gadget", display_name: "Gadget" },+ it("shows every enabled, current data app as a link", async () => {+ fetchMock.get("path:/api/ee/apps", [+ createMockDataApp({ name: "gizmo", display_name: "Gizmo" }),+ createMockDataApp({ name: "gadget", display_name: "Gadget" }),+ createMockDataApp({+ name: "disabled",+ display_name: "Disabled",+ enabled: false,+ }),+ createMockDataApp({+ name: "outdated",+ display_name: "Outdated",+ outdated: true,+ }),]);const onItemSelect = jest.fn();@@ -61,5 +72,8 @@ describe("DataAppsNavbarSection", () => {expect(screen.queryByRole("link", { name: /Contents/ }),).not.toBeInTheDocument();+ expect(+ screen.queryByRole("link", { name: /Disabled|Outdated/ }),+ ).not.toBeInTheDocument();});});diff --git a/resources/migrations/2026/20261024_drop_data_app_group_assignment.yaml b/resources/migrations/2026/20261024_drop_data_app_group_assignment.yamlnew file mode 100644index 00000000000..3c29eafbeca--- /dev/null+++ b/resources/migrations/2026/20261024_drop_data_app_group_assignment.yaml@@ -0,0 +1,61 @@+## A group can open a data app when it can read the app's resource collection, so the collection grants alone record+## who has access. No instance runs data apps in production yet, so the group assignments are dropped without moving+## them into grants, and a rollback brings the table back empty.++databaseChangeLog:+ - changeSet:+ id: pmgj27+ author: ranquild+ comment: Drop data app group assignments, replaced by grants on the app's resource collection+ changes:+ - dropTable:+ tableName: data_app_group_assignment+ rollback:+ - createTable:+ tableName: data_app_group_assignment+ remarks: Permission group assignments for data apps+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ primaryKeyName: pk_data_app_group_assignment+ - column:+ name: data_app_id+ remarks: Data app receiving access through the assigned group+ type: int+ constraints:+ nullable: false+ - column:+ name: permission_group_id+ remarks: Permission group assigned to the data app+ type: int+ constraints:+ nullable: false+ - addUniqueConstraint:+ tableName: data_app_group_assignment+ columnNames: data_app_id, permission_group_id+ constraintName: uq_data_app_group_assignment+ - createIndex:+ tableName: data_app_group_assignment+ indexName: idx_data_app_group_assignment_group+ columns:+ - column:+ name: permission_group_id+ - addForeignKeyConstraint:+ baseTableName: data_app_group_assignment+ baseColumnNames: data_app_id+ referencedTableName: data_app+ referencedColumnNames: id+ constraintName: fk_data_app_group_assignment_app+ onDelete: CASCADE+ - addForeignKeyConstraint:+ baseTableName: data_app_group_assignment+ baseColumnNames: permission_group_id+ referencedTableName: permissions_group+ referencedColumnNames: id+ constraintName: fk_data_app_group_assignment_group+ onDelete: CASCADEdiff --git a/src/metabase/data_apps/core.clj b/src/metabase/data_apps/core.cljindex c5be2fe7fdb..d6fad5d9f63 100644--- a/src/metabase/data_apps/core.clj+++ b/src/metabase/data_apps/core.clj@@ -2,12 +2,6 @@(:require[metabase.premium-features.core :refer [defenterprise]]))-(defenterprise check-data-app-access!- "Authorize the data app before serving its HTML entry point."- metabase-enterprise.data-apps.core- [_request]- nil)-(defenterprise visible-data-app-filter-select"A HoneySQL query of the ids of the data apps that the user described by `user-info` (`:user-id`, `:is-superuser?`)can read, or nil, meaning none, without the data apps feature."diff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.cljindex 13f231a1b50..78000580a6f 100644--- a/src/metabase/models/resolution.clj+++ b/src/metabase/models/resolution.clj@@ -39,7 +39,6 @@:model/ContentTranslation metabase.content-translation.models:model/CustomVizPlugin metabase-enterprise.custom-viz-plugin.models.custom-viz-plugin:model/DataApp metabase-enterprise.data-apps.models.data-app- :model/DataAppGroupAssignment metabase-enterprise.data-apps.models.data-app-group-assignment:model/Dashboard metabase.dashboards.models.dashboard:model/DashboardBookmark metabase.bookmarks.models.bookmark:model/DashboardCard metabase.dashboards.models.dashboard-carddiff --git a/src/metabase/server/routes/index.clj b/src/metabase/server/routes/index.cljindex 757ef96853a..a891b773328 100644--- a/src/metabase/server/routes/index.clj+++ b/src/metabase/server/routes/index.clj@@ -8,7 +8,6 @@[hiccup.util][metabase.appearance.core :as appearance][metabase.config.core :as config]- [metabase.data-apps.core :as data-apps][metabase.initialization-status.core :as init-status][metabase.premium-features.core :as premium-features][metabase.settings.core :as setting]@@ -171,10 +170,4 @@(cond(not (premium-features/enable-data-apps?)) (respond nil)(nil? (:metabase-user-id request)) (respond (login-redirect request))- :else (if-let [error (try- (data-apps/check-data-app-access! request)- nil- (catch Throwable e- e))]- (raise error)- (data-app-shell request respond raise))))+ :else (data-app-shell request respond raise)))diff --git a/test/metabase/app_db/custom_migrations_test.clj b/test/metabase/app_db/custom_migrations_test.cljindex 9ed241161ca..f3b4b687cbe 100644--- a/test/metabase/app_db/custom_migrations_test.clj+++ b/test/metabase/app_db/custom_migrations_test.clj@@ -3123,7 +3123,7 @@(deftest delete-data-app-drafts-test(testing "v65.2026-10-07T00:00:00: draft rows go with their assignments and empty collections"- (impl/test-migrations ["v65.2026-10-07T00:00:00"] [migrate!]+ (impl/test-migrations ["v65.2026-10-07T00:00:00" "v65.2026-10-07T00:00:01"] [migrate!](let [insert-collection! (fn [name](t2/insert-returning-pk! :collection {:name name:slug namediff --git a/test/metabase/app_db/schema_migrations_test.clj b/test/metabase/app_db/schema_migrations_test.cljindex 65f4bdb7960..9808cc7cd5f 100644--- a/test/metabase/app_db/schema_migrations_test.clj+++ b/test/metabase/app_db/schema_migrations_test.clj@@ -4200,3 +4200,10 @@(is (= own (t2/select-one-fn :resource_collection_id :data_app :id other-app)))(testing "a second app on a collection is refused"(is (thrown? Exception (app! "third" shared))))))))++(deftest drop-data-app-group-assignment-test+ (testing "pmgj27: the data app group assignments are dropped, since the app's collection grants record its access"+ (impl/test-migrations ["pmgj27"] [migrate!]+ (is (= [] (t2/select :data_app_group_assignment)))+ (migrate!)+ (is (thrown? Exception (t2/select :data_app_group_assignment))))))diff --git a/test/metabase/cmd/copy_test.clj b/test/metabase/cmd/copy_test.cljindex 8e287bc75bf..efe7ece5f8a 100644--- a/test/metabase/cmd/copy_test.clj+++ b/test/metabase/cmd/copy_test.clj@@ -147,7 +147,6 @@:model/ContentTranslation:model/DashboardFavorite:model/DataApp- :model/DataAppGroupAssignment:model/DataComplexityScore:model/DatabaseRouter:model/Dependencydiff --git a/test/metabase/server/routes_test.clj b/test/metabase/server/routes_test.cljindex 8cac01e9667..b3b0ec9457d 100644--- a/test/metabase/server/routes_test.clj+++ b/test/metabase/server/routes_test.clj@@ -2,8 +2,6 @@(:require[clojure.string :as str][clojure.test :refer :all]- [metabase.data-apps.core :as data-apps]- [metabase.premium-features.core :as premium-features][metabase.server.routes.index :as index][metabase.test :as mt][metabase.test.http-client :as client]))@@ -40,8 +38,7 @@responds nil so routing falls through to the generic embed handler — exactly as if dataapps did not exist";; Stub authorization and HTML rendering to isolate the feature gate.- (with-redefs [data-apps/check-data-app-access! (constantly nil)- index/data-app-shell (fn [_req respond _raise] (respond {:status 200 :body "DATA-APP"}))]+ (with-redefs [index/data-app-shell (fn [_req respond _raise] (respond {:status 200 :body "DATA-APP"}))](let [request {:uri "/embed/apps/sales" :metabase-user-id (mt/user->id :rasta)}];; `enable-data-apps?` also requires the EE code to be present (`config/ee-available?`), so the;; served path exists only on EE; on OSS the entrypoint always falls through.@@ -125,14 +122,3 @@(let [response (get-static-asset 200 {"if-modified-since" held})](is (= 200 (:status response)))(is (not (str/blank? (body-text (:body response)))))))))))--(deftest data-app-entrypoint-raises-access-errors-test- (let [error (ex-info "Access denied" {:status-code 403})- callbacks (atom [])]- (with-redefs [premium-features/enable-data-apps? (constantly true)- data-apps/check-data-app-access! (fn [_request] (throw error))- index/data-app-shell (fn [& _args] (swap! callbacks conj :shell))]- (index/data-app {:metabase-user-id 1}- (fn [response] (swap! callbacks conj [:respond response]))- (fn [error] (swap! callbacks conj [:raise error]))))- (is (= [[:raise error]] @callbacks))))