summaryrefslogtreecommitdiff
path: root/lib/authenticated_system.rb
diff options
context:
space:
mode:
authorsimon <simon@zagal.(none)>2009-02-08 23:15:11 +0100
committerhukl <hukl@eight.local>2009-02-15 20:22:01 +0100
commit9f94a70c3e3d9bf766cb9663b0a904d30a190d85 (patch)
tree4b4bbf567ec60a939d024b083b478d72476700a5 /lib/authenticated_system.rb
parent48ffd4eb446bcaeba7651758ec3002f342702249 (diff)
* initial commit of the stripped restful-authentication
* http basic auth and login from cookie have been removed * no it does not work yet, it's so f*cking secure, it won't even let legitimate users login
Diffstat (limited to 'lib/authenticated_system.rb')
-rw-r--r--lib/authenticated_system.rb127
1 files changed, 127 insertions, 0 deletions
diff --git a/lib/authenticated_system.rb b/lib/authenticated_system.rb
new file mode 100644
index 0000000..7b813f4
--- /dev/null
+++ b/lib/authenticated_system.rb
@@ -0,0 +1,127 @@
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
7 end
8
9 # Accesses the current user from the session.
10 # Future calls avoid the database because nil is not equal to false.
11 def current_user
12 @current_user ||= login_from_session unless @current_user == 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 ? new_user.id : nil
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 #
34 def authorized?(action = action_name, resource = nil)
35 logged_in?
36 end
37
38 # Filter method to enforce a login requirement.
39 #
40 # To require logins for all actions, use this in your controllers:
41 #
42 # before_filter :login_required
43 #
44 # To require logins for specific actions, use this in your controllers:
45 #
46 # before_filter :login_required, :only => [ :edit, :update ]
47 #
48 # To skip this in a subclassed controller:
49 #
50 # skip_before_filter :login_required
51 #
52 def login_required
53 authorized? || 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 user 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 |format|
66 format.html do
67 store_location
68 redirect_to new_session_path
69 end
70 end
71 end
72
73 # Store the URI of the current request in the session.
74 #
75 # We can return to this location by calling #redirect_back_or_default.
76 def store_location
77 session[:return_to] = request.request_uri
78 end
79
80 # Redirect to the URI stored by the most recent store_location call or
81 # to the passed default. Set an appropriately modified
82 # after_filter :store_location, :only => [:index, :new, :show, :edit]
83 # for any controller you want to be bounce-backable.
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?, :authorized? if base.respond_to? :helper_method
93 end
94
95 #
96 # Login
97 #
98
99 # Called from #current_user. First attempt to login by the user id stored in the session.
100 def login_from_session
101 self.current_user = User.find_by_id(session[:user_id]) if session[:user_id]
102 end
103
104 #
105 # Logout
106 #
107
108 # This is ususally what you want; resetting the session willy-nilly wreaks
109 # havoc with forgery protection, and is only strictly necessary on login.
110 # However, **all session state variables should be unset here**.
111 def logout_keeping_session!
112 # Kill server-side auth cookie
113 @current_user.forget_me if @current_user.is_a? User
114 @current_user = false # not logged in, and don't do it for me
115 kill_remember_cookie! # Kill client-side auth cookie
116 session[:user_id] = nil # keeps the session but kill our variable
117 # explicitly kill any other session variables you set
118 end
119
120 # The session should only be reset at the tail end of a form POST --
121 # otherwise the request forgery protection fails. It's only really necessary
122 # when you cross quarantine (logged-out to logged-in).
123 def logout_killing_session!
124 logout_keeping_session!
125 reset_session
126 end
127end