Matchu
4f564db785
Some important little upgrades but mostly straightforward! Note that there's still a known issue where item searches crash, I was hoping that this was a bug in Rails 4.2 that would be fixed on upgading to 5, but nope, oh well! Also uhh I just got a bit silly and didn't actually mean to go all the way to 5.2 in one go, I had meant to start at 5.0… but tbh the 5.1 and 5.2 changes seem small, and this seems to be working, so. Yeah ok let's roll!
76 lines
2 KiB
Ruby
76 lines
2 KiB
Ruby
require 'ipaddr'
|
|
|
|
class ApplicationController < ActionController::Base
|
|
include FragmentLocalization
|
|
|
|
protect_from_forgery
|
|
|
|
helper_method :can_use_image_mode?, :user_is?
|
|
|
|
before_action :set_locale
|
|
before_action :login_as_test_user if Rails.env.development?
|
|
|
|
def authenticate_user! # too lazy to change references to login_path
|
|
redirect_to(login_path) unless user_signed_in?
|
|
end
|
|
|
|
def authorize_user!
|
|
raise AccessDenied unless user_signed_in? && current_user.id == params[:user_id].to_i
|
|
end
|
|
|
|
def can_use_image_mode?
|
|
true
|
|
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
|
|
|
|
PRIVATE_IP_BLOCK = IPAddr.new('192.168.0.0/16')
|
|
def local_only
|
|
raise AccessDenied unless request.ip == '127.0.0.1' || PRIVATE_IP_BLOCK.include?(request.ip)
|
|
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 :file => 'public/403.html', :layout => false, :status => :forbidden
|
|
end
|
|
|
|
def redirect_back!(default=:back)
|
|
redirect_to(params[:return_to] || default)
|
|
end
|
|
|
|
def set_locale
|
|
I18n.locale = infer_locale || I18n.default_locale
|
|
end
|
|
|
|
def user_is?(user)
|
|
user_signed_in? && user == current_user
|
|
end
|
|
|
|
def valid_locale?(locale)
|
|
locale && I18n.usable_locales.include?(locale.to_sym)
|
|
end
|
|
|
|
def login_as_test_user
|
|
test_user = User.find_by_name('test')
|
|
sign_in(:user, test_user, bypass: true)
|
|
end
|
|
end
|
|
|