Module: Ramaze::Helper::Auth

Includes:
Traited
Defined in:
lib/ramaze/helper/auth.rb

Overview

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.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(into)



22
23
24
# File 'lib/ramaze/helper/auth.rb', line 22

def self.included(into)
  into.helper(:stack)
end

Instance Method Details

- (Object) auth_login(user, pass) (private)

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.

Parameters:

  • user (String)

    The users's username.

  • pass (String)

    The user's password.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ramaze/helper/auth.rb', line 81

def (user, pass)
  return unless user and pass
  return if user.empty? or pass.empty?

  return unless table   = ancestral_trait[:auth_table]
  return unless hashify = ancestral_trait[:auth_hashify]

  if table.respond_to?(:to_sym) or table.respond_to?(:to_str)
    table = send(table)
  elsif table.respond_to?(:call)
    table = table.call
  end

  return unless table[user] == hashify.call(pass)

  session[:logged_in] = true
  session[:username]  = user
end

- (Object) auth_logout (private)

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



103
104
105
106
# File 'lib/ramaze/helper/auth.rb', line 103

def auth_logout
  session.delete(:logged_in)
  session.delete(:username)
end

- (String) auth_template (private)

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

Returns:

  • (String)

    The login form.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ramaze/helper/auth.rb', line 112

def auth_template
  <<-TEMPLATE.strip!
<form method="post" action="#{r(:)}">
  <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>
  TEMPLATE
end

- (true false) logged_in? (private)

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

Returns:

  • (true false)

    Whether user is logged in right now



69
70
71
# File 'lib/ramaze/helper/auth.rb', line 69

def logged_in?
  !!session[:logged_in]
end

- (String) login

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

Returns:

  • (String)

    The login template in case the user's login data was incorrect.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ramaze/helper/auth.rb', line 33

def 
  if trait[:auth_post_only] and !request.post?
    return auth_template
  end

  @username, password = request[:username, :password]

  answer(request.referer) if (@username, password)

  return auth_template
end

- (Object) login_required (private)

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



59
60
61
# File 'lib/ramaze/helper/auth.rb', line 59

def 
  call(r(:login)) unless logged_in?
end

- (Object) logout

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



48
49
50
51
# File 'lib/ramaze/helper/auth.rb', line 48

def logout
  auth_logout
  redirect_referrer
end