The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
(ns metabase-enterprise.impersonation.util
(:require
[metabase-enterprise.impersonation.db :as impersonation.db]
[metabase-enterprise.impersonation.driver :as impersonation.driver]
[metabase.api.common :refer [*current-user-id* *is-superuser?*]]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.util.i18n :refer [tru]]))
(defenterprise impersonation-enforced-for-db?
"Returns a boolean if the current user has a connection impersonation policy which should be enforced for the provided
database, even while the `:advanced-permissions` feature is unavailable, so callers fail closed. Will throw an error
if [[api/*current-user-id*]] is not bound."
:feature :none
[db-or-id]
(boolean
(when-not *is-superuser?*
(if *current-user-id*
(seq (impersonation.driver/enforced-impersonations-for-db db-or-id))
;; If no *current-user-id* is bound we can't check for impersonations, so we should throw in this case to avoid
;; returning `false` for users who should actually be using impersonation.
(throw (ex-info (str (tru "No current user found"))
{:status-code 403}))))))
;; TODO: this function should only return true if an impersonation policy is enforced for the user
(defenterprise impersonated-user?
"Returns a boolean if the current user is in a group that has a connection impersonation in place for any database.
Note: this function does not check whether the impersonation is *enforced* for the current user, since another group's
permissions may supersede it. Will throw an error if [[api/*current-user-id*]] is not bound. Uses `:feature :none` so
callers can recognize an impersonated user even when the `:advanced-permissions` feature is temporarily unavailable
(e.g. during a transient token-check failure) and fail closed instead of leaking data."
:feature :none
[]
(boolean
(when-not *is-superuser?*
(if *current-user-id*
(let [group-ids (impersonation.db/group-ids-for-user *current-user-id*)]
(seq
(when (seq group-ids)
(impersonation.db/impersonations-for-groups group-ids))))
;; If no *current-user-id* is bound we can't check for impersonations, so we should throw in this case to avoid
;; returning `false` for users who should actually be using impersonation.
(throw (ex-info (str (tru "No current user found"))
{:status-code 403}))))))