2012-04-15 19:59:39 +02:00
|
|
|
# encoding: utf-8
|
2009-01-06 11:49:19 +01:00
|
|
|
class ApplicationController < ActionController::Base
|
2013-05-31 18:42:28 +02:00
|
|
|
include Foodsoft::ControllerExtensions::Locale
|
|
|
|
helper_method :available_locales
|
2009-03-24 17:01:10 +01:00
|
|
|
|
2011-05-11 13:38:46 +02:00
|
|
|
protect_from_forgery
|
2013-12-22 13:58:49 +01:00
|
|
|
before_filter :select_foodcoop, :authenticate, :store_controller, :items_per_page
|
2009-03-09 13:02:43 +01:00
|
|
|
after_filter :remove_controller
|
2011-05-11 13:38:46 +02:00
|
|
|
|
2013-05-31 18:42:28 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Returns the controller handling the current request.
|
|
|
|
def self.current
|
|
|
|
Thread.current[:application_controller]
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
def current_user
|
2011-05-11 13:38:46 +02:00
|
|
|
# check if there is a valid session and return the logged-in user (its object)
|
|
|
|
if session[:user_id] and params[:foodcoop]
|
|
|
|
# for shared-host installations. check if the cookie-subdomain fits to request.
|
2012-08-24 19:52:38 +02:00
|
|
|
@current_user ||= User.find_by_id(session[:user_id]) if session[:scope] == FoodsoftConfig.scope
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2010-03-20 02:26:30 +01:00
|
|
|
end
|
2012-08-24 19:52:38 +02:00
|
|
|
helper_method :current_user
|
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
def deny_access
|
2012-12-30 16:34:01 +01:00
|
|
|
session[:return_to] = request.original_url
|
2014-01-03 15:27:15 +01:00
|
|
|
redirect_to root_url, alert: I18n.t('application.controller.error_denied', sign_in: ActionController::Base.helpers.link_to(t('application.controller.error_denied_sign_in'), login_path))
|
2010-03-20 02:26:30 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2013-05-31 18:42:28 +02:00
|
|
|
private
|
2014-01-04 20:12:01 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
def authenticate(role = 'any')
|
|
|
|
# Attempt to retrieve authenticated user from controller instance or session...
|
2011-05-11 13:38:46 +02:00
|
|
|
if !current_user
|
2010-03-20 02:26:30 +01:00
|
|
|
# No user at all: redirect to login page.
|
2011-05-11 13:38:46 +02:00
|
|
|
session[:user_id] = nil
|
2012-12-30 16:34:01 +01:00
|
|
|
session[:return_to] = request.original_url
|
2014-01-22 08:59:45 +01:00
|
|
|
redirect_to_login :alert => I18n.t('application.controller.error_authn')
|
2010-03-20 02:26:30 +01:00
|
|
|
else
|
|
|
|
# We have an authenticated user, now check role...
|
|
|
|
# Roles gets the user through his memberships.
|
|
|
|
hasRole = case role
|
2011-05-18 15:52:06 +02:00
|
|
|
when "admin" then current_user.role_admin?
|
|
|
|
when "finance" then current_user.role_finance?
|
|
|
|
when "article_meta" then current_user.role_article_meta?
|
|
|
|
when "suppliers" then current_user.role_suppliers?
|
|
|
|
when "orders" then current_user.role_orders?
|
2013-12-31 11:41:14 +01:00
|
|
|
when "finance_or_orders" then (current_user.role_finance? || current_user.role_orders?)
|
2010-03-20 02:26:30 +01:00
|
|
|
when "any" then true # no role required
|
|
|
|
else false # any unknown role will always fail
|
|
|
|
end
|
|
|
|
if hasRole
|
2011-05-11 13:38:46 +02:00
|
|
|
current_user
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
2010-03-20 02:26:30 +01:00
|
|
|
deny_access
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2010-03-20 02:26:30 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
def authenticate_admin
|
|
|
|
authenticate('admin')
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
def authenticate_finance
|
|
|
|
authenticate('finance')
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
def authenticate_article_meta
|
|
|
|
authenticate('article_meta')
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
def authenticate_suppliers
|
|
|
|
authenticate('suppliers')
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
def authenticate_orders
|
|
|
|
authenticate('orders')
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2013-12-31 11:41:14 +01:00
|
|
|
def authenticate_finance_or_orders
|
|
|
|
authenticate('finance_or_orders')
|
|
|
|
end
|
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
# checks if the current_user is member of given group.
|
|
|
|
# if fails the user will redirected to startpage
|
2013-12-22 14:20:25 +01:00
|
|
|
def authenticate_membership_or_admin(group_id = params[:id])
|
|
|
|
@group = Group.find(group_id)
|
2010-03-20 02:26:30 +01:00
|
|
|
unless @group.member?(@current_user) or @current_user.role_admin?
|
2013-10-02 17:13:49 +02:00
|
|
|
redirect_to root_path, alert: I18n.t('application.controller.error_members_only')
|
2009-01-15 12:14:01 +01:00
|
|
|
end
|
2010-03-20 02:26:30 +01:00
|
|
|
end
|
2009-01-15 12:14:01 +01:00
|
|
|
|
2014-01-04 20:12:01 +01:00
|
|
|
def authenticate_or_token(prefix, role = 'any')
|
|
|
|
if not params[:token].blank?
|
|
|
|
begin
|
|
|
|
TokenVerifier.new(prefix).verify(params[:token])
|
|
|
|
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
|
|
|
redirect_to root_path, alert: I18n.t('application.controller.error_token')
|
|
|
|
end
|
|
|
|
else
|
|
|
|
authenticate(role)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-22 08:59:45 +01:00
|
|
|
# Redirect to the login page, used in authenticate, plugins can override this.
|
|
|
|
def redirect_to_login(options={})
|
|
|
|
redirect_to login_url, options
|
|
|
|
end
|
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
# Stores this controller instance as a thread local varibale to be accessible from outside ActionController/ActionView.
|
|
|
|
def store_controller
|
|
|
|
Thread.current[:application_controller] = self
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
# Sets the thread local variable that holds a reference to the current controller to nil.
|
|
|
|
def remove_controller
|
|
|
|
Thread.current[:application_controller] = nil
|
|
|
|
end
|
2009-01-08 16:33:27 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
# Get supplier in nested resources
|
|
|
|
def find_supplier
|
|
|
|
@supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id]
|
|
|
|
end
|
2009-03-24 17:01:10 +01:00
|
|
|
|
2010-03-20 02:26:30 +01:00
|
|
|
# Set config and database connection for each request
|
|
|
|
# It uses the subdomain to select the appropriate section in the config files
|
|
|
|
# Use this method as a before filter (first filter!) in ApplicationController
|
|
|
|
def select_foodcoop
|
2012-08-24 19:52:38 +02:00
|
|
|
if FoodsoftConfig[:multi_coop_install]
|
|
|
|
if params[:foodcoop].present?
|
2010-03-22 01:25:24 +01:00
|
|
|
begin
|
2012-08-24 19:52:38 +02:00
|
|
|
# Set Config and database connection
|
|
|
|
FoodsoftConfig.select_foodcoop params[:foodcoop]
|
2010-03-22 01:25:24 +01:00
|
|
|
rescue => error
|
2012-08-24 19:52:38 +02:00
|
|
|
redirect_to root_url, alert: error.message
|
2010-03-22 01:25:24 +01:00
|
|
|
end
|
2010-03-20 02:26:30 +01:00
|
|
|
else
|
2012-08-24 19:52:38 +02:00
|
|
|
redirect_to root_url
|
2009-08-13 16:32:38 +02:00
|
|
|
end
|
2009-03-24 17:01:10 +01:00
|
|
|
end
|
2010-03-20 02:26:30 +01:00
|
|
|
end
|
2011-06-10 12:18:55 +02:00
|
|
|
|
|
|
|
def items_per_page
|
2012-08-24 19:52:38 +02:00
|
|
|
if params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100
|
2011-06-10 12:18:55 +02:00
|
|
|
@per_page = params[:per_page].to_i
|
|
|
|
else
|
|
|
|
@per_page = 20
|
|
|
|
end
|
|
|
|
end
|
2012-08-06 12:00:40 +02:00
|
|
|
|
2012-08-24 19:52:38 +02:00
|
|
|
# Always stay in foodcoop url scope
|
|
|
|
def default_url_options(options = {})
|
|
|
|
{foodcoop: FoodsoftConfig.scope}
|
|
|
|
end
|
2013-05-31 18:42:28 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|