Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add archive mode for frozen sites with login (#42934)

Bannon Tanner0e4f5ade3cff
diff --git a/app/controllers/admin/backups_controller.rb b/app/controllers/admin/backups_controller.rb
index a3defb577ae..18b7c32a5e1 100644
--- a/app/controllers/admin/backups_controller.rb
+++ b/app/controllers/admin/backups_controller.rb
@@ -24,6 +24,21 @@ class Admin::BackupsController < Admin::AdminController
:abort_multipart,
:batch_presign_multipart_parts
+ # Backups are ops, not content — an archived site should still be
+ # backup-able, restorable, and downloadable.
+ allow_when_archived :create,
+ :cancel,
+ :email,
+ :destroy,
+ :restore,
+ :rollback,
+ :readonly,
+ :upload_backup_chunk,
+ :create_multipart,
+ :complete_multipart,
+ :abort_multipart,
+ :batch_presign_multipart_parts
+
def index
respond_to do |format|
format.html do
diff --git a/app/controllers/admin/site_settings_controller.rb b/app/controllers/admin/site_settings_controller.rb
index fad00816d70..8a4684b76d0 100644
--- a/app/controllers/admin/site_settings_controller.rb
+++ b/app/controllers/admin/site_settings_controller.rb
@@ -1,6 +1,11 @@
# frozen_string_literal: true
class Admin::SiteSettingsController < Admin::AdminController
+ # On an archived site the only site setting an admin may change is
+ # `site_archived` itself (to toggle archive back off). The guard inside
+ # #update blocks every other setting.
+ allow_when_archived :update
+
rescue_from Discourse::InvalidParameters do |e|
render_json_error e.message, status: 422
end
@@ -36,6 +41,10 @@ class Admin::SiteSettingsController < Admin::AdminController
settings = [{ setting_name: id, value: params[id], backfill: }]
end
+ if SiteSetting.site_archived && settings.any? { |s| s[:setting_name].to_s != "site_archived" }
+ raise Discourse::SiteArchived
+ end
+
SiteSetting::Update.call(
guardian:,
params: {
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index a8e450ed8f2..7fd23d3f1ea 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
include GlobalPath
include Hijack
include ReadOnlyMixin
+ include ArchivedSiteMixin
include ThemeResolver
include VaryHeader
@@ -41,6 +42,7 @@ class ApplicationController < ActionController::Base
before_action :clear_notifications
around_action :with_resolved_locale
before_action :block_if_readonly_mode
+ before_action :block_if_archived
before_action :authorize_mini_profiler
before_action :redirect_to_login_if_required
before_action :block_if_requires_login
@@ -270,6 +272,21 @@ class ApplicationController < ActionController::Base
end
end
+ rescue_from Discourse::SiteArchived do
+ unless response_body
+ respond_to do |format|
+ format.json do
+ render_json_error I18n.t("site_archived_error"), type: :site_archived, status: 503
+ end
+ format.html do
+ render status: :service_unavailable,
+ layout: "no_ember",
+ template: "exceptions/site_archived"
+ end
+ end
+ end
+ end
+
rescue_from SecondFactor::AuthManager::SecondFactorRequired do |e|
if request.xhr?
render json: { second_factor_challenge_nonce: e.nonce }, status: :forbidden
diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb
index 5d357ca22f2..4328877a025 100644
--- a/app/controllers/session_controller.rb
+++ b/app/controllers/session_controller.rb
@@ -18,6 +18,13 @@ class SessionController < ApplicationController
allow_in_readonly_mode :email_login
allow_in_staff_writes_only_mode :create, :forgot_password, :create_login_code, :verify_login_code
+ # Every SessionController action is part of auth. An archived site permits
+ # all of them so existing users can log in, log out, and reset passwords.
+ # New account creation via these actions (SSO first-login, invite login code)
+ # is blocked at the model layer by guards in DiscourseConnect and
+ # InviteRedeemer.
+ skip_before_action :block_if_archived
+
ACTIVATE_USER_KEY = "activate_user"
FORGOT_PASSWORD_EMAIL_LIMIT_PER_DAY = 6
@@ -896,6 +903,12 @@ class SessionController < ApplicationController
end
def process_verified_login_code(matched_user)
+ # Existing-user login is allowed while archived; account creation is not.
+ # The model-layer guard in CreateFromVerifiedEmail exists too, but the
+ # Service framework swallows exceptions inside ModelStep, so raising here
+ # is what actually surfaces the 503 archive response.
+ raise Discourse::SiteArchived if SiteSetting.site_archived && matched_user.nil?
+
if matched_user &&
(matched_user.totp_or_backup_codes_enabled? || matched_user.security_keys_enabled?)
if missing_second_factor_params?
diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb
index 59befac0639..79250766527 100644
--- a/app/controllers/static_controller.rb
+++ b/app/controllers/static_controller.rb
@@ -9,6 +9,10 @@ class StaticController < ApplicationController
before_action :apply_cdn_headers, only: %i[cdn_asset enter favicon service_worker_asset]
+ # `enter` is the post-login redirect helper; it performs no writes and must
+ # work on archived sites so login can complete.
+ allow_when_archived :enter
+
PAGES_WITH_EMAIL_PARAM = %w[login password_reset signup]
MODAL_PAGES = %w[password_reset signup]
DEFAULT_PAGES = {
diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb
index e3c0e16e5dc..2e4127e4e59 100644
--- a/app/controllers/users/omniauth_callbacks_controller.rb
+++ b/app/controllers/users/omniauth_callbacks_controller.rb
@@ -14,6 +14,7 @@ class Users::OmniauthCallbacksController < ApplicationController
# These are usually GET requests but some providers use POST requests
allow_in_staff_writes_only_mode :complete
+ allow_when_archived :complete
def confirm_request
self.class.find_authenticator(params[:provider])
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index a6d482cc8ab..7a2a37bdd56 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -117,6 +117,13 @@ class UsersController < ApplicationController
allow_in_readonly_mode :admin_login
allow_in_staff_writes_only_mode :email_login, :password_reset_update
+ allow_when_archived :email_login,
+ :password_reset_update,
+ :admin_login,
+ :confirm_email_token,
+ :perform_account_activation,
+ :send_activation_email
+
MAX_RECENT_SEARCHES = 5
def index
diff --git a/app/models/discourse_connect.rb b/app/models/discourse_connect.rb
index 06ff49d6e3b..3796c0de7a9 100644
--- a/app/models/discourse_connect.rb
+++ b/app/models/discourse_connect.rb
@@ -247,6 +247,8 @@ class DiscourseConnect < DiscourseConnectBase
user = User.find_by_email(email) if !require_activation
if !user
+ raise Discourse::SiteArchived if SiteSetting.site_archived
+
user_params = {
primary_email:
UserEmail.new(email: email, primary: true) do |user_email|
diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb
index 80c583f6711..ee9b4cdf668 100644
--- a/app/models/invite_redeemer.rb
+++ b/app/models/invite_redeemer.rb
@@ -97,6 +97,8 @@ class InviteRedeemer
email_token: nil,
email_verified: false
)
+ raise Discourse::SiteArchived if SiteSetting.site_archived
+
if username && UsernameValidator.new(username).valid_format? &&
User.username_available?(username, email)
available_username = username
diff --git a/app/services/user/action/create_from_verified_email.rb b/app/services/user/action/create_from_verified_email.rb
index 5f5ca9f082d..fe4fc59d2be 100644
--- a/app/services/user/action/create_from_verified_email.rb
+++ b/app/services/user/action/create_from_verified_email.rb
@@ -7,6 +7,8 @@ class User::Action::CreateFromVerifiedEmail < Service::ActionBase
option :name, optional: true
def call
+ raise Discourse::SiteArchived if SiteSetting.site_archived
+
# A random name beats the generic "userN" fallback here: there is no
# signup form where the user could pick one before the account exists.
# Sites that turn random names off fall through to that generic name.
diff --git a/app/views/exceptions/site_archived.html.erb b/app/views/exceptions/site_archived.html.erb
new file mode 100644
index 00000000000..84be20b8527
--- /dev/null
+++ b/app/views/exceptions/site_archived.html.erb
@@ -0,0 +1 @@
+<p><%= t('site_archived_error') %></p>
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index f4c9b38397d..6cde6b86973 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -2945,6 +2945,10 @@ en:
login_disabled: "Login is disabled while the site is in read only mode."
logout_disabled: "Logout is disabled while the site is in read only mode."
signup_disabled: "Signing up is disabled while the site is in read only mode."
+ site_archived:
+ enabled: "This site is archived. Please continue to browse, but replying, likes, and other actions are disabled."
+ enabled_anonymous: "This site is archived. Please continue to browse or log in, but replying, likes, and other actions are disabled."
+ signup_disabled: "Signing up is disabled on an archived site."
staff_writes_only_mode:
enabled: "This site is in staff only mode. Please continue to browse, but replying, likes, and other actions are limited to staff members only."
enabled_anonymous: "This site is in staff only mode. Please continue to browse, but signing up is unavailable and only staff members can log in."
@@ -8941,6 +8945,14 @@ en:
disable:
title: "Disable read-only mode"
label: "Disable read-only"
+ site_archived:
+ enable:
+ title: "Archive site"
+ label: "Archive site"
+ confirm: "Are you sure you want to archive this site? Users will still be able to log in and browse, but all write actions will be disabled. Operational read-only reasons (failover, maintenance) still take precedence when active."
+ disable:
+ title: "Unarchive site"
+ label: "Unarchive site"
logs:
none: "No logs yet…"
columns:
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index e736403b25a..8eeb90aef7c 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -591,6 +591,7 @@ en:
provider_not_enabled: "You are not permitted to view the requested resource. The authentication provider is not enabled."
provider_not_found: "You are not permitted to view the requested resource. The authentication provider does not exist."
read_only_mode_enabled: "The site is in read only mode. Interactions are disabled."
+ site_archived_error: "This site is archived. Interactions are disabled."
invalid_grant_badge_reason_link: "External or invalid discourse link is not allowed in badge reason"
email_template_cant_be_modified: "This email template can't be modified"
invalid_whisper_access: "Either whispers are not enabled or you do not have access to create whisper posts"
@@ -2362,6 +2363,7 @@ en:
linkedin_oidc_client_id: "Client ID for LinkedIn authentication, registered at <a href='https://www.linkedin.com/developers/apps' target='_blank'>https://www.linkedin.com/developers/apps</a>"
linkedin_oidc_client_secret: "Client secret for LinkedIn authentication, registered at <a href='https://www.linkedin.com/developers/apps' target='_blank'>https://www.linkedin.com/developers/apps</a>"
+ site_archived: "Permanently freeze the site as an archive. Reads and login remain available; new content, likes, and other write actions are disabled. Operational read-only reasons (failover, maintenance) still take precedence when active."
enable_backups: "Allow administrators to create backups of the forum"
allow_restore: "Allow restore, which can replace ALL site data! Leave disabled unless you plan to restore a backup"
maximum_backups: "The maximum amount of backups to keep. Older backups are automatically deleted"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 7a783a90af5..8c2e3b711ca 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -3734,6 +3734,9 @@ legal:
area: "legal"
backups:
+ site_archived:
+ default: false
+ client: true
enable_backups:
default: true
client: true
diff --git a/frontend/discourse/admin/components/admin-backups-actions.gjs b/frontend/discourse/admin/components/admin-backups-actions.gjs
index 119f3a6a4a5..56194524a9e 100644
--- a/frontend/discourse/admin/components/admin-backups-actions.gjs
+++ b/frontend/discourse/admin/components/admin-backups-actions.gjs
@@ -9,6 +9,7 @@ import { i18n } from "discourse-i18n";
export default class AdminBackupsActions extends Component {
@service currentUser;
@service site;
+ @service siteSettings;
@service dialog;
get rollbackDisabled() {
@@ -38,6 +39,18 @@ export default class AdminBackupsActions extends Component {
}
}
+ @action
+ toggleSiteArchived() {
+ if (!this.siteSettings.site_archived) {
+ this.dialog.yesNoConfirm({
+ message: i18n("admin.backups.site_archived.enable.confirm"),
+ didConfirm: () => this.#setSiteArchived(true),
+ });
+ } else {
+ this.#setSiteArchived(false);
+ }
+ }
+
async #toggleReadOnlyMode(enable) {
try {
await ajax("/admin/backups/readonly", {
@@ -50,6 +63,18 @@ export default class AdminBackupsActions extends Component {
}
}
+ async #setSiteArchived(enable) {
+ try {
+ await ajax("/admin/site_settings/site_archived", {
+ type: "PUT",
+ data: { site_archived: enable },
+ });
+ this.siteSettings.site_archived = enable;
+ } catch (err) {
+ popupAjaxError(err);
+ }
+ }
+
<template>
{{#if @backups.isOperationRunning}}
<@actions.Danger
@@ -96,5 +121,22 @@ export default class AdminBackupsActions extends Component {
"admin.backups.read_only.enable.title"
}}
/>
+
+ <@actions.Default
+ class="admin-backups__toggle-site-archived"
+ @action={{this.toggleSiteArchived}}
+ @disabled={{@backups.isOperationRunning}}
+ @icon="box-archive"
+ @label={{if
+ this.siteSettings.site_archived
+ "admin.backups.site_archived.disable.label"
+ "admin.backups.site_archived.enable.label"
+ }}
+ @title={{if
+ this.siteSettings.site_archived
+ "admin.backups.site_archived.disable.title"
+ "admin.backups.site_archived.enable.title"
+ }}
+ />
</template>
}
diff --git a/frontend/discourse/app/components/global-notice.gjs b/frontend/discourse/app/components/global-notice.gjs
index 3f84dc4ae64..29734279ea9 100644
--- a/frontend/discourse/app/components/global-notice.gjs
+++ b/frontend/discourse/app/components/global-notice.gjs
@@ -127,6 +127,15 @@ export default class GlobalNotice extends Component {
id: "alert-read-only",
})
);
+ } else if (this.siteSettings.site_archived) {
+ notices.push(
+ Notice.create({
+ text: this.currentUser
+ ? i18n("site_archived.enabled")
+ : i18n("site_archived.enabled_anonymous"),
+ id: "alert-site-archived",
+ })
+ );
}
const previewThemeId =
diff --git a/frontend/discourse/app/services/login.js b/frontend/discourse/app/services/login.js
index bb687805dd3..91075137721 100644
--- a/frontend/discourse/app/services/login.js
+++ b/frontend/discourse/app/services/login.js
@@ -29,11 +29,13 @@ export default class LoginService extends Service {
}
get readOnlySignupMessage() {
- return i18n(
- this.site.isStaffWritesOnly
- ? "staff_writes_only_mode.signup_disabled"
- : "read_only_mode.signup_disabled"
- );
+ if (this.site.isStaffWritesOnly) {
+ return i18n("staff_writes_only_mode.signup_disabled");
+ }
+ if (this.siteSettings.site_archived) {
+ return i18n("site_archived.signup_disabled");
+ }
+ return i18n("read_only_mode.signup_disabled");
}
@action
diff --git a/lib/archived_site_mixin.rb b/lib/archived_site_mixin.rb
new file mode 100644
index 00000000000..d475cddbf66
--- /dev/null
+++ b/lib/archived_site_mixin.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+# Site archival is a policy state (site_archived setting), orthogonal to the
+# operational readonly stack. When archived, non-GET requests are blocked with
+# Discourse::SiteArchived unless the action is explicitly declared with
+# `allow_when_archived`. Reads are always permitted.
+#
+# Controllers that should be fully exempt (e.g. AdminController) can
+# `skip_before_action :block_if_archived`.
+module ArchivedSiteMixin
+ module ClassMethods
+ def actions_allowed_when_archived
+ @actions_allowed_when_archived ||= []
+ end
+
+ def allow_when_archived(*actions)
+ actions_allowed_when_archived.concat(actions.map(&:to_sym))
+ end
+
+ def allowed_when_archived?(action)
+ actions_allowed_when_archived.include?(action.to_sym)
+ end
+ end
+
+ def block_if_archived
+ return if request.get? || request.head?
+ return unless SiteSetting.site_archived
+ return if self.class.allowed_when_archived?(action_name)
+ raise Discourse::SiteArchived
+ end
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+end
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 9f9494f8065..ae47f57d0cc 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -331,6 +331,11 @@ module Discourse
class ReadOnly < StandardError
end
+ # Raised when the site is archived (site_archived setting) and a write is
+ # attempted from an action not declared with `allow_when_archived`.
+ class SiteArchived < StandardError
+ end
+
# Cross site request forgery
class CSRF < StandardError
end
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index 4601b36234c..3e45f9d320d 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -201,6 +201,8 @@ class PostCreator
end
def create
+ raise Discourse::SiteArchived if SiteSetting.site_archived
+
if valid?
transaction do
build_post_stats
diff --git a/spec/integration/archive_mode_spec.rb b/spec/integration/archive_mode_spec.rb
new file mode 100644
index 00000000000..760c2a3ce4f
--- /dev/null
+++ b/spec/integration/archive_mode_spec.rb
@@ -0,0 +1,138 @@
+# frozen_string_literal: true
+
+RSpec.describe "Site archived" do
+ fab!(:user)
+ fab!(:admin)
+
+ before { SiteSetting.site_archived = true }
+
+ it "does not toggle Discourse.readonly_mode?" do
+ expect(Discourse.readonly_mode?).to eq(false)
+ end
+
+ describe "write endpoints" do
+ before { sign_in(user) }
+
+ it "blocks a non-allowlisted POST with the site-archived error message" do
+ post "/drafts.json", params: { draft_key: "new_topic", sequence: 0, data: "{}" }
+ expect(response.status).to eq(503)
+ expect(response.parsed_body["errors"]).to include(I18n.t("site_archived_error"))
+ end
+ end
+
+ describe "login" do
+ before { SiteSetting.enable_local_logins_via_email = true }
+
+ it "allows email login" do
+ token = Fabricate(:email_token, user: user, scope: EmailToken.scopes[:email_login])
+ post "/session/email-login/#{token.token}.json"
+ expect(response.status).to eq(200)
+ expect(session[:current_user_id]).to eq(user.id)
+ end
+
+ it "allows requesting an email login link (POST /u/email-login)" do
+ post "/u/email-login.json", params: { login: user.username }
+ expect(response.status).not_to eq(503)
+ end
+
+ it "allows the post-login /login redirect helper" do
+ post "/login", params: { redirect: "/" }
+ expect(response).to redirect_to("/")
+ end
+ end
+
+ describe "logout" do
+ before { sign_in(user) }
+
+ it "allows logout" do
+ delete "/session/#{user.username}.json"
+ expect(response.status).not_to eq(503)
+ end
+ end
+
+ describe "admin" do
+ before { sign_in(admin) }
+
+ it "lets an admin toggle site_archived back off" do
+ put "/admin/site_settings/site_archived.json", params: { site_archived: false }
+ expect(response.status).to eq(200).or eq(204)
+ expect(SiteSetting.site_archived).to eq(false)
+ end
+
+ it "blocks admin updates to any other site setting while archived" do
+ put "/admin/site_settings/title.json", params: { title: "New Title" }
+ expect(response.status).to eq(503)
+ expect(response.parsed_body["errors"]).to include(I18n.t("site_archived_error"))
+ expect(SiteSetting.title).not_to eq("New Title")
+ end
+
+ it "blocks other admin writes (categories, users, etc.) while archived" do
+ post "/categories.json", params: { name: "Frozen", color: "AB9364", text_color: "FFFFFF" }
+ expect(response.status).to eq(503)
+ expect(response.parsed_body["errors"]).to include(I18n.t("site_archived_error"))
+ end
+
+ it "allows backup operations (ops, not content)" do
+ post "/admin/backups.json"
+ expect(response.status).not_to eq(503)
+ end
+ end
+
+ describe "non-controller write paths" do
+ it "blocks PostCreator (covers email-in, chat, jobs, moderator posts)" do
+ creator =
+ PostCreator.new(user, title: "Frozen topic", raw: "Some body content here for the test.")
+ expect { creator.create! }.to raise_error(Discourse::SiteArchived)
+ end
+
+ it "blocks invite-based account creation (InviteRedeemer)" do
+ invite = Fabricate(:invite, email: "[email protected]")
+ expect do
+ InviteRedeemer.create_user_from_invite(
+ email: "[email protected]",
+ invite: invite,
+ email_verified: true,
+ )
+ end.to raise_error(Discourse::SiteArchived)
+ end
+
+ it "blocks new-user creation via DiscourseConnect (SSO first-time login)" do
+ sso = DiscourseConnect.new(server_session: {})
+ sso.external_id = "unique_external_id_123"
+ sso.email = "[email protected]"
+ sso.username = "new_sso_user"
+ sso.name = "New SSO User"
+ expect { sso.lookup_or_create_user("127.0.0.1") }.to raise_error(Discourse::SiteArchived)
+ end
+
+ it "still lets existing users log in via DiscourseConnect" do
+ existing = Fabricate(:user, email: "[email protected]")
+ sso = DiscourseConnect.new(server_session: {})
+ sso.external_id = "existing_external_id"
+ sso.email = "[email protected]"
+ sso.username = existing.username
+ expect { sso.lookup_or_create_user("127.0.0.1") }.not_to raise_error
+ end
+
+ it "blocks passwordless-login-code account creation for unknown emails" do
+ expect do
+ User::Action::CreateFromVerifiedEmail.call(
+ email: "[email protected]",
+ ip_address: "127.0.0.1",
+ )
+ end.to raise_error(Discourse::SiteArchived)
+ expect(User.with_email("[email protected]")).to be_empty
+ end
+ end
+
+ context "when an operational readonly reason is also active" do
+ before { Discourse.enable_readonly_mode(Discourse::USER_READONLY_MODE_KEY) }
+ after { Discourse.disable_readonly_mode(Discourse::USER_READONLY_MODE_KEY) }
+
+ it "blocks a non-allowlisted login attempt with the read-only error (readonly takes precedence)" do
+ post "/session.json", params: { login: user.username, password: "password" }
+ expect(response.status).to eq(503)
+ expect(response.parsed_body["errors"]).to include(I18n.t("read_only_mode_enabled"))
+ end
+ end
+end
diff --git a/spec/requests/session_controller_spec.rb b/spec/requests/session_controller_spec.rb
index 17cde373b0c..f6ba8b344f2 100644
--- a/spec/requests/session_controller_spec.rb
+++ b/spec/requests/session_controller_spec.rb
@@ -884,6 +884,33 @@ RSpec.describe SessionController do
expect(session[:current_user_id]).to be_nil
end
+ context "when the site is archived" do
+ before { SiteSetting.site_archived = true }
+
+ it "still logs in an existing user with a correct code" do
+ post "/session/login-code/verify.json", params: { email: user.email, code: }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body.dig("user", "username")).to eq(user.username)
+ expect(session[:current_user_id]).to eq(user.id)
+ expect(EmailLoginCode.active.for_email(user.email)).to be_empty
+ end
+
+ it "does not create an account for an unknown email" do
+ new_email = "[email protected]"
+ new_code = EmailLoginCode.generate!(email: new_email)
+
+ expect do
+ post "/session/login-code/verify.json", params: { email: new_email, code: new_code.code }
+ end.not_to change { User.count }
+
+ expect(response.status).to eq(503)
+ expect(response.parsed_body["errors"]).to include(I18n.t("site_archived_error"))
+ expect(session[:current_user_id]).to be_nil
+ expect(EmailLoginCode.active.for_email(new_email)).not_to be_empty
+ end
+ end
+
it "logs in an existing user with a correct code" do
post "/session/login-code/verify.json", params: { email: user.email, code: }
@@ -1580,6 +1607,41 @@ RSpec.describe SessionController do
end
end
+ context "when the site is archived" do
+ before { SiteSetting.site_archived = true }
+
+ it "allows an existing user to log in via DiscourseConnect" do
+ existing = Fabricate(:user, email: "[email protected]")
+ Fabricate(:single_sign_on_record, user: existing, external_id: "existing-external-id")
+
+ sso = get_sso("/a/")
+ sso.external_id = "existing-external-id"
+ sso.email = "[email protected]"
+ sso.username = existing.username
+
+ get "/session/sso_login", params: Rack::Utils.parse_query(sso.payload), headers: headers
+
+ expect(response.status).not_to eq(503)
+ logged_on_user = Discourse.current_user_provider.new(request.env).current_user
+ expect(logged_on_user&.id).to eq(existing.id)
+ end
+
+ it "does not create a new user via DiscourseConnect (archive blocks account creation)" do
+ sso = get_sso("/a/")
+ sso.external_id = "666"
+ sso.email = "[email protected]"
+ sso.name = "Bob Bobson"
+ sso.username = "bob"
+
+ expect do
+ get "/session/sso_login", params: Rack::Utils.parse_query(sso.payload), headers: headers
+ end.not_to change { User.count }
+
+ logged_on_user = Discourse.current_user_provider.new(request.env).current_user
+ expect(logged_on_user).to eq(nil)
+ end
+ end
+
it "does not create superfluous auth tokens when already logged in" do
user = Fabricate(:user)
sign_in(user)