foodsoft/app/controllers/application_controller.rb

155 lines
4.5 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2009-01-06 11:49:19 +01:00
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :select_language, :select_foodcoop, :authenticate, :store_controller, :items_per_page, :set_redirect_to
after_filter :remove_controller
2009-01-06 11:49:19 +01:00
# Returns the controller handling the current request.
def self.current
Thread.current[:application_controller]
end
protected
def current_user
# 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.
@current_user ||= User.find_by_id(session[:user_id]) if session[:scope] == FoodsoftConfig.scope
2009-01-06 11:49:19 +01:00
end
end
helper_method :current_user
def deny_access
session[:return_to] = request.original_url
redirect_to login_url, :alert => 'Access denied!'
end
2009-01-06 11:49:19 +01:00
private
2009-01-06 11:49:19 +01:00
def authenticate(role = 'any')
# Attempt to retrieve authenticated user from controller instance or session...
if !current_user
# No user at all: redirect to login page.
session[:user_id] = nil
session[:return_to] = request.original_url
redirect_to login_url, :alert => 'Authentication required!'
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?
when "any" then true # no role required
else false # any unknown role will always fail
end
if hasRole
current_user
2009-01-06 11:49:19 +01:00
else
deny_access
end
2009-01-06 11:49:19 +01:00
end
end
2009-01-06 11:49:19 +01:00
def authenticate_admin
authenticate('admin')
end
2009-01-06 11:49:19 +01:00
def authenticate_finance
authenticate('finance')
end
2009-01-06 11:49:19 +01:00
def authenticate_article_meta
authenticate('article_meta')
end
2009-01-06 11:49:19 +01:00
def authenticate_suppliers
authenticate('suppliers')
end
2009-01-06 11:49:19 +01:00
def authenticate_orders
authenticate('orders')
end
2009-01-06 11:49:19 +01:00
# checks if the current_user is member of given group.
# if fails the user will redirected to startpage
def authenticate_membership_or_admin
@group = Group.find(params[:id])
unless @group.member?(@current_user) or @current_user.role_admin?
2012-08-06 12:00:40 +02:00
redirect_to root_path, alert: "Diese Aktion ist nur für Mitglieder der Gruppe erlaubt!"
end
end
# 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
# Sets the thread local variable that holds a reference to the current controller to nil.
def remove_controller
Thread.current[:application_controller] = nil
end
# Get supplier in nested resources
def find_supplier
@supplier = Supplier.find(params[:supplier_id]) if params[:supplier_id]
end
2013-07-09 21:46:04 +02:00
# Get stock_article in nested resources
def find_stock_article
@stock_article = StockArticle.undeleted.find(params[:stock_article_id])
end
# 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
if FoodsoftConfig[:multi_coop_install]
if params[:foodcoop].present?
begin
# Set Config and database connection
FoodsoftConfig.select_foodcoop params[:foodcoop]
rescue => error
redirect_to root_url, alert: error.message
end
else
redirect_to root_url
end
end
end
def items_per_page
if params[:per_page] && params[:per_page].to_i > 0 && params[:per_page].to_i <= 100
@per_page = params[:per_page].to_i
else
@per_page = 20
end
end
2012-08-06 12:00:40 +02:00
def set_redirect_to
session[:redirect_to] = params[:redirect_to] if params[:redirect_to]
end
def back_or_default_path(default = root_path)
if session[:redirect_to].present?
default = session[:redirect_to]
session[:redirect_to] = nil
end
default
end
# Always stay in foodcoop url scope
def default_url_options(options = {})
{foodcoop: FoodsoftConfig.scope}
end
# Used to prevent accidently switching to :en in production mode.
def select_language
I18n.locale = :de
end
2009-01-06 11:49:19 +01:00
end