diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9c0beedf85a..65d688e4135 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,6 +14,7 @@ require_dependency 'global_path' require_dependency 'secure_session' require_dependency 'topic_query' require_dependency 'hijack' +require_dependency 'read_only' class ApplicationController < ActionController::Base include CurrentUser @@ -21,6 +22,7 @@ class ApplicationController < ActionController::Base include JsonError include GlobalPath include Hijack + include ReadOnly attr_reader :theme_ids @@ -80,10 +82,6 @@ class ApplicationController < ActionController::Base (has_escaped_fragment? || CrawlerDetection.crawler?(request.user_agent) || params.key?("print")) end - def add_readonly_header - response.headers['Discourse-Readonly'] = 'true' if @readonly_mode - end - def perform_refresh_session refresh_session(current_user) unless @readonly_mode end @@ -508,10 +506,6 @@ class ApplicationController < ActionController::Base private - def check_readonly_mode - @readonly_mode = Discourse.readonly_mode? - end - def locale_from_header begin # Rails I18n uses underscores between the locale and the region; the request diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index 7071a04ec38..b3b0a749924 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -1,6 +1,13 @@ # frozen_string_literal: true +require "read_only" + class ForumsController < ActionController::Base + include ReadOnly + + before_action :check_readonly_mode + after_action :add_readonly_header + def status if $shutdown render plain: 'shutting down', status: 500 diff --git a/lib/read_only.rb b/lib/read_only.rb new file mode 100644 index 00000000000..d095d18ad85 --- /dev/null +++ b/lib/read_only.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module ReadOnly + + def check_readonly_mode + @readonly_mode = Discourse.readonly_mode? + end + + def add_readonly_header + response.headers['Discourse-Readonly'] = 'true' if @readonly_mode + end + +end diff --git a/spec/requests/forums_controller_spec.rb b/spec/requests/forums_controller_spec.rb new file mode 100644 index 00000000000..c288909dd60 --- /dev/null +++ b/spec/requests/forums_controller_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe ForumsController do + + describe "read only header" do + it "returns no read only header by default" do + get "/srv/status" + expect(response.status).to eq(200) + expect(response.headers['Discourse-Readonly']).to eq(nil) + end + + it "returns a readonly header if the site is read only" do + Discourse.received_readonly! + get "/srv/status" + expect(response.status).to eq(200) + expect(response.headers['Discourse-Readonly']).to eq('true') + end + end + +end