Skip to content

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

Disallow using `-rest` modules in non`-rest` modules (#69045)

Cam Saul843350757c9a
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 93616e38e83..9141063d3a3 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -410,7 +410,6 @@
models
request
users
- users-rest
util}}
;; technically this 'uses' `enterprise/core` and `test` since it tries to load them to see if they exists so we know
@@ -1630,41 +1629,42 @@
users
{:team "UX West"
- :api #{metabase.users.init
+ :api #{metabase.users.core
+ metabase.users.init
metabase.users.models.user
metabase.users.models.user-parameter-value
metabase.users.schema
metabase.users.settings}
- :uses #{api
+ :uses #{analytics
+ api
batch-processing
channel
config
events
models
+ notification
permissions
premium-features
settings
setup
system
tenants
- util}}
+ util
+ enterprise/advanced-permissions}}
users-rest
{:team "UX West"
:api #{metabase.users-rest.api}
- :uses #{analytics
- api
+ :uses #{api
appearance
auth-identity
collections
config
events
models
- notification
permissions
premium-features
request
- settings
sso
tenants
users
@@ -1955,7 +1955,7 @@
setup
startup
sync
- users-rest
+ users
util
enterprise/serialization}}
@@ -2070,7 +2070,7 @@
settings
util
warehouse-schema
- warehouses-rest}}
+ warehouses}}
enterprise/dependencies
{:team "Querying"
@@ -2214,10 +2214,10 @@
system
task
transforms
- users-rest
+ users
util
warehouse-schema
- warehouses-rest
+ warehouses
enterprise/dependencies
enterprise/transforms-python}}
@@ -2378,7 +2378,6 @@
:uses #{analytics
api
collections
- collections-rest
embedding
premium-features
queries
diff --git a/.clj-kondo/src/hooks/common/modules.clj b/.clj-kondo/src/hooks/common/modules.clj
index ddd30d4db7f..0a5f439ba49 100644
--- a/.clj-kondo/src/hooks/common/modules.clj
+++ b/.clj-kondo/src/hooks/common/modules.clj
@@ -75,6 +75,15 @@
(contains? module-api-namespaces ns-symb)
(contains? module-friends current-module))))
+(defn- rest-module? [module]
+ (str/ends-with? module "-rest"))
+
+(defn- routes-module? [module]
+ (str/ends-with? module "-routes"))
+
+(defn- core-module? [module]
+ (str/ends-with? module "core"))
+
(defn usage-error
"Find usage errors when a `required-namespace` is required in the `current-module`. Returns a string describing the
error type if there is one, otherwise `nil` if there are no errors."
@@ -93,4 +102,15 @@
(format "Namespace %s is not an allowed external API namespace for the %s module. [:metabase/modules %s :api]"
required-namespace
required-module
- required-module)))))
+ required-module)
+
+ ;; (for now) rest modules are allowed to use one another; `routes` is ok because it collects routes together
+ ;; and `core` is ok because [[metabase.core.init]] might need to init some of the `-routes` modules'
+ ;; namespaces
+ (and (not ((some-fn rest-module? routes-module? core-module?) current-module))
+ (rest-module? required-module))
+ (format "Do not use -rest modules (%s) in non-rest modules (%s) -- move things from %s to %s if needed"
+ required-module
+ current-module
+ required-module
+ (symbol (str/replace required-module #"-rest$" "")))))))
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/api/user.clj b/enterprise/backend/src/metabase_enterprise/audit_app/api/user.clj
index 57f224388a4..8325b806613 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/api/user.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/api/user.clj
@@ -6,7 +6,7 @@
[metabase.api.macros :as api.macros]
[metabase.audit-app.core :as audit]
[metabase.models.interface :as mi]
- [metabase.users-rest.api :as api.user]
+ [metabase.users.core :as users]
[metabase.util :as u]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
@@ -39,7 +39,7 @@
Archive all Alerts and DashboardSubscriptions created by the User. Only allowed for admins or for the current user."
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
- (api.user/check-self-or-superuser id)
+ (users/check-self-or-superuser id)
;; delete all `PulseChannelRecipient` rows for this User, which means they will no longer receive any
;; Alerts/DashboardSubscriptions
(t2/delete! :model/PulseChannelRecipient :user_id id)
diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
index c08acd81ec9..d6018ad7426 100644
--- a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
@@ -8,7 +8,7 @@
[metabase.settings.core :as setting]
[metabase.util :as u]
[metabase.util.malli.schema :as ms]
- [metabase.warehouses-rest.api :as api.database]
+ [metabase.warehouses.core :as warehouses]
[toucan2.core :as t2]))
;; TODO (Cam 10/28/25) -- fix this endpoint so it uses kebab-case for query parameters for consistency with the rest
@@ -42,7 +42,7 @@
(if-let [invalid-destinations (and check_connection_details
(->> destinations
(keep (fn [{details :details n :name}]
- (let [details-or-error (api.database/test-connection-details (name engine) details)
+ (let [details-or-error (warehouses/test-connection-details engine details)
valid? (not= (:valid details-or-error) false)]
(when-not valid?
[n (dissoc details-or-error :valid)]))))
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/api/document.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/api/document.clj
index ec5c38bd816..6245476636a 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/api/document.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/api/document.clj
@@ -9,7 +9,7 @@
[metabase.api.routes.common :refer [+auth]]
[metabase.query-processor :as qp]
[metabase.util.malli.schema :as ms]
- [metabase.warehouses-rest.api :as warehouses]))
+ [metabase.warehouses.core :as warehouses]))
(set! *warn-on-reflection* true)
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/invite_user.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/invite_user.clj
index 4404aaacb78..38defca6a3f 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/invite_user.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/tools/invite_user.clj
@@ -1,12 +1,12 @@
(ns metabase-enterprise.metabot-v3.tools.invite-user
(:require
- [metabase.users-rest.api :as api.user]))
+ [metabase.users.core :as users]))
(defn invite-user
"Send a Metabase invitation to the address `email`."
[{:keys [email]}]
(try
- (api.user/invite-user {:email email})
+ (users/invite-user! {:email email})
{:output (format "%s has been invited to Metabase." email)}
(catch clojure.lang.ExceptionInfo e
(let [d (ex-data e)]
diff --git a/enterprise/backend/src/metabase_enterprise/stale/api.clj b/enterprise/backend/src/metabase_enterprise/stale/api.clj
index 62e56ea13e1..84ffb1317c7 100644
--- a/enterprise/backend/src/metabase_enterprise/stale/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/stale/api.clj
@@ -8,7 +8,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
- [metabase.collections-rest.api :as api.collection]
+ [metabase.collections.core :as collections]
[metabase.collections.models.collection :as collection]
[metabase.premium-features.core :as premium-features]
[metabase.queries.core :as queries]
@@ -94,7 +94,7 @@
"For dashboards, we want `here` and `location` since they can contain cards as children."
[dashboards]
(for [{parent-coll :collection
- :as dashboard} (api.collection/annotate-dashboards dashboards)]
+ :as dashboard} (collections/annotate-dashboards dashboards)]
(assoc dashboard
:location (or (some-> parent-coll collection/children-location)
"/")
diff --git a/modules/drivers/presto-jdbc/test/metabase/driver/presto_jdbc_test.clj b/modules/drivers/presto-jdbc/test/metabase/driver/presto_jdbc_test.clj
index 242351850d3..6213ff06e5f 100644
--- a/modules/drivers/presto-jdbc/test/metabase/driver/presto_jdbc_test.clj
+++ b/modules/drivers/presto-jdbc/test/metabase/driver/presto_jdbc_test.clj
@@ -16,7 +16,7 @@
[metabase.test :as mt]
[metabase.test.data.presto-jdbc :as data.presto-jdbc]
[metabase.test.fixtures :as fixtures]
- [metabase.warehouses-rest.api :as api.database]
+ [metabase.warehouses.core :as warehouses]
[toucan2.core :as t2])
(:import
(java.io File)))
@@ -230,7 +230,7 @@
;; the others (ex: :auto_run_queries and :refingerprint) are one level up (fields in the model, not in the details
;; JSON blob)
(let [db-details (assoc (:details (mt/db)) :let-user-control-scheduling false)]
- (is (nil? (api.database/test-database-connection :presto-jdbc db-details)))))))
+ (is (nil? (warehouses/test-database-connection :presto-jdbc db-details)))))))
(deftest ^:parallel kerberos-properties-test
(testing "Kerberos related properties are set correctly"
diff --git a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
index 693abc5dcae..72d304ede27 100644
--- a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
+++ b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
@@ -18,7 +18,7 @@
[metabase.test.data.interface :as tx]
[metabase.test.data.sql-jdbc :as sql-jdbc.tx]
[metabase.test.fixtures :as fixtures]
- [metabase.warehouses-rest.api :as api.database]
+ [metabase.warehouses.core :as warehouses]
[toucan2.core :as t2]
[toucan2.tools.with-temp :as t2.with-temp])
(:import
@@ -246,7 +246,7 @@
;; the others (ex: :auto_run_queries and :refingerprint) are one level up (fields in the model, not in the details
;; JSON blob)
(let [db-details (assoc (:details (mt/db)) :let-user-control-scheduling false)]
- (is (nil? (api.database/test-database-connection :starburst db-details)))))))
+ (is (nil? (warehouses/test-database-connection :starburst db-details)))))))
(deftest kerberos-properties-test
(testing "Kerberos related properties are set correctly"
diff --git a/src/metabase/collections/core.clj b/src/metabase/collections/core.clj
index 1e6330072b5..c16a5e34850 100644
--- a/src/metabase/collections/core.clj
+++ b/src/metabase/collections/core.clj
@@ -2,10 +2,12 @@
"Main namespace for interacting with collections"
(:require
[metabase.collections.models.collection]
+ [metabase.collections.util]
[potemkin :as p]))
(comment
- metabase.collections.models.collection/keep-me)
+ metabase.collections.models.collection/keep-me
+ metabase.collections.util/keep-me)
(p/import-vars
[metabase.collections.models.collection
@@ -23,4 +25,6 @@
remote-synced-collection
remote-synced-collection?
shared-tenant-collection?
- transforms-ns])
+ transforms-ns]
+ [metabase.collections.util
+ annotate-dashboards])
diff --git a/src/metabase/collections/util.clj b/src/metabase/collections/util.clj
new file mode 100644
index 00000000000..08f94c487ce
--- /dev/null
+++ b/src/metabase/collections/util.clj
@@ -0,0 +1,25 @@
+(ns metabase.collections.util
+ (:require
+ [toucan2.core :as t2]))
+
+(defn annotate-dashboards
+ "Populates 'here' on dashboards (`below` is impossible since they can't contain collections)"
+ [dashboards]
+ (let [dashboard-ids (into #{} (map :id dashboards))
+ dashboards-containing-cards (->> (when (seq dashboard-ids)
+ (t2/query {:select-distinct [:dashboard_id]
+ :from :report_card
+ :where [:and
+ [:= :archived false]
+ [:in :dashboard_id dashboard-ids]
+ [:exists {:select 1
+ :from :report_dashboardcard
+ :where [:and
+ [:= :report_dashboardcard.card_id :report_card.id]
+ [:= :report_dashboardcard.dashboard_id :report_card.dashboard_id]]}]]}))
+ (map :dashboard_id)
+ (into #{}))]
+ (for [dashboard dashboards]
+ (cond-> dashboard
+ (contains? dashboards-containing-cards (:id dashboard))
+ (assoc :here #{:card})))))
diff --git a/src/metabase/collections_rest/api.clj b/src/metabase/collections_rest/api.clj
index ac1ef9277e7..efe068bb2f3 100644
--- a/src/metabase/collections_rest/api.clj
+++ b/src/metabase/collections_rest/api.clj
@@ -14,6 +14,7 @@
[metabase.api.macros :as api.macros]
[metabase.app-db.core :as mdb]
[metabase.collections-rest.settings :as collections-rest.settings]
+ [metabase.collections.core :as collections]
[metabase.collections.models.collection :as collection]
[metabase.collections.models.collection.root :as collection.root]
[metabase.eid-translation.core :as eid-translation]
@@ -682,32 +683,10 @@
(dissoc :display :authority_level :icon :personal_owner_id :collection_preview
:dataset_query :table_id :query_type :is_upload)))
-(defn annotate-dashboards
- "Populates 'here' on dashboards (`below` is impossible since they can't contain collections)"
- [dashboards]
- (let [dashboard-ids (into #{} (map :id dashboards))
- dashboards-containing-cards (->> (when (seq dashboard-ids)
- (t2/query {:select-distinct [:dashboard_id]
- :from :report_card
- :where [:and
- [:= :archived false]
- [:in :dashboard_id dashboard-ids]
- [:exists {:select 1
- :from :report_dashboardcard
- :where [:and
- [:= :report_dashboardcard.card_id :report_card.id]
- [:= :report_dashboardcard.dashboard_id :report_card.dashboard_id]]}]]}))
- (map :dashboard_id)
- (into #{}))]
- (for [dashboard dashboards]
- (cond-> dashboard
- (contains? dashboards-containing-cards (:id dashboard))
- (assoc :here #{:card})))))
-
(defmethod post-process-collection-children :dashboard
[_ _options parent-collection rows]
(->> rows
- (annotate-dashboards)
+ collections/annotate-dashboards
(map (partial post-process-dashboard parent-collection))))
(defenterprise snippets-collection-filter-clause
diff --git a/src/metabase/comments/api.clj b/src/metabase/comments/api.clj
index abeaf925796..e39c600f417 100644
--- a/src/metabase/comments/api.clj
+++ b/src/metabase/comments/api.clj
@@ -11,7 +11,7 @@
[metabase.comments.models.comment-reaction :as comment-reaction]
[metabase.events.core :as events]
[metabase.request.core :as request]
- [metabase.users-rest.api :as api.user]
+ [metabase.users.core :as users]
[metabase.users.models.user :as user]
[metabase.util :as u]
[metabase.util.i18n :refer [deferred-tru]]
@@ -304,7 +304,7 @@
:total (:count (t2/query-one
(merge {:select [[[:count [:distinct :core_user.id]] :count]]
:from :core_user}
- (api.user/filter-clauses-without-paging clauses))))
+ (users/filter-clauses-without-paging clauses))))
:limit (request/limit)
:offset (request/offset)}))
diff --git a/src/metabase/users/core.clj b/src/metabase/users/core.clj
new file mode 100644
index 00000000000..5a34aa1be81
--- /dev/null
+++ b/src/metabase/users/core.clj
@@ -0,0 +1,14 @@
+(ns metabase.users.core
+ (:require
+ [metabase.users.util]
+ [potemkin :as p]))
+
+(comment metabase.users.util/keep-me)
+
+(p/import-vars
+ [metabase.users.util
+ check-self-or-superuser
+ fetch-user
+ filter-clauses-without-paging
+ invite-user!
+ maybe-set-user-group-memberships!])
diff --git a/src/metabase/users/schema.clj b/src/metabase/users/schema.clj
index 07e272d47d4..64c35038c6c 100644
--- a/src/metabase/users/schema.clj
+++ b/src/metabase/users/schema.clj
@@ -2,6 +2,7 @@
(:require
[metabase.util.i18n :refer [deferred-tru]]
[metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]))
(def LoginAttributes
@@ -29,3 +30,12 @@
[:locale {:optional true} [:maybe ms/KeywordOrString]]
[:type {:optional true} [:maybe ms/KeywordOrString]]
[:tenant_id {:optional true} [:maybe ms/PositiveInt]]])
+
+(mr/def ::user-group-membership
+ "Group Membership info of a User.
+ In which :is_group_manager is only included if `advanced-permissions` is enabled."
+ [:map
+ [:id ms/PositiveInt]
+ [:is_group_manager
+ {:optional true, :description "Only relevant if `advanced-permissions` is enabled. If it is, you should always include this key."}
+ :boolean]])
diff --git a/src/metabase/users/util.clj b/src/metabase/users/util.clj
new file mode 100644
index 00000000000..5c1f3a82a5f
--- /dev/null
+++ b/src/metabase/users/util.clj
@@ -0,0 +1,98 @@
+(ns metabase.users.util
+ (:require
+ [clojure.set :as set]
+ [metabase.analytics.core :as analytics]
+ [metabase.api.common :as api]
+ [metabase.config.core :as config]
+ [metabase.notification.core :as notification]
+ [metabase.permissions.core :as perms]
+ [metabase.premium-features.core :as premium-features]
+ [metabase.settings.core :as setting]
+ [metabase.users.models.user :as user]
+ [metabase.users.schema :as users.schema]
+ [metabase.util :as u]
+ [metabase.util.i18n :refer [tru]]
+ [metabase.util.malli :as mu]
+ [toucan2.core :as t2]))
+
+(defn check-self-or-superuser
+ "Check that `user-id` is *current-user-id*` or that `*current-user*` is a superuser, or throw a 403."
+ [user-id]
+ {:pre [(integer? user-id)]}
+ (api/check-403
+ (or
+ (= user-id api/*current-user-id*)
+ api/*is-superuser?*)))
+
+(defn- maybe-set-user-permissions-groups!
+ [user-or-id new-groups-or-ids]
+ (when (and new-groups-or-ids
+ (not= (user/group-ids user-or-id)
+ (set (map u/the-id new-groups-or-ids))))
+ (api/check-superuser)
+ (user/set-permissions-groups! user-or-id new-groups-or-ids)))
+
+(mu/defn maybe-set-user-group-memberships!
+ "Implementation for `POST /api/user` and friends; set the PermissionsGroupMemberships for a `user-or-id`."
+ [user-or-id
+ new-user-group-memberships :- [:maybe [:sequential ::users.schema/user-group-membership]]
+ & [is-superuser?]]
+ (when new-user-group-memberships
+ ;; if someone passed in both `:is_superuser` and `:group_ids`, make sure the whether the admin group is in group_ids
+ ;; agrees with is_superuser -- don't want to have ambiguous behavior
+ (when (some? is-superuser?)
+ (api/checkp (= is-superuser? (contains? (set (map :id new-user-group-memberships)) (u/the-id (perms/admin-group))))
+ "is_superuser" (tru "Value of is_superuser must correspond to presence of Admin group ID in group_ids.")))
+ (if-let [f (and (premium-features/enable-advanced-permissions?)
+ config/ee-available?
+ (requiring-resolve 'metabase-enterprise.advanced-permissions.models.permissions.group-manager/set-user-group-memberships!))]
+ (f user-or-id new-user-group-memberships)
+ (maybe-set-user-permissions-groups! user-or-id (map :id new-user-group-memberships)))))
+
+(defn fetch-user
+ "Implementation for `/api/user` endpoints; fetch a User from the app DB."
+ [& query-criteria]
+ (apply t2/select-one (vec (cons :model/User user/admin-or-self-visible-columns)) query-criteria))
+
+(mu/defn invite-user!
+ "Implementation for `POST /api/user`, invites a user to Metabase."
+ [{:keys [email
+ user-group-memberships
+ source
+ tenant-id]
+ :as attributes} :- [:map
+ [:source {:optional true, :default :admin} [:enum :setup :admin]]]]
+ (api/check-superuser)
+ (api/checkp (not (t2/exists? :model/User :%lower.email (u/lower-case-en email)))
+ "email" (tru "Email address already in use."))
+ (api/checkp (not (and tenant-id
+ (not (setting/get :use-tenants))))
+ "tenant_id"
+ (tru "Cannot create a Tenant User as Tenants are not enabled for this instance."))
+ (t2/with-transaction [_conn]
+ (let [new-user-id (u/the-id
+ (notification/with-skip-sending-notification (boolean tenant-id)
+ (user/create-and-invite-user!
+ (-> attributes
+ (u/select-non-nil-keys [:first-name :last-name :email :password :login-attributes :tenant-id])
+ (set/rename-keys {:first-name :first_name
+ :last-name :last_name
+ :user-group-memberships :user_group_memberships
+ :login-attributes :login_attributes
+ :tenant-id :tenant_id}))
+ @api/*current-user*
+ (= source :setup))))]
+ (maybe-set-user-group-memberships! new-user-id user-group-memberships)
+ (when (= source :setup)
+ (maybe-set-user-permissions-groups! new-user-id [(perms/all-users-group) (perms/admin-group)]))
+ (analytics/track-event! :snowplow/invite
+ {:event :invite-sent
+ :invited-user-id new-user-id
+ :source (or source :admin)})
+ (-> (fetch-user :id new-user-id)
+ (t2/hydrate :user_group_memberships)))))
+
+(defn filter-clauses-without-paging
+ "Given a where clause, return a clause that can be used to count."
+ [clauses]
+ (dissoc clauses :order-by :limit :offset))
diff --git a/src/metabase/users_rest/api.clj b/src/metabase/users_rest/api.clj
index 2b09c6a4eae..fa615d7043d 100644
--- a/src/metabase/users_rest/api.clj
+++ b/src/metabase/users_rest/api.clj
@@ -1,9 +1,9 @@
(ns metabase.users-rest.api
"/api/user endpoints"
(:require
+ [clojure.set :as set]
[honey.sql.helpers :as sql.helpers]
[java-time.api :as t]
- [metabase.analytics.core :as analytics]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.appearance.core :as appearance]
@@ -12,35 +12,24 @@
[metabase.config.core :as config]
[metabase.events.core :as events]
[metabase.models.interface :as mi]
- [metabase.notification.core :as notification]
[metabase.permissions.core :as perms]
[metabase.premium-features.core :as premium-features]
[metabase.request.core :as request]
- [metabase.settings.core :as setting]
[metabase.sso.core :as sso]
[metabase.tenants.core :as tenants]
+ [metabase.users.core :as users]
[metabase.users.models.user :as user]
[metabase.users.schema :as users.schema]
[metabase.users.settings :as users.settings]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
[metabase.util.malli :as mu]
- [metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
[metabase.util.password :as u.password]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
-(defn check-self-or-superuser
- "Check that `user-id` is *current-user-id*` or that `*current-user*` is a superuser, or throw a 403."
- [user-id]
- {:pre [(integer? user-id)]}
- (api/check-403
- (or
- (= user-id api/*current-user-id*)
- api/*is-superuser?*)))
-
(defn check-not-internal-user
"Check that `user-id` is not the id of the Internal User."
[user-id]
@@ -48,41 +37,6 @@
(api/check (not= user-id config/internal-mb-user-id)
[400 (tru "Not able to modify the internal user")]))
-(defn- fetch-user [& query-criteria]
- (apply t2/select-one (vec (cons :model/User user/admin-or-self-visible-columns)) query-criteria))
-
-(defn- maybe-set-user-permissions-groups! [user-or-id new-groups-or-ids]
- (when (and new-groups-or-ids
- (not (= (user/group-ids user-or-id)
- (set (map u/the-id new-groups-or-ids)))))
- (api/check-superuser)
- (user/set-permissions-groups! user-or-id new-groups-or-ids)))
-
-(mr/def ::user-group-membership
- "Group Membership info of a User.
- In which :is_group_manager is only included if `advanced-permissions` is enabled."
- [:map
- [:id ms/PositiveInt]
- [:is_group_manager
- {:optional true, :description "Only relevant if `advanced-permissions` is enabled. If it is, you should always include this key."}
- :boolean]])
-
-(mu/defn- maybe-set-user-group-memberships!
- [user-or-id
- new-user-group-memberships :- [:maybe [:sequential ::user-group-membership]]
- & [is-superuser?]]
- (when new-user-group-memberships
- ;; if someone passed in both `:is_superuser` and `:group_ids`, make sure the whether the admin group is in group_ids
- ;; agrees with is_superuser -- don't want to have ambiguous behavior
- (when (some? is-superuser?)
- (api/checkp (= is-superuser? (contains? (set (map :id new-user-group-memberships)) (u/the-id (perms/admin-group))))
- "is_superuser" (tru "Value of is_superuser must correspond to presence of Admin group ID in group_ids.")))
- (if-let [f (and (premium-features/enable-advanced-permissions?)
- config/ee-available?
- (requiring-resolve 'metabase-enterprise.advanced-permissions.models.permissions.group-manager/set-user-group-memberships!))]
- (f user-or-id new-user-group-memberships)
- (maybe-set-user-permissions-groups! user-or-id (map :id new-user-group-memberships)))))
-
(defn- updated-user-name [user-before-update changes]
(let [[previous current] (map #(select-keys % [:first_name :last_name]) [user-before-update changes])
updated-names (merge previous current)]
@@ -173,15 +127,10 @@
:else
user/non-admin-or-self-visible-columns))
-(defn filter-clauses-without-paging
- "Given a where clause, return a clause that can be used to count."
- [clauses]
- (dissoc clauses :order-by :limit :offset))
-
(defn- just-me
"Return only the current user as a paginated response."
[]
- {:data [(fetch-user :id api/*current-user-id*)]
+ {:data [(users/fetch-user :id api/*current-user-id*)]
:total 1
:limit (request/limit)
:offset (request/offset)})
@@ -270,13 +219,14 @@
(or api/*is-superuser?*
api/*is-group-manager?*)
(t2/hydrate :group_ids)
- ;; if there is a group_id clause, make sure the list is deduped in case the same user is in multiple groups
+ ;; if there is a group_id clause, make sure the list is deduped in case the same user is in
+ ;; multiple groups
group_id
distinct)
:total (-> (t2/query
(merge {:select [[[:count [:distinct :core_user.id]] :count]]
:from :core_user}
- (filter-clauses-without-paging clauses)))
+ (users/filter-clauses-without-paging clauses)))
first
:count)
:limit (request/limit)
@@ -313,7 +263,7 @@
[:= :tenant_id (:tenant_id @api/*current-user*)])
true (sql.helpers/order-by [:%lower.last_name :asc] [:%lower.first_name :asc]))]
{:data (t2/select (vec (cons :model/User (user-visible-columns))) clauses)
- :total (t2/count :model/User (filter-clauses-without-paging clauses))
+ :total (t2/count :model/User (users/filter-clauses-without-paging clauses))
:limit (request/limit)
:offset (request/offset)}))
(within-group [] (let [user-ids (same-groups-user-ids api/*current-user-id*)
@@ -322,7 +272,7 @@
(seq user-ids) (sql.helpers/where [:in :core_user.id user-ids])
true (sql.helpers/order-by [:%lower.last_name :asc] [:%lower.first_name :asc]))]
{:data (t2/select (vec (cons :model/User (user-visible-columns))) clauses)
- :total (t2/count :model/User (filter-clauses-without-paging clauses))
+ :total (t2/count :model/User (users/filter-clauses-without-paging clauses))
:limit (request/limit)
:offset (request/offset)}))]
(cond
@@ -453,10 +403,10 @@
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
(try
- (check-self-or-superuser id)
+ (users/check-self-or-superuser id)
(catch clojure.lang.ExceptionInfo _e
(perms/check-group-manager)))
- (-> (api/check-404 (fetch-user :id id))
+ (-> (api/check-404 (users/fetch-user :id id))
(t2/hydrate :user_group_memberships)
add-structured-attributes))
@@ -464,34 +414,6 @@
;;; | Creating a new User -- POST /api/user |
;;; +----------------------------------------------------------------------------------------------------------------+
-(defn invite-user
- "Implementation for `POST /`, invites a user to Metabase."
- [{:keys [email user_group_memberships source] :as body}]
- (api/check-superuser)
- (api/checkp (not (t2/exists? :model/User :%lower.email (u/lower-case-en email)))
- "email" (tru "Email address already in use."))
- (api/checkp (not (and (:tenant_id body)
- (not (setting/get :use-tenants))))
- "tenant_id"
- (tru "Cannot create a Tenant User as Tenants are not enabled for this instance."))
- (t2/with-transaction [_conn]
- (let [new-user-id (u/the-id
- (notification/with-skip-sending-notification (boolean (:tenant_id body))
- (user/create-and-invite-user!
- (u/select-keys-when body
- :non-nil [:first_name :last_name :email :password :login_attributes :tenant_id])
- @api/*current-user*
- (= source "setup"))))]
- (maybe-set-user-group-memberships! new-user-id user_group_memberships)
- (when (= source "setup")
- (maybe-set-user-permissions-groups! new-user-id [(perms/all-users-group) (perms/admin-group)]))
- (analytics/track-event! :snowplow/invite
- {:event :invite-sent
- :invited-user-id new-user-id
- :source (or source "admin")})
- (-> (fetch-user :id new-user-id)
- (t2/hydrate :user_group_memberships)))))
-
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
@@ -504,11 +426,15 @@
[:first_name {:optional true} [:maybe ms/NonBlankString]]
[:last_name {:optional true} [:maybe ms/NonBlankString]]
[:email ms/Email]
- [:user_group_memberships {:optional true} [:maybe [:sequential ::user-group-membership]]]
+ [:user_group_memberships {:optional true} [:maybe [:sequential ::users.schema/user-group-membership]]]
[:login_attributes {:optional true} [:maybe users.schema/LoginAttributes]]
- [:source {:optional true, :default "admin"} [:maybe ms/NonBlankString]]
+ [:source {:optional true, :default :admin} [:maybe keyword?]]
[:tenant_id {:optional true} [:maybe ms/PositiveInt]]]]
- (invite-user body))
+ (users/invite-user! (set/rename-keys body {:first_name :first-name
+ :last_name :last-name
+ :user_group_memberships :user-group-memberships
+ :login_attributes :login-attributes
+ :tenant_id :tenant-id})))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Updating a User -- PUT /api/user/:id |
@@ -561,7 +487,7 @@
[:email {:optional true} [:maybe ms/Email]]
[:first_name {:optional true} [:maybe ms/NonBlankString]]
[:last_name {:optional true} [:maybe ms/NonBlankString]]
- [:user_group_memberships {:optional true} [:maybe [:sequential ::user-group-membership]]]
+ [:user_group_memberships {:optional true} [:maybe [:sequential ::users.schema/user-group-membership]]]
[:is_superuser {:optional true} [:maybe :boolean]]
[:is_data_analyst {:optional true} [:maybe :boolean]]
[:is_group_manager {:optional true} [:maybe :boolean]]
@@ -569,12 +495,12 @@
[:locale {:optional true} [:maybe ms/ValidLocale]]
[:tenant_id {:optional true} [:maybe ms/PositiveInt]]]]
(try
- (check-self-or-superuser id)
+ (users/check-self-or-superuser id)
(catch clojure.lang.ExceptionInfo _e
(perms/check-group-manager)))
(check-not-internal-user id)
;; only allow updates if the specified account is active
- (api/let-404 [user-before-update (fetch-user :id id, :is_active true)]
+ (api/let-404 [user-before-update (users/fetch-user :id id, :is_active true)]
;; Google/LDAP non-admin users can't change their email to prevent account hijacking
(when (contains? body :email)
(api/check-403 (valid-email-update? user-before-update email)))
@@ -615,8 +541,8 @@
(if is_data_analyst
(perms/add-user-to-group! id data-analyst-group-id)
(perms/remove-user-from-group! id data-analyst-group-id)))))
- (maybe-set-user-group-memberships! id user_group_memberships is_superuser)))
- (-> (fetch-user :id id)
+ (users/maybe-set-user-group-memberships! id user_group_memberships is_superuser)))
+ (-> (users/fetch-user :id id)
(t2/hydrate :user_group_memberships)))
;;; +----------------------------------------------------------------------------------------------------------------+
@@ -634,7 +560,7 @@
:ldap (when (sso/ldap-enabled) :ldap)
(:sso_source existing-user))})
;; now return the existing user whether they were originally active or not
- (fetch-user :id (u/the-id existing-user)))
+ (users/fetch-user :id (u/the-id existing-user)))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
@@ -674,7 +600,7 @@
{:keys [password old_password]} :- [:map
[:password ms/ValidPassword]]
request]
- (check-self-or-superuser id)
+ (users/check-self-or-superuser id)
(api/let-404 [user (t2/select-one [:model/User :id :last_login :password_salt :password],
:id id,
:type :personal,
@@ -728,7 +654,7 @@
[{:keys [id modal]} :- [:map
[:id ms/PositiveInt]
[:modal [:enum "qbnewb" "datasetnewb"]]]]
- (check-self-or-superuser id)
+ (users/check-self-or-superuser id)
(check-not-internal-user id)
(let [k (or (get {"qbnewb" :is_qbnewb
"datasetnewb" :is_datasetnewb}
diff --git a/src/metabase/warehouses/core.clj b/src/metabase/warehouses/core.clj
index feb8b04491b..fa2767153b8 100644
--- a/src/metabase/warehouses/core.clj
+++ b/src/metabase/warehouses/core.clj
@@ -2,12 +2,19 @@
(:require
[metabase.warehouses.provider-detection]
[metabase.warehouses.settings]
+ [metabase.warehouses.util]
[potemkin :as p]))
-(comment metabase.warehouses.settings/keep-me)
+(comment metabase.warehouses.provider-detection/keep-me
+ metabase.warehouses.settings/keep-me
+ metabase.warehouses.util/keep-me)
(p/import-vars
[metabase.warehouses.provider-detection
detect-provider-from-database]
[metabase.warehouses.settings
- cloud-gateway-ips])
+ cloud-gateway-ips]
+ [metabase.warehouses.util
+ get-database
+ test-connection-details
+ test-database-connection])
diff --git a/src/metabase/warehouses/util.clj b/src/metabase/warehouses/util.clj
new file mode 100644
index 00000000000..76678060f3b
--- /dev/null
+++ b/src/metabase/warehouses/util.clj
@@ -0,0 +1,99 @@
+(ns metabase.warehouses.util
+ (:require
+ [metabase.api.common :as api]
+ [metabase.driver :as driver]
+ [metabase.driver.util :as driver.u]
+ [metabase.util :as u]
+ [metabase.util.i18n :refer [deferred-tru tru]]
+ [metabase.util.log :as log]
+ [metabase.util.malli :as mu]
+ [metabase.util.malli.schema :as ms]
+ [toucan2.core :as t2]))
+
+(mu/defn get-database
+ "Retrieve database respecting `include-editable-data-model?`, `exclude-uneditable-details?` and `include-mirror-databases?`"
+ ([id] (get-database id {}))
+ ([id :- ms/PositiveInt
+ {:keys [include-editable-data-model?
+ exclude-uneditable-details?
+ include-destination-databases?]}
+ :- [:map
+ [:include-editable-data-model? {:optional true :default false} ms/MaybeBooleanValue]
+ [:exclude-uneditable-details? {:optional true :default false} ms/MaybeBooleanValue]
+ [:include-destination-databases? {:optional true :default false} ms/MaybeBooleanValue]]]
+ (let [filter-by-data-access? (not (or include-editable-data-model? exclude-uneditable-details?))
+ database (api/check-404 (if include-destination-databases?
+ (t2/select-one :model/Database :id id)
+ (t2/select-one :model/Database :id id :router_database_id nil)))
+ router-db-id (:router_database_id database)]
+ (cond-> database
+ filter-by-data-access? api/read-check
+ (or exclude-uneditable-details?
+ router-db-id) api/write-check))))
+
+(defn test-database-connection
+ "Try out the connection details for a database and useful error message if connection fails, returns `nil` if
+ connection succeeds."
+ [engine {:keys [host port] :as details}, & {:keys [log-exception]
+ :or {log-exception true}}]
+ {:pre [(some? engine)]}
+ (let [engine (keyword engine)
+ details (assoc details :engine engine)]
+ (try
+ (cond
+ (driver.u/can-connect-with-details? engine details :throw-exceptions)
+ nil
+
+ (and host port (u/host-port-up? host port))
+ {:message (tru "Connection to ''{0}:{1}'' successful, but could not connect to DB."
+ host port)}
+
+ (and host (u/host-up? host))
+ {:message (tru "Connection to host ''{0}'' successful, but port {1} is invalid."
+ host port)
+ :errors {:port (deferred-tru "check your port settings")}}
+
+ host
+ {:message (tru "Host ''{0}'' is not reachable" host)
+ :errors {:host (deferred-tru "check your host settings")}}
+
+ :else
+ {:message (tru "Unable to connect to database.")})
+ (catch Throwable e
+ (when (and log-exception (not (some->> e ex-cause ex-data ::driver/can-connect-message?)))
+ (log/error e "Cannot connect to Database"))
+ (if (-> e ex-data :message)
+ (ex-data e)
+ {:message (ex-message e)})))))
+
+;; TODO - Just make `:ssl` a `feature`
+(defn- supports-ssl?
+ "Does the given `engine` have an `:ssl` setting?"
+ [driver]
+ {:pre [(driver/available? driver)]}
+ (let [driver-props (driver.u/collect-all-props-by-name (driver/connection-properties driver))]
+ (contains? driver-props "ssl")))
+
+(mu/defn test-connection-details :- :map
+ "Try a making a connection to database `engine` with `details`.
+
+ If the `details` has SSL explicitly enabled, go with that and do not accept plaintext connections. If it is disabled,
+ try twice: once with SSL, and a second time without if the first fails. If either attempt is successful, returns
+ the details used to successfully connect. Otherwise returns a map with the connection error message. (This map will
+ also contain the key `:valid` = `false`, which you can use to distinguish an error from valid details.)"
+ [engine :- [:or keyword? string?]
+ details :- :map]
+ (let [;; Try SSL first if SSL is supported and not already enabled
+ ;; If not successful or not applicable, details-with-ssl will be nil
+ details-with-ssl (assoc details :ssl true)
+ details-with-ssl (when (and (supports-ssl? (keyword engine))
+ (not (true? (:ssl details)))
+ (nil? (test-database-connection engine details-with-ssl :log-exception false)))
+ details-with-ssl)]
+ (or
+ ;; Opportunistic SSL
+ details-with-ssl
+ ;; Try with original parameters
+ (some-> (test-database-connection engine details)
+ (assoc :valid false))
+ details)))
diff --git a/src/metabase/warehouses_rest/api.clj b/src/metabase/warehouses_rest/api.clj
index f07bf09ad53..3f8f5acc572 100644
--- a/src/metabase/warehouses_rest/api.clj
+++ b/src/metabase/warehouses_rest/api.clj
@@ -43,6 +43,7 @@
[metabase.util.quick-task :as quick-task]
[metabase.warehouse-schema.models.field :refer [readable-fields-only]]
[metabase.warehouse-schema.table :as schema.table]
+ [metabase.warehouses.core :as warehouses]
[metabase.warehouses.models.database :as database]
[toucan2.core :as t2]))
@@ -422,27 +423,6 @@
; filter hidden fields
(= include "tables.fields") (map #(update % :fields filter-sensitive-fields))))))))
-(mu/defn get-database
- "Retrieve database respecting `include-editable-data-model?`, `exclude-uneditable-details?` and `include-mirror-databases?`"
- ([id] (get-database id {}))
- ([id :- ms/PositiveInt
- {:keys [include-editable-data-model?
- exclude-uneditable-details?
- include-destination-databases?]}
- :- [:map
- [:include-editable-data-model? {:optional true :default false} ms/MaybeBooleanValue]
- [:exclude-uneditable-details? {:optional true :default false} ms/MaybeBooleanValue]
- [:include-destination-databases? {:optional true :default false} ms/MaybeBooleanValue]]]
- (let [filter-by-data-access? (not (or include-editable-data-model? exclude-uneditable-details?))
- database (api/check-404 (if include-destination-databases?
- (t2/select-one :model/Database :id id)
- (t2/select-one :model/Database :id id :router_database_id nil)))
- router-db-id (:router_database_id database)]
- (cond-> database
- filter-by-data-access? api/read-check
- (or exclude-uneditable-details?
- router-db-id) api/write-check))))
-
(mu/defn- check-database-exists
([id] (check-database-exists id {}))
([id :- ms/PositiveInt
@@ -496,10 +476,10 @@
[:include_editable_data_model {:optional true} ms/MaybeBooleanValue]
[:exclude_uneditable_details {:optional true} ms/MaybeBooleanValue]]]
(present-database
- (get-database id {:include include
- :include-editable-data-model? include_editable_data_model
- :exclude-uneditable-details? exclude_uneditable_details
- :include-destination-databases? true})
+ (warehouses/get-database id {:include include
+ :include-editable-data-model? include_editable_data_model
+ :exclude-uneditable-details? exclude_uneditable_details
+ :include-destination-databases? true})
{:include include
:include-editable-data-model? include_editable_data_model
:exclude-uneditable-details? exclude_uneditable_details}))
@@ -588,7 +568,7 @@
(saved-cards-virtual-db-metadata :question :include-tables? true, :include-fields? true))
(defn- db-metadata [id include-hidden? include-editable-data-model? remove_inactive? skip-fields?]
- (let [db (-> (get-database id {:include-editable-data-model? include-editable-data-model?})
+ (let [db (-> (warehouses/get-database id {:include-editable-data-model? include-editable-data-model?})
(t2/hydrate
(if skip-fields?
[:tables :segments :metrics]
@@ -770,7 +750,7 @@
{:keys [prefix substring]} :- [:map
[:prefix {:optional true} [:maybe ms/NonBlankString]]
[:substring {:optional true} [:maybe ms/NonBlankString]]]]
- (api/read-check (get-database id))
+ (api/read-check (warehouses/get-database id))
(when (and (str/blank? prefix) (str/blank? substring))
(throw (ex-info (tru "Must include prefix or search") {:status-code 400})))
(try
@@ -806,7 +786,7 @@
{:keys [query include_dashboard_questions]} :- [:map
[:query ms/NonBlankString]
[:include_dashboard_questions {:optional true} ms/BooleanValue]]]
- (api/read-check (get-database id))
+ (api/read-check (warehouses/get-database id))
(try
(->> (autocomplete-cards id query include_dashboard_questions)
(filter mi/can-read?)
@@ -824,7 +804,7 @@
"Get a list of all `Fields` in `Database`."
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
- (get-database id)
+ (warehouses/get-database id)
(let [fields (filter mi/can-read? (-> (t2/select [:model/Field :id :name :display_name :table_id :base_type :semantic_type]
:table_id [:in (t2/select-fn-set :id :model/Table, :db_id id)]
:visibility_type [:not-in ["sensitive" "retired"]])
@@ -853,80 +833,13 @@
(let [[db-perm-check field-perm-check] (if (Boolean/parseBoolean include_editable_data_model)
[check-db-data-model-perms mi/can-write?]
[api/read-check mi/can-read?])]
- (db-perm-check (get-database id {:include-editable-data-model? true}))
+ (db-perm-check (warehouses/get-database id {:include-editable-data-model? true}))
(sort-by (comp u/lower-case-en :name :table)
(filter field-perm-check (-> (database/pk-fields {:id id})
(t2/hydrate :table))))))
;;; ----------------------------------------------- POST /api/database -----------------------------------------------
-(defn test-database-connection
- "Try out the connection details for a database and useful error message if connection fails, returns `nil` if
- connection succeeds."
- [engine {:keys [host port] :as details}, & {:keys [log-exception]
- :or {log-exception true}}]
- {:pre [(some? engine)]}
- (let [engine (keyword engine)
- details (assoc details :engine engine)]
- (try
- (cond
- (driver.u/can-connect-with-details? engine details :throw-exceptions)
- nil
-
- (and host port (u/host-port-up? host port))
- {:message (tru "Connection to ''{0}:{1}'' successful, but could not connect to DB."
- host port)}
-
- (and host (u/host-up? host))
- {:message (tru "Connection to host ''{0}'' successful, but port {1} is invalid."
- host port)
- :errors {:port (deferred-tru "check your port settings")}}
-
- host
- {:message (tru "Host ''{0}'' is not reachable" host)
- :errors {:host (deferred-tru "check your host settings")}}
-
- :else
- {:message (tru "Unable to connect to database.")})
- (catch Throwable e
- (when (and log-exception (not (some->> e ex-cause ex-data ::driver/can-connect-message?)))
- (log/error e "Cannot connect to Database"))
- (if (-> e ex-data :message)
- (ex-data e)
- {:message (.getMessage e)})))))
-
-;; TODO - Just make `:ssl` a `feature`
-(defn- supports-ssl?
- "Does the given `engine` have an `:ssl` setting?"
- [driver]
- {:pre [(driver/available? driver)]}
- (let [driver-props (driver.u/collect-all-props-by-name (driver/connection-properties driver))]
- (contains? driver-props "ssl")))
-
-(mu/defn test-connection-details :- :map
- "Try a making a connection to database `engine` with `details`.
-
- If the `details` has SSL explicitly enabled, go with that and do not accept plaintext connections. If it is disabled,
- try twice: once with SSL, and a second time without if the first fails. If either attempt is successful, returns
- the details used to successfully connect. Otherwise returns a map with the connection error message. (This map will
- also contain the key `:valid` = `false`, which you can use to distinguish an error from valid details.)"
- [engine :- DBEngineString
- details :- :map]
- (let [;; Try SSL first if SSL is supported and not already enabled
- ;; If not successful or not applicable, details-with-ssl will be nil
- details-with-ssl (assoc details :ssl true)
- details-with-ssl (when (and (supports-ssl? (keyword engine))
- (not (true? (:ssl details)))
- (nil? (test-database-connection engine details-with-ssl :log-exception false)))
- details-with-ssl)]
- (or
- ;; Opportunistic SSL
- details-with-ssl
- ;; Try with original parameters
- (some-> (test-database-connection engine details)
- (assoc :valid false))
- details)))
-
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
@@ -952,7 +865,7 @@
(api/check (premium-features/enable-cache-granular-controls?)
[402 (tru (str "The cache TTL database setting is only enabled if you have a premium token with the "
"cache granular controls feature."))]))
- (let [details-or-error (test-connection-details engine details)
+ (let [details-or-error (warehouses/test-connection-details engine details)
valid? (not= (:valid details-or-error) false)]
(if valid?
;; no error, proceed with creation. If record is inserted successfully, publish a `:database-create` event.
@@ -1002,7 +915,7 @@
[:engine DBEngineString]
[:details :map]]]]]
(api/check-superuser)
- (let [details-or-error (test-connection-details engine details)]
+ (let [details-or-error (warehouses/test-connection-details engine details)]
;; details that come back without a `:valid` key at all are... valid!
(update details-or-error :valid (comp not false?))))
@@ -1069,8 +982,8 @@
details-changed? (some-> details (not= (:details existing-database)))
engine-changed? (some-> engine keyword (not= (:engine existing-database)))
conn-error (when (or details-changed? engine-changed?)
- (test-database-connection (or engine (:engine existing-database))
- (or details (:details existing-database))))
+ (warehouses/test-database-connection (or engine (:engine existing-database))
+ (or details (:details existing-database))))
full-sync? (some-> is_full_sync boolean)
on-demand? (boolean is_on_demand)]
(if conn-error
@@ -1125,8 +1038,8 @@
(catch Exception e
(throw (ex-info (ex-message e) (assoc (ex-data e) :status-code 400) e))))))
(t2/update! :model/Database id updates)
- ;; unlike the other fields, folks might want to nil out cache_ttl. it should also only be settable on EE
- ;; with the advanced-config feature enabled.
+ ;; unlike the other fields, folks might want to nil out cache_ttl. it should also only be settable on EE
+ ;; with the advanced-config feature enabled.
(when (premium-features/enable-cache-granular-controls?)
(t2/update! :model/Database id {:cache_ttl cache_ttl}))
@@ -1180,7 +1093,7 @@
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
;; just wrap this in a future so it happens async
- (let [db (api/write-check (get-database id {:exclude-uneditable-details? true}))]
+ (let [db (api/write-check (warehouses/get-database id {:exclude-uneditable-details? true}))]
(events/publish-event! :event/database-manual-sync {:object db :user-id api/*current-user-id*})
(if-let [ex (try
;; it's okay to allow testing H2 connections during sync. We only want to disallow you from testing them for the
@@ -1213,7 +1126,7 @@
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
;; manual full sync needs to be async, but this is a simple update of `Database`
- (let [db (api/write-check (get-database id {:exclude-uneditable-details? true}))
+ (let [db (api/write-check (warehouses/get-database id {:exclude-uneditable-details? true}))
tables (map api/write-check (:tables (first (add-tables [db]))))]
(sync-util/set-initial-database-sync-complete! db)
;; avoid n+1
@@ -1244,7 +1157,7 @@
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
;; just wrap this is a future so it happens async
- (let [db (api/write-check (get-database id {:exclude-uneditable-details? true}))]
+ (let [db (api/write-check (warehouses/get-database id {:exclude-uneditable-details? true}))]
(events/publish-event! :event/database-manual-scan {:object db :user-id api/*current-user-id*})
(analytics/track-event! :snowplow/simple_event {:event "database_manual_scan" :target_id id})
;; Grant full permissions so that permission checks pass during sync. If a user has DB detail perms
@@ -1280,7 +1193,7 @@
"Discards all saved field values for this `Database`."
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
- (let [db (api/write-check (get-database id {:exclude-uneditable-details? true}))]
+ (let [db (api/write-check (warehouses/get-database id {:exclude-uneditable-details? true}))]
(events/publish-event! :event/database-discard-field-values {:object db :user-id api/*current-user-id*})
(analytics/track-event! :snowplow/simple_event {:event "database_discard_field_values" :target_id id})
(delete-all-field-values-for-database! db))
@@ -1336,7 +1249,7 @@
"Returns a list of all syncable schemas found for the database `id`."
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
- (let [db (get-database id)]
+ (let [db (warehouses/get-database id)]
(api/check-403 (or (:is_attached_dwh db)
(perms/has-db-transforms-permission? api/*current-user-id* (:id db))
(and (mi/can-write? db)
@@ -1383,7 +1296,7 @@
allowed-schemas (set (map :schema filtered-tables))]
(filter #(contains? allowed-schemas %) schemas))
schemas))]
- (get-database id {:include-editable-data-model? include-editable-data-model?})
+ (warehouses/get-database id {:include-editable-data-model? include-editable-data-model?})
(->> (t2/select-fn-set :schema :model/Table
:db_id id :active true
(merge
@@ -1577,7 +1490,7 @@
(let [{:keys [engine details]} (t2/select-one :model/Database :id id)]
;; we only want to prevent creating new H2 databases. Testing the existing database is fine.
(binding [driver.settings/*allow-testing-h2-connections* true]
- (if-let [err-map (test-database-connection engine details)]
+ (if-let [err-map (warehouses/test-database-connection engine details)]
(merge err-map {:status "error"})
{:status "ok"}))))
@@ -1646,5 +1559,5 @@
"Get all database-local settings and their availability for the given database."
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
- (let [database (api/read-check (get-database id))]
+ (let [database (api/read-check (warehouses/get-database id))]
{:settings (database-local-settings database)}))
diff --git a/test/metabase/auth_provider/impl_test.clj b/test/metabase/auth_provider/impl_test.clj
index ad720c390c5..a9533799a35 100644
--- a/test/metabase/auth_provider/impl_test.clj
+++ b/test/metabase/auth_provider/impl_test.clj
@@ -7,7 +7,7 @@
[metabase.test.data.interface :as tx]
[metabase.test.http-client :as client]
[metabase.util.json :as json]
- [metabase.warehouses-rest.api :as api.database]))
+ [metabase.warehouses.core :as warehouses]))
(deftest auth-integration-test
(mt/test-drivers #{:postgres :mysql}
@@ -24,11 +24,11 @@
db
(is (auth-details original-details (:details (mt/db))))
(testing "Connection tests"
- (is (some? (api.database/test-database-connection (:engine db) (:details db)))))
+ (is (some? (warehouses/test-database-connection (:engine db) (:details db)))))
(testing "With feature"
(mt/with-premium-features #{:database-auth-providers}
(testing "Connection tests"
- (is (nil? (api.database/test-database-connection (:engine db) (:details db)))))
+ (is (nil? (warehouses/test-database-connection (:engine db) (:details db)))))
(testing "Syncing does not blow up"
(sync/sync-database! (mt/db)))
(testing "Querying"
diff --git a/test/metabase/core/modules_test.clj b/test/metabase/core/modules_test.clj
index 851afc36908..3848da9d948 100644
--- a/test/metabase/core/modules_test.clj
+++ b/test/metabase/core/modules_test.clj
@@ -168,3 +168,18 @@
(is (empty? missing)))
(testing (format "Remove %s from %s" (pr-str extraneous) (pr-str ks))
(is (empty? extraneous)))))))
+
+(defn- rest-module? [module]
+ (str/ends-with? module "-rest"))
+
+(deftest ^:parallel do-not-use-rest-modules-in-other-modules-test
+ (doseq [[module {:keys [uses], :as _config}] (dev.deps-graph/kondo-config)
+ :when (not (rest-module? module))
+ used-module (when (set? uses)
+ uses)]
+ (is (not (rest-module? used-module))
+ (format "Do not use -rest modules (%s) in non-rest modules (%s) -- move things from %s to %s if needed"
+ used-module
+ module
+ used-module
+ (symbol (str/replace used-module #"-rest$" ""))))))
diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.clj
index e26cd426f55..4fb2308652b 100644
--- a/test/metabase/warehouses_rest/api_test.clj
+++ b/test/metabase/warehouses_rest/api_test.clj
@@ -40,6 +40,8 @@
[metabase.util.random :as u.random]
[metabase.warehouse-schema.table :as schema.table]
[metabase.warehouses-rest.api :as api.database]
+ [metabase.warehouses.core :as warehouses]
+ [metabase.warehouses.util :as warehouses.util]
[ring.util.codec :as codec]
[toucan2.core :as t2])
(:import
@@ -1607,7 +1609,7 @@
(defn- test-connection-details! [engine details]
(with-redefs [driver.settings/*allow-testing-h2-connections* true]
- (#'api.database/test-connection-details engine details)))
+ (warehouses/test-connection-details engine details)))
(deftest validate-database-test
(testing "POST /api/database/validate"
@@ -1651,12 +1653,12 @@
(let [call-count (atom 0)
ssl-values (atom [])
valid? (atom false)]
- (with-redefs [api.database/test-database-connection (fn [_ details & _]
- (swap! call-count inc)
- (swap! ssl-values conj (:ssl details))
- (if @valid? nil {:valid false}))]
+ (with-redefs [warehouses.util/test-database-connection (fn [_ details & _]
+ (swap! call-count inc)
+ (swap! ssl-values conj (:ssl details))
+ (if @valid? nil {:valid false}))]
(testing "with SSL enabled, do not allow non-SSL connections"
- (#'api.database/test-connection-details "postgres" {:ssl true})
+ (#'warehouses.util/test-connection-details "postgres" {:ssl true})
(is (= 1 @call-count))
(is (= [true] @ssl-values)))
@@ -1664,7 +1666,7 @@
(reset! ssl-values [])
(testing "with SSL disabled, try twice (once with, once without SSL)"
- (#'api.database/test-connection-details "postgres" {:ssl false})
+ (#'warehouses.util/test-connection-details "postgres" {:ssl false})
(is (= 2 @call-count))
(is (= [true false] @ssl-values)))
@@ -1672,7 +1674,7 @@
(reset! ssl-values [])
(testing "with SSL unspecified, try twice (once with, once without SSL)"
- (#'api.database/test-connection-details "postgres" {})
+ (#'warehouses.util/test-connection-details "postgres" {})
(is (= 2 @call-count))
(is (= [true nil] @ssl-values)))
@@ -1682,7 +1684,7 @@
(testing "with SSL disabled, but working try once (since SSL work we don't try without SSL)"
(is (= {:ssl true}
- (#'api.database/test-connection-details "postgres" {:ssl false})))
+ (#'warehouses.util/test-connection-details "postgres" {:ssl false})))
(is (= 1 @call-count))
(is (= [true] @ssl-values)))))))