2014-03-12 04:42:59 +00:00
|
|
|
class ApplicationController < ActionController::Base
|
|
|
|
# Prevent CSRF attacks by raising an exception.
|
|
|
|
# For APIs, you may want to use :null_session instead.
|
|
|
|
protect_from_forgery with: :exception
|
2014-04-01 07:10:21 +00:00
|
|
|
before_action :set_locale
|
2014-04-14 10:40:17 +00:00
|
|
|
helper_method :current_site, :current_user
|
2014-04-08 10:46:27 +00:00
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
def default_url_options(options={})
|
|
|
|
{ locale: I18n.locale }
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_locale
|
2014-05-14 04:07:51 +00:00
|
|
|
in_use_locales = current_site.in_use_locales
|
|
|
|
|
|
|
|
if params[:locale]
|
|
|
|
session[:locale] = in_use_locales.include?(params[:locale].to_sym) ? params[:locale] : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if !params[:locale] and !session[:locale]
|
|
|
|
if current_site.enable_language_detection
|
|
|
|
browser_locale = request.env['HTTP_ACCEPT_LANGUAGE'].split(',').first.underscore rescue nil
|
|
|
|
session[:locale] = in_use_locales.include?(browser_locale.to_sym) ? browser_locale : nil
|
|
|
|
elsif current_site.default_locale
|
|
|
|
session[:locale] = current_site.default_locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
session[:locale] = session[:locale].blank? ? I18n.default_locale : session[:locale]
|
|
|
|
|
|
|
|
I18n.locale = session[:locale]
|
2014-05-16 09:25:43 +00:00
|
|
|
|
|
|
|
@site_in_use_locales = [I18n.locale]+(in_use_locales-[I18n.locale])
|
|
|
|
@site_valid_locales = [I18n.locale]+(current_site.valid_locales-[I18n.locale])
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_layout
|
|
|
|
f = File.join("../../templates/", "#{@key}", '/home/page.html.erb')
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_key
|
2014-05-20 02:31:44 +00:00
|
|
|
@key = current_site.template
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
|
2014-04-08 10:46:27 +00:00
|
|
|
def current_site
|
2014-04-21 02:48:36 +00:00
|
|
|
@current_site = Site.first
|
2014-04-08 10:46:27 +00:00
|
|
|
end
|
2014-04-14 10:40:17 +00:00
|
|
|
|
2014-05-20 02:31:44 +00:00
|
|
|
def frontent_allowed
|
|
|
|
current_user.nil? and !current_site.frontend_open
|
|
|
|
end
|
|
|
|
|
2014-04-14 10:40:17 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def current_user
|
2014-04-21 02:48:36 +00:00
|
|
|
@current_user ||= User.find(session[:user_id]) if session[:user_id] rescue nil
|
2014-04-14 10:40:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def authenticate_user
|
|
|
|
if session[:user_id]
|
|
|
|
# set current user object to @current_user object variable
|
2014-04-21 02:48:36 +00:00
|
|
|
@current_user = User.find(session[:user_id]) rescue nil
|
|
|
|
|
|
|
|
redirect_to new_session_path if @current_user.nil?
|
|
|
|
return true
|
2014-04-14 10:40:17 +00:00
|
|
|
else
|
|
|
|
redirect_to new_session_path
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2014-03-12 04:42:59 +00:00
|
|
|
end
|