0
+module AuthenticatedSystem
0
+ # Returns true or false if the person is logged in.
0
+ # Preloads @current_person with the person model if they're logged in.
0
+ current_person != :false
0
+ # Accesses the current person from the session. Set it to :false if login fails
0
+ # so that future calls do not hit the database.
0
+ @current_person ||= (login_from_session || login_from_basic_auth || login_from_cookie || :false)
0
+ # Store the given person id in the session.
0
+ def current_person=(new_person)
0
+ session[:person_id] = (new_person.nil? || new_person.is_a?(Symbol)) ? nil : new_person.id
0
+ @current_person = new_person || :false
0
+ # Check if the person is authorized
0
+ # Override this method in your controllers if you want to restrict access
0
+ # to only a few actions or if you want to check if the person
0
+ # has the correct rights.
0
+ # # only allow nonbobs
0
+ # current_person.login != "bob"
0
+ # Filter method to enforce a login requirement.
0
+ # To require logins for all actions, use this in your controllers:
0
+ # before_filter :login_required
0
+ # To require logins for specific actions, use this in your controllers:
0
+ # before_filter :login_required, :only => [ :edit, :update ]
0
+ # To skip this in a subclassed controller:
0
+ # skip_before_filter :login_required
0
+ authorized? || access_denied
0
+ # Redirect as appropriate when an access request fails.
0
+ # The default action is to redirect to the login screen.
0
+ # Override this method in your controllers if you want to have special
0
+ # behavior in case the person is not authorized
0
+ # to access the requested action. For example, a popup window might
0
+ # simply close itself.
0
+ respond_to do |format|
0
+ redirect_to new_session_path
0
+ request_http_basic_authentication 'Web Password'
0
+ # Store the URI of the current request in the session.
0
+ # We can return to this location by calling #redirect_back_or_default.
0
+ session[:return_to] = request.request_uri
0
+ # Redirect to the URI stored by the most recent store_location call or
0
+ # to the passed default.
0
+ def redirect_back_or_default(default)
0
+ redirect_to(session[:return_to] || default)
0
+ session[:return_to] = nil
0
+ # Inclusion hook to make #current_person and #logged_in?
0
+ # available as ActionView helper methods.
0
+ def self.included(base)
0
+ base.send :helper_method, :current_person, :logged_in?
0
+ # Called from #current_person. First attempt to login by the person id stored in the session.
0
+ def login_from_session
0
+ self.current_person = Person.find(session[:person_id]) if session[:person_id]
0
+ # Called from #current_person. Now, attempt to login by basic authentication information.
0
+ def login_from_basic_auth
0
+ authenticate_with_http_basic do |username, password|
0
+ self.current_person = Person.authenticate(username, password)
0
+ # Called from #current_person. Finaly, attempt to login by an expiring token in the cookie.
0
+ person = cookies[:auth_token] && Person.find_by_remember_token(cookies[:auth_token])
0
+ if person && person.remember_token?
0
+ cookies[:auth_token] = { :value => person.remember_token, :expires => person.remember_token_expires_at }
0
+ self.current_person = person