Included Modules

Class Index [+]

Quicksearch

Ramaze::Helper::Auth

The Auth helper can be used for authentication without using a model. This can be useful when working with very basic applications that don’t require database access.

If you’re looking for a way to do authentication using a model you should take a look at Helper::User instead.

Public Class Methods

included(into) click to toggle source
    # File lib/ramaze/helper/auth.rb, line 22
22:       def self.included(into)
23:         into.helper(:stack)
24:       end

Public Instance Methods

login() click to toggle source

Log a user in based on the :username and :password key in the request hash.

@return [String] The login template in case the user’s login data was

 incorrect.
    # File lib/ramaze/helper/auth.rb, line 33
33:       def login
34:         if trait[:auth_post_only] and !request.post?
35:           return auth_template
36:         end
37: 
38:         @username, password = request[:username, :password]
39: 
40:         answer(request.referer) if auth_login(@username, password)
41: 
42:         return auth_template
43:       end
logout() click to toggle source

Log the user out and redirect him back to the previous page.

    # File lib/ramaze/helper/auth.rb, line 48
48:       def logout
49:         auth_logout
50:         redirect_referrer
51:       end

Private Instance Methods

auth_login(user, pass) click to toggle source

Try to log the user in based on the username and password. This method is called by the login() method and shouldn’t be called directly.

@param [String] user The users’s username. @param [String] pass The user’s password.

    # File lib/ramaze/helper/auth.rb, line 81
81:       def auth_login(user, pass)
82:         return unless user and pass
83:         return if user.empty? or pass.empty?
84: 
85:         return unless table   = ancestral_trait[:auth_table]
86:         return unless hashify = ancestral_trait[:auth_hashify]
87: 
88:         if table.respond_to?(:to_sym) or table.respond_to?(:to_str)
89:           table = send(table)
90:         elsif table.respond_to?(:call)
91:           table = table.call
92:         end
93: 
94:         return unless table[user] == hashify.call(pass)
95: 
96:         session[:logged_in] = true
97:         session[:username]  = user
98:       end
auth_logout() click to toggle source

Remove the session items that specified that the user was logged in.

     # File lib/ramaze/helper/auth.rb, line 103
103:       def auth_logout
104:         session.delete(:logged_in)
105:         session.delete(:username)
106:       end
auth_template() click to toggle source

Method that returns a small form that can be used for logging in.

@return [String] The login form.

     # File lib/ramaze/helper/auth.rb, line 112
112:       def auth_template
113:         <form method="post" action="#{r(:login)}">  <ul style="list-style:none;">    <li>Username: <input type="text" name="username" value="#@username"/></li>    <li>Password: <input type="password" name="password" /></li>    <li><input type="submit" /></li>  </ul></form>.strip!
114:       end
logged_in?() click to toggle source

Validate the user’s session and return a boolean that indicates if the user is logged in or not.

@return [true false] Whether user is logged in right now

    # File lib/ramaze/helper/auth.rb, line 69
69:       def logged_in?
70:         !!session[:logged_in]
71:       end
login_required() click to toggle source

Validate the user’s session and redirect him/her to the login page in case the user isn’t logged in.

    # File lib/ramaze/helper/auth.rb, line 59
59:       def login_required
60:         call(r(:login)) unless logged_in?
61:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.