1 <?xml version=
"1.0" encoding=
"iso-8859-1"?>
3 PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en" lang=
"en">
8 <title>Module: AuthenticatedSystem
</title>
9 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1" />
10 <meta http-equiv=
"Content-Script-Type" content=
"text/javascript" />
11 <link rel=
"stylesheet" href=
".././rdoc-style.css" type=
"text/css" media=
"screen" />
12 <script type=
"text/javascript">
15 function popupCode( url ) {
16 window.open(url,
"Code",
"resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
19 function toggleCode( id ) {
20 if ( document.getElementById )
21 elem = document.getElementById( id );
22 else if ( document.all )
23 elem = eval(
"document.all." + id );
27 elemStyle = elem.style;
29 if ( elemStyle.display !=
"block" ) {
30 elemStyle.display =
"block"
32 elemStyle.display =
"none"
38 // Make codeblocks hidden by default
39 document.writeln(
"<style type=\"text/css\
">div.method-source-code { display: none }</style>" )
49 <div id=
"classHeader">
50 <table class=
"header-table">
51 <tr class=
"top-aligned-row">
52 <td><strong>Module
</strong></td>
53 <td class=
"class-name-in-header">AuthenticatedSystem
</td>
55 <tr class=
"top-aligned-row">
56 <td><strong>In:
</strong></td>
58 <a href=
"../files/lib/authenticated_system_rb.html">
59 lib/authenticated_system.rb
67 <!-- banner header -->
69 <div id=
"bodyContent">
73 <div id=
"contextContent">
79 <div id=
"method-list">
80 <h3 class=
"section-bar">Methods
</h3>
82 <div class=
"name-list">
83 <a href=
"#M000048">access_denied
</a>
84 <a href=
"#M000046">authorized?
</a>
85 <a href=
"#M000044">current_user
</a>
86 <a href=
"#M000045">current_user=
</a>
87 <a href=
"#M000051">included
</a>
88 <a href=
"#M000043">logged_in?
</a>
89 <a href=
"#M000052">login_from_cookie
</a>
90 <a href=
"#M000047">login_required
</a>
91 <a href=
"#M000042">offline?
</a>
92 <a href=
"#M000050">redirect_back_or_default
</a>
93 <a href=
"#M000049">store_location
</a>
111 <!-- if method_list -->
113 <h3 class=
"section-bar">Protected Class methods
</h3>
115 <div id=
"method-M000051" class=
"method-detail">
116 <a name=
"M000051"></a>
118 <div class=
"method-heading">
119 <a href=
"#M000051" class=
"method-signature">
120 <span class=
"method-name">included
</span><span class=
"method-args">(base)
</span>
124 <div class=
"method-description">
126 Inclusion hook to make
<a
127 href=
"AuthenticatedSystem.html#M000044">current_user
</a> and
<a
128 href=
"AuthenticatedSystem.html#M000043">logged_in?
</a> available as
129 ActionView helper methods.
131 <p><a class=
"source-toggle" href=
"#"
132 onclick=
"toggleCode('M000051-source');return false;">[Source]
</a></p>
133 <div class=
"method-source-code" id=
"M000051-source">
135 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
106</span>
136 106:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-keyword kw">self
</span>.
<span class=
"ruby-identifier">included
</span>(
<span class=
"ruby-identifier">base
</span>)
137 107:
<span class=
"ruby-identifier">base
</span>.
<span class=
"ruby-identifier">send
</span> <span class=
"ruby-identifier">:helper_method
</span>,
<span class=
"ruby-identifier">:current_user
</span>,
<span class=
"ruby-identifier">:logged_in?
</span>,
<span class=
"ruby-identifier">:offline?
</span>
138 108:
<span class=
"ruby-keyword kw">end
</span>
144 <h3 class=
"section-bar">Protected Instance methods
</h3>
146 <div id=
"method-M000048" class=
"method-detail">
147 <a name=
"M000048"></a>
149 <div class=
"method-heading">
150 <a href=
"#M000048" class=
"method-signature">
151 <span class=
"method-name">access_denied
</span><span class=
"method-args">()
</span>
155 <div class=
"method-description">
157 Redirect as appropriate when an access request fails.
160 The default action is to redirect to the login screen.
163 Override this method in your controllers if you want to have special
164 behavior in case the user is not authorized to access the requested action.
165 For example, a popup window might simply close itself.
167 <p><a class=
"source-toggle" href=
"#"
168 onclick=
"toggleCode('M000048-source');return false;">[Source]
</a></p>
169 <div class=
"method-source-code" id=
"M000048-source">
171 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
75</span>
172 75:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">access_denied
</span>
173 76:
<span class=
"ruby-identifier">respond_to
</span> <span class=
"ruby-keyword kw">do
</span> <span class=
"ruby-operator">|
</span><span class=
"ruby-identifier">accepts
</span><span class=
"ruby-operator">|
</span>
174 77:
<span class=
"ruby-identifier">accepts
</span>.
<span class=
"ruby-identifier">html
</span> <span class=
"ruby-keyword kw">do
</span>
175 78:
<span class=
"ruby-identifier">store_location
</span>
176 79:
<span class=
"ruby-identifier">redirect_to
</span> <span class=
"ruby-identifier">:controller
</span> =
<span class=
"ruby-operator">></span> <span class=
"ruby-value str">'sessions'
</span>,
<span class=
"ruby-identifier">:action
</span> =
<span class=
"ruby-operator">></span> <span class=
"ruby-value str">'new'
</span>
177 80:
<span class=
"ruby-keyword kw">end
</span>
178 81:
<span class=
"ruby-identifier">accepts
</span>.
<span class=
"ruby-identifier">xml
</span> <span class=
"ruby-keyword kw">do
</span>
179 82:
<span class=
"ruby-identifier">headers
</span>[
<span class=
"ruby-value str">"Status
"</span>] =
<span class=
"ruby-value str">"Unauthorized
"</span>
180 83:
<span class=
"ruby-identifier">headers
</span>[
<span class=
"ruby-value str">"WWW-Authenticate
"</span>] =
<span class=
"ruby-value str">%(Basic realm=
"Web Password
")
</span>
181 84:
<span class=
"ruby-identifier">render
</span> <span class=
"ruby-identifier">:text
</span> =
<span class=
"ruby-operator">></span> <span class=
"ruby-value str">"Couldn't authenticate you
"</span>,
<span class=
"ruby-identifier">:status
</span> =
<span class=
"ruby-operator">></span> <span class=
"ruby-value str">'
401 Unauthorized'
</span>
182 85:
<span class=
"ruby-keyword kw">end
</span>
183 86:
<span class=
"ruby-keyword kw">end
</span>
184 87:
<span class=
"ruby-keyword kw">false
</span>
185 88:
<span class=
"ruby-keyword kw">end
</span>
191 <div id=
"method-M000046" class=
"method-detail">
192 <a name=
"M000046"></a>
194 <div class=
"method-heading">
195 <a href=
"#M000046" class=
"method-signature">
196 <span class=
"method-name">authorized?
</span><span class=
"method-args">()
</span>
200 <div class=
"method-description">
202 Check if the user is authorized.
205 Override this method in your controllers if you want to restrict access to
206 only a few actions or if you want to check if the user has the correct
215 current_user.login !=
"bob
"
218 <p><a class=
"source-toggle" href=
"#"
219 onclick=
"toggleCode('M000046-source');return false;">[Source]
</a></p>
220 <div class=
"method-source-code" id=
"M000046-source">
222 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
43</span>
223 43:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">authorized?
</span>
224 44:
<span class=
"ruby-keyword kw">true
</span>
225 45:
<span class=
"ruby-keyword kw">end
</span>
231 <div id=
"method-M000044" class=
"method-detail">
232 <a name=
"M000044"></a>
234 <div class=
"method-heading">
235 <a href=
"#M000044" class=
"method-signature">
236 <span class=
"method-name">current_user
</span><span class=
"method-args">()
</span>
240 <div class=
"method-description">
242 Accesses the current user from the session.
244 <p><a class=
"source-toggle" href=
"#"
245 onclick=
"toggleCode('M000044-source');return false;">[Source]
</a></p>
246 <div class=
"method-source-code" id=
"M000044-source">
248 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
13</span>
249 13:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">current_user
</span>
250 14:
<span class=
"ruby-keyword kw">if
</span> <span class=
"ruby-identifier">offline?
</span>
251 15:
<span class=
"ruby-ivar">@current_user
</span> <span class=
"ruby-operator">||=
</span> <span class=
"ruby-constant">User
</span>.
<span class=
"ruby-identifier">find
</span>(
<span class=
"ruby-identifier">:first
</span>)
<span class=
"ruby-operator">||
</span> <span class=
"ruby-identifier">:false
</span>
252 16:
<span class=
"ruby-keyword kw">else
</span>
253 17:
<span class=
"ruby-ivar">@current_user
</span> <span class=
"ruby-operator">||=
</span> (
<span class=
"ruby-identifier">session
</span>[
<span class=
"ruby-identifier">:user
</span>]
<span class=
"ruby-operator">&&</span> <span class=
"ruby-constant">User
</span>.
<span class=
"ruby-identifier">find_by_id
</span>(
<span class=
"ruby-identifier">session
</span>[
<span class=
"ruby-identifier">:user
</span>]))
<span class=
"ruby-operator">||
</span> <span class=
"ruby-identifier">:false
</span>
254 18:
<span class=
"ruby-keyword kw">end
</span>
255 19:
<span class=
"ruby-keyword kw">end
</span>
261 <div id=
"method-M000045" class=
"method-detail">
262 <a name=
"M000045"></a>
264 <div class=
"method-heading">
265 <a href=
"#M000045" class=
"method-signature">
266 <span class=
"method-name">current_user=
</span><span class=
"method-args">(new_user)
</span>
270 <div class=
"method-description">
272 Store the given user in the session.
274 <p><a class=
"source-toggle" href=
"#"
275 onclick=
"toggleCode('M000045-source');return false;">[Source]
</a></p>
276 <div class=
"method-source-code" id=
"M000045-source">
278 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
22</span>
279 22:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">current_user=
</span>(
<span class=
"ruby-identifier">new_user
</span>)
280 23:
<span class=
"ruby-keyword kw">if
</span> <span class=
"ruby-identifier">offline?
</span>
281 24:
<span class=
"ruby-ivar">@current_user
</span> =
<span class=
"ruby-identifier">new_user
</span>
282 25:
<span class=
"ruby-keyword kw">else
</span>
283 26:
<span class=
"ruby-identifier">session
</span>[
<span class=
"ruby-identifier">:user
</span>] = (
<span class=
"ruby-identifier">new_user
</span>.
<span class=
"ruby-identifier">nil?
</span> <span class=
"ruby-operator">||
</span> <span class=
"ruby-identifier">new_user
</span>.
<span class=
"ruby-identifier">is_a?
</span>(
<span class=
"ruby-constant">Symbol
</span>))
<span class=
"ruby-operator">?
</span> <span class=
"ruby-keyword kw">nil
</span> <span class=
"ruby-operator">:
</span> <span class=
"ruby-identifier">new_user
</span>.
<span class=
"ruby-identifier">id
</span>
284 27:
<span class=
"ruby-ivar">@current_user
</span> =
<span class=
"ruby-identifier">new_user
</span>
285 28:
<span class=
"ruby-keyword kw">end
</span>
286 29:
<span class=
"ruby-keyword kw">end
</span>
292 <div id=
"method-M000043" class=
"method-detail">
293 <a name=
"M000043"></a>
295 <div class=
"method-heading">
296 <a href=
"#M000043" class=
"method-signature">
297 <span class=
"method-name">logged_in?
</span><span class=
"method-args">()
</span>
301 <div class=
"method-description">
303 Returns true or false if the user is logged in. Preloads @
<a
304 href=
"AuthenticatedSystem.html#M000044">current_user
</a> with the user
305 model if they
’re logged in.
307 <p><a class=
"source-toggle" href=
"#"
308 onclick=
"toggleCode('M000043-source');return false;">[Source]
</a></p>
309 <div class=
"method-source-code" id=
"M000043-source">
311 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
8</span>
312 8:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">logged_in?
</span>
313 9:
<span class=
"ruby-identifier">current_user
</span> <span class=
"ruby-operator">!=
</span> <span class=
"ruby-identifier">:false
</span>
314 10:
<span class=
"ruby-keyword kw">end
</span>
320 <div id=
"method-M000052" class=
"method-detail">
321 <a name=
"M000052"></a>
323 <div class=
"method-heading">
324 <a href=
"#M000052" class=
"method-signature">
325 <span class=
"method-name">login_from_cookie
</span><span class=
"method-args">()
</span>
329 <div class=
"method-description">
331 When called with before_filter :
<a
332 href=
"AuthenticatedSystem.html#M000052">login_from_cookie
</a> will check
333 for an :auth_token cookie and log the user back in if apropriate
335 <p><a class=
"source-toggle" href=
"#"
336 onclick=
"toggleCode('M000052-source');return false;">[Source]
</a></p>
337 <div class=
"method-source-code" id=
"M000052-source">
339 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
112</span>
340 112:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">login_from_cookie
</span>
341 113:
<span class=
"ruby-keyword kw">return
</span> <span class=
"ruby-keyword kw">unless
</span> <span class=
"ruby-identifier">cookies
</span>[
<span class=
"ruby-identifier">:auth_token
</span>]
<span class=
"ruby-operator">&&</span> <span class=
"ruby-operator">!
</span><span class=
"ruby-identifier">logged_in?
</span>
342 114:
<span class=
"ruby-identifier">user
</span> =
<span class=
"ruby-constant">User
</span>.
<span class=
"ruby-identifier">find_by_remember_token
</span>(
<span class=
"ruby-identifier">cookies
</span>[
<span class=
"ruby-identifier">:auth_token
</span>])
343 115:
<span class=
"ruby-keyword kw">if
</span> <span class=
"ruby-identifier">user
</span> <span class=
"ruby-operator">&&</span> <span class=
"ruby-identifier">user
</span>.
<span class=
"ruby-identifier">remember_token?
</span>
344 116:
<span class=
"ruby-identifier">user
</span>.
<span class=
"ruby-identifier">remember_me
</span>
345 117:
<span class=
"ruby-keyword kw">self
</span>.
<span class=
"ruby-identifier">current_user
</span> =
<span class=
"ruby-identifier">user
</span>
346 118:
<span class=
"ruby-identifier">cookies
</span>[
<span class=
"ruby-identifier">:auth_token
</span>] = {
<span class=
"ruby-identifier">:value
</span> =
<span class=
"ruby-operator">></span> <span class=
"ruby-keyword kw">self
</span>.
<span class=
"ruby-identifier">current_user
</span>.
<span class=
"ruby-identifier">remember_token
</span> ,
<span class=
"ruby-identifier">:expires
</span> =
<span class=
"ruby-operator">></span> <span class=
"ruby-keyword kw">self
</span>.
<span class=
"ruby-identifier">current_user
</span>.
<span class=
"ruby-identifier">remember_token_expires_at
</span> }
347 119:
<span class=
"ruby-identifier">flash
</span>[
<span class=
"ruby-identifier">:notice
</span>] =
<span class=
"ruby-value str">"Logged in successfully
"</span>
348 120:
<span class=
"ruby-keyword kw">end
</span>
349 121:
<span class=
"ruby-keyword kw">end
</span>
355 <div id=
"method-M000047" class=
"method-detail">
356 <a name=
"M000047"></a>
358 <div class=
"method-heading">
359 <a href=
"#M000047" class=
"method-signature">
360 <span class=
"method-name">login_required
</span><span class=
"method-args">()
</span>
364 <div class=
"method-description">
366 Filter method to enforce a login requirement.
369 To require logins for all actions, use this in your controllers:
372 before_filter :login_required
375 To require logins for specific actions, use this in your controllers:
378 before_filter :login_required, :only =
> [ :edit, :update ]
381 To skip this in a subclassed controller:
384 skip_before_filter :login_required
386 <p><a class=
"source-toggle" href=
"#"
387 onclick=
"toggleCode('M000047-source');return false;">[Source]
</a></p>
388 <div class=
"method-source-code" id=
"M000047-source">
390 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
61</span>
391 61:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">login_required
</span>
392 62:
<span class=
"ruby-identifier">username
</span>,
<span class=
"ruby-identifier">passwd
</span> =
<span class=
"ruby-identifier">get_auth_data
</span>
393 63:
<span class=
"ruby-keyword kw">self
</span>.
<span class=
"ruby-identifier">current_user
</span> <span class=
"ruby-operator">||=
</span> <span class=
"ruby-constant">User
</span>.
<span class=
"ruby-identifier">authenticate
</span>(
<span class=
"ruby-identifier">username
</span>,
<span class=
"ruby-identifier">passwd
</span>)
<span class=
"ruby-operator">||
</span> <span class=
"ruby-identifier">:false
</span> <span class=
"ruby-keyword kw">if
</span> <span class=
"ruby-identifier">username
</span> <span class=
"ruby-operator">&&</span> <span class=
"ruby-identifier">passwd
</span>
394 64:
<span class=
"ruby-identifier">logged_in?
</span> <span class=
"ruby-operator">&&</span> <span class=
"ruby-identifier">authorized?
</span> <span class=
"ruby-value">?
</span><span class=
"ruby-keyword kw">true
</span> <span class=
"ruby-operator">:
</span> <span class=
"ruby-identifier">access_denied
</span>
395 65:
<span class=
"ruby-keyword kw">end
</span>
401 <div id=
"method-M000042" class=
"method-detail">
402 <a name=
"M000042"></a>
404 <div class=
"method-heading">
405 <a href=
"#M000042" class=
"method-signature">
406 <span class=
"method-name">offline?
</span><span class=
"method-args">()
</span>
410 <div class=
"method-description">
411 <p><a class=
"source-toggle" href=
"#"
412 onclick=
"toggleCode('M000042-source');return false;">[Source]
</a></p>
413 <div class=
"method-source-code" id=
"M000042-source">
415 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
3</span>
416 3:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">offline?
</span>
417 4: (
<span class=
"ruby-constant">ENV
</span>[
<span class=
"ruby-value str">'OFFLINE_MODE'
</span>])
<span class=
"ruby-operator">?
</span> (
<span class=
"ruby-constant">ENV
</span>[
<span class=
"ruby-value str">'OFFLINE_MODE'
</span>]
<span class=
"ruby-operator">==
</span> <span class=
"ruby-value str">'true'
</span>)
<span class=
"ruby-operator">:
</span> <span class=
"ruby-keyword kw">false
</span>
418 5:
<span class=
"ruby-keyword kw">end
</span>
424 <div id=
"method-M000050" class=
"method-detail">
425 <a name=
"M000050"></a>
427 <div class=
"method-heading">
428 <a href=
"#M000050" class=
"method-signature">
429 <span class=
"method-name">redirect_back_or_default
</span><span class=
"method-args">(default)
</span>
433 <div class=
"method-description">
435 Redirect to the URI stored by the most recent
<a
436 href=
"AuthenticatedSystem.html#M000049">store_location
</a> call or to the
439 <p><a class=
"source-toggle" href=
"#"
440 onclick=
"toggleCode('M000050-source');return false;">[Source]
</a></p>
441 <div class=
"method-source-code" id=
"M000050-source">
443 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
99</span>
444 99:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">redirect_back_or_default
</span>(
<span class=
"ruby-identifier">default
</span>)
445 100:
<span class=
"ruby-identifier">session
</span>[
<span class=
"ruby-identifier">:return_to
</span>]
<span class=
"ruby-operator">?
</span> <span class=
"ruby-identifier">redirect_to
</span>(
<span class=
"ruby-identifier">session
</span>[
<span class=
"ruby-identifier">:return_to
</span>])
<span class=
"ruby-operator">:
</span> <span class=
"ruby-identifier">redirect_to
</span>(
<span class=
"ruby-identifier">default
</span>)
446 101:
<span class=
"ruby-identifier">session
</span>[
<span class=
"ruby-identifier">:return_to
</span>] =
<span class=
"ruby-keyword kw">nil
</span>
447 102:
<span class=
"ruby-keyword kw">end
</span>
453 <div id=
"method-M000049" class=
"method-detail">
454 <a name=
"M000049"></a>
456 <div class=
"method-heading">
457 <a href=
"#M000049" class=
"method-signature">
458 <span class=
"method-name">store_location
</span><span class=
"method-args">()
</span>
462 <div class=
"method-description">
464 Store the URI of the current request in the session.
467 We can return to this location by calling
<a
468 href=
"AuthenticatedSystem.html#M000050">redirect_back_or_default
</a>.
470 <p><a class=
"source-toggle" href=
"#"
471 onclick=
"toggleCode('M000049-source');return false;">[Source]
</a></p>
472 <div class=
"method-source-code" id=
"M000049-source">
474 <span class=
"ruby-comment cmt"># File lib/authenticated_system.rb, line
93</span>
475 93:
<span class=
"ruby-keyword kw">def
</span> <span class=
"ruby-identifier">store_location
</span>
476 94:
<span class=
"ruby-identifier">session
</span>[
<span class=
"ruby-identifier">:return_to
</span>] =
<span class=
"ruby-identifier">request
</span>.
<span class=
"ruby-identifier">request_uri
</span>
477 95:
<span class=
"ruby-keyword kw">end
</span>
490 <div id=
"validator-badges">
491 <p><small><a href=
"http://validator.w3.org/check/referer">[Validate]
</a></small></p>