changelog shortlog tags changeset manifest revisions annotate raw

lib/authenticated_system.rb

changeset 7: ac1024130232
author: moriq@moriq.com
date: Wed Mar 05 03:57:54 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: generate authenticated again.
mercurial import したときに db/migrate lib が消えてた。orz
1module AuthenticatedSystem
2 protected
3 # Returns true or false if the user is logged in.
4 # Preloads @current_user with the user model if they're logged in.
5 def logged_in?
6 current_user != :false
7 end
8
9 # Accesses the current user from the session. Set it to :false if login fails
10 # so that future calls do not hit the database.
11 def current_user
12 @current_user ||= (login_from_session || login_from_basic_auth || login_from_cookie || :false)
13 end
14
15 # Store the given user id in the session.
16 def current_user=(new_user)
17 session[:user_id] = (new_user.nil? || new_user.is_a?(Symbol)) ? nil : new_user.id
18 @current_user = new_user || :false
19 end
20
21 # Check if the user 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 user
25 # has the correct rights.
26 #
27 # Example:
28 #
29 # # only allow nonbobs
30 # def authorized?
31 # current_user.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 user 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_session_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_user and #logged_in?
90 # available as ActionView helper methods.
91 def self.included(base)
92 base.send :helper_method, :current_user, :logged_in?
93 end
94
95 # Called from #current_user. First attempt to login by the user id stored in the session.
96 def login_from_session
97 self.current_user = User.find_by_id(session[:user_id]) if session[:user_id]
98 end
99
100 # Called from #current_user. 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_user = User.authenticate(username, password)
104 end
105 end
106
107 # Called from #current_user. Finaly, attempt to login by an expiring token in the cookie.
108 def login_from_cookie
109 user = cookies[:auth_token] && User.find_by_remember_token(cookies[:auth_token])
110 if user && user.remember_token?
111 user.remember_me
112 cookies[:auth_token] = { :value => user.remember_token, :expires => user.remember_token_expires_at }
113 self.current_user = user
114 end
115 end
116end