Skip to content

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

BE modularization decoupling Halloween edition (#65236)

Cam Saul734949f26768
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 5f024c623f3..ccc6560903d 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -381,7 +381,7 @@
core
{:team "UX West"
- :api #{metabase.core.initialization-status}
+ :api #{}
:uses :any}
dashboards
@@ -585,6 +585,11 @@
task
util}}
+ initialization-status
+ {:team "UX West"
+ :api #{metabase.initialization-status.core}
+ :uses #{}}
+
internal-stats
{:team "UX West"
:api #{metabase.internal-stats.core}
@@ -796,9 +801,9 @@
permissions
{:team "UX West"
- :api #{metabase.permissions.api
- metabase.permissions.core
- metabase.permissions.init}
+ :api #{metabase.permissions.core
+ metabase.permissions.init
+ metabase.permissions.schema}
:uses #{api
app-db
audit-app
@@ -809,11 +814,21 @@
models
premium-features
remote-sync
- request
settings
util
enterprise/advanced-permissions}}
+ permissions-rest
+ {:team "UX West"
+ :api #{metabase.permissions-rest.api}
+ :uses #{api
+ audit-app
+ events
+ permissions
+ premium-features
+ request
+ util}}
+
pivot
{:team "UX West"
:api #{metabase.pivot.core}
@@ -908,7 +923,7 @@
metabase.queries.core
metabase.queries.init
metabase.queries.models.query ; TODO FIXME
- metabase.queries.schema} ; TODO FIXME
+ metabase.queries.schema} ; TODO FIXME
:uses #{analytics
analyze
api
@@ -1037,7 +1052,7 @@
:uses #{api
config
embedding
- permissions ; FIXME
+ permissions ; FIXME
session
settings
users
@@ -1133,9 +1148,9 @@
app-db
appearance
config
- core
driver
embedding
+ initialization-status
lib-be
premium-features
query-processor
@@ -1152,7 +1167,7 @@
metabase.session.core
metabase.session.init
metabase.session.models.session ; TODO -- shouldn't be exposed as an API namespace
- metabase.session.settings} ; TODO -- shouldn't be exposed as an API namespace
+ metabase.session.settings} ; TODO -- shouldn't be exposed as an API namespace
:uses #{api
app-db
channel
@@ -1169,19 +1184,25 @@
settings
{:team "DevEx"
- :api #{metabase.settings.api
- metabase.settings.core
+ :api #{metabase.settings.core
metabase.settings.init}
:uses #{api
app-db
config
events
models
- permissions
premium-features
util
enterprise/advanced-permissions}}
+ settings-rest
+ {:team "DevEx"
+ :api #{metabase.settings-rest.api}
+ :uses #{api
+ permissions
+ settings
+ util}}
+
setup
{:team "UX West"
:api #{metabase.setup.api
@@ -1587,8 +1608,14 @@
enterprise/api
{:team "UX West"
- :api #{metabase-enterprise.api.routes
- metabase-enterprise.api.routes.common}
+ :api #{metabase-enterprise.api.core}
+ :uses #{api
+ premium-features
+ util}}
+
+ enterprise/api-routes
+ {:team "UX West"
+ :api #{metabase-enterprise.api-routes.routes}
:uses :any}
enterprise/audit-app
diff --git a/dev/src/dev/deps_graph.clj b/dev/src/dev/deps_graph.clj
index c0a557f1e5e..58e8e1c96d9 100644
--- a/dev/src/dev/deps_graph.clj
+++ b/dev/src/dev/deps_graph.clj
@@ -2,6 +2,7 @@
(:require
[clojure.edn :as edn]
[clojure.java.io :as io]
+ [clojure.set :as set]
[clojure.string :as str]
[clojure.tools.namespace.file :as ns.file]
[clojure.tools.namespace.find :as ns.find]
@@ -299,7 +300,7 @@
(into (sorted-set) (map :depends-on-namespace) (external-usages deps module-symb))))
(defn module-dependencies
- "Build a graph of module => set of modules it refers to."
+ "Build a graph of module => set of modules it directly depends on."
([deps]
(letfn [(reduce-module-deps [module-deps module deps]
(reduce
@@ -432,12 +433,106 @@
ddiff/minimize)))
(defn print-kondo-config-diff
- "Print the diff between how the config would look if regenerated with [[generate-config]] versus how it looks in
+ "Print the diff between how the config would look if regenerated with [[generate-config]] versus how it looks in
reality ([[kondo-config]]). Use this to suggest updates to make to the config file."
- []
- (ddiff/pretty-print (kondo-config-diff)))
+ []
+ (ddiff/pretty-print (kondo-config-diff)))
(comment
+ (external-usages 'core)
+
(module-dependencies (dependencies) 'lib)
(module-usages-of-other-module 'lib 'models))
+
+(defn all-module-deps-paths
+ "Build a map of
+
+ dep => path-to-dep
+
+ for each dependency (direct or indirect) of a module, e.g.
+
+ (all-module-deps-paths 'settings)
+ ;; =>
+ {api [] ; settings depends on api directly
+ api-keys [permissions collections]} ; settings depends on permissions which depends on collections which depends on api-keys"
+ ([module]
+ (all-module-deps-paths (dependencies) module))
+ ([deps module]
+ (all-module-deps-paths deps module (sorted-map) (atom #{}) []))
+ ([deps module acc already-seen path]
+ (let [module-deps (module-dependencies deps module)
+ new-deps (remove @already-seen module-deps)
+ acc (into acc
+ (map (fn [dep]
+ [dep path]))
+ new-deps)]
+ (swap! already-seen into new-deps)
+ (reduce
+ (fn [acc new-dep]
+ (all-module-deps-paths deps new-dep acc already-seen (conj path new-dep)))
+ acc
+ new-deps))))
+
+(defn module-dependencies-by-namespace
+ "Return a map of external dependency of `module` => set of namespaces in `module` that use it:
+
+ (module-dependencies-by-namespace 'permissions)
+ ;; =>
+ {api #{metabase.permissions.api
+ metabase.permissions.models.collection.graph
+ ...}
+ app-db #{metabase.permissions.api
+ metabase.permissions.models.permissions-group
+ ...}
+ ...}"
+ ([module]
+ (module-dependencies-by-namespace (dependencies) module))
+
+ ([deps module]
+ (into (sorted-map)
+ (map (fn [dep]
+ [dep (into (sorted-set) (keys (module-usages-of-other-module deps module dep)))]))
+ (module-dependencies deps module))))
+
+(defn dependencies-eliminated-by-removing-namespaces
+ "Return the set of `module` dependencies that we could eliminate if we were to split namespace(s) off into a separate
+ module.
+
+ ;; if we move `metabase.permissions.api` into a separate module then `permissions` no longer has a dependency on
+ ;; `request`
+ (dependencies-eliminated-by-removing-namespaces 'permissions 'metabase.permissions.api)
+ ;; =>
+ #{request}"
+ [module namespace-symb-or-set]
+ (let [deps (dependencies)
+ namespace-symbs (if (symbol? namespace-symb-or-set)
+ #{namespace-symb-or-set}
+ namespace-symb-or-set)
+ dep->namespaces (module-dependencies-by-namespace deps module)]
+ (into (sorted-set)
+ (keep (fn [[dep namespaces]]
+ (when (empty? (set/difference namespaces namespace-symbs))
+ dep)))
+ dep->namespaces)))
+
+(defn leaf-modules
+ "Modules that are leaf nodes in the module dependency tree -- nothing else depends on them."
+ []
+ (let [deps (dependencies)]
+ (into (sorted-set)
+ (comp (map :module)
+ (keep (fn [module]
+ (when (zero? (count (external-usages deps module)))
+ module))))
+ deps)))
+
+(defn non-dependencies
+ "Modules that `module` does not depend on, either directly or indirectly -- changes to any of these modules should not
+ affect `module`."
+ [module]
+ (let [deps (dependencies)
+ all-modules (into (sorted-set) (map :module) deps)]
+ (set/difference
+ all-modules
+ (set (keys (all-module-deps-paths deps module))))))
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/application_permissions.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/application_permissions.clj
index 9ec057f7e34..4c6f77c5633 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/application_permissions.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/models/permissions/application_permissions.clj
@@ -1,6 +1,6 @@
(ns metabase-enterprise.advanced-permissions.models.permissions.application-permissions
- "Code for generating and updating the Application Permission graph. See [[metabase.permissions.models.permissions]] for more
- details and for the code for generating and updating the *data* permissions graph."
+ "Code for generating and updating the Application Permission graph. See [[metabase.permissions.models.permissions]]
+ for more details and for the code for generating and updating the *data* permissions graph."
(:require
[clojure.data :as data]
[metabase.permissions.core :as perms]
@@ -71,7 +71,7 @@
(mu/defn update-graph!
"Update the application Permissions graph.
- This works just like [[metabase.permissions.models.data-permissions.graph/update-data-perms-graph!]], but for
+ This works just like [[metabase.permissions-rest.data-permissions.graph/update-data-perms-graph!]], but for
Application permissions; refer to that function's extensive documentation to get a sense for how this works."
([new-graph :- ApplicationPermissionsGraph]
(update-graph! new-graph false))
diff --git a/enterprise/backend/src/metabase_enterprise/api/core.clj b/enterprise/backend/src/metabase_enterprise/api/core.clj
new file mode 100644
index 00000000000..f6942b928ca
--- /dev/null
+++ b/enterprise/backend/src/metabase_enterprise/api/core.clj
@@ -0,0 +1,12 @@
+(ns metabase-enterprise.api.core
+ (:require
+ [metabase-enterprise.api.routes.common]
+ [potemkin :as p]))
+
+(comment metabase-enterprise.api.routes.common/keep-me)
+
+#_{:clj-kondo/ignore [:deprecated-var]}
+(p/import-vars
+ [metabase-enterprise.api.routes.common
+ +require-premium-feature
+ +when-premium-feature])
diff --git a/enterprise/backend/src/metabase_enterprise/api/routes/common.clj b/enterprise/backend/src/metabase_enterprise/api/routes/common.clj
index 1a3fd2902dc..56c9f960343 100644
--- a/enterprise/backend/src/metabase_enterprise/api/routes/common.clj
+++ b/enterprise/backend/src/metabase_enterprise/api/routes/common.clj
@@ -36,8 +36,8 @@
question, rather than a generic 'endpoint does not exist' 404 error.
In general, it's probably better NOT to swap out API endpoints, because it's not obvious at all that it happened,
- and it makes it hard for us to nicely structure our contexts in [[metabase-enterprise.api.routes/routes]]. So only
- do this if there's absolutely no other way (which is probably not the case)."
+ and it makes it hard for us to nicely structure our contexts in [[metabase-enterprise.api-routes.routes/routes]]. So
+ only do this if there's absolutely no other way (which is probably not the case)."
[feature handler]
(open-api/handler-with-open-api-spec
(fn [request respond raise]
diff --git a/enterprise/backend/src/metabase_enterprise/api/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
similarity index 97%
rename from enterprise/backend/src/metabase_enterprise/api/routes.clj
rename to enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
index 1f66db3a0f9..5cfafb93c38 100644
--- a/enterprise/backend/src/metabase_enterprise/api/routes.clj
+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj
@@ -1,4 +1,4 @@
-(ns metabase-enterprise.api.routes
+(ns metabase-enterprise.api-routes.routes
"API routes that are only available when running Metabase® Enterprise Edition™. Even tho these routes are available,
not all routes might work unless we have a valid premium features token to enable those features.
@@ -11,7 +11,7 @@
[metabase-enterprise.ai-entity-analysis.api]
[metabase-enterprise.ai-sql-fixer.api]
[metabase-enterprise.ai-sql-generation.api]
- [metabase-enterprise.api.routes.common :as ee.api.common]
+ [metabase-enterprise.api.core :as ee.api]
[metabase-enterprise.audit-app.api.routes]
[metabase-enterprise.billing.api.routes]
[metabase-enterprise.cloud-add-ons.api]
@@ -78,7 +78,7 @@
(let [handler (cond-> handler
(simple-symbol? handler) api.macros/ns-handler)]
(->> handler
- (ee.api.common/+require-premium-feature required-feature (required-feature->message required-feature)))))
+ (ee.api/+require-premium-feature required-feature (required-feature->message required-feature)))))
(def ^:private naughty-routes-map
"The following routes are NAUGHTY and do not follow the naming convention (i.e., they do not start with
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/api/routes.clj b/enterprise/backend/src/metabase_enterprise/audit_app/api/routes.clj
index 9327f453fd3..24136e3b568 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/api/routes.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/api/routes.clj
@@ -1,6 +1,7 @@
(ns metabase-enterprise.audit-app.api.routes
"API endpoints that are only enabled if we have a premium token with the `:audit-app` feature. These live under
- `/api/ee/audit-app/`. Feature-flagging for these routes happens in [[metabase-enterprise.api.routes/routes]]."
+ `/api/ee/audit-app/`. Feature-flagging for these routes happens
+ in [[metabase-enterprise.api-routes.routes/routes]]."
(:require
[metabase-enterprise.audit-app.api.user]
[metabase.api.macros :as api.macros]
diff --git a/enterprise/backend/src/metabase_enterprise/content_translation/routes.clj b/enterprise/backend/src/metabase_enterprise/content_translation/routes.clj
index 347634ccc81..038d91eac24 100644
--- a/enterprise/backend/src/metabase_enterprise/content_translation/routes.clj
+++ b/enterprise/backend/src/metabase_enterprise/content_translation/routes.clj
@@ -3,7 +3,7 @@
(:require
[clojure.data.csv :as csv]
[clojure.string :as str]
- [metabase-enterprise.api.routes.common :as ee.api.common]
+ [metabase-enterprise.api.core :as ee.api]
[metabase-enterprise.content-translation.dictionary :as dictionary]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
@@ -81,7 +81,7 @@
(throw (ex-info (str (tru "Locale is required.")) {:status-code 400}))))
(defn- +require-content-translation [handler]
- (ee.api.common/+require-premium-feature :content-translation (deferred-tru "Content translation") handler))
+ (ee.api/+require-premium-feature :content-translation (deferred-tru "Content translation") handler))
(def ^{:arglists '([request respond raise])} routes
"`/api/ee/content-translation` routes."
diff --git a/enterprise/backend/src/metabase_enterprise/content_verification/api/routes.clj b/enterprise/backend/src/metabase_enterprise/content_verification/api/routes.clj
index 4aa5d70e9b8..cc76bdf752b 100644
--- a/enterprise/backend/src/metabase_enterprise/content_verification/api/routes.clj
+++ b/enterprise/backend/src/metabase_enterprise/content_verification/api/routes.clj
@@ -1,6 +1,6 @@
(ns metabase-enterprise.content-verification.api.routes
(:require
- [metabase-enterprise.api.routes.common :as ee.api.common]
+ [metabase-enterprise.api.core :as ee.api]
[metabase-enterprise.content-verification.api.moderation-review]
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
@@ -9,7 +9,7 @@
(comment metabase-enterprise.content-verification.api.moderation-review/keep-me)
(defn- +require-content-verification [handler]
- (ee.api.common/+require-premium-feature :content-verification (deferred-tru "Content verification") handler))
+ (ee.api/+require-premium-feature :content-verification (deferred-tru "Content verification") handler))
(def ^{:arglists '([request respond raise])} routes
"/api/moderation-review routes. Only available if we have a premium token with the `:content-verification` feature."
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/api/routes.clj b/enterprise/backend/src/metabase_enterprise/sandbox/api/routes.clj
index 732dccace81..26433a3d998 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/api/routes.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/api/routes.clj
@@ -1,7 +1,7 @@
(ns metabase-enterprise.sandbox.api.routes
"API routes that are only enabled if we have a premium token with the `:sandboxes` feature."
(:require
- [metabase-enterprise.api.routes.common :as ee.api.common]
+ [metabase-enterprise.api.core :as ee.api]
[metabase-enterprise.sandbox.api.gtap]
[metabase-enterprise.sandbox.api.table]
[metabase-enterprise.sandbox.api.user]
@@ -23,7 +23,7 @@
change this to `/sandboxes` or something like that."
(->> (handlers/route-map-handler sandbox-route-map)
+auth
- (ee.api.common/+require-premium-feature :sandboxes (deferred-tru "Sandboxes"))))
+ (ee.api/+require-premium-feature :sandboxes (deferred-tru "Sandboxes"))))
(def ^{:arglists '([request respond raise])} sandbox-table-routes
"/api/table overrides for sandboxing.
@@ -33,4 +33,4 @@
(->> (api.macros/ns-handler 'metabase-enterprise.sandbox.api.table)
+auth
#_{:clj-kondo/ignore [:deprecated-var]}
- (ee.api.common/+when-premium-feature :sandboxes)))
+ (ee.api/+when-premium-feature :sandboxes)))
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/models/permissions/block_permissions_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/models/permissions/block_permissions_test.clj
index ea16d36a1b1..d445613a7ee 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/models/permissions/block_permissions_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/models/permissions/block_permissions_test.clj
@@ -6,9 +6,9 @@
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.models.interface :as mi]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.core :as perms]
[metabase.permissions.models.data-permissions :as data-perms]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.query-processor :as qp]
[metabase.query-processor.middleware.permissions :as qp.perms]
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj
index 38701d28027..547da8625f0 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/query_processor/middleware/permissions_test.clj
@@ -9,8 +9,8 @@
[metabase.lib.schema :as lib.schema]
[metabase.lib.test-metadata :as meta]
[metabase.lib.util.match :as lib.util.match]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.core :as perms]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
[metabase.permissions.test-util :as perms.test-util]
[metabase.query-processor.api :as api.dataset]
[metabase.query-processor.reducible :as qp.reducible]
diff --git a/enterprise/backend/test/metabase_enterprise/api/session_test.clj b/enterprise/backend/test/metabase_enterprise/api/session_test.clj
index 69cb0338409..9f044a069f6 100644
--- a/enterprise/backend/test/metabase_enterprise/api/session_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/api/session_test.clj
@@ -1,4 +1,5 @@
(ns metabase-enterprise.api.session-test
+ ;; TODO (Cam 10/30/25) -- Move this somewhere better
(:require
[clojure.test :refer :all]
[metabase.test :as mt]
diff --git a/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj b/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj
index 342b24069fa..d4c2e90097b 100644
--- a/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj
@@ -11,7 +11,7 @@
[metabase.audit-app.core :as audit]
[metabase.core.core :as mbc]
[metabase.models.serialization :as serdes]
- [metabase.permissions.models.data-permissions :as data-perms]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.plugins.core :as plugins]
[metabase.sync.task.sync-databases :as task.sync-databases]
@@ -65,7 +65,7 @@
:perms/manage-table-metadata :no
:perms/view-data :unrestricted
:perms/create-queries :no}
- (-> (data-perms/data-permissions-graph :db-id audit/audit-db-id :audit? true)
+ (-> (data-perms.graph/data-permissions-graph :db-id audit/audit-db-id :audit? true)
(get-in [(u/the-id (perms-group/all-users)) audit/audit-db-id])))))
(testing "Audit DB does not have scheduled syncs"
diff --git a/enterprise/backend/test/metabase_enterprise/database_replication/api_test.clj b/enterprise/backend/test/metabase_enterprise/database_replication/api_test.clj
index 41e27e6e5ca..8034707c359 100644
--- a/enterprise/backend/test/metabase_enterprise/database_replication/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/database_replication/api_test.clj
@@ -11,7 +11,7 @@
(set! *warn-on-reflection* true)
(deftest required-features-test
- ;; checked in metabase-enterprise.api.routes/ee-routes-map
+ ;; checked in [[metabase-enterprise.api-routes.routes/ee-routes-map]]
(let [required-features [[:attached-dwh "Attached DWH"]
[:etl-connections "ETL Connections"]
[:etl-connections-pg "ETL Connections PG replication"]]]
diff --git a/enterprise/backend/test/metabase_enterprise/api/gsheets_test.clj b/enterprise/backend/test/metabase_enterprise/gsheets/api_test.clj
similarity index 99%
rename from enterprise/backend/test/metabase_enterprise/api/gsheets_test.clj
rename to enterprise/backend/test/metabase_enterprise/gsheets/api_test.clj
index 6ac611855ef..5ae0bea3cae 100644
--- a/enterprise/backend/test/metabase_enterprise/api/gsheets_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/gsheets/api_test.clj
@@ -1,4 +1,4 @@
-(ns metabase-enterprise.api.gsheets-test
+(ns metabase-enterprise.gsheets.api-test
(:require
[clojure.java.io :as io]
[clojure.test :refer [deftest is testing]]
diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/api_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/api_test.clj
index 97295d8bf00..92ba4b28f3b 100644
--- a/enterprise/backend/test/metabase_enterprise/impersonation/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/impersonation/api_test.clj
@@ -2,7 +2,7 @@
"Tests for creating and updating Connection Impersonation configs via the permisisons API"
(:require
[clojure.test :refer :all]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.test :as mt]
[metabase.util :as u]
[toucan2.core :as t2]))
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/api/card_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/api/card_test.clj
index 0e656169ede..e88fbc14a82 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/api/card_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/api/card_test.clj
@@ -4,6 +4,7 @@
[metabase-enterprise.test :as met]
[metabase.permissions.core :as perms]
[metabase.permissions.models.data-permissions :as data-perms]
+ [metabase.permissions.schema :as permissions.schema]
[metabase.queries.api.card-test :as api.card-test]
[metabase.query-processor :as qp]
[metabase.test :as mt]
@@ -121,8 +122,8 @@
[:blocked :query-builder-and-native]
[:blocked :query-builder]]]
(is (= (count cases)
- (- (* (-> data-perms/Permissions :perms/view-data :values count)
- (-> data-perms/Permissions :perms/create-queries :values count))
+ (- (* (-> permissions.schema/data-permissions :perms/view-data :values count)
+ (-> permissions.schema/data-permissions :perms/create-queries :values count))
(count invalid-cases)))
"Please test these permissions settings behaviors exhaustively: if you add perms, add the tests for them.")
(mt/with-no-data-perms-for-all-users!
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/api/gtap_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/api/gtap_test.clj
index 02ea65152df..eb86588f4cf 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/api/gtap_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/api/gtap_test.clj
@@ -3,7 +3,7 @@
[clojure.test :refer :all]
[metabase.api.response :as api.response]
[metabase.driver.util :as driver.util]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.premium-features.core :as premium-features]
[metabase.test :as mt]
[metabase.test.http-client :as client]
diff --git a/src/metabase/api_routes/routes.clj b/src/metabase/api_routes/routes.clj
index 5dba7c47712..734a8598e1c 100644
--- a/src/metabase/api_routes/routes.clj
+++ b/src/metabase/api_routes/routes.clj
@@ -29,7 +29,7 @@
[metabase.model-persistence.api]
[metabase.native-query-snippets.api]
[metabase.notification.api]
- [metabase.permissions.api]
+ [metabase.permissions-rest.api]
[metabase.premium-features.api]
[metabase.product-feedback.api]
[metabase.public-sharing.api]
@@ -40,7 +40,7 @@
[metabase.search.api]
[metabase.segments.api]
[metabase.session.api]
- [metabase.settings.api]
+ [metabase.settings-rest.api]
[metabase.setup.api]
[metabase.sso.api]
[metabase.sync.api]
@@ -76,13 +76,13 @@
metabase.login-history.api/keep-me
metabase.model-persistence.api/keep-me
metabase.native-query-snippets.api/keep-me
- metabase.permissions.api/keep-me
+ metabase.permissions-rest.api/keep-me
metabase.product-feedback.api/keep-me
metabase.public-sharing.api/keep-me
metabase.query-processor.api/keep-me
metabase.revisions.api/keep-me
metabase.segments.api/keep-me
- metabase.settings.api/keep-me
+ metabase.settings-rest.api/keep-me
metabase.setup.api/keep-me
metabase.task-history.api/keep-me
metabase.testing-api.api/keep-me
@@ -160,7 +160,7 @@
"/native-query-snippet" (+auth 'metabase.native-query-snippets.api)
"/notification" metabase.notification.api/notification-routes
"/notify" (+static-apikey metabase.sync.api/notify-routes)
- "/permissions" (+auth 'metabase.permissions.api)
+ "/permissions" (+auth 'metabase.permissions-rest.api)
"/persist" (+auth 'metabase.model-persistence.api)
"/premium-features" (+auth metabase.premium-features.api/routes)
"/preview_embed" (+auth metabase.embedding.api/preview-embedding-routes)
@@ -171,7 +171,7 @@
"/search" (+auth metabase.search.api/routes)
"/segment" (+auth 'metabase.segments.api)
"/session" metabase.session.api/routes
- "/setting" (+auth 'metabase.settings.api)
+ "/setting" (+auth 'metabase.settings-rest.api)
"/setup" 'metabase.setup.api
"/slack" (+auth metabase.channel.api/slack-routes)
"/table" (+auth metabase.warehouse-schema.api/table-routes)
@@ -195,11 +195,11 @@
(def ^{:arglists '([request respond raise])} routes
"Ring routes for API endpoints."
- ;; EE routes defined in [[metabase-enterprise.api.routes/routes]] always get the first chance to handle a request, if
- ;; they exist. If they don't exist, this handler returns `nil` which means we will try the next handler.
+ ;; EE routes defined in [[metabase-enterprise.api-routes.routes/routes]] always get the first chance to handle a
+ ;; request, if they exist. If they don't exist, this handler returns `nil` which means we will try the next handler.
(handlers/routes
(if (and config/ee-available? (not *compile-files*))
- (requiring-resolve 'metabase-enterprise.api.routes/routes)
+ (requiring-resolve 'metabase-enterprise.api-routes.routes/routes)
pass-thru-handler)
(handlers/route-map-handler route-map)
(if (and config/dev-available? (not *compile-files*))
diff --git a/src/metabase/core/core.clj b/src/metabase/core/core.clj
index f7f6ad7dc7d..cc02546391a 100644
--- a/src/metabase/core/core.clj
+++ b/src/metabase/core/core.clj
@@ -12,12 +12,12 @@
[metabase.config.core :as config]
[metabase.core.config-from-file :as config-from-file]
[metabase.core.init]
- [metabase.core.initialization-status :as init-status]
[metabase.driver.h2]
[metabase.driver.mysql]
[metabase.driver.postgres]
[metabase.embedding.settings :as embed.settings]
[metabase.events.core :as events]
+ [metabase.initialization-status.core :as init-status]
[metabase.logger.core :as logger]
[metabase.notification.core :as notification]
[metabase.plugins.core :as plugins]
diff --git a/src/metabase/driver/sql/references.clj b/src/metabase/driver/sql/references.clj
index cf09c05d5c2..0ea81203451 100644
--- a/src/metabase/driver/sql/references.clj
+++ b/src/metabase/driver/sql/references.clj
@@ -1,4 +1,5 @@
(ns metabase.driver.sql.references
+ (:refer-clojure :exclude [every? mapv select-keys some])
(:require
[macaw.ast-types :as macaw.ast-types]
[metabase.driver :as driver]
diff --git a/src/metabase/core/initialization_status.clj b/src/metabase/initialization_status/core.clj
similarity index 95%
rename from src/metabase/core/initialization_status.clj
rename to src/metabase/initialization_status/core.clj
index 8d98b952cb2..a8a6e8cebc6 100644
--- a/src/metabase/core/initialization_status.clj
+++ b/src/metabase/initialization_status/core.clj
@@ -1,4 +1,4 @@
-(ns metabase.core.initialization-status
+(ns metabase.initialization-status.core
"Code related to tracking the progress of metabase initialization.
This is kept in a separate, tiny namespace so it can be loaded right away when the application launches
(and so we don't need to wait for [[metabase.core.core]] to load to check the status).")
diff --git a/src/metabase/permissions/api/permission_graph.clj b/src/metabase/permissions/api/permission_graph.clj
deleted file mode 100644
index 5362c16e709..00000000000
--- a/src/metabase/permissions/api/permission_graph.clj
+++ /dev/null
@@ -1,164 +0,0 @@
-(ns metabase.permissions.api.permission-graph
- "Convert the permission graph's naive json conversion into the correct types.
-
- The strategy here is to use s/conform to tag every value that needs to be converted with the conversion strategy,
- then postwalk to actually perform the conversion."
- (:require
- [clojure.spec.alpha :as s]
- [clojure.spec.gen.alpha :as gen]
- [clojure.walk :as walk]
- [metabase.util :as u]
- [metabase.util.i18n :refer [trs]]))
-
-(set! *warn-on-reflection* true)
-
-(defmulti ^:private convert
- "convert values from the naively converted json to what we REALLY WANT"
- {:arglists '([spec-conformed])}
- first)
-
-(defmethod convert :kw->int [[_ k]] (Integer/parseInt (name k)))
-(defmethod convert :str->kw [[_ s]] (keyword s))
-
-;; Convert a keyword to string without excluding the namespace.
-;; e.g: :schema/name => "schema/name".
-;; Primarily used for schema-name since schema are allowed to have "/"
-;; and calling (name s) returning a substring after "/".
-(defmethod convert :kw->str [[_ s]] (u/qualified-name s))
-(defmethod convert :nil->none [[_ _]] :none)
-(defmethod convert :identity [[_ x]] x)
-(defmethod convert :global-execute [[_ x]] x)
-(defmethod convert :db-exeute [[_ x]] x)
-
-;;; --------------------------------------------------- Common ----------------------------------------------------
-
-(defn- kw-int->int-decoder [kw-int]
- (if (int? kw-int)
- kw-int
- (parse-long (name kw-int))))
-
-(def DecodableKwInt
- "Integer malli schema that knows how to decode itself from the :123 sort of shape used in perm-graphs"
- [:int {:decode/perm-graph kw-int->int-decoder}])
-
-(def ^:private Id DecodableKwInt)
-(def ^:private GroupId DecodableKwInt)
-
-;; ids come in as keywordized numbers
-(s/def ::id (s/with-gen (s/or :kw->int (s/and keyword? #(re-find #"^\d+$" (name %))))
- #(gen/fmap (comp keyword str) (s/gen pos-int?))))
-
-(def ^:private Native
- "native permissions"
- [:maybe [:enum :write :none :full :limited]])
-
-;;; ------------------------------------------------ Data Permissions ------------------------------------------------
-
-(def ^:private Perms
- "Perms that get reused for TablePerms and SchemaPerms"
- [:enum
- :all :segmented :none :full :limited :unrestricted :legacy-no-self-service :sandboxed :query-builder :no :blocked])
-
-(def ^:private TablePerms
- [:or Perms [:map
- [:read {:optional true} [:enum :all :none]]
- [:query {:optional true} [:enum :all :none :segmented]]]])
-
-(def ^:private SchemaPerms
- [:or Perms [:map-of Id TablePerms]])
-
-(def ^:private SchemaGraph
- [:map-of
- [:string {:decode/perm-graph name}]
- SchemaPerms])
-
-(def ^:private Schemas
- [:or
- [:enum
- :all
- :segmented
- :none
- :block
- :blocked
- :full
- :limited
- :impersonated
- :unrestricted
- :sandboxed
- :legacy-no-self-service
- :query-builder-and-native
- :query-builder
- :no]
- SchemaGraph])
-
-(def ^:private DataPerms
- [:map
- [:native {:optional true} Native]
- [:schemas {:optional true} Schemas]])
-
-(def StrictDataPerms
- "Data perms that care about how view-data and make-queries are related to one another.
- If you have write access for native queries, you must have data access to all schemas."
- [:and
- DataPerms
- [:fn {:error/fn (fn [_ _] (trs "Invalid DB permissions: If you have write access for native queries, you must have data access to all schemas."))}
- (fn [{:keys [native schemas]}]
- (not (and (= native :write) schemas (not (#{:all :impersonated} schemas)))))]])
-
-(def StrictDbGraph
- "like db-graph, but with added validations:
- - Ensures 'view-data' is not 'blocked' if 'create-queries' is 'query-builder-and-native'."
- [:schema {:registry {"StrictDataPerms" StrictDataPerms}}
- [:map-of
- Id
- [:and
- [:map
- [:view-data {:optional true} Schemas]
- [:create-queries {:optional true} Schemas]
- [:data {:optional true} "StrictDataPerms"]
- [:download {:optional true} "StrictDataPerms"]
- [:data-model {:optional true} "StrictDataPerms"]
- [:details {:optional true} [:enum :yes :no]]]
- [:fn {:error/fn (fn [_ _] (trs "Invalid DB permissions: If you have write access for native queries, you must have data access to all schemas."))}
- (fn [db-entry]
- (let [{:keys [create-queries view-data]} db-entry]
- (not (and (= create-queries :query-builder-and-native) (= view-data :blocked)))))]]]])
-
-(def DataPermissionsGraph
- "Used to transform, and verify data permissions graph"
- [:map
- [:groups [:map-of GroupId [:maybe StrictDbGraph]]]])
-
-(def StrictApiPermissionsGraph
- "Top level strict data graph schema expected over the API. Includes revision ID for avoiding concurrent updates."
- [:map
- [:revision {:optional true} [:maybe int?]]
- [:force {:optional true} [:maybe boolean?]]
- [:groups [:map-of GroupId [:maybe StrictDbGraph]]]])
-
-;;; --------------------------------------------- Execution Permissions ----------------------------------------------
-
-(s/def ::execute (s/or :str->kw #{"all" "none"}))
-
-(s/def ::execute-graph
- (s/or :global-execute ::execute
- :db-exeute (s/map-of ::id ::execute
- :conform-keys true)))
-
-(s/def :metabase.permissions.api.permission-graph.execution/groups
- (s/map-of ::id
- ::execute-graph
- :conform-keys true))
-
-(s/def ::execution-permissions-graph
- (s/keys :req-un [:metabase.permissions.api.permission-graph.execution/groups]))
-
-(defn converted-json->graph
- "The permissions graph is received as JSON. That JSON is naively converted. This performs a further conversion to
- convert graph keys and values to the types we want to work with."
- [spec kwj]
- (->> (s/conform spec kwj)
- (walk/postwalk (fn [x]
- (if (and (vector? x) (get-method convert (first x)))
- (convert x)
- x)))))
diff --git a/src/metabase/permissions/core.clj b/src/metabase/permissions/core.clj
index 3f1adde17e0..bdc19aa1760 100644
--- a/src/metabase/permissions/core.clj
+++ b/src/metabase/permissions/core.clj
@@ -1,5 +1,6 @@
(ns metabase.permissions.core
"`permissions` module API namespace."
+ {:clj-kondo/config '{:linters {:missing-docstring {:level :off}}}}
(:require
[metabase.permissions.models.application-permissions-revision]
[metabase.permissions.models.collection-permission-graph-revision]
@@ -9,6 +10,7 @@
[metabase.permissions.models.permissions]
[metabase.permissions.models.permissions-group]
[metabase.permissions.models.permissions-group-membership]
+ [metabase.permissions.models.permissions-revision]
[metabase.permissions.path]
[metabase.permissions.user]
[metabase.permissions.util]
@@ -24,6 +26,7 @@
metabase.permissions.models.permissions/keep-me
metabase.permissions.models.permissions-group/keep-me
metabase.permissions.models.permissions-group-membership/keep-me
+ metabase.permissions.models.permissions-revision/keep-me
metabase.permissions.path/keep-me
metabase.permissions.user/keep-me
metabase.permissions.util/keep-me
@@ -46,6 +49,7 @@
set-new-database-permissions!
set-new-table-permissions!
set-table-permission!
+ set-table-permissions!
table-permission-for-user
table-permission-for-groups
user-has-any-perms-of-type?
@@ -104,6 +108,7 @@
increment-implicit-perms-revision!
save-perms-revision!]
[metabase.permissions.validation
+ check-advanced-permissions-enabled
check-group-manager
check-has-application-permission
check-manager-of-group]
@@ -111,17 +116,11 @@
graph
update-graph!])
-;;; import these vars with different names to make their purpose more obvious. These actually do have docstrings but
-;;; Kondo gets tripped up here.
+;;; import these vars with different names to make their purpose more obvious.
-#_{:clj-kondo/ignore [:missing-docstring]}
-(p/import-def metabase.permissions.models.permissions-group/all-users all-users-group)
-
-#_{:clj-kondo/ignore [:missing-docstring]}
-(p/import-def metabase.permissions.models.permissions-group/admin admin-group)
-
-#_{:clj-kondo/ignore [:missing-docstring]}
-(p/import-def metabase.permissions.models.application-permissions-revision/latest-id latest-application-permissions-revision-id)
-
-#_{:clj-kondo/ignore [:missing-docstring]}
+(p/import-def metabase.permissions.models.permissions-group/all-users all-users-group)
+(p/import-def metabase.permissions.models.permissions-group/admin admin-group)
+(p/import-def metabase.permissions.models.application-permissions-revision/latest-id latest-application-permissions-revision-id)
(p/import-def metabase.permissions.models.collection-permission-graph-revision/latest-id latest-collection-permissions-revision-id)
+(p/import-def metabase.permissions.models.permissions-revision/latest-id latest-permissions-revision-id)
+(p/import-def metabase.permissions.models.data-permissions/least-permissive-value least-permissive-data-perms-value)
diff --git a/src/metabase/permissions/models/data_permissions.clj b/src/metabase/permissions/models/data_permissions.clj
index 6a31a9d47b3..462843a7137 100644
--- a/src/metabase/permissions/models/data_permissions.clj
+++ b/src/metabase/permissions/models/data_permissions.clj
@@ -4,9 +4,9 @@
[clojure.string :as str]
[medley.core :as m]
[metabase.api.common :as api]
- [metabase.audit-app.core :as audit]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.models.interface :as mi]
+ [metabase.permissions.schema :as permissions.schema]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
@@ -32,67 +32,37 @@
:type mi/transform-keyword
:value mi/transform-keyword})
-;;; ---------------------------------------- Permission definitions ---------------------------------------------------
-
-;; IMPORTANT: If you add a new permission type, `:values` must be ordered from *most* permissive to *least* permissive.
-;;
-;; - When fetching a user's permissions, the default behavior is to return the *most* permissive value from any group the
-;; user is in. This can be overridden by definding a custom implementation of `coalesce`.
-;;
-;; - If a user does not have any value for the permission when it is fetched, the *least* permissive value is used as a
-;; fallback.
-
-(def Permissions
- "Permissions which apply to individual databases or tables."
- ;; `legacy-no-self-service` is a deprecated permission which behaves the same as `:unrestricted` but does not override
- ;; `:blocked` in other groups
- {:perms/view-data {:model :model/Table :values [:unrestricted :legacy-no-self-service :blocked]}
- :perms/create-queries {:model :model/Table :values [:query-builder-and-native :query-builder :no]}
- :perms/download-results {:model :model/Table :values [:one-million-rows :ten-thousand-rows :no]}
- :perms/manage-table-metadata {:model :model/Table :values [:yes :no]}
- :perms/manage-database {:model :model/Database :values [:yes :no]}})
-
-(def PermissionType
- "Malli spec for valid permission types."
- (into [:enum {:error/message "Invalid permission type"}]
- (keys Permissions)))
-
-(def PermissionValue
- "Malli spec for a keyword that matches any value in [[Permissions]]."
- (into [:enum {:error/message "Invalid permission value"}]
- (distinct (mapcat :values (vals Permissions)))))
-
;;; ------------------------------------------- Misc Utils ------------------------------------------------------------
(defn least-permissive-value
"The *least* permissive value for a given perm type. This value is used as a fallback when a user does not have a
value for the permission in the database."
[perm-type]
- (-> Permissions perm-type :values last))
+ (-> permissions.schema/data-permissions perm-type :values last))
(defn most-permissive-value
"The *most* permissive value for a given perm type. This is the default value for superusers."
[perm-type]
- (-> Permissions perm-type :values first))
+ (-> permissions.schema/data-permissions perm-type :values first))
(mu/defn at-least-as-permissive?
"Returns true if value1 is at least as permissive as value2 for the given permission type."
- [perm-type :- PermissionType
- value1 :- PermissionValue
- value2 :- PermissionValue]
- (let [^PersistentVector values (-> Permissions perm-type :values)]
+ [perm-type :- ::permissions.schema/data-permission-type
+ value1 :- ::permissions.schema/data-permission-value
+ value2 :- ::permissions.schema/data-permission-value]
+ (let [^PersistentVector values (-> permissions.schema/data-permissions perm-type :values)]
(<= (.indexOf values value1)
(.indexOf values value2))))
(def ^:private model-by-perm-type
"A map from permission types directly to model identifiers (or `nil`)."
- (update-vals Permissions :model))
+ (update-vals permissions.schema/data-permissions :model))
(defn- assert-value-matches-perm-type
[perm-type perm-value]
- (when-not (contains? (set (get-in Permissions [perm-type :values])) perm-value)
+ (when-not (contains? (set (get-in permissions.schema/data-permissions [perm-type :values])) perm-value)
(throw (ex-info (tru "Permission type {0} cannot be set to {1}" perm-type perm-value)
- {perm-type (Permissions perm-type)}))))
+ {perm-type (permissions.schema/data-permissions perm-type)}))))
;;; ---------------------------------------- Caching ------------------------------------------------------------------
@@ -221,13 +191,13 @@
(defmethod coalesce :default
[perm-type perm-values]
- (let [ordered-values (-> Permissions perm-type :values)]
+ (let [ordered-values (-> permissions.schema/data-permissions perm-type :values)]
(first (filter (set perm-values) ordered-values))))
(defmethod coalesce :perms/view-data
[perm-type perm-values]
(let [perm-values (set perm-values)
- ordered-values (-> Permissions perm-type :values)]
+ ordered-values (-> permissions.schema/data-permissions perm-type :values)]
(if (and (perm-values :blocked)
(not (perm-values :unrestricted)))
;; Block in one group overrides `legacy-no-self-service` in another, but not unrestricted
@@ -237,7 +207,7 @@
(defn coalesce-most-restrictive
"In some cases (fetching schema permissions) we need to coalesce permissions using the most restrictive option."
[perm-type perm-values]
- (let [ordered-values (-> Permissions perm-type :values reverse)]
+ (let [ordered-values (-> permissions.schema/data-permissions perm-type :values reverse)]
(first (filter (set perm-values) ordered-values))))
(defn- is-superuser?
@@ -246,13 +216,13 @@
api/*is-superuser?*
(t2/select-one-fn :is_superuser :model/User :id user-id)))
-(mu/defn database-permission-for-user :- PermissionValue
+(mu/defn database-permission-for-user :- ::permissions.schema/data-permission-value
"Returns the effective permission value for a given user, permission type, and database ID. If the user has
multiple permissions for the given type in different groups, they are coalesced into a single value."
[user-id perm-type database-id]
(when (not= :model/Database (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} is a table-level permission." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(if (is-superuser? user-id)
(most-permissive-value perm-type)
(let [perm-values (->> (get-permissions user-id perm-type database-id)
@@ -290,13 +260,13 @@
(defn- get-additional-table-permission! [{:keys [db-id table-id]} perm-type]
(get-in *additional-table-permissions* [db-id table-id perm-type]))
-(mu/defn table-permission-for-groups :- PermissionValue
+(mu/defn table-permission-for-groups :- ::permissions.schema/data-permission-value
"Returns the effective permission value provided by a set of *group-ids*, for a provided permission type, database
ID, and table ID."
[group-ids perm-type database-id table-id]
(when (not= :model/Table (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} is not a table-level permission." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(let [perm-values (t2/select-fn-set :value
:model/DataPermissions
{:select [[:p.perm_value :value]]
@@ -320,13 +290,13 @@
(table-permission-for-groups group-ids perm-type database-id table-id)
perm-value))
-(mu/defn table-permission-for-user :- PermissionValue
+(mu/defn table-permission-for-user :- ::permissions.schema/data-permission-value
"Returns the effective permission value for a given user, permission type, and database ID, and table ID. If the user
has multiple permissions for the given type in different groups, they are coalesced into a single value."
[user-id perm-type database-id table-id]
(when (not= :model/Table (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} is a database-level permission." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(if (is-superuser? user-id)
(most-permissive-value perm-type)
(let [perm-values (->> (get-permissions user-id perm-type database-id)
@@ -357,14 +327,14 @@
vals
set))
-(mu/defn full-schema-permission-for-user :- PermissionValue
+(mu/defn full-schema-permission-for-user :- ::permissions.schema/data-permission-value
"Returns the effective *schema-level* permission value for a given user, permission type, and database ID, and
schema name. If the user has multiple permissions for the given type in different groups, they are coalesced into a
single value. The schema-level permission is the *most* restrictive table-level permission within that schema."
[user-id perm-type database-id schema-name]
(when (not= :model/Table (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} is not a table-level permission." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(if (is-superuser? user-id)
(most-permissive-value perm-type)
;; The schema-level permission is the most-restrictive table-level permission within a schema. So for each group,
@@ -381,14 +351,14 @@
(or (coalesce perm-type perm-values)
(least-permissive-value perm-type)))))
-(mu/defn full-db-permission-for-user :- PermissionValue
+(mu/defn full-db-permission-for-user :- ::permissions.schema/data-permission-value
"Returns the effective *db-level* permission value for a given user, permission type, and database ID. If the user
has multiple permissions for the given type in different groups, they are coalesced into a single value. The
db-level permission is the *most* restrictive table-level permission within that database."
[user-id perm-type database-id]
(when (not= :model/Table (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} is not a table-level permission." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(if (is-superuser? user-id)
(most-permissive-value perm-type)
;; The DB-level permission is the most-restrictive table-level permission within a DB. So for each group, select the
@@ -403,7 +373,7 @@
(or (coalesce perm-type perm-values)
(least-permissive-value perm-type)))))
-(mu/defn schema-permission-for-user :- PermissionValue
+(mu/defn schema-permission-for-user :- ::permissions.schema/data-permission-value
"Returns the effective *schema-level* permission value for a given user, permission type, and database ID, and
schema name. If the user has multiple permissions for the given type in different groups, they are coalesced into a
single value. The schema-level permission is the *least* restrictive table-level permission within that schema.
@@ -413,7 +383,7 @@
(let [schema-name (or schema-name "")]
(when (not= :model/Table (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} is not a table-level permission." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(if (is-superuser? user-id)
(most-permissive-value perm-type)
;; The schema-level permission is the most-restrictive table-level permission within a schema. So for each group,
@@ -435,13 +405,13 @@
(schema-permission-for-user user-id perm-type database-id schema)
perm-value))
-(mu/defn most-permissive-database-permission-for-user :- PermissionValue
+(mu/defn most-permissive-database-permission-for-user :- ::permissions.schema/data-permission-value
"Similar to checking _partial_ permissions with permissions paths - what is the *most permissive* permission the
user has on any of the tables within this database?"
[user-id perm-type database-id]
(when (not= :model/Table (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} is not a table-level permission." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(if (is-superuser? user-id)
(most-permissive-value perm-type)
(let [perm-values (->> (get-permissions user-id perm-type database-id)
@@ -450,7 +420,7 @@
(or (coalesce perm-type perm-values)
(least-permissive-value perm-type)))))
-(mu/defn native-download-permission-for-user :- PermissionValue
+(mu/defn native-download-permission-for-user :- ::permissions.schema/data-permission-value
"Returns the effective download permission value for a given user and database ID, for native queries on the database.
For each group, the native download permission for a database is equal to the lowest permission level of any table in
the database."
@@ -491,7 +461,7 @@
"Returns the graph representing admin permissions for all groups"
[& {:keys [db-id perm-type]}]
(let [db-ids (if db-id [db-id] (t2/select-pks-vec :model/Database))
- perm-types (if perm-type [perm-type] (keys Permissions))]
+ perm-types (if perm-type [perm-type] (keys permissions.schema/data-permissions))]
(into {} (map (fn [db-id]
[db-id (into {} (map (fn [perm] [perm (most-permissive-value perm)])
perm-types))])
@@ -560,58 +530,12 @@
{}
granular-graph))))
-;;; ---------------------------------------- Fetching the data permissions graph --------------------------------------
-
-(def ^:private Graph
- [:map-of [:int {:title "group-id" :min 0}]
- [:map-of [:int {:title "db-id" :min 0}]
- [:map-of PermissionType
- [:or
- PermissionValue
- [:map-of [:string {:title "schema"}]
- [:map-of
- [:int {:title "table-id" :min 0}]
- PermissionValue]]]]]])
-
-(mu/defn data-permissions-graph :- Graph
- "Returns a tree representation of all data permissions. Can be optionally filtered by group ID, database ID,
- and/or permission type. This is intended to power the permissions editor in the admin panel, and should not be used
- for permission enforcement, as it will read much more data than necessary."
- [& {:keys [group-id group-ids db-id perm-type audit?]}]
- (let [data-perms (t2/select [:model/DataPermissions
- [:perm_type :type]
- [:group_id :group-id]
- [:perm_value :value]
- [:db_id :db-id]
- [:schema_name :schema]
- [:table_id :table-id]]
- {:where [:and
- (when perm-type [:= :perm_type (u/qualified-name perm-type)])
- (when db-id [:= :db_id db-id])
- (when group-id [:= :group_id group-id])
- (when group-ids [:in :group_id group-ids])
- (when-not audit? [:not= :db_id audit/audit-db-id])]})]
- (reduce
- (fn [graph {group-id :group-id
- perm-type :type
- value :value
- db-id :db-id
- schema :schema
- table-id :table-id}]
- (let [schema (or schema "")
- path (if table-id
- [group-id db-id perm-type schema table-id]
- [group-id db-id perm-type])]
- (assoc-in graph path value)))
- {}
- data-perms)))
-
;;; --------------------------------------------- Updating permissions ------------------------------------------------
(defn- assert-valid-permission
[{:keys [perm_type perm_value] :as permission}]
- (when-not (mr/validate PermissionType perm_type)
- (throw (ex-info (str/join (mu/explain PermissionType perm_type)) permission)))
+ (when-not (mr/validate ::permissions.schema/data-permission-type perm_type)
+ (throw (ex-info (str/join (mu/explain ::permissions.schema/data-permission-type perm_type)) permission)))
(assert-value-matches-perm-type perm_type perm_value))
(t2/define-before-insert :model/DataPermissions
@@ -639,7 +563,7 @@
- :to-insert - sequence of DataPermissions models to insert "
[group-or-id :- TheIdable
db-or-id :- TheIdable
- perm-type :- PermissionType
+ perm-type :- ::permissions.schema/data-permission-type
value :- :keyword]
(let [group-id (u/the-id group-or-id)
db-id (u/the-id db-or-id)
@@ -688,7 +612,7 @@
Block permissions (i.e. :perms/view-data :blocked) can be set at the table or database-level."
[group-or-id :- TheIdable
db-or-id :- TheIdable
- perm-type :- PermissionType
+ perm-type :- ::permissions.schema/data-permission-type
value :- :keyword]
(t2/with-transaction [_conn]
(let [{:keys [to-insert to-delete]} (build-database-permission group-or-id db-or-id perm-type value)]
@@ -701,7 +625,7 @@
"Given a group and a permission type, returns the lowest permission level for that group in any database, at the DB or table-level.
This is used to determine the default permission level for the group when a new database is added."
[group-id perm-type]
- (let [lowest-to-highest-values (-> Permissions perm-type :values reverse)]
+ (let [lowest-to-highest-values (-> permissions.schema/data-permissions perm-type :values reverse)]
(first (filter
(fn [value]
(t2/exists? :model/DataPermissions
@@ -881,11 +805,11 @@
- :to-delete - sequence of DataPermissions models to delete
- :to-insert - sequence of DataPermissions models to insert "
[group-or-id :- TheIdable
- perm-type :- PermissionType
+ perm-type :- ::permissions.schema/data-permission-type
table-perms :- [:map-of TheIdable :keyword]]
(when (not= :model/Table (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} cannot be set on tables." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(if (empty? table-perms)
;; if `table-perms` is empty, there's nothing to do
{:to-delete [] :to-insert []}
@@ -931,7 +855,7 @@
is removed and table-level rows are are added for all of its tables. Similarly, if setting a table-level permission to a value
that results in all of the database's tables having the same permission, it is replaced with a single database-level row."
[group-or-id :- TheIdable
- perm-type :- PermissionType
+ perm-type :- ::permissions.schema/data-permission-type
table-perms :- [:map-of TheIdable :keyword]]
(t2/with-transaction [_conn]
(let [{:keys [to-delete to-insert]} (build-table-permissions group-or-id perm-type table-perms)]
@@ -944,7 +868,7 @@
"Sets permissions for a single table to the specified value for a given group."
[group-or-id :- TheIdable
table-or-id :- TheIdable
- perm-type :- PermissionType
+ perm-type :- ::permissions.schema/data-permission-type
value :- :keyword]
(set-table-permissions! group-or-id perm-type {table-or-id value}))
@@ -952,7 +876,7 @@
"Infers the permission value for a new table based on existing permissions in the schema. Returns a permission value
if every table in the schema has the same value, otherwise returns nil."
[db-id group-id schema-name perm-type]
- (let [possible-values (:values (get Permissions perm-type))
+ (let [possible-values (:values (get permissions.schema/data-permissions perm-type))
schema-perms-check (mapv (fn [value]
(t2/exists? :model/DataPermissions
:perm_type (u/qualified-name perm-type)
@@ -980,11 +904,11 @@
- Otherwise we use the provided `default-value`."
[groups-or-ids :- [:sequential TheIdable]
table-or-id :- TheIdable
- perm-type :- PermissionType
+ perm-type :- ::permissions.schema/data-permission-type
default-value :- :keyword]
(when (not= :model/Table (model-by-perm-type perm-type))
(throw (ex-info (tru "Permission type {0} cannot be set on tables." perm-type)
- {perm-type (Permissions perm-type)})))
+ {perm-type (permissions.schema/data-permissions perm-type)})))
(when (seq groups-or-ids)
(t2/with-transaction [_conn]
(let [group-ids (map u/the-id groups-or-ids)
diff --git a/src/metabase/permissions/models/data_permissions/sql.clj b/src/metabase/permissions/models/data_permissions/sql.clj
index f680019cd4e..f8b50187d0f 100644
--- a/src/metabase/permissions/models/data_permissions/sql.clj
+++ b/src/metabase/permissions/models/data_permissions/sql.clj
@@ -1,7 +1,7 @@
(ns metabase.permissions.models.data-permissions.sql
"Helper functions for models using data permissions to construct `visisble-query` methods from."
(:require
- [metabase.permissions.models.data-permissions :as data-perms]
+ [metabase.permissions.schema :as permissions.schema]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu])
(:import
@@ -10,34 +10,35 @@
(set! *warn-on-reflection* true)
(defmulti perm-type-to-least-int-case
- "Converts a given `data-perm/PermissionType` keyword and either a column for a literal value into a case when ... then ... SQL statement
- that converts the given column or literal into the index of the value for provide permission type.
+ "Converts a given `data-perm/PermissionType` keyword and either a column for a literal value into a case when ... then
+ ... SQL statement that converts the given column or literal into the index of the value for provide permission type.
- For example, calling with perm-type :perms/view-data and a column :perm-value creates a SQL Statement like:
+ For example, calling with perm-type :perms/view-data and a column :perm-value creates a SQL Statement like:
- ```sql
- CASE WHEN \"perm_value\" = 'unrestricted' THEN 0 ELSE ...
- ```
- This lets us write SQL statements to compare permissions values by their index position in the same way we do in the
- `data-perms/at-least-as-permissive?` function"
+ ```sql
+ CASE WHEN \"perm_value\" = 'unrestricted' THEN 0 ELSE ...
+ ```
+
+ This lets us write SQL statements to compare permissions values by their index position in the same way we do in the
+ `data-perms/at-least-as-permissive?` function"
{:arglists '([perm-type column])}
(fn [perm-type _] perm-type))
(mu/defn- perm-type-to-int-case
- [perm-type :- data-perms/PermissionType
+ [perm-type :- ::permissions.schema/data-permission-type
column :- :keyword]
(into [:case]
(apply concat
(map-indexed (fn [idx perm-value] [[:= column (h2x/literal perm-value)] [:inline idx]])
- (-> data-perms/Permissions perm-type :values)))))
+ (-> permissions.schema/data-permissions perm-type :values)))))
(mu/defmethod perm-type-to-least-int-case :default
- [perm-type :- data-perms/PermissionType
+ [perm-type :- ::permissions.schema/data-permission-type
column :- :keyword]
[:min (perm-type-to-int-case perm-type column)])
(mu/defmethod perm-type-to-least-int-case :perms/view-data
- [_ :- data-perms/PermissionType
+ [_ :- ::permissions.schema/data-permission-type
column :- :keyword]
;; blocked has a higher 'prioirty' than legacy-no-self-service when determining what permission level the user has
(let [minimum-perm-value [:min
@@ -52,13 +53,13 @@
[:= minimum-perm-value [:inline 2]] [:inline 1]]))
(mu/defn- perm-type-to-int-inline :- [:tuple [:= :inline] nat-int?]
- [perm-type :- data-perms/PermissionType
- level :- data-perms/PermissionValue]
- (let [^PersistentVector values (-> data-perms/Permissions perm-type :values)]
+ [perm-type :- ::permissions.schema/data-permission-type
+ level :- ::permissions.schema/data-permission-value]
+ (let [^PersistentVector values (-> permissions.schema/data-permissions perm-type :values)]
[:inline (.indexOf values level)]))
(mu/defn- perm-type-to-most-int-case
- [perm-type :- data-perms/PermissionType
+ [perm-type :- ::permissions.schema/data-permission-type
column :- :keyword]
[:max (perm-type-to-int-case perm-type column)])
@@ -117,8 +118,8 @@
most or least restrictive permission a user has in any of the groups they are a member of. If that value is left out we will assume we
want the least restrict permission value."
[:map-of
- data-perms/PermissionType
- [:or data-perms/PermissionValue [:tuple data-perms/PermissionValue [:enum :most :least]]]])
+ ::permissions.schema/data-permission-type
+ [:or ::permissions.schema/data-permission-value [:tuple ::permissions.schema/data-permission-value [:enum :most :least]]]])
(mu/defn visible-table-filter-select
"Selects a column from tables that are visible to the provided user given a mapping of permission types to the required value or the required
diff --git a/src/metabase/permissions/models/permissions_group.clj b/src/metabase/permissions/models/permissions_group.clj
index 8f9606d92ea..1a510054456 100644
--- a/src/metabase/permissions/models/permissions_group.clj
+++ b/src/metabase/permissions/models/permissions_group.clj
@@ -190,3 +190,24 @@
"Returns a boolean representing whether this group is a tenant group."
[group-id]
(t2/select-one-fn :is_tenant_group :model/PermissionsGroup :id (u/the-id group-id)))
+
+(defn- group-id->num-members
+ "Return a map of `PermissionsGroup` ID -> number of members in the group. (This doesn't include entries for empty
+ groups.)"
+ []
+ (let [results (mdb/query
+ {:select [[:pgm.group_id :group_id] [[:count :pgm.id] :members]]
+ :from [[:permissions_group_membership :pgm]]
+ :left-join [[:core_user :user] [:= :pgm.user_id :user.id]]
+ :where [:= :user.is_active true]
+ :group-by [:pgm.group_id]})]
+ (zipmap
+ (map :group_id results)
+ (map :members results))))
+
+(methodical/defmethod t2/batched-hydrate [:model/PermissionsGroup :member_count]
+ "Efficiently add `:member_count` to PermissionGroups."
+ [_model _k groups]
+ (let [group-id->num-members (group-id->num-members)]
+ (for [group groups]
+ (assoc group :member_count (get group-id->num-members (u/the-id group) 0)))))
diff --git a/src/metabase/permissions/models/permissions_revision.clj b/src/metabase/permissions/models/permissions_revision.clj
index 2b851f6966d..1b93ed44c5b 100644
--- a/src/metabase/permissions/models/permissions_revision.clj
+++ b/src/metabase/permissions/models/permissions_revision.clj
@@ -20,8 +20,8 @@
(throw (Exception. (tru "You cannot update a PermissionsRevision!"))))
(defn latest-id
- "Return the ID of the newest `PermissionsRevision`, or zero if none have been made yet.
- (This is used by the permissions graph update logic that checks for changes since the original graph was fetched)."
+ "Return the ID of the newest `PermissionsRevision`, or zero if none have been made yet. (This is used by the
+ permissions graph update logic that checks for changes since the original graph was fetched)."
[]
(or (:id (t2/select-one [:model/PermissionsRevision [:%max.id :id]]))
0))
diff --git a/src/metabase/permissions/schema.clj b/src/metabase/permissions/schema.clj
new file mode 100644
index 00000000000..edbc6806019
--- /dev/null
+++ b/src/metabase/permissions/schema.clj
@@ -0,0 +1,33 @@
+(ns metabase.permissions.schema
+ (:require
+ [metabase.util.malli.registry :as mr]))
+
+;;; ---------------------------------------- Permission definitions ---------------------------------------------------
+
+;; IMPORTANT: If you add a new permission type, `:values` must be ordered from *most* permissive to *least* permissive.
+;;
+;; - When fetching a user's permissions, the default behavior is to return the *most* permissive value from any group the
+;; user is in. This can be overridden by definding a custom implementation of `coalesce`.
+;;
+;; - If a user does not have any value for the permission when it is fetched, the *least* permissive value is used as a
+;; fallback.
+
+(def data-permissions
+ "Permissions which apply to individual databases or tables."
+ ;; `legacy-no-self-service` is a deprecated permission which behaves the same as `:unrestricted` but does not override
+ ;; `:blocked` in other groups
+ {:perms/view-data {:model :model/Table, :values [:unrestricted :legacy-no-self-service :blocked]}
+ :perms/create-queries {:model :model/Table, :values [:query-builder-and-native :query-builder :no]}
+ :perms/download-results {:model :model/Table, :values [:one-million-rows :ten-thousand-rows :no]}
+ :perms/manage-table-metadata {:model :model/Table, :values [:yes :no]}
+ :perms/manage-database {:model :model/Database, :values [:yes :no]}})
+
+(mr/def ::data-permission-type
+ "Malli spec for valid permission types."
+ (into [:enum {:error/message "Invalid permission type"}]
+ (keys data-permissions)))
+
+(mr/def ::data-permission-value
+ "Malli spec for a keyword that matches any value in [[Permissions]]."
+ (into [:enum {:error/message "Invalid permission value"}]
+ (distinct (mapcat :values (vals data-permissions)))))
diff --git a/src/metabase/permissions/api.clj b/src/metabase/permissions_rest/api.clj
similarity index 82%
rename from src/metabase/permissions/api.clj
rename to src/metabase/permissions_rest/api.clj
index 588372ce7c6..ab6c90f3c74 100644
--- a/src/metabase/permissions/api.clj
+++ b/src/metabase/permissions_rest/api.clj
@@ -1,4 +1,4 @@
-(ns metabase.permissions.api
+(ns metabase.permissions-rest.api
"/api/permissions endpoints."
(:require
[clojure.data :as data]
@@ -7,16 +7,10 @@
[malli.transform :as mtx]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
- [metabase.app-db.core :as mdb]
[metabase.events.core :as events]
- [metabase.models.interface :as mi]
- [metabase.permissions.api.permission-graph :as api.permission-graph]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
+ [metabase.permissions-rest.schema :as permissions-rest.schema]
[metabase.permissions.core :as perms]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
- [metabase.permissions.models.permissions-group :as perms-group]
- [metabase.permissions.models.permissions-revision :as perms-revision]
- [metabase.permissions.util :as perms.u]
- [metabase.permissions.validation :as validation]
[metabase.premium-features.core :as premium-features :refer [defenterprise]]
[metabase.request.core :as request]
[metabase.util :as u]
@@ -80,13 +74,13 @@
[:force {:default false} [:maybe ms/BooleanValue]]]
body :- :map]
(api/check-superuser)
- (let [new-graph (mc/decode api.permission-graph/StrictApiPermissionsGraph
+ (let [new-graph (mc/decode ::permissions-rest.schema/strict-api-permissions-graph
body
(mtx/transformer
mtx/string-transformer
(mtx/transformer {:name :perm-graph})))]
- (when-not (mr/validate api.permission-graph/DataPermissionsGraph new-graph)
- (let [explained (mu/explain api.permission-graph/DataPermissionsGraph new-graph)]
+ (when-not (mr/validate ::permissions-rest.schema/data-permissions-graph new-graph)
+ (let [explained (mu/explain ::permissions-rest.schema/data-permissions-graph new-graph)]
(throw (ex-info (tru "Cannot parse permissions graph because it is invalid: {0}" (pr-str explained))
{:status-code 400}))))
(t2/with-transaction [_conn]
@@ -96,10 +90,10 @@
(:groups new-graph))
old (or old {})
new (or new {})]
- (perms.u/log-permissions-changes old new)
- (when-not force (perms.u/check-revision-numbers old-graph new-graph))
+ (perms/log-permissions-changes old new)
+ (when-not force (perms/check-revision-numbers old-graph new-graph))
(data-perms.graph/update-data-perms-graph! {:groups new})
- (perms.u/save-perms-revision! :model/PermissionsRevision (:revision old-graph) old new)
+ (perms/save-perms-revision! :model/PermissionsRevision (:revision old-graph) old new)
(let [sandbox-updates (:sandboxes new-graph)
sandboxes (when sandbox-updates
(upsert-sandboxes! sandbox-updates))
@@ -107,7 +101,7 @@
impersonations (when impersonation-updates
(insert-impersonations! impersonation-updates))
group-ids (-> new-graph :groups keys)]
- (merge {:revision (perms-revision/latest-id)}
+ (merge {:revision (perms/latest-permissions-revision-id)}
(when-not skip-graph {:groups (:groups (data-perms.graph/api-graph {:group-ids group-ids}))})
(when sandboxes {:sandboxes sandboxes})
(when impersonations {:impersonations impersonations})))))))
@@ -116,20 +110,6 @@
;;; | PERMISSIONS GROUP ENDPOINTS |
;;; +----------------------------------------------------------------------------------------------------------------+
-(defn- group-id->num-members
- "Return a map of `PermissionsGroup` ID -> number of members in the group. (This doesn't include entries for empty
- groups.)"
- []
- (let [results (mdb/query
- {:select [[:pgm.group_id :group_id] [[:count :pgm.id] :members]]
- :from [[:permissions_group_membership :pgm]]
- :left-join [[:core_user :user] [:= :pgm.user_id :user.id]]
- :where [:= :user.is_active true]
- :group-by [:pgm.group_id]})]
- (zipmap
- (map :group_id results)
- (map :members results))))
-
(defn- ordered-groups
"Return a sequence of ordered `PermissionsGroups`."
[limit offset query]
@@ -139,14 +119,6 @@
(some? offset) (sql.helpers/offset offset)
(some? query) (sql.helpers/where query))))
-(mi/define-batched-hydration-method add-member-counts
- :member_count
- "Efficiently add `:member_count` to PermissionGroups."
- [groups]
- (let [group-id->num-members (group-id->num-members)]
- (for [group groups]
- (assoc group :member_count (get group-id->num-members (u/the-id group) 0)))))
-
(api.macros/defendpoint :get "/group"
"Fetch all `PermissionsGroups`, including a count of the number of `:members` in that group.
This API requires superuser or group manager of more than one group.
@@ -154,9 +126,9 @@
is manager of."
[]
(try
- (validation/check-group-manager)
+ (perms/check-group-manager)
(catch clojure.lang.ExceptionInfo _e
- (validation/check-has-application-permission :setting)))
+ (perms/check-has-application-permission :setting)))
(let [query (when (and (not api/*is-superuser?*)
(premium-features/enable-advanced-permissions?)
api/*is-group-manager?*)
@@ -172,7 +144,7 @@
"Fetch the details for a certain permissions group."
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
- (validation/check-group-manager id)
+ (perms/check-group-manager id)
(api/check-404
(-> (t2/select-one :model/PermissionsGroup :id id)
(t2/hydrate :members))))
@@ -196,7 +168,7 @@
_query-params
{:keys [name]} :- [:map
[:name ms/NonBlankString]]]
- (validation/check-manager-of-group group-id)
+ (perms/check-manager-of-group group-id)
(let [group (t2/select-one :model/PermissionsGroup :id group-id)]
(api/check-404 group)
(t2/update! :model/PermissionsGroup group-id
@@ -212,7 +184,7 @@
"Delete a specific `PermissionsGroup`."
[{:keys [group-id]} :- [:map
[:group-id ms/PositiveInt]]]
- (validation/check-manager-of-group group-id)
+ (perms/check-manager-of-group group-id)
(let [group (t2/select-one :model/PermissionsGroup :id group-id)]
(t2/delete! :model/PermissionsGroup :id group-id)
(events/publish-event! :event/group-delete {:object group
@@ -229,7 +201,7 @@
:group_id <id>
:is_group_manager boolean}]}"
[]
- (validation/check-group-manager)
+ (perms/check-group-manager)
(group-by :user_id (t2/select [:model/PermissionsGroupMembership [:id :membership_id] :group_id :user_id :is_group_manager]
(cond-> {}
(and (not api/*is-superuser?*)
@@ -250,10 +222,10 @@
[:user_id ms/PositiveInt]
[:is_group_manager {:default false} [:maybe :boolean]]]]
(let [is_group_manager (boolean is_group_manager)]
- (validation/check-manager-of-group group_id)
+ (perms/check-manager-of-group group_id)
(when is_group_manager
;; enable `is_group_manager` require advanced-permissions enabled
- (validation/check-advanced-permissions-enabled :group-manager)
+ (perms/check-advanced-permissions-enabled :group-manager)
(api/check
(t2/exists? :model/User :id user_id :is_superuser false)
[400 (tru "Admin cant be a group manager.")]))
@@ -271,12 +243,12 @@
{:keys [is_group_manager]} :- [:map
[:is_group_manager :boolean]]]
;; currently this API is only used to update the `is_group_manager` flag and it requires advanced-permissions
- (validation/check-advanced-permissions-enabled :group-manager)
+ (perms/check-advanced-permissions-enabled :group-manager)
;; Make sure only Super user or Group Managers can call this
- (validation/check-group-manager)
+ (perms/check-group-manager)
(let [old (t2/select-one :model/PermissionsGroupMembership :id id)]
(api/check-404 old)
- (validation/check-manager-of-group (:group_id old))
+ (perms/check-manager-of-group (:group_id old))
(api/check
(t2/exists? :model/User :id (:user_id old) :is_superuser false)
[400 (tru "Admin cant be a group manager.")])
@@ -288,9 +260,9 @@
"Remove all members from a `PermissionsGroup`. Returns a 400 (Bad Request) if the group ID is for the admin group."
[{:keys [group-id]} :- [:map
[:group-id ms/PositiveInt]]]
- (validation/check-manager-of-group group-id)
+ (perms/check-manager-of-group group-id)
(api/check-404 (t2/exists? :model/PermissionsGroup :id group-id))
- (api/check-400 (not= group-id (u/the-id (perms-group/admin))))
+ (api/check-400 (not= group-id (u/the-id (perms/admin-group))))
(t2/delete! :model/PermissionsGroupMembership :group_id group-id)
api/generic-204-no-content)
@@ -300,6 +272,6 @@
[:id ms/PositiveInt]]]
(let [membership (t2/select-one :model/PermissionsGroupMembership :id id)]
(api/check-404 membership)
- (validation/check-manager-of-group (:group_id membership))
+ (perms/check-manager-of-group (:group_id membership))
(t2/delete! :model/PermissionsGroupMembership :id id)
api/generic-204-no-content))
diff --git a/src/metabase/permissions_rest/api/permission_graph.clj b/src/metabase/permissions_rest/api/permission_graph.clj
new file mode 100644
index 00000000000..39971c8d1ee
--- /dev/null
+++ b/src/metabase/permissions_rest/api/permission_graph.clj
@@ -0,0 +1,27 @@
+(ns metabase.permissions-rest.api.permission-graph
+ "Convert the permission graph's naive json conversion into the correct types.
+
+ The strategy here is to use s/conform to tag every value that needs to be converted with the conversion strategy,
+ then postwalk to actually perform the conversion."
+ (:require
+ [metabase.util :as u]))
+
+(set! *warn-on-reflection* true)
+
+(defmulti ^:private convert
+ "convert values from the naively converted json to what we REALLY WANT"
+ {:arglists '([spec-conformed])}
+ first)
+
+(defmethod convert :kw->int [[_ k]] (Integer/parseInt (name k)))
+(defmethod convert :str->kw [[_ s]] (keyword s))
+
+;; Convert a keyword to string without excluding the namespace.
+;; e.g: :schema/name => "schema/name".
+;; Primarily used for schema-name since schema are allowed to have "/"
+;; and calling (name s) returning a substring after "/".
+(defmethod convert :kw->str [[_ s]] (u/qualified-name s))
+(defmethod convert :nil->none [[_ _]] :none)
+(defmethod convert :identity [[_ x]] x)
+(defmethod convert :global-execute [[_ x]] x)
+(defmethod convert :db-exeute [[_ x]] x)
diff --git a/src/metabase/permissions/models/data_permissions/graph.clj b/src/metabase/permissions_rest/data_permissions/graph.clj
similarity index 76%
rename from src/metabase/permissions/models/data_permissions/graph.clj
rename to src/metabase/permissions_rest/data_permissions/graph.clj
index 798013c4e4b..b8923a4e3c3 100644
--- a/src/metabase/permissions/models/data_permissions/graph.clj
+++ b/src/metabase/permissions_rest/data_permissions/graph.clj
@@ -1,4 +1,4 @@
-(ns metabase.permissions.models.data-permissions.graph
+(ns metabase.permissions-rest.data-permissions.graph
"Code involving reading and writing the API-style data permission graph. This is the graph which we use to represent
permissions when communicating with the frontend, which has different keys and a slightly different structure
from the one returned by `metabase.models.data-permissions/data-permissions-graph`, which is based directly on the
@@ -10,17 +10,17 @@
[clojure.string :as str]
[medley.core :as m]
[metabase.audit-app.core :as audit]
- [metabase.permissions.api.permission-graph :as api.permission-graph]
- [metabase.permissions.models.data-permissions :as data-perms]
- [metabase.permissions.models.permissions-group :as perms-group]
- [metabase.permissions.models.permissions-revision :as perms-revision]
+ [metabase.permissions-rest.schema :as permissions-rest.schema]
+ [metabase.permissions.core :as perms]
+ [metabase.permissions.schema :as permissions.schema]
[metabase.premium-features.core :as premium-features :refer [defenterprise]]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
[metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]
[toucan2.core :as t2]))
-;; See also: [[data-perms/Permissions]]
+;; See also: [[permissions.schema/data-permissions]]
(def ^:private ->api-keys
{:perms/view-data :view-data
:perms/create-queries :create-queries
@@ -56,9 +56,9 @@
(mu/defn ellide? :- :boolean
"If a table has the least permissive value for a perm type, leave it out,
Unless it's :data perms, in which case, leave it out only if it's no-self-service"
- [type :- data-perms/PermissionType
- value :- data-perms/PermissionValue]
- (= (data-perms/least-permissive-value type) value))
+ [type :- ::permissions.schema/data-permission-type
+ value :- ::permissions.schema/data-permission-value]
+ (= (perms/least-permissive-data-perms-value type) value))
(defn- rename-or-ellide-kv
"Renames a kv pair from the data-permissions-graph to an API-style data permissions graph (which we send to the client)."
@@ -69,7 +69,7 @@
(mu/defn- api-table-perms
"Helper to transform 'leaf' values with table-level schemas in the data permissions graph into an API-style data permissions value.
Coalesces permissions at the schema level if all table-level permissions within a schema are identical."
- [type :- data-perms/PermissionType
+ [type :- ::permissions.schema/data-permission-type
schema->table-id->api-val]
(let [transform-val (fn [perm-val] ((->api-vals type) perm-val))
coalesce-or-transform (fn [table-id->perm]
@@ -125,7 +125,7 @@
"These are not stored in the data-permissions table, but the API expects them to be there (for legacy reasons), so here we populate it.
For every db in the incoming graph, adds on admin permissions."
[api-graph {:keys [db-id group-ids group-id audit?]}]
- (let [admin-group-id (u/the-id (perms-group/admin))
+ (let [admin-group-id (u/the-id (perms/admin-group))
db-ids (if db-id [db-id] (t2/select-pks-vec :model/Database
{:where [:and
(when-not audit? [:not= :id audit/audit-db-id])]}))]
@@ -151,7 +151,51 @@
(into {}))
m))
-(mu/defn api-graph :- api.permission-graph/DataPermissionsGraph
+(mr/def ::graph
+ [:map-of [:int {:title "group-id" :min 0}]
+ [:map-of [:int {:title "db-id" :min 0}]
+ [:map-of ::permissions.schema/data-permission-type
+ [:or
+ ::permissions.schema/data-permission-value
+ [:map-of [:string {:title "schema"}]
+ [:map-of
+ [:int {:title "table-id" :min 0}]
+ ::permissions.schema/data-permission-value]]]]]])
+
+(mu/defn data-permissions-graph :- ::graph
+ "Returns a tree representation of all data permissions. Can be optionally filtered by group ID, database ID,
+ and/or permission type. This is intended to power the permissions editor in the admin panel, and should not be used
+ for permission enforcement, as it will read much more data than necessary."
+ [& {:keys [group-id group-ids db-id perm-type audit?]}]
+ (let [data-perms (t2/select [:model/DataPermissions
+ [:perm_type :type]
+ [:group_id :group-id]
+ [:perm_value :value]
+ [:db_id :db-id]
+ [:schema_name :schema]
+ [:table_id :table-id]]
+ {:where [:and
+ (when perm-type [:= :perm_type (u/qualified-name perm-type)])
+ (when db-id [:= :db_id db-id])
+ (when group-id [:= :group_id group-id])
+ (when group-ids [:in :group_id group-ids])
+ (when-not audit? [:not= :db_id audit/audit-db-id])]})]
+ (reduce
+ (fn [graph {group-id :group-id
+ perm-type :type
+ value :value
+ db-id :db-id
+ schema :schema
+ table-id :table-id}]
+ (let [schema (or schema "")
+ path (if table-id
+ [group-id db-id perm-type schema table-id]
+ [group-id db-id perm-type])]
+ (assoc-in graph path value)))
+ {}
+ data-perms)))
+
+(mu/defn api-graph :- ::permissions-rest.schema/data-permissions-graph
"Converts the backend representation of the data permissions graph to the representation we send over the API. Mainly
renames permission types and values from the names stored in the database to the ones expected by the frontend.
- Converts DB key names to API key names
@@ -163,13 +207,13 @@
([& {:as opts}
:- [:map
- [:group-id {:optional true} [:maybe pos-int?]]
+ [:group-id {:optional true} [:maybe pos-int?]]
[:group-ids {:optional true} [:maybe [:sequential pos-int?]]]
- [:db-id {:optional true} [:maybe pos-int?]]
- [:audit? {:optional true} [:maybe :boolean]]
- [:perm-type {:optional true} [:maybe data-perms/PermissionType]]]]
- (let [graph (data-perms/data-permissions-graph opts)]
- {:revision (perms-revision/latest-id)
+ [:db-id {:optional true} [:maybe pos-int?]]
+ [:audit? {:optional true} [:maybe :boolean]]
+ [:perm-type {:optional true} [:maybe ::permissions.schema/data-permission-type]]]]
+ (let [graph (data-permissions-graph opts)]
+ {:revision (perms/latest-permissions-revision-id)
:groups (-> graph
rename-perms
remove-empty-vals
@@ -209,7 +253,7 @@
:all :yes
:none :no)))
(update-keys (fn [table-id] {:id table-id :db_id db-id :schema schema})))]
- (data-perms/set-table-permissions! group-id :perms/manage-table-metadata new-table-perms)))
+ (perms/set-table-permissions! group-id :perms/manage-table-metadata new-table-perms)))
(defn- update-schema-level-metadata-permissions!
[group-id db-id schema new-schema-perms]
@@ -219,10 +263,10 @@
(when (seq tables)
(case new-schema-perms
:all
- (data-perms/set-table-permissions! group-id :perms/manage-table-metadata (zipmap tables (repeat :yes)))
+ (perms/set-table-permissions! group-id :perms/manage-table-metadata (zipmap tables (repeat :yes)))
:none
- (data-perms/set-table-permissions! group-id :perms/manage-table-metadata (zipmap tables (repeat :no))))))))
+ (perms/set-table-permissions! group-id :perms/manage-table-metadata (zipmap tables (repeat :no))))))))
(defn- update-db-level-metadata-permissions!
[group-id db-id new-db-perms]
@@ -232,10 +276,10 @@
(update-schema-level-metadata-permissions! group-id db-id schema schema-changes))
(case schemas
:all
- (data-perms/set-database-permission! group-id db-id :perms/manage-table-metadata :yes)
+ (perms/set-database-permission! group-id db-id :perms/manage-table-metadata :yes)
:none
- (data-perms/set-database-permission! group-id db-id :perms/manage-table-metadata :no)))))
+ (perms/set-database-permission! group-id db-id :perms/manage-table-metadata :no)))))
(defn- update-table-level-download-permissions!
[group-id db-id schema new-table-perms]
@@ -247,7 +291,7 @@
:limited :ten-thousand-rows
:none :no)))
(update-keys (fn [table-id] {:id table-id :db_id db-id :schema schema})))]
- (data-perms/set-table-permissions! group-id :perms/download-results new-table-perms)))
+ (perms/set-table-permissions! group-id :perms/download-results new-table-perms)))
(defn- update-schema-level-download-permissions!
[group-id db-id schema new-schema-perms]
@@ -257,13 +301,13 @@
(when (seq tables)
(case new-schema-perms
:full
- (data-perms/set-table-permissions! group-id :perms/download-results (zipmap tables (repeat :one-million-rows)))
+ (perms/set-table-permissions! group-id :perms/download-results (zipmap tables (repeat :one-million-rows)))
:limited
- (data-perms/set-table-permissions! group-id :perms/download-results (zipmap tables (repeat :ten-thousand-rows)))
+ (perms/set-table-permissions! group-id :perms/download-results (zipmap tables (repeat :ten-thousand-rows)))
:none
- (data-perms/set-table-permissions! group-id :perms/download-results (zipmap tables (repeat :no))))))))
+ (perms/set-table-permissions! group-id :perms/download-results (zipmap tables (repeat :no))))))))
(defn- update-db-level-download-permissions!
[group-id db-id new-db-perms]
@@ -273,24 +317,24 @@
(update-schema-level-download-permissions! group-id db-id schema schema-changes))
(case schemas
:full
- (data-perms/set-database-permission! group-id db-id :perms/download-results :one-million-rows)
+ (perms/set-database-permission! group-id db-id :perms/download-results :one-million-rows)
:limited
- (data-perms/set-database-permission! group-id db-id :perms/download-results :ten-thousand-rows)
+ (perms/set-database-permission! group-id db-id :perms/download-results :ten-thousand-rows)
:none
- (data-perms/set-database-permission! group-id db-id :perms/download-results :no)))))
+ (perms/set-database-permission! group-id db-id :perms/download-results :no)))))
(defn- update-details-perms!
[group-id db-id value]
- (data-perms/set-database-permission! group-id db-id :perms/manage-database value))
+ (perms/set-database-permission! group-id db-id :perms/manage-database value))
(defn- update-table-level-create-queries-permissions!
[group-id db-id schema new-table-perms]
(let [new-table-perms (update-keys
new-table-perms
(fn [table-id] {:id table-id :db_id db-id :schema schema}))]
- (data-perms/set-table-permissions! group-id :perms/create-queries new-table-perms)))
+ (perms/set-table-permissions! group-id :perms/create-queries new-table-perms)))
(defn- update-schema-level-create-queries-permissions!
[group-id db-id schema new-schema-perms]
@@ -298,7 +342,7 @@
(update-table-level-create-queries-permissions! group-id db-id schema new-schema-perms)
(let [tables (t2/select :model/Table :db_id db-id :schema (not-empty schema))]
(when (seq tables)
- (data-perms/set-table-permissions! group-id :perms/create-queries (zipmap tables (repeat new-schema-perms)))))))
+ (perms/set-table-permissions! group-id :perms/create-queries (zipmap tables (repeat new-schema-perms)))))))
(defn- update-db-level-create-queries-permissions!
[group-id db-id new-db-perms]
@@ -306,7 +350,7 @@
(doseq [[schema new-schema-perms] new-db-perms]
(update-schema-level-create-queries-permissions! group-id db-id schema new-schema-perms))
(when new-db-perms
- (data-perms/set-database-permission! group-id db-id :perms/create-queries new-db-perms))))
+ (perms/set-database-permission! group-id db-id :perms/create-queries new-db-perms))))
(defn- update-table-level-view-data-permissions!
[group-id db-id schema new-table-perms]
@@ -322,7 +366,7 @@
:sandboxed :unrestricted
:legacy-no-self-service :legacy-no-self-service
:blocked :blocked))))]
- (data-perms/set-table-permissions! group-id :perms/view-data new-table-perms)))
+ (perms/set-table-permissions! group-id :perms/view-data new-table-perms)))
(defn- update-schema-level-view-data-permissions!
[group-id db-id schema new-schema-perms]
@@ -330,7 +374,7 @@
(update-table-level-view-data-permissions! group-id db-id schema new-schema-perms)
(let [tables (t2/select :model/Table :db_id db-id :schema (not-empty schema))]
(when (seq tables)
- (data-perms/set-table-permissions! group-id :perms/view-data (zipmap tables (repeat new-schema-perms)))))))
+ (perms/set-table-permissions! group-id :perms/view-data (zipmap tables (repeat new-schema-perms)))))))
(defn- update-db-level-view-data-permissions!
[group-id db-id new-db-perms]
@@ -339,17 +383,17 @@
(update-schema-level-view-data-permissions! group-id db-id schema new-schema-perms))
(case new-db-perms
(:unrestricted :impersonated)
- (data-perms/set-database-permission! group-id db-id :perms/view-data :unrestricted)
+ (perms/set-database-permission! group-id db-id :perms/view-data :unrestricted)
;; Support setting legacy-no-self-service for testing purposes, though the UI shouldn't allow it normally
:legacy-no-self-service
- (data-perms/set-database-permission! group-id db-id :perms/view-data :legacy-no-self-service)
+ (perms/set-database-permission! group-id db-id :perms/view-data :legacy-no-self-service)
:blocked
(do
(when-not (premium-features/has-feature? :advanced-permissions)
(throw (ee-permissions-exception :blocked)))
- (data-perms/set-database-permission! group-id db-id :perms/view-data :blocked)))))
+ (perms/set-database-permission! group-id db-id :perms/view-data :blocked)))))
(defn check-audit-db-permissions
"Check that the changes coming in does not attempt to change audit database permission. Admins should
@@ -387,7 +431,7 @@
(mu/defn update-data-perms-graph!
"Takes an API-style perms graph and sets the permissions in the database accordingly. Additionally ensures
impersonations and sandboxes are consistent if necessary."
- ([graph-updates :- api.permission-graph/DataPermissionsGraph]
+ ([graph-updates :- ::permissions-rest.schema/data-permissions-graph]
(when (seq graph-updates)
(let [group-updates (:groups graph-updates)]
(check-audit-db-permissions group-updates)
diff --git a/src/metabase/permissions_rest/schema.clj b/src/metabase/permissions_rest/schema.clj
new file mode 100644
index 00000000000..e5c4bebcb32
--- /dev/null
+++ b/src/metabase/permissions_rest/schema.clj
@@ -0,0 +1,115 @@
+(ns metabase.permissions-rest.schema
+ (:require
+ [metabase.util.i18n :refer [trs]]
+ [metabase.util.malli.registry :as mr]))
+
+(defn- kw-int->int-decoder [kw-int]
+ (if (int? kw-int)
+ kw-int
+ (parse-long (name kw-int))))
+
+(mr/def ::decodable-keyword-int
+ "Integer malli schema that knows how to decode itself from the :123 sort of shape used in perm-graphs"
+ [:int {:decode/perm-graph kw-int->int-decoder}])
+
+(mr/def ::id ::decodable-keyword-int)
+(mr/def ::group-id ::decodable-keyword-int)
+
+(mr/def ::native
+ "native permissions"
+ [:maybe [:enum :write :none :full :limited]])
+
+(mr/def ::perms
+ "Perms that get reused for TablePerms and SchemaPerms"
+ [:enum
+ :all
+ :blocked
+ :full
+ :legacy-no-self-service
+ :limited
+ :no
+ :none
+ :query-builder
+ :sandboxed
+ :segmented
+ :unrestricted])
+
+(mr/def ::table-perms
+ [:or
+ [:ref ::perms]
+ [:map
+ [:read {:optional true} [:enum :all :none]]
+ [:query {:optional true} [:enum :all :none :segmented]]]])
+
+(mr/def ::schema-perms
+ [:or
+ [:ref ::perms]
+ [:map-of [:ref ::id] [:ref ::table-perms]]])
+
+(mr/def ::schema-graph
+ [:map-of
+ [:string {:decode/perm-graph name}]
+ [:ref ::schema-perms]])
+
+(mr/def ::schemas
+ [:or
+ [:enum
+ :all
+ :segmented
+ :none
+ :block
+ :blocked
+ :full
+ :limited
+ :impersonated
+ :unrestricted
+ :sandboxed
+ :legacy-no-self-service
+ :query-builder-and-native
+ :query-builder
+ :no]
+ [:ref ::schema-graph]])
+
+(mr/def ::data-perms
+ [:map
+ [:native {:optional true} [:ref ::native]]
+ [:schemas {:optional true} [:ref ::schemas]]])
+
+(mr/def ::strict-data-perms
+ "Data perms that care about how view-data and make-queries are related to one another.
+ If you have write access for native queries, you must have data access to all schemas."
+ [:and
+ [:ref ::data-perms]
+ [:fn {:error/fn (fn [_ _] (trs "Invalid DB permissions: If you have write access for native queries, you must have data access to all schemas."))}
+ (fn [{:keys [native schemas]}]
+ (not (and (= native :write) schemas (not (#{:all :impersonated} schemas)))))]])
+
+(mr/def ::strict-db-graph
+ "like db-graph, but with added validations:
+ - Ensures 'view-data' is not 'blocked' if 'create-queries' is 'query-builder-and-native'."
+ [:map-of
+ [:ref ::id]
+ [:and
+ [:map
+ [:view-data {:optional true} [:ref ::schemas]]
+ [:create-queries {:optional true} [:ref ::schemas]]
+ [:data {:optional true} [:ref ::strict-data-perms]]
+ [:download {:optional true} [:ref ::strict-data-perms]]
+ [:data-model {:optional true} [:ref ::strict-data-perms]]
+ [:details {:optional true} [:enum :yes :no]]]
+ [:fn {:error/fn (fn [_ _] (trs "Invalid DB permissions: If you have write access for native queries, you must have data access to all schemas."))}
+ (fn [db-entry]
+ (let [{:keys [create-queries view-data]} db-entry]
+ (not (and (= create-queries :query-builder-and-native) (= view-data :blocked)))))]]])
+
+(mr/def ::data-permissions-graph
+ "Used to transform, and verify data permissions graph"
+ [:map
+ [:groups [:map-of [:ref ::group-id] [:maybe [:ref ::strict-db-graph]]]]])
+
+(mr/def ::strict-api-permissions-graph
+ "Top level strict data graph schema expected over the API. Includes revision ID for avoiding concurrent updates."
+ [:map
+ [:revision {:optional true} [:maybe int?]]
+ [:force {:optional true} [:maybe boolean?]]
+ [:groups [:map-of [:ref ::group-id] [:maybe [:ref ::strict-db-graph]]]]])
diff --git a/src/metabase/server/middleware/session.clj b/src/metabase/server/middleware/session.clj
index b880f2cdfe1..075993db0a2 100644
--- a/src/metabase/server/middleware/session.clj
+++ b/src/metabase/server/middleware/session.clj
@@ -23,7 +23,7 @@
[metabase.api-keys.schema :as api-keys.schema]
[metabase.app-db.core :as mdb]
[metabase.config.core :as config]
- [metabase.core.initialization-status :as init-status]
+ [metabase.initialization-status.core :as init-status]
[metabase.premium-features.core :as premium-features]
[metabase.request.core :as request]
[metabase.request.schema :as request.schema]
diff --git a/src/metabase/server/routes.clj b/src/metabase/server/routes.clj
index a548fb3b0c6..d378cca5c85 100644
--- a/src/metabase/server/routes.clj
+++ b/src/metabase/server/routes.clj
@@ -7,7 +7,7 @@
[metabase.api.macros :as api.macros]
[metabase.app-db.core :as mdb]
[metabase.appearance.core :as appearance]
- [metabase.core.initialization-status :as init-status]
+ [metabase.initialization-status.core :as init-status]
[metabase.query-processor.schema :as qp.schema]
[metabase.server.auth-wrapper :as auth-wrapper]
[metabase.server.middleware.embedding-sdk-bundle :as mw.embedding-sdk-bundle]
diff --git a/src/metabase/server/routes/index.clj b/src/metabase/server/routes/index.clj
index fd4a1bba38b..2d88ccdc12e 100644
--- a/src/metabase/server/routes/index.clj
+++ b/src/metabase/server/routes/index.clj
@@ -8,7 +8,7 @@
[hiccup.util]
[metabase.appearance.core :as appearance]
[metabase.config.core :as config]
- [metabase.core.initialization-status :as init-status]
+ [metabase.initialization-status.core :as init-status]
[metabase.settings.core :as setting]
[metabase.system.core :as system]
[metabase.util.embed :as embed]
diff --git a/src/metabase/settings/core.clj b/src/metabase/settings/core.clj
index 9b9399fe75d..9a1c456e4bf 100644
--- a/src/metabase/settings/core.clj
+++ b/src/metabase/settings/core.clj
@@ -110,6 +110,7 @@
set-value-of-type!
setting-env-map-name
string->boolean
+ user-facing-value
user-readable-values-map
uuid-nonce-base
validate-settings-formatting!
@@ -163,3 +164,9 @@
[new-values & body]
`(binding [metabase.settings.models.setting/*user-local-values* ~new-values]
~@body))
+
+(defmacro with-enforced-setting-access-checks
+ "Enable checks on Setting access."
+ [& body]
+ `(binding [metabase.settings.models.setting/*enforce-setting-access-checks* true]
+ ~@body))
diff --git a/src/metabase/settings/api.clj b/src/metabase/settings_rest/api.clj
similarity index 94%
rename from src/metabase/settings/api.clj
rename to src/metabase/settings_rest/api.clj
index cb56fc5ee63..23f2b1c9b94 100644
--- a/src/metabase/settings/api.clj
+++ b/src/metabase/settings_rest/api.clj
@@ -1,16 +1,16 @@
-(ns metabase.settings.api
+(ns metabase.settings-rest.api
"/api/setting endpoints"
(:require
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.permissions.core :as perms]
- [metabase.settings.models.setting :as setting]
+ [metabase.settings.core :as setting]
[metabase.util :as u]))
(defn- do-with-setting-access-control
[thunk]
(try
- (binding [setting/*enforce-setting-access-checks* true]
+ (setting/with-enforced-setting-access-checks
(thunk))
(catch clojure.lang.ExceptionInfo e
;; Throw a generic 403 for non-admins, so as to not reveal details about settings
diff --git a/src/metabase/startup/core.clj b/src/metabase/startup/core.clj
index 01f2dc708be..6a4a7335a4a 100644
--- a/src/metabase/startup/core.clj
+++ b/src/metabase/startup/core.clj
@@ -1,6 +1,5 @@
(ns metabase.startup.core
"Defines the `def-startup-logic!` multimethod, which is used to run initialization logic when the server starts up."
-
(:require
[metabase.util :as u]
[metabase.util.log :as log]))
diff --git a/test/metabase/dashboards/api_test.clj b/test/metabase/dashboards/api_test.clj
index 85362269f37..e019364be9b 100644
--- a/test/metabase/dashboards/api_test.clj
+++ b/test/metabase/dashboards/api_test.clj
@@ -26,8 +26,8 @@
[metabase.parameters.chain-filter :as chain-filter]
[metabase.parameters.chain-filter-test :as chain-filter-test]
[metabase.parameters.dashboard :as parameters.dashboard]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.data-permissions :as data-perms]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.permissions :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.permissions.test-util :as perms.test-util]
diff --git a/test/metabase/permissions/models/data_permissions_test.clj b/test/metabase/permissions/models/data_permissions_test.clj
index c293e29e784..83e392327f6 100644
--- a/test/metabase/permissions/models/data_permissions_test.clj
+++ b/test/metabase/permissions/models/data_permissions_test.clj
@@ -3,6 +3,7 @@
[clojure.test :refer :all]
[metabase.api.common :as api]
[metabase.app-db.schema-migrations-test.impl :as impl]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.data-permissions :as data-perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
@@ -378,7 +379,7 @@
{database-id-1 {:perms/view-data
{"PUBLIC"
{table-id-1 :legacy-no-self-service}}}}}
- (data-perms/data-permissions-graph))))
+ (data-perms.graph/data-permissions-graph))))
(testing "Additional data permissions are included when set"
(data-perms/set-table-permission! group-id-1 table-id-3 :perms/download-results :one-million-rows)
@@ -393,7 +394,7 @@
{""
{table-id-3 :one-million-rows}}
:perms/manage-database :yes}}}
- (data-perms/data-permissions-graph))))
+ (data-perms.graph/data-permissions-graph))))
(testing "Data permissions graph can be filtered by group ID, database ID, and permission type"
(is (= {group-id-1
@@ -413,7 +414,7 @@
{table-id-3 :one-million-rows}}
:perms/manage-database :yes
:perms/create-queries :no}}}
- (data-perms/data-permissions-graph :group-id group-id-1)))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1)))
(is (= {group-id-1
{database-id-1 {:perms/view-data
@@ -424,17 +425,17 @@
:perms/manage-table-metadata
{"PUBLIC"
{table-id-1 :yes}}}}}
- (data-perms/data-permissions-graph :group-id group-id-1
- :db-id database-id-1)))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1
+ :db-id database-id-1)))
(is (= {group-id-1
{database-id-1 {:perms/view-data
{"PUBLIC"
{table-id-1 :unrestricted
table-id-2 :legacy-no-self-service}}}}}
- (data-perms/data-permissions-graph :group-id group-id-1
- :db-id database-id-1
- :perm-type :perms/view-data)))))))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1
+ :db-id database-id-1
+ :perm-type :perms/view-data)))))))
(deftest most-restrictive-per-group-works
(is (= #{:query-builder-and-native}
diff --git a/test/metabase/permissions/models/permissions_group_test.clj b/test/metabase/permissions/models/permissions_group_test.clj
index 3b881681f94..f329e34d14e 100644
--- a/test/metabase/permissions/models/permissions_group_test.clj
+++ b/test/metabase/permissions/models/permissions_group_test.clj
@@ -3,10 +3,9 @@
[clojure.test :refer :all]
[metabase.config.core :as config]
[metabase.models.interface :as mi]
- [metabase.permissions.api-test-util :as perm-test-util]
+ [metabase.permissions-rest.api-test-util :as perm-test-util]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.core :as perms]
- [metabase.permissions.models.data-permissions :as data-perms]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
@@ -158,7 +157,7 @@
:perms/download-results :one-million-rows
:perms/manage-table-metadata :no
:perms/manage-database :no}}}
- (data-perms/data-permissions-graph :group-id group-id :db-id db-id))))))
+ (data-perms.graph/data-permissions-graph :group-id group-id :db-id db-id))))))
(mt/with-temp [:model/Database {db-id :id} {}]
(mt/with-no-data-perms-for-all-users!
@@ -171,7 +170,7 @@
:perms/download-results :no
:perms/manage-table-metadata :no
:perms/manage-database :no}}}
- (data-perms/data-permissions-graph :group-id group-id :db-id db-id)))))))))
+ (data-perms.graph/data-permissions-graph :group-id group-id :db-id db-id)))))))))
(deftest hydrate-members-tests
(mt/with-temp [:model/PermissionsGroup {group-id-1 :id} {}
diff --git a/test/metabase/permissions/test_util.clj b/test/metabase/permissions/test_util.clj
index a4b7cc09e88..b5585b4aebc 100644
--- a/test/metabase/permissions/test_util.clj
+++ b/test/metabase/permissions/test_util.clj
@@ -4,6 +4,7 @@
[metabase.permissions.models.data-permissions :as data-perms]
[metabase.permissions.models.permissions :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
+ [metabase.permissions.schema :as permissions.schema]
[metabase.test.data :as data]
[metabase.test.initialize :as initialize]
[metabase.util :as u]
@@ -83,7 +84,7 @@
;; force creation of test-data if it is not already created
(data/db)
(with-restored-data-perms-for-group! (u/the-id (perms-group/all-users))
- (doseq [[perm-type _] data-perms/Permissions
+ (doseq [[perm-type _] permissions.schema/data-permissions
db-id (t2/select-pks-set :model/Database)]
(data-perms/set-database-permission! (perms-group/all-users)
db-id
@@ -104,7 +105,7 @@
;; make sure app DB is set up and test users are created
(initialize/initialize-if-needed! :db :test-users)
(with-restored-data-perms-for-group! (u/the-id (perms-group/all-users))
- (doseq [[perm-type _] data-perms/Permissions
+ (doseq [[perm-type _] permissions.schema/data-permissions
db-id (t2/select-pks-set :model/Database)]
(data-perms/set-database-permission! (perms-group/all-users)
db-id
diff --git a/test/metabase/permissions/api_test.clj b/test/metabase/permissions_rest/api_test.clj
similarity index 99%
rename from test/metabase/permissions/api_test.clj
rename to test/metabase/permissions_rest/api_test.clj
index 356deced1a1..b7d2046fd33 100644
--- a/test/metabase/permissions/api_test.clj
+++ b/test/metabase/permissions_rest/api_test.clj
@@ -1,14 +1,14 @@
-(ns metabase.permissions.api-test
+(ns metabase.permissions-rest.api-test
"Tests for `/api/permissions` endpoints."
(:require
[clojure.test :refer :all]
[medley.core :as m]
[metabase.config.core :as config]
- [metabase.permissions.api :as api.permissions]
- [metabase.permissions.api-test-util :as perm-test-util]
+ [metabase.permissions-rest.api :as api.permissions]
+ [metabase.permissions-rest.api-test-util :as perm-test-util]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.core :as perms]
[metabase.permissions.models.data-permissions :as data-perms]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
diff --git a/test/metabase/permissions/api_test_util.clj b/test/metabase/permissions_rest/api_test_util.clj
similarity index 53%
rename from test/metabase/permissions/api_test_util.clj
rename to test/metabase/permissions_rest/api_test_util.clj
index 6495f4f0b7f..26f9494d819 100644
--- a/test/metabase/permissions/api_test_util.clj
+++ b/test/metabase/permissions_rest/api_test_util.clj
@@ -1,12 +1,12 @@
-(ns metabase.permissions.api-test-util
+(ns metabase.permissions-rest.api-test-util
(:require
[malli.core :as mc]
[malli.transform :as mtx]
- [metabase.permissions.api.permission-graph :as api.permission-graph]
+ [metabase.permissions-rest.schema :as permissions-rest.schema]
[metabase.util.malli.registry :as mr]))
-(def ^:private graph-output-schema
- [:map-of @#'api.permission-graph/GroupId @#'api.permission-graph/StrictDbGraph])
+(mr/def ::graph-output
+ [:map-of ::permissions-rest.schema/group-id ::permissions-rest.schema/strict-db-graph])
(defn- decode-and-validate [schema value]
(mr/validate schema (mc/decode schema value (mtx/string-transformer))))
@@ -14,4 +14,4 @@
(defn validate-graph-api-groups
"Handles string->keyword transformations in DataPerms"
[graph]
- (decode-and-validate graph-output-schema graph))
+ (decode-and-validate ::graph-output graph))
diff --git a/test/metabase/permissions/models/data_permissions/graph_test.clj b/test/metabase/permissions_rest/data_permissions/graph_test.clj
similarity index 97%
rename from test/metabase/permissions/models/data_permissions/graph_test.clj
rename to test/metabase/permissions_rest/data_permissions/graph_test.clj
index 55b4e9e720b..d0a7420279c 100644
--- a/test/metabase/permissions/models/data_permissions/graph_test.clj
+++ b/test/metabase/permissions_rest/data_permissions/graph_test.clj
@@ -1,9 +1,9 @@
-(ns metabase.permissions.models.data-permissions.graph-test
+(ns metabase.permissions-rest.data-permissions.graph-test
(:require
[clojure.test :refer :all]
[metabase.audit-app.core :as audit]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.data-permissions :as data-perms]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.util :as u]
@@ -21,7 +21,7 @@
(are [api-graph db-graph] (= db-graph
(do
(data-perms.graph/update-data-perms-graph! {:groups api-graph})
- (data-perms/data-permissions-graph :group-id group-id-1)))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1)))
{group-id-1
{database-id-1
{:view-data :unrestricted}}}
@@ -65,7 +65,7 @@
;; Clear default perms for the group
(t2/delete! :model/DataPermissions :group_id group-id-1)
(data-perms.graph/update-data-perms-graph! {:groups api-graph})
- (data-perms/data-permissions-graph :group-id group-id-1)))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1)))
{group-id-1
{database-id-1
{:create-queries :query-builder-and-native}}}
@@ -135,7 +135,7 @@
(are [api-graph db-graph] (= db-graph
(do
(data-perms.graph/update-data-perms-graph! {:groups api-graph})
- (data-perms/data-permissions-graph :group-id group-id-1)))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1)))
;; Setting granular data access permissions
{group-id-1
{database-id-1
@@ -204,7 +204,7 @@
(are [api-graph db-graph] (= db-graph
(do
(data-perms.graph/update-data-perms-graph! {:groups api-graph})
- (data-perms/data-permissions-graph :group-id group-id-1)))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1)))
;; Setting granular download permissions
{group-id-1
{database-id-1
@@ -269,7 +269,7 @@
(are [api-graph db-graph] (= db-graph
(do
(data-perms.graph/update-data-perms-graph! {:groups api-graph})
- (data-perms/data-permissions-graph :group-id group-id-1)))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1)))
;; Setting granular data model editing permissions
{group-id-1
{database-id-1
@@ -328,7 +328,7 @@
(are [api-graph db-graph] (= db-graph
(do
(data-perms.graph/update-data-perms-graph! {:groups api-graph})
- (data-perms/data-permissions-graph :group-id group-id-1)))
+ (data-perms.graph/data-permissions-graph :group-id group-id-1)))
;; Granting permission to edit database details
{group-id-1
{database-id-1
diff --git a/test/metabase/server/middleware/session_test.clj b/test/metabase/server/middleware/session_test.clj
index b5642e9120a..5616cde92c5 100644
--- a/test/metabase/server/middleware/session_test.clj
+++ b/test/metabase/server/middleware/session_test.clj
@@ -8,7 +8,7 @@
[metabase.api.common :refer [*current-user* *current-user-id* *is-group-manager?* *is-superuser?*]]
[metabase.app-db.core :as mdb]
[metabase.config.core :as config]
- [metabase.core.initialization-status :as init-status]
+ [metabase.initialization-status.core :as init-status]
[metabase.premium-features.core :as premium-features]
[metabase.request.core :as request]
[metabase.server.middleware.session :as mw.session]
diff --git a/test/metabase/settings/api_test.clj b/test/metabase/settings_rest/api_test.clj
similarity index 99%
rename from test/metabase/settings/api_test.clj
rename to test/metabase/settings_rest/api_test.clj
index a33fd00dfef..758ccb9fc67 100644
--- a/test/metabase/settings/api_test.clj
+++ b/test/metabase/settings_rest/api_test.clj
@@ -1,4 +1,4 @@
-(ns metabase.settings.api-test
+(ns metabase.settings-rest.api-test
(:require
[clojure.string :as str]
[clojure.test :refer :all]
diff --git a/test/metabase/test/initialize/web_server.clj b/test/metabase/test/initialize/web_server.clj
index e1f83fce835..72902cfa7e4 100644
--- a/test/metabase/test/initialize/web_server.clj
+++ b/test/metabase/test/initialize/web_server.clj
@@ -1,7 +1,7 @@
(ns metabase.test.initialize.web-server
(:require
[metabase.config.core :as config]
- [metabase.core.initialization-status :as init-status]
+ [metabase.initialization-status.core :as init-status]
[metabase.server.core :as server]
[metabase.server.test-handler :as server.test-handler]
[metabase.settings.core :as setting]
diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.clj
index e5dc2e85c54..e1a0cb0fb2d 100644
--- a/test/metabase/test/util.clj
+++ b/test/metabase/test/util.clj
@@ -21,8 +21,8 @@
[metabase.config.core :as config]
[metabase.content-verification.models.moderation-review :as moderation-review]
[metabase.lib.core :as lib]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.core :as perms]
- [metabase.permissions.models.data-permissions.graph :as data-perms.graph]
[metabase.permissions.test-util :as perms.test-util]
[metabase.premium-features.test-util :as premium-features.test-util]
[metabase.query-processor.util :as qp.util]
diff --git a/test/metabase/warehouse_schema/models/table_test.clj b/test/metabase/warehouse_schema/models/table_test.clj
index 1f08caf5226..6990016318a 100644
--- a/test/metabase/warehouse_schema/models/table_test.clj
+++ b/test/metabase/warehouse_schema/models/table_test.clj
@@ -4,6 +4,7 @@
[clojure.test :refer :all]
[metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
+ [metabase.permissions-rest.data-permissions.graph :as data-perms.graph]
[metabase.permissions.core :as perms]
[metabase.permissions.models.data-permissions :as data-perms]
[metabase.permissions.models.permissions-group :as perms-group]
@@ -106,7 +107,7 @@
:perms/download-results :one-million-rows
:perms/manage-table-metadata :no
:perms/manage-database :no}}}
- (data-perms/data-permissions-graph :group-id all-users-group-id :db-id db-id))))
+ (data-perms.graph/data-permissions-graph :group-id all-users-group-id :db-id db-id))))
;; A new group starts with the same perms as All Users
(is (partial=
@@ -117,7 +118,7 @@
:perms/download-results :one-million-rows
:perms/manage-table-metadata :no
:perms/manage-database :no}}}
- (data-perms/data-permissions-graph :group-id group-id :db-id db-id)))
+ (data-perms.graph/data-permissions-graph :group-id group-id :db-id db-id)))
(testing "A new table has appropriate defaults, when perms are already set granularly for the DB"
(data-perms/set-table-permission! group-id table-id-1 :perms/create-queries :no)
@@ -145,7 +146,7 @@
table-id-2 :yes
table-id-3 :no}}
:perms/manage-database :no}}}
- (data-perms/data-permissions-graph :group-id group-id :db-id db-id)))))))))
+ (data-perms.graph/data-permissions-graph :group-id group-id :db-id db-id)))))))))
(deftest cleanup-permissions-after-delete-table-test
(mt/with-temp