app/controllers/alchemy/base_controller.rb in alchemy_cms-2.3.2 vs app/controllers/alchemy/base_controller.rb in alchemy_cms-2.4.beta2
- old
+ new
@@ -13,19 +13,23 @@
# Returns a host string with the domain the app is running on.
def current_server
# For local development server
if request.port != 80
- "http://#{request.host}:#{request.port}"
+ "#{request.protocol}#{request.host}:#{request.port}"
# For remote production server
else
- "http://#{request.host}"
+ "#{request.protocol}#{request.host}"
end
end
+ # Returns the configuratin value of given key.
+ #
+ # Config file is in +config/alchemy/config.yml+
+ #
def configuration(name)
- return Alchemy::Config.get(name)
+ Alchemy::Config.get(name)
end
def multi_language?
Language.published.count > 1
end
@@ -54,22 +58,23 @@
::I18n.locale = session[:current_locale]
elsif params[:locale].present? && ::I18n.available_locales.include?(params[:locale].to_sym)
session[:current_locale] = ::I18n.locale = params[:locale]
elsif current_user && current_user.language.present?
::I18n.locale = current_user.language
+ elsif Rails.env == 'test' # OMG I hate to do this. But it helps...
+ ::I18n.locale = 'en'
else
::I18n.locale = request.env['HTTP_ACCEPT_LANGUAGE'].try(:scan, /^[a-z]{2}/).try(:first)
end
end
# Sets the language for rendering pages in pages controller
def set_language
if params[:lang].blank? and session[:language_id].blank?
set_language_to_default
elsif !params[:lang].blank?
- set_language_from(params[:lang])
- ::I18n.locale = params[:lang]
+ ::I18n.locale = set_language_from(params[:lang])
end
end
def set_language_from(language_code_or_id)
if language_code_or_id.is_a?(String) && language_code_or_id.match(/^\d+$/)
@@ -93,13 +98,12 @@
end
end
def store_language_in_session(language)
if language && language.id
- return if language.id == session[:language_id]
- session[:language_code] = language.code
session[:language_id] = language.id
+ session[:language_code] = language.code
else
logger.warn "!!!! Language not found for #{language.inspect}. Setting to default!"
set_language_to_default
end
end
@@ -157,10 +161,32 @@
end
@page = Page.language_root_for(session[:language_id])
render :file => Rails.root.join("public/404.html"), :status => 404, :layout => !@page.nil?
end
+ # Enforce ssl for login and all admin modules.
+ #
+ # Default is +false+
+ #
+ # === Usage
+ #
+ # #config.yml
+ # require_ssl: true
+ #
+ # === Note
+ #
+ # You have to create a ssl certificate if you want to use the ssl protection
+ #
+ def ssl_required?
+ (Rails.env == 'production' || Rails.env == 'staging') && configuration(:require_ssl)
+ end
+
+ # Redirects request to ssl.
+ def enforce_ssl
+ redirect_to url_for(protocol: 'https')
+ end
+
protected
def permission_denied
if current_user
if current_user.role == 'registered'
@@ -169,10 +195,10 @@
if request.referer == alchemy.login_url
render :file => Rails.root.join('public/422'), :status => 422
elsif request.xhr?
respond_to do |format|
format.js {
- render :js => "Alchemy.growl('#{t('You are not authorized')}', 'warning'); Alchemy.enableButton('button.button, a.button, input.button');"
+ render :js => "Alchemy.growl('#{t('You are not authorized')}', 'warning'); Alchemy.Buttons.enable();"
}
format.html {
render :partial => 'alchemy/admin/partials/flash', :locals => {:message => t('You are not authorized'), :flash_type => 'warning'}
}
end