metamaps--metamaps/app/controllers/application_controller.rb

59 lines
1.2 KiB
Ruby
Raw Normal View History

2012-09-23 02:39:12 +00:00
class ApplicationController < ActionController::Base
protect_from_forgery
# this is for global login
include ContentHelper
2012-09-23 02:39:12 +00:00
helper_method :user
helper_method :authenticated?
helper_method :admin?
2012-09-23 02:39:12 +00:00
2014-10-07 21:46:09 +00:00
def after_sign_in_path_for(resource)
sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')
if request.referer == sign_in_url
super
else
stored_location_for(resource) || request.referer || root_path
end
end
2012-09-23 02:39:12 +00:00
private
def require_no_user
if authenticated?
flash[:warning] = "You must be logged out."
store and redirect_to edit_user_path(user)
2012-09-23 02:39:12 +00:00
return false
end
end
def require_user
unless authenticated?
flash[:warning] = "You must be logged in."
store_location and redirect_to new_user_session_path
2012-09-23 02:39:12 +00:00
return false
end
end
def require_admin
unless authenticated? && user.admin
redirect_to root_url, notice: "You need to be an admin for that."
return false
end
end
2012-09-23 02:39:12 +00:00
def user
current_user
end
def authenticated?
current_user
end
def admin?
current_user && current_user.admin
end
2012-09-23 02:39:12 +00:00
end