2018-01-17 00:32:52 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-10-09 02:22:41 -04:00
|
|
|
require 'current_user'
|
2013-10-09 00:10:37 -04:00
|
|
|
require_dependency 'canonical_url'
|
2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'discourse'
|
|
|
|
require_dependency 'custom_renderer'
|
2013-10-09 00:10:37 -04:00
|
|
|
require_dependency 'archetype'
|
2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'rate_limiter'
|
2014-02-20 16:07:02 -05:00
|
|
|
require_dependency 'crawler_detection'
|
2014-04-02 13:22:10 -04:00
|
|
|
require_dependency 'json_error'
|
2014-05-30 00:17:35 -04:00
|
|
|
require_dependency 'letter_avatar'
|
2014-11-13 23:39:17 -05:00
|
|
|
require_dependency 'distributed_cache'
|
2015-03-09 15:24:16 -04:00
|
|
|
require_dependency 'global_path'
|
2016-12-19 02:00:22 -05:00
|
|
|
require_dependency 'secure_session'
|
2017-05-02 13:21:46 -04:00
|
|
|
require_dependency 'topic_query'
|
2017-11-23 23:31:23 -05:00
|
|
|
require_dependency 'hijack'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class ApplicationController < ActionController::Base
|
|
|
|
include CurrentUser
|
2013-02-13 06:04:43 -05:00
|
|
|
include CanonicalURL::ControllerExtensions
|
2014-04-02 13:22:10 -04:00
|
|
|
include JsonError
|
2015-03-09 15:24:16 -04:00
|
|
|
include GlobalPath
|
2017-11-23 23:31:23 -05:00
|
|
|
include Hijack
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-05-12 12:41:26 -04:00
|
|
|
attr_reader :theme_key
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
serialization_scope :guardian
|
|
|
|
|
|
|
|
protect_from_forgery
|
|
|
|
|
2013-07-29 01:13:13 -04:00
|
|
|
# Default Rails 3.2 lets the request through with a blank session
|
|
|
|
# we are being more pedantic here and nulling session / current_user
|
|
|
|
# and then raising a CSRF exception
|
|
|
|
def handle_unverified_request
|
|
|
|
# NOTE: API key is secret, having it invalidates the need for a CSRF token
|
2016-12-15 20:05:20 -05:00
|
|
|
unless is_api? || is_user_api?
|
2013-07-29 01:13:13 -04:00
|
|
|
super
|
|
|
|
clear_current_user
|
2017-04-10 08:01:25 -04:00
|
|
|
render plain: "[\"BAD CSRF\"]", status: 403
|
2013-07-29 01:13:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-07 01:29:30 -04:00
|
|
|
before_action :check_readonly_mode
|
2017-08-31 00:06:56 -04:00
|
|
|
before_action :handle_theme
|
|
|
|
before_action :set_current_user_for_logs
|
|
|
|
before_action :clear_notifications
|
|
|
|
before_action :set_locale
|
|
|
|
before_action :set_mobile_view
|
|
|
|
before_action :block_if_readonly_mode
|
|
|
|
before_action :authorize_mini_profiler
|
|
|
|
before_action :redirect_to_login_if_required
|
2018-01-31 23:17:59 -05:00
|
|
|
before_action :block_if_requires_login
|
|
|
|
before_action :preload_json
|
2017-08-31 00:06:56 -04:00
|
|
|
before_action :check_xhr
|
|
|
|
after_action :add_readonly_header
|
|
|
|
after_action :perform_refresh_session
|
|
|
|
after_action :dont_cache_page
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2014-02-14 17:10:08 -05:00
|
|
|
layout :set_layout
|
|
|
|
|
2018-05-16 03:25:49 -04:00
|
|
|
if Rails.env == "development"
|
|
|
|
after_action :remember_theme_key
|
|
|
|
|
|
|
|
def remember_theme_key
|
|
|
|
if @theme_key
|
|
|
|
Stylesheet::Watcher.theme_key = @theme_key if defined? Stylesheet::Watcher
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-20 17:02:26 -05:00
|
|
|
def has_escaped_fragment?
|
|
|
|
SiteSetting.enable_escaped_fragments? && params.key?("_escaped_fragment_")
|
|
|
|
end
|
|
|
|
|
2014-10-30 14:26:35 -04:00
|
|
|
def use_crawler_layout?
|
2018-01-16 00:28:11 -05:00
|
|
|
@use_crawler_layout ||=
|
|
|
|
request.user_agent &&
|
|
|
|
(request.content_type.blank? || request.content_type.include?('html')) &&
|
|
|
|
!['json', 'rss'].include?(params[:format]) &&
|
|
|
|
(has_escaped_fragment? || CrawlerDetection.crawler?(request.user_agent) || params.key?("print"))
|
2014-10-30 14:26:35 -04:00
|
|
|
end
|
|
|
|
|
2015-04-24 14:32:18 -04:00
|
|
|
def add_readonly_header
|
2017-09-07 01:29:30 -04:00
|
|
|
response.headers['Discourse-Readonly'] = 'true' if @readonly_mode
|
2015-04-24 14:32:18 -04:00
|
|
|
end
|
|
|
|
|
2016-07-24 22:07:31 -04:00
|
|
|
def perform_refresh_session
|
2017-10-20 00:41:40 -04:00
|
|
|
refresh_session(current_user) unless @readonly_mode
|
2016-07-24 22:07:31 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 13:05:00 -05:00
|
|
|
def immutable_for(duration)
|
|
|
|
response.cache_control[:max_age] = duration.to_i
|
|
|
|
response.cache_control[:public] = true
|
|
|
|
response.cache_control[:extras] = ["immutable"]
|
|
|
|
end
|
|
|
|
|
2016-11-15 01:00:28 -05:00
|
|
|
def dont_cache_page
|
|
|
|
if !response.headers["Cache-Control"] && response.cache_control.blank?
|
|
|
|
response.headers["Cache-Control"] = "no-store, must-revalidate, no-cache, private"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-15 11:54:26 -05:00
|
|
|
def slow_platform?
|
|
|
|
request.user_agent =~ /Android/
|
|
|
|
end
|
|
|
|
|
2014-02-14 17:10:08 -05:00
|
|
|
def set_layout
|
2014-10-30 14:26:35 -04:00
|
|
|
use_crawler_layout? ? 'crawler' : 'application'
|
2014-02-14 17:10:08 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Some exceptions
|
2015-03-22 21:20:50 -04:00
|
|
|
class RenderEmpty < StandardError; end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-02-08 15:25:03 -05:00
|
|
|
# Render nothing
|
2013-02-05 14:16:51 -05:00
|
|
|
rescue_from RenderEmpty do
|
|
|
|
render 'default/empty'
|
|
|
|
end
|
|
|
|
|
2016-03-15 16:06:50 -04:00
|
|
|
def render_rate_limit_error(e)
|
2018-03-13 11:12:41 -04:00
|
|
|
retry_time_in_seconds = e&.available_in
|
|
|
|
|
|
|
|
render_json_error(
|
|
|
|
e.description,
|
|
|
|
type: :rate_limit,
|
|
|
|
status: 429,
|
|
|
|
extras: { wait_seconds: retry_time_in_seconds },
|
|
|
|
headers: { 'Retry-After': retry_time_in_seconds },
|
|
|
|
)
|
2016-03-15 16:06:50 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# If they hit the rate limiter
|
|
|
|
rescue_from RateLimiter::LimitExceeded do |e|
|
2016-03-15 16:06:50 -04:00
|
|
|
render_rate_limit_error(e)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-04-29 11:49:58 -04:00
|
|
|
rescue_from PG::ReadOnlySqlTransaction do |e|
|
|
|
|
Discourse.received_readonly!
|
2017-10-20 00:41:40 -04:00
|
|
|
Rails.logger.error("#{e.class} #{e.message}: #{e.backtrace.join("\n")}")
|
2015-04-29 11:49:58 -04:00
|
|
|
raise Discourse::ReadOnly
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
rescue_from Discourse::NotLoggedIn do |e|
|
2015-02-08 16:35:09 -05:00
|
|
|
if (request.format && request.format.json?) || request.xhr? || !request.get?
|
2017-09-23 10:39:58 -04:00
|
|
|
rescue_discourse_actions(:not_logged_in, 403, include_ember: true)
|
2014-03-05 11:48:06 -05:00
|
|
|
else
|
2015-10-30 07:51:16 -04:00
|
|
|
rescue_discourse_actions(:not_found, 404)
|
2014-03-05 11:48:06 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2018-01-11 22:15:10 -05:00
|
|
|
rescue_from Discourse::InvalidParameters do |e|
|
|
|
|
message = I18n.t('invalid_params', message: e.message)
|
|
|
|
if (request.format && request.format.json?) || request.xhr? || !request.get?
|
|
|
|
rescue_discourse_actions(:invalid_parameters, 400, include_ember: true, custom_message_translated: message)
|
|
|
|
else
|
|
|
|
rescue_discourse_actions(:not_found, 400, custom_message_translated: message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-04 20:11:46 -05:00
|
|
|
rescue_from ActiveRecord::StatementInvalid do |e|
|
2017-02-17 12:09:53 -05:00
|
|
|
Discourse.reset_active_record_cache_if_needed(e)
|
2016-12-04 20:11:46 -05:00
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
2016-02-24 04:09:30 -05:00
|
|
|
class PluginDisabled < StandardError; end
|
|
|
|
|
2016-03-23 12:13:29 -04:00
|
|
|
# Handles requests for giant IDs that throw pg exceptions
|
2017-08-31 00:06:56 -04:00
|
|
|
rescue_from ActiveModel::RangeError do |e|
|
|
|
|
if e.message =~ /ActiveModel::Type::Integer/
|
2016-03-23 12:13:29 -04:00
|
|
|
rescue_discourse_actions(:not_found, 404)
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-22 17:00:08 -05:00
|
|
|
rescue_from Discourse::NotFound, PluginDisabled, ActionController::RoutingError do
|
2015-02-09 13:19:47 -05:00
|
|
|
rescue_discourse_actions(:not_found, 404)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-09-23 10:39:58 -04:00
|
|
|
rescue_from Discourse::InvalidAccess do |e|
|
2018-02-09 19:09:54 -05:00
|
|
|
|
|
|
|
if e.opts[:delete_cookie].present?
|
|
|
|
cookies.delete(e.opts[:delete_cookie])
|
|
|
|
end
|
2017-09-23 10:39:58 -04:00
|
|
|
rescue_discourse_actions(
|
|
|
|
:invalid_access,
|
|
|
|
403,
|
|
|
|
include_ember: true,
|
|
|
|
custom_message: e.custom_message
|
|
|
|
)
|
2013-06-20 11:47:33 -04:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:37:28 -05:00
|
|
|
rescue_from Discourse::ReadOnly do
|
2016-12-07 01:04:42 -05:00
|
|
|
render_json_error I18n.t('read_only_mode_enabled'), type: :read_only, status: 503
|
2014-02-12 23:37:28 -05:00
|
|
|
end
|
|
|
|
|
2017-09-23 10:39:58 -04:00
|
|
|
def rescue_discourse_actions(type, status_code, opts = nil)
|
|
|
|
opts ||= {}
|
2017-04-04 16:22:14 -04:00
|
|
|
show_json_errors = (request.format && request.format.json?) ||
|
|
|
|
(request.xhr?) ||
|
2017-09-28 04:09:27 -04:00
|
|
|
((params[:external_id] || '').ends_with? '.json')
|
2017-04-04 16:22:14 -04:00
|
|
|
|
2018-01-11 22:15:10 -05:00
|
|
|
message = opts[:custom_message_translated] || I18n.t(opts[:custom_message] || type)
|
|
|
|
|
2017-09-28 09:50:01 -04:00
|
|
|
if show_json_errors
|
2015-02-08 16:35:09 -05:00
|
|
|
# HACK: do not use render_json_error for topics#show
|
|
|
|
if request.params[:controller] == 'topics' && request.params[:action] == 'show'
|
2018-01-11 22:15:10 -05:00
|
|
|
return render status: status_code, layout: false, plain: (status_code == 404 || status_code == 410) ? build_not_found_page(status_code) : message
|
2015-02-08 16:35:09 -05:00
|
|
|
end
|
|
|
|
|
2018-01-11 22:15:10 -05:00
|
|
|
render_json_error message, type: type, status: status_code
|
2013-05-30 14:46:02 -04:00
|
|
|
else
|
2017-09-28 20:35:23 -04:00
|
|
|
begin
|
2018-02-14 15:29:01 -05:00
|
|
|
# 404 pages won't have the session and theme_keys without these:
|
2017-09-28 20:35:23 -04:00
|
|
|
current_user
|
2018-02-14 15:29:01 -05:00
|
|
|
handle_theme
|
2017-09-28 20:35:23 -04:00
|
|
|
rescue Discourse::InvalidAccess
|
2018-01-11 22:15:10 -05:00
|
|
|
return render plain: message, status: status_code
|
2017-09-28 20:35:23 -04:00
|
|
|
end
|
|
|
|
|
2017-09-23 10:39:58 -04:00
|
|
|
render html: build_not_found_page(status_code, opts[:include_ember] ? 'application' : 'no_ember')
|
2013-05-30 14:46:02 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-02-04 16:23:39 -05:00
|
|
|
# If a controller requires a plugin, it will raise an exception if that plugin is
|
|
|
|
# disabled. This allows plugins to be disabled programatically.
|
|
|
|
def self.requires_plugin(plugin_name)
|
2017-08-31 00:06:56 -04:00
|
|
|
before_action do
|
2015-02-04 16:23:39 -05:00
|
|
|
raise PluginDisabled.new if Discourse.disabled_plugin_names.include?(plugin_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-12 01:27:58 -04:00
|
|
|
def set_current_user_for_logs
|
|
|
|
if current_user
|
2017-07-27 21:20:09 -04:00
|
|
|
Logster.add_to_env(request.env, "username", current_user.username)
|
2015-06-16 03:43:36 -04:00
|
|
|
response.headers["X-Discourse-Username"] = current_user.username
|
2014-05-12 01:27:58 -04:00
|
|
|
end
|
2015-06-15 20:27:42 -04:00
|
|
|
response.headers["X-Discourse-Route"] = "#{controller_name}/#{action_name}"
|
2014-05-12 01:27:58 -04:00
|
|
|
end
|
|
|
|
|
2016-02-15 03:29:35 -05:00
|
|
|
def clear_notifications
|
2017-09-07 01:29:30 -04:00
|
|
|
if current_user && !@readonly_mode
|
2016-02-15 03:29:35 -05:00
|
|
|
|
2018-01-17 00:32:52 -05:00
|
|
|
cookie_notifications = cookies['cn']
|
|
|
|
notifications = request.headers['Discourse-Clear-Notifications']
|
2016-02-15 03:29:35 -05:00
|
|
|
|
|
|
|
if cookie_notifications
|
|
|
|
if notifications.present?
|
2018-01-17 00:32:52 -05:00
|
|
|
notifications += ",#{cookie_notifications}"
|
2016-02-15 03:29:35 -05:00
|
|
|
else
|
|
|
|
notifications = cookie_notifications
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if notifications.present?
|
|
|
|
notification_ids = notifications.split(",").map(&:to_i)
|
2016-09-16 02:14:00 -04:00
|
|
|
Notification.read(current_user, notification_ids)
|
2018-05-25 21:11:10 -04:00
|
|
|
current_user.reload
|
|
|
|
current_user.publish_notifications_state
|
2016-02-15 03:29:35 -05:00
|
|
|
cookies.delete('cn')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-28 14:31:39 -05:00
|
|
|
def set_locale
|
2016-02-06 14:49:39 -05:00
|
|
|
if !current_user
|
2016-03-14 19:18:19 -04:00
|
|
|
if SiteSetting.set_locale_from_accept_language_header
|
2018-01-22 08:44:17 -05:00
|
|
|
locale = locale_from_header
|
2016-02-06 14:49:39 -05:00
|
|
|
else
|
2018-01-22 08:44:17 -05:00
|
|
|
locale = SiteSetting.default_locale
|
2016-02-06 14:49:39 -05:00
|
|
|
end
|
|
|
|
else
|
2018-01-22 08:44:17 -05:00
|
|
|
locale = current_user.effective_locale
|
2016-02-06 14:49:39 -05:00
|
|
|
end
|
2018-01-22 08:44:17 -05:00
|
|
|
|
|
|
|
I18n.locale = I18n.locale_available?(locale) ? locale : :en
|
2015-11-13 15:42:01 -05:00
|
|
|
I18n.ensure_all_loaded!
|
2013-02-28 14:31:39 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def store_preloaded(key, json)
|
|
|
|
@preloaded ||= {}
|
2013-02-25 11:42:20 -05:00
|
|
|
# I dislike that there is a gsub as opposed to a gsub!
|
|
|
|
# but we can not be mucking with user input, I wonder if there is a way
|
2013-02-11 01:28:21 -05:00
|
|
|
# to inject this safty deeper in the library or even in AM serializer
|
|
|
|
@preloaded[key] = json.gsub("</", "<\\/")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# If we are rendering HTML, preload the session data
|
|
|
|
def preload_json
|
2013-06-04 12:56:12 -04:00
|
|
|
# We don't preload JSON on xhr or JSON request
|
2015-01-24 00:03:44 -05:00
|
|
|
return if request.xhr? || request.format.json?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-05-20 03:12:16 -04:00
|
|
|
# if we are posting in makes no sense to preload
|
|
|
|
return if request.method != "GET"
|
|
|
|
|
|
|
|
# TODO should not be invoked on redirection so this should be further deferred
|
2013-08-05 16:25:44 -04:00
|
|
|
preload_anonymous_data
|
2013-06-04 12:56:12 -04:00
|
|
|
|
2013-08-05 16:25:44 -04:00
|
|
|
if current_user
|
|
|
|
current_user.sync_notification_channel_position
|
2017-03-20 05:16:53 -04:00
|
|
|
preload_current_user_data
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-18 14:47:22 -05:00
|
|
|
def set_mobile_view
|
|
|
|
session[:mobile_view] = params[:mobile_view] if params.has_key?(:mobile_view)
|
|
|
|
end
|
|
|
|
|
2018-01-17 00:32:52 -05:00
|
|
|
NO_CUSTOM = "no_custom"
|
|
|
|
NO_PLUGINS = "no_plugins"
|
|
|
|
ONLY_OFFICIAL = "only_official"
|
|
|
|
SAFE_MODE = "safe_mode"
|
2015-01-06 01:39:08 -05:00
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
def resolve_safe_mode
|
2018-04-25 11:46:54 -04:00
|
|
|
return unless guardian.can_enable_safe_mode?
|
2018-04-23 14:50:52 -04:00
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
safe_mode = params[SAFE_MODE]
|
|
|
|
if safe_mode
|
2017-04-14 14:00:46 -04:00
|
|
|
request.env[NO_CUSTOM] = !!safe_mode.include?(NO_CUSTOM)
|
|
|
|
request.env[NO_PLUGINS] = !!safe_mode.include?(NO_PLUGINS)
|
|
|
|
request.env[ONLY_OFFICIAL] = !!safe_mode.include?(ONLY_OFFICIAL)
|
2017-04-14 13:35:12 -04:00
|
|
|
end
|
|
|
|
end
|
2015-01-06 01:39:08 -05:00
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
def handle_theme
|
|
|
|
|
2018-01-17 00:32:52 -05:00
|
|
|
return if request.xhr? || request.format == "json" || request.format == "js"
|
2017-04-14 13:35:12 -04:00
|
|
|
return if request.method != "GET"
|
|
|
|
|
|
|
|
resolve_safe_mode
|
|
|
|
return if request.env[NO_CUSTOM]
|
|
|
|
|
2018-03-15 01:17:09 -04:00
|
|
|
theme_key = request[:preview_theme_key]
|
2017-05-12 12:41:26 -04:00
|
|
|
|
2017-05-15 12:48:08 -04:00
|
|
|
user_option = current_user&.user_option
|
|
|
|
|
|
|
|
unless theme_key
|
|
|
|
key, seq = cookies[:theme_key]&.split(",")
|
2017-05-24 11:39:01 -04:00
|
|
|
if key && seq && seq.to_i == user_option&.theme_key_seq.to_i
|
2017-05-15 12:48:08 -04:00
|
|
|
theme_key = key
|
|
|
|
end
|
2017-05-12 12:41:26 -04:00
|
|
|
end
|
2017-04-14 13:35:12 -04:00
|
|
|
|
2017-05-15 12:48:08 -04:00
|
|
|
theme_key ||= user_option&.theme_key
|
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
if theme_key && !guardian.allow_theme?(theme_key)
|
|
|
|
theme_key = nil
|
2014-06-20 12:06:36 -04:00
|
|
|
end
|
2015-01-06 01:39:08 -05:00
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
theme_key ||= SiteSetting.default_theme_key
|
|
|
|
theme_key = nil if theme_key.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-04-14 13:35:12 -04:00
|
|
|
@theme_key = request.env[:resolved_theme_key] = theme_key
|
2013-11-12 12:13:17 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(current_user)
|
|
|
|
end
|
|
|
|
|
2015-06-08 12:07:35 -04:00
|
|
|
def current_homepage
|
2017-11-09 14:45:19 -05:00
|
|
|
current_user&.user_option&.homepage || SiteSetting.anonymous_homepage
|
2015-06-08 12:07:35 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def serialize_data(obj, serializer, opts = nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
# If it's an array, apply the serializer as an each_serializer to the elements
|
2017-07-27 21:20:09 -04:00
|
|
|
serializer_opts = { scope: guardian }.merge!(opts || {})
|
2014-04-16 11:18:09 -04:00
|
|
|
if obj.respond_to?(:to_ary)
|
2013-02-05 14:16:51 -05:00
|
|
|
serializer_opts[:each_serializer] = serializer
|
2014-04-16 11:18:09 -04:00
|
|
|
ActiveModel::ArraySerializer.new(obj.to_ary, serializer_opts).as_json
|
2013-02-07 10:45:24 -05:00
|
|
|
else
|
2013-05-29 16:49:34 -04:00
|
|
|
serializer.new(obj, serializer_opts).as_json
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-29 16:49:34 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-29 16:49:34 -04:00
|
|
|
# This is odd, but it seems that in Rails `render json: obj` is about
|
|
|
|
# 20% slower than calling MultiJSON.dump ourselves. I'm not sure why
|
|
|
|
# Rails doesn't call MultiJson.dump when you pass it json: obj but
|
|
|
|
# it seems we don't need whatever Rails is doing.
|
2017-07-27 21:20:09 -04:00
|
|
|
def render_serialized(obj, serializer, opts = nil)
|
2015-03-31 12:58:56 -04:00
|
|
|
render_json_dump(serialize_data(obj, serializer, opts), opts)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def render_json_dump(obj, opts = nil)
|
2015-03-31 12:58:56 -04:00
|
|
|
opts ||= {}
|
2015-04-27 13:52:37 -04:00
|
|
|
if opts[:rest_serializer]
|
|
|
|
obj['__rest_serializer'] = "1"
|
|
|
|
opts.each do |k, v|
|
|
|
|
obj[k] = v if k.to_s.start_with?("refresh_")
|
|
|
|
end
|
2015-11-30 15:22:58 -05:00
|
|
|
|
|
|
|
obj['extras'] = opts[:extras] if opts[:extras]
|
2017-09-11 16:44:20 -04:00
|
|
|
obj['meta'] = opts[:meta] if opts[:meta]
|
2015-04-27 13:52:37 -04:00
|
|
|
end
|
|
|
|
|
2015-03-31 12:58:56 -04:00
|
|
|
render json: MultiJson.dump(obj), status: opts[:status] || 200
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-06 11:55:54 -05:00
|
|
|
def can_cache_content?
|
2015-10-28 15:11:36 -04:00
|
|
|
current_user.blank? && flash[:authentication_data].blank?
|
2013-02-06 11:55:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Our custom cache method
|
|
|
|
def discourse_expires_in(time_length)
|
|
|
|
return unless can_cache_content?
|
2014-01-04 02:53:27 -05:00
|
|
|
Middleware::AnonymousCache.anon_cache(request.env, time_length)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def fetch_user_from_params(opts = nil, eager_load = [])
|
2014-08-28 12:07:13 -04:00
|
|
|
opts ||= {}
|
2014-06-18 14:40:15 -04:00
|
|
|
user = if params[:username]
|
2016-06-10 22:37:33 -04:00
|
|
|
username_lower = params[:username].downcase.chomp('.json')
|
2016-01-15 06:16:00 -05:00
|
|
|
find_opts = { username_lower: username_lower }
|
2016-01-15 06:34:28 -05:00
|
|
|
find_opts[:active] = true unless opts[:include_inactive] || current_user.try(:staff?)
|
2016-06-27 22:01:00 -04:00
|
|
|
result = User
|
|
|
|
(result = result.includes(*eager_load)) if !eager_load.empty?
|
|
|
|
result.find_by(find_opts)
|
2014-06-18 14:40:15 -04:00
|
|
|
elsif params[:external_id]
|
2016-06-10 22:37:33 -04:00
|
|
|
external_id = params[:external_id].chomp('.json')
|
2015-02-02 12:58:02 -05:00
|
|
|
SingleSignOnRecord.find_by(external_id: external_id).try(:user)
|
2014-06-18 14:40:15 -04:00
|
|
|
end
|
2015-05-06 21:00:51 -04:00
|
|
|
raise Discourse::NotFound if user.blank?
|
2013-05-22 11:20:16 -04:00
|
|
|
|
|
|
|
guardian.ensure_can_see!(user)
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2013-09-04 11:53:00 -04:00
|
|
|
def post_ids_including_replies
|
2018-01-22 11:23:19 -05:00
|
|
|
post_ids = params[:post_ids].map(&:to_i)
|
|
|
|
post_ids |= PostReply.where(post_id: params[:reply_post_ids]).pluck(:reply_id) if params[:reply_post_ids]
|
2013-09-04 11:53:00 -04:00
|
|
|
post_ids
|
|
|
|
end
|
|
|
|
|
2015-05-22 02:15:46 -04:00
|
|
|
def no_cookies
|
|
|
|
# do your best to ensure response has no cookies
|
|
|
|
# longer term we may want to push this into middleware
|
|
|
|
headers.delete 'Set-Cookie'
|
|
|
|
request.session_options[:skip] = true
|
|
|
|
end
|
|
|
|
|
2016-10-04 12:01:42 -04:00
|
|
|
def permalink_redirect_or_not_found
|
|
|
|
url = request.fullpath
|
|
|
|
permalink = Permalink.find_by_url(url)
|
|
|
|
|
|
|
|
if permalink.present?
|
|
|
|
# permalink present, redirect to that URL
|
|
|
|
if permalink.external_url
|
|
|
|
redirect_to permalink.external_url, status: :moved_permanently
|
|
|
|
elsif permalink.target_url
|
|
|
|
redirect_to "#{Discourse::base_uri}#{permalink.target_url}", status: :moved_permanently
|
|
|
|
else
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# redirect to 404
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-19 02:00:22 -05:00
|
|
|
def secure_session
|
|
|
|
SecureSession.new(session["secure_session_id"] ||= SecureRandom.hex)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
private
|
|
|
|
|
2017-09-07 01:29:30 -04:00
|
|
|
def check_readonly_mode
|
|
|
|
@readonly_mode = Discourse.readonly_mode?
|
|
|
|
end
|
|
|
|
|
2016-02-06 14:49:39 -05:00
|
|
|
def locale_from_header
|
|
|
|
begin
|
|
|
|
# Rails I18n uses underscores between the locale and the region; the request
|
|
|
|
# headers use hyphens.
|
2016-03-04 07:17:14 -05:00
|
|
|
require 'http_accept_language' unless defined? HttpAcceptLanguage
|
2016-06-10 22:37:33 -04:00
|
|
|
available_locales = I18n.available_locales.map { |locale| locale.to_s.tr('_', '-') }
|
2016-03-04 07:17:14 -05:00
|
|
|
parser = HttpAcceptLanguage::Parser.new(request.env["HTTP_ACCEPT_LANGUAGE"])
|
2016-06-10 22:37:33 -04:00
|
|
|
parser.language_region_compatible_from(available_locales).tr('-', '_')
|
2016-02-06 14:49:39 -05:00
|
|
|
rescue
|
|
|
|
# If Accept-Language headers are not set.
|
|
|
|
I18n.default_locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-05 16:25:44 -04:00
|
|
|
def preload_anonymous_data
|
2014-02-24 14:24:18 -05:00
|
|
|
store_preloaded("site", Site.json_for(guardian))
|
2013-08-05 16:25:44 -04:00
|
|
|
store_preloaded("siteSettings", SiteSetting.client_settings_json)
|
2018-03-04 19:04:23 -05:00
|
|
|
store_preloaded("themeSettings", Theme.settings_for_client(@theme_key))
|
2013-12-17 12:49:22 -05:00
|
|
|
store_preloaded("customHTML", custom_html_json)
|
2014-06-18 14:04:10 -04:00
|
|
|
store_preloaded("banner", banner_json)
|
2014-12-22 19:12:26 -05:00
|
|
|
store_preloaded("customEmoji", custom_emoji)
|
2015-12-23 12:09:18 -05:00
|
|
|
store_preloaded("translationOverrides", I18n.client_overrides_json(I18n.locale))
|
2013-08-05 16:25:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def preload_current_user_data
|
2013-11-06 12:56:26 -05:00
|
|
|
store_preloaded("currentUser", MultiJson.dump(CurrentUserSerializer.new(current_user, scope: guardian, root: false)))
|
2016-12-02 01:03:31 -05:00
|
|
|
report = TopicTrackingState.report(current_user)
|
2015-09-06 21:57:50 -04:00
|
|
|
serializer = ActiveModel::ArraySerializer.new(report, each_serializer: TopicTrackingStateSerializer)
|
2013-08-05 16:25:44 -04:00
|
|
|
store_preloaded("topicTrackingStates", MultiJson.dump(serializer))
|
|
|
|
end
|
|
|
|
|
2013-12-17 12:49:22 -05:00
|
|
|
def custom_html_json
|
2015-02-09 11:48:42 -05:00
|
|
|
target = view_context.mobile_view? ? :mobile : :desktop
|
2017-07-27 21:20:09 -04:00
|
|
|
|
|
|
|
data =
|
|
|
|
if @theme_key
|
|
|
|
{
|
|
|
|
top: Theme.lookup_field(@theme_key, target, "after_header"),
|
|
|
|
footer: Theme.lookup_field(@theme_key, target, "footer")
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
2014-06-04 21:39:33 -04:00
|
|
|
|
|
|
|
if DiscoursePluginRegistry.custom_html
|
|
|
|
data.merge! DiscoursePluginRegistry.custom_html
|
|
|
|
end
|
|
|
|
|
2017-11-03 11:32:32 -04:00
|
|
|
DiscoursePluginRegistry.html_builders.each do |name, _|
|
2017-08-07 11:28:56 -04:00
|
|
|
if name.start_with?("client:")
|
2017-11-03 11:32:32 -04:00
|
|
|
data[name.sub(/^client:/, '')] = DiscoursePluginRegistry.build_html(name, self)
|
2017-08-07 11:28:56 -04:00
|
|
|
end
|
2017-04-17 15:47:21 -04:00
|
|
|
end
|
|
|
|
|
2014-06-04 21:39:33 -04:00
|
|
|
MultiJson.dump(data)
|
2013-12-17 12:25:27 -05:00
|
|
|
end
|
|
|
|
|
2014-11-13 23:39:17 -05:00
|
|
|
def self.banner_json_cache
|
|
|
|
@banner_json_cache ||= DistributedCache.new("banner_json")
|
|
|
|
end
|
|
|
|
|
2014-06-18 14:04:10 -04:00
|
|
|
def banner_json
|
2014-11-13 23:39:17 -05:00
|
|
|
json = ApplicationController.banner_json_cache["json"]
|
|
|
|
|
|
|
|
unless json
|
2017-02-24 06:56:13 -05:00
|
|
|
topic = Topic.where(archetype: Archetype.banner).first
|
2014-11-13 23:39:17 -05:00
|
|
|
banner = topic.present? ? topic.banner : {}
|
|
|
|
ApplicationController.banner_json_cache["json"] = json = MultiJson.dump(banner)
|
|
|
|
end
|
|
|
|
|
|
|
|
json
|
2014-06-18 14:04:10 -04:00
|
|
|
end
|
|
|
|
|
2014-12-22 19:12:26 -05:00
|
|
|
def custom_emoji
|
|
|
|
serializer = ActiveModel::ArraySerializer.new(Emoji.custom, each_serializer: EmojiSerializer)
|
|
|
|
MultiJson.dump(serializer)
|
|
|
|
end
|
|
|
|
|
2015-02-08 15:25:03 -05:00
|
|
|
# Render action for a JSON error.
|
|
|
|
#
|
2018-03-13 11:12:41 -04:00
|
|
|
# obj - a translated string, an ActiveRecord model, or an array of translated strings
|
2015-02-23 00:28:50 -05:00
|
|
|
# opts:
|
2018-03-13 11:12:41 -04:00
|
|
|
# type - a machine-readable description of the error
|
|
|
|
# status - HTTP status code to return
|
|
|
|
# headers - extra headers for the response
|
2017-07-27 21:20:09 -04:00
|
|
|
def render_json_error(obj, opts = {})
|
2017-04-15 00:11:02 -04:00
|
|
|
opts = { status: opts } if opts.is_a?(Integer)
|
2018-03-13 11:12:41 -04:00
|
|
|
opts.fetch(:headers, {}).each { |name, value| headers[name.to_s] = value }
|
|
|
|
|
2017-09-06 10:21:07 -04:00
|
|
|
render json: MultiJson.dump(create_errors_json(obj, opts)), status: opts[:status] || 422
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def success_json
|
2014-06-18 14:04:10 -04:00
|
|
|
{ success: 'OK' }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def failed_json
|
2014-06-18 14:04:10 -04:00
|
|
|
{ failed: 'FAILED' }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def json_result(obj, opts = {})
|
2013-02-05 14:16:51 -05:00
|
|
|
if yield(obj)
|
|
|
|
json = success_json
|
|
|
|
|
|
|
|
# If we were given a serializer, add the class to the json that comes back
|
|
|
|
if opts[:serializer].present?
|
2013-04-11 20:07:46 -04:00
|
|
|
json[obj.class.name.underscore] = opts[:serializer].new(obj, scope: guardian).serializable_hash
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: MultiJson.dump(json)
|
|
|
|
else
|
2014-09-08 15:17:31 -04:00
|
|
|
error_obj = nil
|
|
|
|
if opts[:additional_errors]
|
|
|
|
error_target = opts[:additional_errors].find do |o|
|
|
|
|
target = obj.send(o)
|
|
|
|
target && target.errors.present?
|
|
|
|
end
|
|
|
|
error_obj = obj.send(error_target) if error_target
|
|
|
|
end
|
|
|
|
render_json_error(error_obj || obj)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def mini_profiler_enabled?
|
2017-09-05 23:26:03 -04:00
|
|
|
defined?(Rack::MiniProfiler) && (guardian.is_developer? || Rails.env.development?)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_mini_profiler
|
|
|
|
return unless mini_profiler_enabled?
|
|
|
|
Rack::MiniProfiler.authorize_request
|
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
def check_xhr
|
2013-08-05 16:25:44 -04:00
|
|
|
# bypass xhr check on PUT / POST / DELETE provided api key is there, otherwise calling api is annoying
|
2016-12-15 20:05:20 -05:00
|
|
|
return if !request.get? && (is_api? || is_user_api?)
|
2013-08-05 16:25:44 -04:00
|
|
|
raise RenderEmpty.new unless ((request.format && request.format.json?) || request.xhr?)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2018-01-31 23:17:59 -05:00
|
|
|
def self.requires_login(arg = {})
|
|
|
|
@requires_login_arg = arg
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.requires_login_arg
|
|
|
|
@requires_login_arg
|
|
|
|
end
|
|
|
|
|
|
|
|
def block_if_requires_login
|
|
|
|
if arg = self.class.requires_login_arg
|
|
|
|
check =
|
|
|
|
if except = arg[:except]
|
|
|
|
!except.include?(action_name.to_sym)
|
|
|
|
elsif only = arg[:only]
|
|
|
|
only.include?(action_name.to_sym)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
ensure_logged_in if check
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def ensure_logged_in
|
|
|
|
raise Discourse::NotLoggedIn.new unless current_user.present?
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2015-04-10 17:00:50 -04:00
|
|
|
def ensure_staff
|
|
|
|
raise Discourse::InvalidAccess.new unless current_user && current_user.staff?
|
|
|
|
end
|
|
|
|
|
2016-09-22 11:12:34 -04:00
|
|
|
def ensure_admin
|
|
|
|
raise Discourse::InvalidAccess.new unless current_user && current_user.admin?
|
|
|
|
end
|
|
|
|
|
2016-09-14 16:36:08 -04:00
|
|
|
def ensure_wizard_enabled
|
|
|
|
raise Discourse::InvalidAccess.new unless SiteSetting.wizard_enabled?
|
|
|
|
end
|
|
|
|
|
2015-08-11 11:27:56 -04:00
|
|
|
def destination_url
|
|
|
|
request.original_url unless request.original_url =~ /uploads/
|
|
|
|
end
|
|
|
|
|
2013-06-04 18:32:36 -04:00
|
|
|
def redirect_to_login_if_required
|
2016-08-25 20:37:02 -04:00
|
|
|
return if current_user || (request.format.json? && is_api?)
|
2014-01-24 07:47:35 -05:00
|
|
|
|
2015-02-23 17:10:44 -05:00
|
|
|
if SiteSetting.login_required?
|
2017-05-25 14:10:15 -04:00
|
|
|
flash.keep
|
|
|
|
|
2015-02-23 17:10:44 -05:00
|
|
|
if SiteSetting.enable_sso?
|
2015-08-11 11:27:56 -04:00
|
|
|
# save original URL in a session so we can redirect after login
|
|
|
|
session[:destination_url] = destination_url
|
2015-03-08 20:45:36 -04:00
|
|
|
redirect_to path('/session/sso')
|
2018-03-27 07:52:07 -04:00
|
|
|
elsif params[:authComplete].present?
|
|
|
|
redirect_to path("/login?authComplete=true")
|
2015-02-23 17:10:44 -05:00
|
|
|
else
|
2015-08-11 11:27:56 -04:00
|
|
|
# save original URL in a cookie (javascript redirects after login in this case)
|
|
|
|
cookies[:destination_url] = destination_url
|
2018-03-07 12:43:54 -05:00
|
|
|
redirect_to path("/login")
|
2015-02-23 17:10:44 -05:00
|
|
|
end
|
2015-02-23 16:20:36 -05:00
|
|
|
end
|
2013-06-04 18:32:36 -04:00
|
|
|
end
|
|
|
|
|
2014-02-12 23:37:28 -05:00
|
|
|
def block_if_readonly_mode
|
2015-03-08 20:45:36 -04:00
|
|
|
return if request.fullpath.start_with?(path "/admin/backups")
|
2017-09-07 01:29:30 -04:00
|
|
|
raise Discourse::ReadOnly.new if !(request.get? || request.head?) && @readonly_mode
|
2014-02-12 23:37:28 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def build_not_found_page(status = 404, layout = false)
|
2017-11-21 16:13:09 -05:00
|
|
|
if SiteSetting.bootstrap_error_pages?
|
|
|
|
preload_json
|
|
|
|
layout = 'application' if layout == 'no_ember'
|
|
|
|
end
|
|
|
|
|
2014-07-04 16:18:09 -04:00
|
|
|
category_topic_ids = Category.pluck(:topic_id).compact
|
2015-07-28 04:02:39 -04:00
|
|
|
@container_class = "wrap not-found-container"
|
2017-07-27 21:20:09 -04:00
|
|
|
@top_viewed = TopicQuery.new(nil, except_topic_ids: category_topic_ids).list_top_for("monthly").topics.first(10)
|
2017-09-28 03:07:09 -04:00
|
|
|
@recent = Topic.includes(:category).where.not(id: category_topic_ids).recent(10)
|
2013-05-30 14:46:02 -04:00
|
|
|
@slug = params[:slug].class == String ? params[:slug] : ''
|
2013-06-06 14:41:27 -04:00
|
|
|
@slug = (params[:id].class == String ? params[:id] : '') if @slug.blank?
|
2017-07-27 21:20:09 -04:00
|
|
|
@slug.tr!('-', ' ')
|
2018-05-23 16:58:47 -04:00
|
|
|
@hide_google = true if SiteSetting.login_required
|
2013-07-11 16:38:46 -04:00
|
|
|
render_to_string status: status, layout: layout, formats: [:html], template: '/exceptions/not_found'
|
2013-05-30 14:46:02 -04:00
|
|
|
end
|
|
|
|
|
2018-03-05 23:20:39 -05:00
|
|
|
def is_asset_path
|
|
|
|
request.env['DISCOURSE_IS_ASSET_PATH'] = 1
|
|
|
|
end
|
|
|
|
|
2013-06-19 22:11:14 -04:00
|
|
|
protected
|
2013-06-17 02:09:59 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def render_post_json(post, add_raw = true)
|
2014-09-02 17:37:19 -04:00
|
|
|
post_serializer = PostSerializer.new(post, scope: guardian, root: false)
|
2014-09-24 22:02:41 -04:00
|
|
|
post_serializer.add_raw = add_raw
|
2014-09-02 17:37:19 -04:00
|
|
|
|
|
|
|
counts = PostAction.counts_for([post], current_user)
|
|
|
|
if counts && counts = counts[post.id]
|
|
|
|
post_serializer.post_actions = counts
|
|
|
|
end
|
|
|
|
render_json_dump(post_serializer)
|
|
|
|
end
|
|
|
|
|
2013-06-19 22:11:14 -04:00
|
|
|
# returns an array of integers given a param key
|
|
|
|
# returns nil if key is not found
|
|
|
|
def param_to_integer_list(key, delimiter = ',')
|
|
|
|
if params[key]
|
|
|
|
params[key].split(delimiter).map(&:to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|