impress/app/controllers/application_controller.rb

75 lines
1.8 KiB
Ruby
Raw Normal View History

require 'ipaddr'
2010-05-14 15:12:31 -07:00
class ApplicationController < ActionController::Base
include FragmentLocalization
2010-05-14 15:12:31 -07:00
protect_from_forgery
2011-06-27 12:33:34 -07:00
helper_method :current_user, :user_signed_in?
before_action :set_locale
2011-06-27 12:33:34 -07:00
def authenticate_user!
redirect_to(new_auth_user_session_path) unless user_signed_in?
2011-07-12 21:25:14 -07:00
end
def authorize_user!
raise AccessDenied unless user_signed_in? && current_user.id == params[:user_id].to_i
end
def current_user
if auth_user_signed_in?
User.where(remote_id: current_auth_user.id).first
else
nil
end
end
def user_signed_in?
auth_user_signed_in?
2011-06-27 12:33:34 -07:00
end
def infer_locale
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}"
http_accept_language.language_region_compatible_from(I18n.public_locales.map(&:to_s)) ||
I18n.default_locale
end
2013-12-08 20:59:36 -08:00
PRIVATE_IP_BLOCK = IPAddr.new('192.168.0.0/16')
2013-12-08 20:59:36 -08:00
def local_only
raise AccessDenied unless request.ip == '127.0.0.1' || PRIVATE_IP_BLOCK.include?(request.ip)
2013-12-08 20:59:36 -08:00
end
def localized_fragment_exist?(key)
localized_key = localize_fragment_key(key, locale)
fragment_exist?(localized_key)
end
def not_found(record_name='record')
raise ActionController::RoutingError.new("#{record_name} not found")
end
class AccessDenied < StandardError;end
rescue_from AccessDenied, :with => :on_access_denied
def on_access_denied
render template: 'public/403.html', :layout => false, :status => :forbidden
end
2011-07-20 12:16:22 -07:00
def redirect_back!(default=:back)
redirect_to(params[:return_to] || default)
end
def set_locale
I18n.locale = infer_locale || I18n.default_locale
end
def valid_locale?(locale)
locale && I18n.usable_locales.include?(locale.to_sym)
end
2010-05-14 15:12:31 -07:00
end
2011-06-27 12:33:34 -07:00