osem-fcy/app/controllers/application_controller.rb

99 lines
3 KiB
Ruby
Raw Normal View History

2013-01-07 09:04:09 +01:00
class ApplicationController < ActionController::Base
include ApplicationHelper
protect_from_forgery
before_filter :get_conferences
2013-08-25 13:40:44 +03:00
before_filter :store_location
2014-08-12 11:51:59 +03:00
before_filter :verify_user_admin
helper_method :date_string
2014-08-12 11:51:59 +03:00
# Ensure every controller authorizes resource or skips authorization (skip_authorization_check)
check_authorization unless: :devise_controller?
2013-08-25 13:40:44 +03:00
def store_location
2014-07-30 23:34:09 +02:00
session[:return_to] = request.fullpath if request.get? && controller_name != "user_sessions" && controller_name != "sessions"
2013-08-25 13:40:44 +03:00
end
def after_sign_in_path_for(resource)
2014-08-12 11:51:59 +03:00
if (can? :view, Conference) &&
(!session[:return_to] ||
session[:return_to] &&
session[:return_to] == root_path)
admin_conference_index_path
else
if session[:return_to] &&
!session[:return_to].start_with?(user_registration_path)
logger.debug "Returning to #{session[:return_to]}"
session[:return_to]
else
logger.debug "Not returning to #{session[:return_to]} because it would loop"
super
end
end
2013-08-25 13:40:44 +03:00
end
def get_conferences
@conferences =Conference.all
end
2013-01-07 09:04:09 +01:00
2014-08-12 11:51:59 +03:00
def verify_user_admin
if self.class.to_s.split('::').first == 'Admin' && verify_user
unless (current_user.has_role? :organizer, :any) || (current_user.has_role? :cfp, :any) ||
(current_user.has_role? :info_desk, :any) ||
(current_user.has_role? :volunteers_coordinator, :any) || current_user.is_admin
raise CanCan::AccessDenied.new('You are not authorized to access this area!')
end
end
end
2013-01-07 09:04:09 +01:00
def verify_user
:authenticate_user!
if (current_user.nil?)
redirect_to new_user_session_path
return false
2013-01-07 09:04:09 +01:00
end
2013-01-15 14:08:22 +01:00
true
2013-01-07 09:04:09 +01:00
end
2014-08-12 11:51:59 +03:00
def current_ability
@current_ability ||= Ability.new(current_user)
2013-01-07 09:04:09 +01:00
end
rescue_from CanCan::AccessDenied do |exception|
Rails.logger.debug("Access denied!")
2014-07-21 14:49:05 +02:00
redirect_to root_path, alert: exception.message
2013-01-07 09:04:09 +01:00
end
def not_found
raise ActionController::RoutingError.new('Not Found')
end
##
# Returns a string build from the start and end date of the given conference.
#
# If the conference starts and ends in the same month and year
# * %B %d - %d, %Y (January 17 - 21 2014)
# If the conference ends in another month but in the same year
# * %B %d - %B %d, %Y (January 31 - February 02 2014)
# All other cases
# * %B %d, %Y - %B %d, %Y (December 30, 2013 - January 02, 2014)
def date_string(start_date, end_date)
startstr = 'Unknown - '
endstr = 'Unknown'
# When the conference in the same month
if start_date.month == end_date.month && start_date.year == end_date.year
startstr = start_date.strftime('%B %d - ')
endstr = end_date.strftime('%d, %Y')
elsif start_date.month != end_date.month && start_date.year == end_date.year
startstr = start_date.strftime('%B %d - ')
endstr = end_date.strftime('%B %d, %Y')
else
startstr = start_date.strftime('%B %d, %Y - ')
endstr = end_date.strftime('%B %d, %Y')
end
result = startstr + endstr
result
end
2013-01-07 09:04:09 +01:00
end