module AuthenticatedSystem protected def current_user_session @current_user_session ||= UserSession.find end # Returns true or false if the user is logged in. # Preloads @current_user with the user model if they're logged in. def logged_in? !!current_user end def current_user @current_user ||= (cus = current_user_session) && cus.record end def require_user unless current_user store_location flash[:notice] = "You must be logged in to access this page" redirect_to login_path return false end end def require_no_user if current_user store_location flash[:notice] = "You must be logged out to access this page" redirect_to account_path return false end end # Check if the user is authorized # # Override this method in your controllers if you want to restrict access # to only a few actions or if you want to check if the user # has the correct rights. # # Example: # # # only allow nonbobs # def authorized? # current_user.login != "bob" # end def authorized? logged_in? end # Filter method to enforce a login requirement. # # To require logins for all actions, use this in your controllers: # # before_filter :login_required # # To require logins for specific actions, use this in your controllers: # # before_filter :login_required, :only => [ :edit, :update ] # # To skip this in a subclassed controller: # # skip_before_filter :login_required # def login_required authorized? || access_denied end #def require_user # unless current_user # store_location # flash[:notice] = "You must be logged in to access this page" # redirect_to login_path # return false # end #end #def require_no_user # if current_user # store_location # flash[:notice] = "You must be logged out to access this page" # redirect_to account_path # return false # end #end # Redirect as appropriate when an access request fails. # # The default action is to redirect to the login screen. # # Override this method in your controllers if you want to have special # behavior in case the user is not authorized # to access the requested action. For example, a popup window might # simply close itself. def access_denied respond_to do |format| format.html do store_location redirect_to new_session_path end format.any do request_http_basic_authentication 'Web Password' end end end # Store the URI of the current request in the session. # # We can return to this location by calling #redirect_back_or_default. def store_location session[:return_to] = request.fullpath end # Redirect to the URI stored by the most recent store_location call or # to the passed default. def redirect_back_or_default(default) redirect_to(session[:return_to] || default) session[:return_to] = nil end def refinery_user? logged_in? && current_user.has_role?(:refinery) end def self.included(base) base.send :helper_method, :current_user, :current_user_session, :logged_in?, :refinery_user? if base.respond_to? :helper_method end end