Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 98
- Log:
Initial import of Hub, an account management application.
- Author:
- adh
- Date:
- Thu Oct 19 15:18:43 +0100 2006
- Size:
- 4865 Bytes
1 | module AuthenticatedSystem |
2 | protected |
3 | # Returns true or false if the user is logged in. |
4 | # Preloads @current_<%= file_name %> with the user model if they're logged in. |
5 | def logged_in? |
6 | (@current_<%= file_name %> ||= session[:<%= file_name %>] ? <%= class_name %>.find_by_id(session[:<%= file_name %>]) : :false).is_a?(<%= class_name %>) |
7 | end |
8 | |
9 | # Accesses the current <%= file_name %> from the session. |
10 | def current_<%= file_name %> |
11 | @current_<%= file_name %> if logged_in? |
12 | end |
13 | |
14 | # Store the given <%= file_name %> in the session. |
15 | def current_<%= file_name %>=(new_<%= file_name %>) |
16 | session[:<%= file_name %>] = (new_<%= file_name %>.nil? || new_<%= file_name %>.is_a?(Symbol)) ? nil : new_<%= file_name %>.id |
17 | @current_<%= file_name %> = new_<%= file_name %> |
18 | end |
19 | |
20 | # Check if the <%= file_name %> is authorized. |
21 | # |
22 | # Override this method in your controllers if you want to restrict access |
23 | # to only a few actions or if you want to check if the <%= file_name %> |
24 | # has the correct rights. |
25 | # |
26 | # Example: |
27 | # |
28 | # # only allow nonbobs |
29 | # def authorize? |
30 | # current_<%= file_name %>.login != "bob" |
31 | # end |
32 | def authorized? |
33 | true |
34 | end |
35 | |
36 | # Filter method to enforce a login requirement. |
37 | # |
38 | # To require logins for all actions, use this in your controllers: |
39 | # |
40 | # before_filter :login_required |
41 | # |
42 | # To require logins for specific actions, use this in your controllers: |
43 | # |
44 | # before_filter :login_required, :only => [ :edit, :update ] |
45 | # |
46 | # To skip this in a subclassed controller: |
47 | # |
48 | # skip_before_filter :login_required |
49 | # |
50 | def login_required |
51 | username, passwd = get_auth_data |
52 | self.current_<%= file_name %> ||= <%= class_name %>.authenticate(username, passwd) || :false if username && passwd |
53 | logged_in? && authorized? ? true : access_denied |
54 | end |
55 | |
56 | # Redirect as appropriate when an access request fails. |
57 | # |
58 | # The default action is to redirect to the login screen. |
59 | # |
60 | # Override this method in your controllers if you want to have special |
61 | # behavior in case the <%= file_name %> is not authorized |
62 | # to access the requested action. For example, a popup window might |
63 | # simply close itself. |
64 | def access_denied |
65 | respond_to do |accepts| |
66 | accepts.html do |
67 | store_location |
68 | redirect_to :controller => '/<%= controller_file_name %>', :action => 'login' |
69 | end |
70 | accepts.xml do |
71 | headers["Status"] = "Unauthorized" |
72 | headers["WWW-Authenticate"] = %(Basic realm="Web Password") |
73 | render :text => "Could't authenticate you", :status => '401 Unauthorized' |
74 | end |
75 | end |
76 | false |
77 | end |
78 | |
79 | # Store the URI of the current request in the session. |
80 | # |
81 | # We can return to this location by calling #redirect_back_or_default. |
82 | def store_location |
83 | session[:return_to] = request.request_uri |
84 | end |
85 | |
86 | # Redirect to the URI stored by the most recent store_location call or |
87 | # to the passed default. |
88 | def redirect_back_or_default(default) |
89 | session[:return_to] ? redirect_to_url(session[:return_to]) : redirect_to(default) |
90 | session[:return_to] = nil |
91 | end |
92 | |
93 | # Inclusion hook to make #current_<%= file_name %> and #logged_in? |
94 | # available as ActionView helper methods. |
95 | def self.included(base) |
96 | base.send :helper_method, :current_<%= file_name %>, :logged_in? |
97 | end |
98 | |
99 | # When called with before_filter :login_from_cookie will check for an :auth_token |
100 | # cookie and log the user back in if apropriate |
101 | def login_from_cookie |
102 | return unless cookies[:auth_token] && !logged_in? |
103 | user = <%= class_name %>.find_by_remember_token(cookies[:auth_token]) |
104 | if user && user.remember_token? |
105 | user.remember_me |
106 | self.current_<%= file_name %> = user |
107 | cookies[:auth_token] = { :value => self.current_<%= file_name %>.remember_token , :expires => self.current_<%= file_name %>.remember_token_expires_at } |
108 | flash[:notice] = "Logged in successfully" |
109 | end |
110 | end |
111 | |
112 | private |
113 | # gets BASIC auth info |
114 | def get_auth_data |
115 | user, pass = nil, nil |
116 | # extract authorisation credentials |
117 | if request.env.has_key? 'X-HTTP_AUTHORIZATION' |
118 | # try to get it where mod_rewrite might have put it |
119 | authdata = request.env['X-HTTP_AUTHORIZATION'].to_s.split |
120 | elsif request.env.has_key? 'HTTP_AUTHORIZATION' |
121 | # this is the regular location |
122 | authdata = request.env['HTTP_AUTHORIZATION'].to_s.split |
123 | end |
124 | |
125 | # at the moment we only support basic authentication |
126 | if authdata && authdata[0] == 'Basic' |
127 | user, pass = Base64.decode64(authdata[1]).split(':')[0..1] |
128 | end |
129 | return [user, pass] |
130 | end |
131 | end |