2015-07-17 22:04:53 -07:00
|
|
|
require 'ipaddr'
|
|
|
|
|
2010-05-14 15:12:31 -07:00
|
|
|
class ApplicationController < ActionController::Base
|
2012-12-29 22:46:36 -08:00
|
|
|
include FragmentLocalization
|
|
|
|
|
2010-05-14 15:12:31 -07:00
|
|
|
protect_from_forgery
|
2011-06-27 12:33:34 -07:00
|
|
|
|
2011-07-20 09:39:18 -07:00
|
|
|
helper_method :can_use_image_mode?, :user_is?
|
2012-12-29 22:46:36 -08:00
|
|
|
|
|
|
|
before_filter :set_locale
|
2023-07-29 10:39:07 -07:00
|
|
|
before_filter :login_as_test_user if Rails.env.development?
|
2011-06-27 12:33:34 -07:00
|
|
|
|
2011-07-12 21:25:14 -07:00
|
|
|
def authenticate_user! # too lazy to change references to login_path
|
|
|
|
redirect_to(login_path) unless user_signed_in?
|
|
|
|
end
|
|
|
|
|
2011-07-26 17:27:23 -07:00
|
|
|
def authorize_user!
|
|
|
|
raise AccessDenied unless user_signed_in? && current_user.id == params[:user_id].to_i
|
|
|
|
end
|
|
|
|
|
2011-06-27 12:33:34 -07:00
|
|
|
def can_use_image_mode?
|
2011-08-07 16:52:11 -07:00
|
|
|
true
|
2011-06-27 12:33:34 -07:00
|
|
|
end
|
2012-06-05 09:44:11 -07:00
|
|
|
|
2012-12-29 22:46:36 -08:00
|
|
|
def infer_locale
|
2013-01-11 09:07:11 -08:00
|
|
|
return params[:locale] if valid_locale?(params[:locale])
|
|
|
|
return cookies[:locale] if valid_locale?(cookies[:locale])
|
|
|
|
Rails.logger.debug "Preferred languages: #{http_accept_language.user_preferred_languages}"
|
2013-01-26 22:35:22 -08:00
|
|
|
http_accept_language.language_region_compatible_from(I18n.public_locales.map(&:to_s)) ||
|
2013-01-11 09:07:11 -08:00
|
|
|
I18n.default_locale
|
2012-12-29 22:46:36 -08:00
|
|
|
end
|
2013-12-08 20:59:36 -08:00
|
|
|
|
2015-07-17 22:04:53 -07:00
|
|
|
PRIVATE_IP_BLOCK = IPAddr.new('192.168.0.0/16')
|
2013-12-08 20:59:36 -08:00
|
|
|
def local_only
|
2015-07-17 22:04:53 -07:00
|
|
|
raise AccessDenied unless request.ip == '127.0.0.1' || PRIVATE_IP_BLOCK.include?(request.ip)
|
2013-12-08 20:59:36 -08:00
|
|
|
end
|
2012-12-29 22:46:36 -08:00
|
|
|
|
|
|
|
def localized_fragment_exist?(key)
|
|
|
|
localized_key = localize_fragment_key(key, locale)
|
|
|
|
fragment_exist?(localized_key)
|
|
|
|
end
|
|
|
|
|
2012-06-05 09:44:11 -07:00
|
|
|
def not_found(record_name='record')
|
|
|
|
raise ActionController::RoutingError.new("#{record_name} not found")
|
|
|
|
end
|
2011-07-15 13:15:57 -07:00
|
|
|
|
|
|
|
class AccessDenied < StandardError;end
|
|
|
|
|
|
|
|
rescue_from AccessDenied, :with => :on_access_denied
|
|
|
|
|
|
|
|
def on_access_denied
|
|
|
|
render :file => 'public/403.html', :layout => false, :status => :forbidden
|
|
|
|
end
|
2011-07-20 09:39:18 -07:00
|
|
|
|
2011-07-20 12:16:22 -07:00
|
|
|
def redirect_back!(default=:back)
|
|
|
|
redirect_to(params[:return_to] || default)
|
|
|
|
end
|
2012-12-29 22:46:36 -08:00
|
|
|
|
|
|
|
def set_locale
|
|
|
|
I18n.locale = infer_locale || I18n.default_locale
|
|
|
|
end
|
2011-07-20 12:16:22 -07:00
|
|
|
|
2011-07-20 09:39:18 -07:00
|
|
|
def user_is?(user)
|
|
|
|
user_signed_in? && user == current_user
|
|
|
|
end
|
2013-01-11 09:07:11 -08:00
|
|
|
|
|
|
|
def valid_locale?(locale)
|
2013-01-17 20:16:34 -08:00
|
|
|
locale && I18n.usable_locales.include?(locale.to_sym)
|
2013-01-11 09:07:11 -08:00
|
|
|
end
|
2023-07-29 10:39:07 -07:00
|
|
|
|
|
|
|
def login_as_test_user
|
|
|
|
test_user = User.find_by_name('test')
|
|
|
|
sign_in(:user, test_user, bypass: true)
|
|
|
|
end
|
2010-05-14 15:12:31 -07:00
|
|
|
end
|
2011-06-27 12:33:34 -07:00
|
|
|
|