Added two new views: user/login and user/passreq.
[punbbonrails.git] / lib / authenticated_system.rb
blobe9a84215d179ca40525f98e39d8f56d00a925761
1 module AuthenticatedSystem
2   protected
3   def logged_in?
4     current_user
5   end
7   # Accesses the current user from the session.
8   def current_user
9     @current_user ||= session[:user] ? User.find_by_id(session[:user]) : nil
10   end
12   # Store the given user in the session.
13   def current_user=(new_user)
14     session[:user] = new_user.nil? ? nil : new_user.id
15     @current_user = new_user
16   end
18   # Check if the user is authorized.
19   #
20   # Override this method in your controllers if you want to restrict access
21   # to only a few actions or if you want to check if the user
22   # has the correct rights.
23   #
24   # Example:
25   #
26   #  # only allow nonbobs
27   #  def authorize?(user)
28   #    user.login != "bob"
29   #  end
30   def authorized?(user)
31      true
32   end
34   # Check whether or not to protect an action.
35   #
36   # Override this method in your controllers if you only want to protect
37   # certain actions.
38   #
39   # Example:
40   #
41   #  # don't protect the login and the about method
42   #  def protect?(action)
43   #    if ['action', 'about'].include?(action)
44   #       return false
45   #    else
46   #       return true
47   #    end
48   #  end
49   def protect?(action)
50     true
51   end
53   # Filter method to enforce a login requirement.
54   #
55   # To require logins for all actions, use this in your controllers:
56   #
57   #   before_filter :login_required
58   #
59   # To require logins for specific actions, use this in your controllers:
60   #
61   #   before_filter :login_required, :only => [ :edit, :update ]
62   #
63   # To skip this in a subclassed controller:
64   #
65   #   skip_before_filter :login_required
66   #
67   def login_required
68     # Skip this filter if the requested action is not protected
69     return true unless protect?(action_name)
71     # Check if user is logged in and authorized
72     return true if logged_in? and authorized?(current_user)
74     # Store current location so that we can redirect back after login
75     store_location
77     # Call access_denied for an appropriate redirect and stop the filter
78     # chain here
79     access_denied and return false
80   end
82   # Redirect as appropriate when an access request fails.
83   #
84   # The default action is to redirect to the login screen.
85   #
86   # Override this method in your controllers if you want to have special
87   # behavior in case the user is not authorized
88   # to access the requested action.  For example, a popup window might
89   # simply close itself.
90   def access_denied
91     redirect_to :controller => 'user', :action => 'login'
92   end  
94   # Store the URI of the current request in the session.
95   #
96   # We can return to this location by calling #redirect_back_or_default.
97   def store_location
98     session[:return_to] = request.request_uri
99   end
101   # Redirect to the URI stored by the most recent store_location call or
102   # to the passed default.
103   def redirect_back_or_default(default)
104     session[:return_to] ? redirect_to_url(session[:return_to]) : redirect_to(default)
105     session[:return_to] = nil
106   end
108   # Inclusion hook to make #current_user and #logged_in?
109   # available as ActionView helper methods.
110   def self.included(base)
111     base.send :helper_method, :current_user, :logged_in?
112   end