changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/restful_authentication/generators/authenticated/templates/authenticated_system.rb

changeset 4: 43c5e6930eee
author: moriq@moriq.com
date: Wed Mar 05 01:17:41 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugin restful_authentication.
1module AuthenticatedSystem
2 protected
3 # Returns true or false if the <%= file_name %> is logged in.
4 # Preloads @current_<%= file_name %> with the <%= file_name %> model if they're logged in.
5 def logged_in?
6 current_<%= file_name %> != :false
7 end
8
9 # Accesses the current <%= file_name %> from the session. Set it to :false if login fails
10 # so that future calls do not hit the database.
11 def current_<%= file_name %>
12 @current_<%= file_name %> ||= (login_from_session || login_from_basic_auth || login_from_cookie || :false)
13 end
14
15 # Store the given <%= file_name %> id in the session.
16 def current_<%= file_name %>=(new_<%= file_name %>)
17 session[:<%= file_name %>_id] = (new_<%= file_name %>.nil? || new_<%= file_name %>.is_a?(Symbol)) ? nil : new_<%= file_name %>.id
18 @current_<%= file_name %> = new_<%= file_name %> || :false
19 end
20
21 # Check if the <%= file_name %> is authorized
22 #
23 # Override this method in your controllers if you want to restrict access
24 # to only a few actions or if you want to check if the <%= file_name %>
25 # has the correct rights.
26 #
27 # Example:
28 #
29 # # only allow nonbobs
30 # def authorized?
31 # current_<%= file_name %>.login != "bob"
32 # end
33 def authorized?
34 logged_in?
35 end
36
37 # Filter method to enforce a login requirement.
38 #
39 # To require logins for all actions, use this in your controllers:
40 #
41 # before_filter :login_required
42 #
43 # To require logins for specific actions, use this in your controllers:
44 #
45 # before_filter :login_required, :only => [ :edit, :update ]
46 #
47 # To skip this in a subclassed controller:
48 #
49 # skip_before_filter :login_required
50 #
51 def login_required
52 authorized? || access_denied
53 end
54
55 # Redirect as appropriate when an access request fails.
56 #
57 # The default action is to redirect to the login screen.
58 #
59 # Override this method in your controllers if you want to have special
60 # behavior in case the <%= file_name %> is not authorized
61 # to access the requested action. For example, a popup window might
62 # simply close itself.
63 def access_denied
64 respond_to do |format|
65 format.html do
66 store_location
67 redirect_to new_<%= controller_singular_name %>_path
68 end
69 format.any do
70 request_http_basic_authentication 'Web Password'
71 end
72 end
73 end
74
75 # Store the URI of the current request in the session.
76 #
77 # We can return to this location by calling #redirect_back_or_default.
78 def store_location
79 session[:return_to] = request.request_uri
80 end
81
82 # Redirect to the URI stored by the most recent store_location call or
83 # to the passed default.
84 def redirect_back_or_default(default)
85 redirect_to(session[:return_to] || default)
86 session[:return_to] = nil
87 end
88
89 # Inclusion hook to make #current_<%= file_name %> and #logged_in?
90 # available as ActionView helper methods.
91 def self.included(base)
92 base.send :helper_method, :current_<%= file_name %>, :logged_in?
93 end
94
95 # Called from #current_<%= file_name %>. First attempt to login by the <%= file_name %> id stored in the session.
96 def login_from_session
97 self.current_<%= file_name %> = <%= class_name %>.find_by_id(session[:<%= file_name %>_id]) if session[:<%= file_name %>_id]
98 end
99
100 # Called from #current_<%= file_name %>. Now, attempt to login by basic authentication information.
101 def login_from_basic_auth
102 authenticate_with_http_basic do |username, password|
103 self.current_<%= file_name %> = <%= class_name %>.authenticate(username, password)
104 end
105 end
106
107 # Called from #current_<%= file_name %>. Finaly, attempt to login by an expiring token in the cookie.
108 def login_from_cookie
109 <%= file_name %> = cookies[:auth_token] && <%= class_name %>.find_by_remember_token(cookies[:auth_token])
110 if <%= file_name %> && <%= file_name %>.remember_token?
111 <%= file_name %>.remember_me
112 cookies[:auth_token] = { :value => <%= file_name %>.remember_token, :expires => <%= file_name %>.remember_token_expires_at }
113 self.current_<%= file_name %> = <%= file_name %>
114 end
115 end
116end