2013-10-09 14:22:41 +08:00
|
|
|
require 'current_user'
|
2013-10-09 12:10:37 +08:00
|
|
|
require_dependency 'canonical_url'
|
2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'discourse'
|
|
|
|
require_dependency 'custom_renderer'
|
2013-10-09 12:10:37 +08:00
|
|
|
require_dependency 'archetype'
|
2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'rate_limiter'
|
2014-02-21 05:07:02 +08:00
|
|
|
require_dependency 'crawler_detection'
|
2014-04-03 01:22:10 +08:00
|
|
|
require_dependency 'json_error'
|
2014-05-30 12:17:35 +08:00
|
|
|
require_dependency 'letter_avatar'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
class ApplicationController < ActionController::Base
|
|
|
|
include CurrentUser
|
2013-02-13 19:04:43 +08:00
|
|
|
include CanonicalURL::ControllerExtensions
|
2014-04-03 01:22:10 +08:00
|
|
|
include JsonError
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
serialization_scope :guardian
|
|
|
|
|
|
|
|
protect_from_forgery
|
|
|
|
|
2013-07-29 13:13:13 +08: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
|
|
|
|
unless is_api?
|
|
|
|
super
|
|
|
|
clear_current_user
|
2013-08-27 13:56:12 +08:00
|
|
|
render text: "['BAD CSRF']", status: 403
|
2013-07-29 13:13:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-12 13:27:58 +08:00
|
|
|
before_filter :set_current_user_for_logs
|
2013-12-04 19:49:54 +08:00
|
|
|
before_filter :set_locale
|
2013-12-19 03:47:22 +08:00
|
|
|
before_filter :set_mobile_view
|
2013-02-06 03:16:51 +08:00
|
|
|
before_filter :inject_preview_style
|
2013-11-13 01:13:17 +08:00
|
|
|
before_filter :disable_customization
|
2014-02-13 12:37:28 +08:00
|
|
|
before_filter :block_if_readonly_mode
|
2013-02-06 03:16:51 +08:00
|
|
|
before_filter :authorize_mini_profiler
|
|
|
|
before_filter :store_incoming_links
|
|
|
|
before_filter :preload_json
|
|
|
|
before_filter :check_xhr
|
2013-06-05 06:32:36 +08:00
|
|
|
before_filter :redirect_to_login_if_required
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2014-02-15 06:10:08 +08:00
|
|
|
layout :set_layout
|
|
|
|
|
2014-02-21 06:02:26 +08:00
|
|
|
def has_escaped_fragment?
|
|
|
|
SiteSetting.enable_escaped_fragments? && params.key?("_escaped_fragment_")
|
|
|
|
end
|
|
|
|
|
2014-02-15 06:10:08 +08:00
|
|
|
def set_layout
|
2014-02-21 06:02:26 +08:00
|
|
|
has_escaped_fragment? || CrawlerDetection.crawler?(request.user_agent) ? 'crawler' : 'application'
|
2014-02-15 06:10:08 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
rescue_from Exception do |exception|
|
2013-11-13 01:13:17 +08:00
|
|
|
unless [ActiveRecord::RecordNotFound,
|
|
|
|
ActionController::RoutingError,
|
|
|
|
ActionController::UnknownController,
|
|
|
|
AbstractController::ActionNotFound].include? exception.class
|
2013-02-07 23:45:24 +08:00
|
|
|
begin
|
2013-02-06 03:16:51 +08:00
|
|
|
ErrorLog.report_async!(exception, self, request, current_user)
|
2013-02-07 23:45:24 +08:00
|
|
|
rescue
|
2013-02-06 03:16:51 +08:00
|
|
|
# dont care give up
|
|
|
|
end
|
|
|
|
end
|
|
|
|
raise
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Some exceptions
|
|
|
|
class RenderEmpty < Exception; end
|
|
|
|
|
|
|
|
# Render nothing unless we are an xhr request
|
|
|
|
rescue_from RenderEmpty do
|
|
|
|
render 'default/empty'
|
|
|
|
end
|
|
|
|
|
|
|
|
# If they hit the rate limiter
|
|
|
|
rescue_from RateLimiter::LimitExceeded do |e|
|
|
|
|
|
|
|
|
time_left = ""
|
|
|
|
if e.available_in < 1.minute.to_i
|
|
|
|
time_left = I18n.t("rate_limiter.seconds", count: e.available_in)
|
|
|
|
elsif e.available_in < 1.hour.to_i
|
2013-02-07 23:45:24 +08:00
|
|
|
time_left = I18n.t("rate_limiter.minutes", count: (e.available_in / 1.minute.to_i))
|
2013-02-06 03:16:51 +08:00
|
|
|
else
|
2013-02-07 23:45:24 +08:00
|
|
|
time_left = I18n.t("rate_limiter.hours", count: (e.available_in / 1.hour.to_i))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: {errors: [I18n.t("rate_limiter.too_many_requests", time_left: time_left)]}, status: 429
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from Discourse::NotLoggedIn do |e|
|
|
|
|
raise e if Rails.env.test?
|
2014-03-06 00:48:06 +08:00
|
|
|
|
|
|
|
if request.get?
|
|
|
|
redirect_to "/"
|
|
|
|
else
|
|
|
|
render status: 403, json: failed_json.merge(message: I18n.t(:not_logged_in))
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from Discourse::NotFound do
|
2014-01-07 23:32:09 +08:00
|
|
|
rescue_discourse_actions("[error: 'not found']", 404) # TODO: this breaks json responses
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from Discourse::InvalidAccess do
|
2014-02-27 06:53:44 +08:00
|
|
|
rescue_discourse_actions("[error: 'invalid access']", 403, true) # TODO: this breaks json responses
|
2013-06-20 23:47:33 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:37:28 +08:00
|
|
|
rescue_from Discourse::ReadOnly do
|
2014-02-14 00:34:24 +08:00
|
|
|
render status: 405, json: failed_json.merge(message: I18n.t("read_only_mode_enabled"))
|
2014-02-13 12:37:28 +08:00
|
|
|
end
|
|
|
|
|
2014-02-27 06:53:44 +08:00
|
|
|
def rescue_discourse_actions(message, error, include_ember=false)
|
2013-05-31 02:46:02 +08:00
|
|
|
if request.format && request.format.json?
|
2014-01-07 23:32:09 +08:00
|
|
|
# TODO: this doesn't make sense. Stuffing an html page into a json response will cause
|
|
|
|
# $.parseJSON to fail in the browser. Also returning text like "[error: 'invalid access']"
|
|
|
|
# from the above rescue_from blocks will fail because that isn't valid json.
|
2013-07-12 04:38:46 +08:00
|
|
|
render status: error, layout: false, text: (error == 404) ? build_not_found_page(error) : message
|
2013-05-31 02:46:02 +08:00
|
|
|
else
|
2014-02-27 06:53:44 +08:00
|
|
|
render text: build_not_found_page(error, include_ember ? 'application' : 'no_js')
|
2013-05-31 02:46:02 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-05-12 13:27:58 +08:00
|
|
|
def set_current_user_for_logs
|
|
|
|
if current_user
|
|
|
|
Logster.add_to_env(request.env,"username",current_user.username)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-01 03:31:39 +08:00
|
|
|
def set_locale
|
2014-02-09 09:35:46 +08:00
|
|
|
I18n.locale = if SiteSetting.allow_user_locale && current_user && current_user.locale.present?
|
2014-02-08 11:24:10 +08:00
|
|
|
current_user.locale
|
|
|
|
else
|
|
|
|
SiteSetting.default_locale
|
|
|
|
end
|
2013-03-01 03:31:39 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def store_preloaded(key, json)
|
|
|
|
@preloaded ||= {}
|
2013-02-26 00:42:20 +08: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 14:28:21 +08:00
|
|
|
# to inject this safty deeper in the library or even in AM serializer
|
|
|
|
@preloaded[key] = json.gsub("</", "<\\/")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# If we are rendering HTML, preload the session data
|
|
|
|
def preload_json
|
2013-06-05 00:56:12 +08:00
|
|
|
# We don't preload JSON on xhr or JSON request
|
|
|
|
return if request.xhr?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-08-06 04:25:44 +08:00
|
|
|
preload_anonymous_data
|
2013-06-05 00:56:12 +08:00
|
|
|
|
2013-08-06 04:25:44 +08:00
|
|
|
if current_user
|
|
|
|
preload_current_user_data
|
|
|
|
current_user.sync_notification_channel_position
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-19 03:47:22 +08:00
|
|
|
def set_mobile_view
|
|
|
|
session[:mobile_view] = params[:mobile_view] if params.has_key?(:mobile_view)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def inject_preview_style
|
|
|
|
style = request['preview-style']
|
2013-02-07 23:45:24 +08:00
|
|
|
session[:preview_style] = style if style
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-11-13 01:13:17 +08:00
|
|
|
def disable_customization
|
|
|
|
session[:disable_customization] = params[:customization] == "0" if params.has_key?(:customization)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def guardian
|
|
|
|
@guardian ||= Guardian.new(current_user)
|
|
|
|
end
|
|
|
|
|
2013-05-30 04:49:34 +08:00
|
|
|
def serialize_data(obj, serializer, opts={})
|
2013-02-06 03:16:51 +08:00
|
|
|
# If it's an array, apply the serializer as an each_serializer to the elements
|
|
|
|
serializer_opts = {scope: guardian}.merge!(opts)
|
2014-04-16 23:18:09 +08:00
|
|
|
if obj.respond_to?(:to_ary)
|
2013-02-06 03:16:51 +08:00
|
|
|
serializer_opts[:each_serializer] = serializer
|
2014-04-16 23:18:09 +08:00
|
|
|
ActiveModel::ArraySerializer.new(obj.to_ary, serializer_opts).as_json
|
2013-02-07 23:45:24 +08:00
|
|
|
else
|
2013-05-30 04:49:34 +08:00
|
|
|
serializer.new(obj, serializer_opts).as_json
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-30 04:49:34 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-30 04:49:34 +08: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.
|
|
|
|
def render_serialized(obj, serializer, opts={})
|
|
|
|
render_json_dump(serialize_data(obj, serializer, opts))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_json_dump(obj)
|
|
|
|
render json: MultiJson.dump(obj)
|
|
|
|
end
|
|
|
|
|
2013-02-07 00:55:54 +08:00
|
|
|
def can_cache_content?
|
2013-10-16 13:39:18 +08:00
|
|
|
!current_user.present?
|
2013-02-07 00:55:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Our custom cache method
|
|
|
|
def discourse_expires_in(time_length)
|
|
|
|
return unless can_cache_content?
|
2014-01-04 15:53:27 +08:00
|
|
|
Middleware::AnonymousCache.anon_cache(request.env, time_length)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-22 23:20:16 +08:00
|
|
|
def fetch_user_from_params
|
|
|
|
username_lower = params[:username].downcase
|
|
|
|
username_lower.gsub!(/\.json$/, '')
|
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
user = User.find_by(username_lower: username_lower)
|
2013-05-22 23:20:16 +08:00
|
|
|
raise Discourse::NotFound.new if user.blank?
|
|
|
|
|
|
|
|
guardian.ensure_can_see!(user)
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2013-09-04 23:53:00 +08:00
|
|
|
def post_ids_including_replies
|
|
|
|
post_ids = params[:post_ids].map {|p| p.to_i}
|
|
|
|
if params[:reply_post_ids]
|
|
|
|
post_ids << PostReply.where(post_id: params[:reply_post_ids].map {|p| p.to_i}).pluck(:reply_id)
|
|
|
|
post_ids.flatten!
|
|
|
|
post_ids.uniq!
|
|
|
|
end
|
|
|
|
post_ids
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
private
|
|
|
|
|
2013-08-06 04:25:44 +08:00
|
|
|
def preload_anonymous_data
|
2014-02-25 03:24:18 +08:00
|
|
|
store_preloaded("site", Site.json_for(guardian))
|
2013-08-06 04:25:44 +08:00
|
|
|
store_preloaded("siteSettings", SiteSetting.client_settings_json)
|
2013-12-18 01:49:22 +08:00
|
|
|
store_preloaded("customHTML", custom_html_json)
|
2013-08-06 04:25:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def preload_current_user_data
|
2013-11-07 01:56:26 +08:00
|
|
|
store_preloaded("currentUser", MultiJson.dump(CurrentUserSerializer.new(current_user, scope: guardian, root: false)))
|
2013-08-06 04:25:44 +08:00
|
|
|
serializer = ActiveModel::ArraySerializer.new(TopicTrackingState.report([current_user.id]), each_serializer: TopicTrackingStateSerializer)
|
|
|
|
store_preloaded("topicTrackingStates", MultiJson.dump(serializer))
|
|
|
|
end
|
|
|
|
|
2013-12-18 01:49:22 +08:00
|
|
|
def custom_html_json
|
2014-06-05 09:39:33 +08:00
|
|
|
data = {
|
2013-12-18 01:25:27 +08:00
|
|
|
top: SiteContent.content_for(:top),
|
2014-02-08 05:04:00 +08:00
|
|
|
bottom: SiteContent.content_for(:bottom)
|
2014-06-05 09:39:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if SiteSetting.tos_accept_required && !current_user
|
|
|
|
data[:tos_signup_form_message] = SiteContent.content_for(:tos_signup_form_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
if DiscoursePluginRegistry.custom_html
|
|
|
|
data.merge! DiscoursePluginRegistry.custom_html
|
|
|
|
end
|
|
|
|
|
|
|
|
MultiJson.dump(data)
|
2013-12-18 01:25:27 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def render_json_error(obj)
|
2014-04-03 01:22:10 +08:00
|
|
|
render json: MultiJson.dump(create_errors_json(obj)), status: 422
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def success_json
|
|
|
|
{success: 'OK'}
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed_json
|
|
|
|
{failed: 'FAILED'}
|
|
|
|
end
|
|
|
|
|
|
|
|
def json_result(obj, opts={})
|
|
|
|
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-12 08:07:46 +08:00
|
|
|
json[obj.class.name.underscore] = opts[:serializer].new(obj, scope: guardian).serializable_hash
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: MultiJson.dump(json)
|
|
|
|
else
|
|
|
|
render_json_error(obj)
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def mini_profiler_enabled?
|
2013-03-05 08:42:44 +08:00
|
|
|
defined?(Rack::MiniProfiler) && current_user.try(:admin?)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_mini_profiler
|
|
|
|
return unless mini_profiler_enabled?
|
|
|
|
Rack::MiniProfiler.authorize_request
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_incoming_links
|
2013-04-26 14:18:41 +08:00
|
|
|
IncomingLink.add(request,current_user) unless request.xhr?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
def check_xhr
|
2013-08-06 04:25:44 +08:00
|
|
|
# bypass xhr check on PUT / POST / DELETE provided api key is there, otherwise calling api is annoying
|
|
|
|
return if !request.get? && api_key_valid?
|
|
|
|
raise RenderEmpty.new unless ((request.format && request.format.json?) || request.xhr?)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_logged_in
|
|
|
|
raise Discourse::NotLoggedIn.new unless current_user.present?
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-06-05 06:32:36 +08:00
|
|
|
def redirect_to_login_if_required
|
2014-01-24 20:47:35 +08:00
|
|
|
return if current_user || (request.format.json? && api_key_valid?)
|
|
|
|
|
|
|
|
redirect_to :login if SiteSetting.login_required?
|
2013-06-05 06:32:36 +08:00
|
|
|
end
|
|
|
|
|
2014-02-13 12:37:28 +08:00
|
|
|
def block_if_readonly_mode
|
2014-02-14 00:34:24 +08:00
|
|
|
return if request.fullpath.start_with?("/admin/backups")
|
|
|
|
raise Discourse::ReadOnly.new if !request.get? && Discourse.readonly_mode?
|
2014-02-13 12:37:28 +08:00
|
|
|
end
|
|
|
|
|
2013-07-12 04:38:46 +08:00
|
|
|
def build_not_found_page(status=404, layout=false)
|
2013-11-14 01:26:32 +08:00
|
|
|
@top_viewed = Topic.top_viewed(10)
|
|
|
|
@recent = Topic.recent(10)
|
2013-05-31 02:46:02 +08:00
|
|
|
@slug = params[:slug].class == String ? params[:slug] : ''
|
2013-06-07 02:41:27 +08:00
|
|
|
@slug = (params[:id].class == String ? params[:id] : '') if @slug.blank?
|
2013-05-31 02:46:02 +08:00
|
|
|
@slug.gsub!('-',' ')
|
2013-07-12 04:38:46 +08:00
|
|
|
render_to_string status: status, layout: layout, formats: [:html], template: '/exceptions/not_found'
|
2013-05-31 02:46:02 +08:00
|
|
|
end
|
|
|
|
|
2013-06-20 10:11:14 +08:00
|
|
|
protected
|
2013-06-17 14:09:59 +08:00
|
|
|
|
|
|
|
def api_key_valid?
|
2013-10-23 03:53:08 +08:00
|
|
|
request["api_key"] && ApiKey.where(key: request["api_key"]).exists?
|
2013-06-17 14:09:59 +08:00
|
|
|
end
|
|
|
|
|
2013-06-20 10:11:14 +08: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-06 03:16:51 +08:00
|
|
|
end
|