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
Reenable deactivated tenants when autoprovisioning (#67947)
John Swansonbd14e1497477
diff --git a/enterprise/backend/src/metabase_enterprise/sso/providers/jwt.clj b/enterprise/backend/src/metabase_enterprise/sso/providers/jwt.cljindex 32c04e3dfd4..b33e8007639 100644--- a/enterprise/backend/src/metabase_enterprise/sso/providers/jwt.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/providers/jwt.clj@@ -134,10 +134,14 @@;; Authentication succeeded - check account creation policy;; TODO(edpaget): 2025/11/11 this should return an error condition instead of throwing:else- (do (when-not (and (:user request) (get-in request [:user :is_active]))- (sso-utils/check-user-provisioning :jwt))- ;; If the user was deactivated but user provisioning is allowed reactive the user- (next-method provider (assoc-in request [:user-data :is_active] true)))))+ (let [provisioning-enabled? (sso-settings/jwt-user-provisioning-enabled?)]+ (when-not (and (:user request) (get-in request [:user :is_active]))+ (sso-utils/check-user-provisioning :jwt))+ ;; If the user was deactivated but user provisioning is allowed reactive the user+ ;; Pass provisioning status for tenant reactivation logic+ (next-method provider (-> request+ (assoc-in [:user-data :is_active] true)+ (assoc :user-provisioning-enabled? provisioning-enabled?))))))(defn- group-names->ids"Translate a user's group names to a set of MB group IDs using the configured mappings"diff --git a/enterprise/backend/src/metabase_enterprise/sso/providers/saml.clj b/enterprise/backend/src/metabase_enterprise/sso/providers/saml.cljindex 099084232c1..6be6b046b08 100644--- a/enterprise/backend/src/metabase_enterprise/sso/providers/saml.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/providers/saml.clj@@ -163,10 +163,14 @@;; Authentication succeeded - check account creation policy;; TODO(edpaget): 2025/11/11 this should return an error condition instead of throwing:else- (do (when-not (and (:user request) (get-in request [:user :is_active]))- (sso-utils/check-user-provisioning :saml))- ;; If the user was deactivated but user provisioning is allowed reactive the user- (next-method provider (assoc-in request [:user-data :is_active] true)))))+ (let [provisioning-enabled? (sso-settings/saml-user-provisioning-enabled?)]+ (when-not (and (:user request) (get-in request [:user :is_active]))+ (sso-utils/check-user-provisioning :saml))+ ;; If the user was deactivated but user provisioning is allowed reactive the user+ ;; Pass provisioning status for tenant reactivation logic+ (next-method provider (-> request+ (assoc-in [:user-data :is_active] true)+ (assoc :user-provisioning-enabled? provisioning-enabled?))))))(defn- group-names->ids"Translate a user's group names to a set of MB group IDs using the configured mappings"diff --git a/enterprise/backend/src/metabase_enterprise/tenants/api.clj b/enterprise/backend/src/metabase_enterprise/tenants/api.cljindex ce4ebf3bc3b..ec271ebe183 100644--- a/enterprise/backend/src/metabase_enterprise/tenants/api.clj+++ b/enterprise/backend/src/metabase_enterprise/tenants/api.clj@@ -81,7 +81,8 @@[:attributes {:optional true} [:maybe tenant/Attributes]][:is_active {:optional true} [:maybe ms/BooleanValue]]])-(mu/defn- update-tenant!+(mu/defn update-tenant!+ "Updates a tenant, publishing any necessary events after doing so"[tenant-id :- ms/PositiveInt{:keys [is_active] :as tenant} :- UpdateTenantArguments](when (false? is_active)diff --git a/enterprise/backend/src/metabase_enterprise/tenants/auth_provider.clj b/enterprise/backend/src/metabase_enterprise/tenants/auth_provider.cljindex 2860d158e15..78e02d316d1 100644--- a/enterprise/backend/src/metabase_enterprise/tenants/auth_provider.clj+++ b/enterprise/backend/src/metabase_enterprise/tenants/auth_provider.clj@@ -37,26 +37,37 @@:user/tenant-slug (t2/select-one-fn :slug :model/Tenant :id tenant_id):tenant-slug/tenant-id (:id existing-tenant):tenant-slug/slug (:slug existing-tenant)- :status-code 403}))+ :status-code 403})))))++(defn- validate-with-tenants-disabled! [{:keys [user tenant-slug]}]+ (when (or (:tenant_id user) (not-empty tenant-slug))+ (throw (ex-info "Tenants and tenant users are disabled." {:status-code 403}))))- (and existing-tenant- (not (:is_active existing-tenant)))+(defn- reactivate-tenant!+ "Reactivate a disabled tenant and any users that were deactivated along with it."+ [{tenant-id :id}]+ (api.tenants/update-tenant! tenant-id {:is_active true}))++(defn- maybe-reactivate-tenant!+ "If the tenant is disabled and user provisioning is enabled,+ reactivate the tenant. Otherwise throw an error if the tenant is disabled."+ [existing-tenant user-provisioning-enabled?]+ (when (and existing-tenant (not (:is_active existing-tenant)))+ (if user-provisioning-enabled?+ (reactivate-tenant! existing-tenant)(throw (ex-info "Tenant is not active"{:tenant-slug/tenant-id (:id existing-tenant):tenant-slug/slug (:slug existing-tenant):tenant-slug/is-active (:is_active existing-tenant):status-code 403})))))-(defn- validate-with-tenants-disabled! [{:keys [user tenant-slug]}]- (when (or (:tenant_id user) (not-empty tenant-slug))- (throw (ex-info "Tenants and tenant users are disabled." {:status-code 403}))))-(defn- create-tenant-if-not-exists!- [{:as request :keys [user tenant-slug]} existing-tenant]+ [{:as request :keys [user tenant-slug user-provisioning-enabled?]} existing-tenant](if-not (setting/get :use-tenants)(do (validate-with-tenants-disabled! request)request)(do (validate-user-and-tenant-slug! user existing-tenant (boolean tenant-slug))+ (maybe-reactivate-tenant! existing-tenant user-provisioning-enabled?)(cond-> request(boolean tenant-slug)(assoc-in [:user-data :tenant_id]diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.cljindex 24e9a0076c5..e8e993770a1 100644--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj@@ -726,7 +726,7 @@(testing "their name is unchanged"(is (= [first-name last-name] (t2/select-one-fn (juxt :first_name :last_name) :model/User :email email))))))))))))-(deftest a-user-cannot-log-in-with-a-deactivated-tenant+(deftest a-user-can-log-in-with-a-deactivated-tenant(with-jwt-default-setup!(mt/with-additional-premium-features #{:tenants}(mt/with-temporary-setting-values [use-tenants true]@@ -736,9 +736,9 @@:is_active false}:model/User {existing-email :email} {:tenant_id tenant-id}](mt/with-temporary-setting-values [use-tenants true]- (testing "a new user fails to log in with correct error message"+ (testing "a new user can log into a deactivated tenant"(mt/with-model-cleanup [:model/User]- (let [response (client/client-real-response :get 403 "/auth/sso"+ (let [response (client/client-real-response :get 302 "/auth/sso"{:request-options {:redirect-strategy :none}}:return_to default-redirect-uri:jwt@@ -748,10 +748,10 @@:first_name "New":last_name "User"}default-jwt-secret))]- (is (not (saml-test/successful-login? response)))- (is (str/includes? (:body response) "Tenant is not active")))))+ (is (saml-test/successful-login? response))+ (is (t2/select-one-fn :is_active :model/Tenant :id tenant-id)))))(testing "an existing user also fails to log in with correct error message"- (let [response (client/client-real-response :get 403 "/auth/sso"+ (let [response (client/client-real-response :get 302 "/auth/sso"{:request-options {:redirect-strategy :none}}:return_to default-redirect-uri:jwt@@ -761,8 +761,48 @@:first_name "Existing":last_name "User"}default-jwt-secret))]- (is (not (saml-test/successful-login? response)))- (is (str/includes? (:body response) "Tenant is not active"))))))))))+ (is (saml-test/successful-login? response))+ (is (t2/select-one-fn :is_active :model/Tenant :id tenant-id))))))))))++(deftest users-cannot-log-into-deactivated-tenant-with-provisioning-disabled+ (with-jwt-default-setup!+ (mt/with-additional-premium-features #{:tenants}+ (mt/with-temporary-setting-values [use-tenants true]+ (mt/with-temp [:model/PermissionsGroup _my-group {:name (str ::my-group)}+ :model/Tenant {tenant-id :id} {:slug "tenant-mctenantson"+ :name "Tenant McTenantson"+ :is_active false}+ :model/User {existing-email :email} {:tenant_id tenant-id}]+ (with-redefs [sso-settings/jwt-user-provisioning-enabled? (constantly false)]+ (testing "with user provisioning turned off"+ (testing "a new user cannot log into a deactivated tenant, and the tenant doesn't get activated"+ (mt/with-model-cleanup [:model/User]+ (let [response (client/client-real-response :get 401 "/auth/sso"+ {:request-options {:redirect-strategy :none}}+ :return_to default-redirect-uri+ :jwt+ (jwt/sign+ {:email "[email protected]"+ "@tenant" "tenant-mctenantson"+ :first_name "New"+ :last_name "User"}+ default-jwt-secret))]+ (is (not (saml-test/successful-login? response)))+ (is (not (t2/select-one-fn :is_active :model/Tenant :id tenant-id))))))+ (testing "an existing user cannot log into a deactivated tenant"+ (let [response (client/client-real-response :get 403 "/auth/sso"+ {:request-options {:redirect-strategy :none}}+ :return_to default-redirect-uri+ :jwt+ (jwt/sign+ {:email existing-email+ "@tenant" "tenant-mctenantson"+ :first_name "Existing"+ :last_name "User"}+ default-jwt-secret))]+ (is (not (saml-test/successful-login? response)))+ (is (str/includes? (:body response) "Tenant is not active"))+ (is (not (t2/select-one-fn :is_active :model/Tenant :id tenant-id))))))))))));; not yet - we need to figure out what to do about personal collections here first#_(deftest existing-users-can-be-updated-with-a-tenantdiff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.cljindex 9eaf916be39..b1fbbb26c1f 100644--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj@@ -994,7 +994,7 @@(is (successful-login? response))(is (nil? (t2/select-one-fn :tenant_id :model/User :email "[email protected]"))))))))))))))-(deftest a-user-cannot-log-in-with-a-deactivated-tenant-via-saml+(deftest a-user-can-log-into-deactivated-tenant-via-saml(with-other-sso-types-disabled!(with-saml-default-setup!(mt/with-additional-premium-features #{:tenants}@@ -1010,8 +1010,8 @@(mt/with-model-cleanup [:model/User](let [req-options (saml-post-request-options (new-user-with-tenant-saml-test-response)default-redirect-uri)- response (client/client-real-response :post 401 "/auth/sso" req-options)]- (is (not (successful-login? response))))))+ response (client/client-real-response :post 302 "/auth/sso" req-options)]+ (is (successful-login? response)))))(testing "an existing user also fails to log in"(with-redefs [saml.p/saml-response->attributes(fn [_]@@ -1019,8 +1019,37 @@"tenant" "tenant-mctenantson"})](let [req-options (saml-post-request-options (new-user-with-tenant-saml-test-response)default-redirect-uri)- response (client/client-real-response :post 401 "/auth/sso" req-options)]- (is (not (successful-login? response))))))))))))))+ response (client/client-real-response :post 302 "/auth/sso" req-options)]+ (is (successful-login? response)))))))))))))++(deftest a-user-can-not-log-into-deactivated-tenant-via-saml-if-provisioning-is-off+ (with-other-sso-types-disabled!+ (with-saml-default-setup!+ (mt/with-additional-premium-features #{:tenants}+ (mt/with-temporary-setting-values [use-tenants true+ saml-attribute-tenant "tenant"]+ (mt/with-temp [:model/Tenant {tenant-id :id} {:slug "tenant-mctenantson"+ :name "Tenant McTenantson"+ :is_active false}+ :model/User {existing-email :email} {:tenant_id tenant-id}]+ (with-redefs [sso-settings/saml-user-provisioning-enabled? (constantly false)]+ (do-with-some-validators-disabled!+ (fn []+ (testing "a new user fails to log in"+ (mt/with-model-cleanup [:model/User]+ (let [req-options (saml-post-request-options (new-user-with-tenant-saml-test-response)+ default-redirect-uri)+ response (client/client-real-response :post 401 "/auth/sso" req-options)]+ (is (not (successful-login? response))))))+ (testing "an existing user also fails to log in"+ (with-redefs [saml.p/saml-response->attributes+ (fn [_]+ {"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress" existing-email+ "tenant" "tenant-mctenantson"})]+ (let [req-options (saml-post-request-options (new-user-with-tenant-saml-test-response)+ default-redirect-uri)+ response (client/client-real-response :post 401 "/auth/sso" req-options)]+ (is (not (successful-login? response)))))))))))))))(deftest a-tenant-cannot-be-changed-once-set-via-saml(with-other-sso-types-disabled!