Module: TentSteakFeatures::AuthView

Included in:
AuthController
Defined in:
lib/tent_steak/auth.rb

Overview

TentSteak :auth feature to simplify user authentication. Provides a customizable #current_user method and HTML form helpers for the view.

Constant Summary collapse

ACTION_LOGIN =
"action_login"
ACTION_LOGOUT =
"action_logout"
AUTH_LOGOUT =
"auth_logout"
AUTH_VALID =
"auth_valid"
AUTH_DENIED =
"auth_denied"

Instance Method Summary collapse

Instance Method Details

#auth_form(action, form_class = "auth_form", title_class = "auth_title", field_size = 12) ⇒ Object

Create auth login form if not already logged in, or a logout form if currently logged in. Pass in a block to be rendered if user is logged in; this is useful for options available only to authenticated users.



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tent_steak/auth.rb', line 90

def auth_form(action, form_class = "auth_form", title_class = "auth_title", field_size = 12)
  username = @input.username || current_user
  div(:class => form_class) do
    if logged_in?
      p("Logged in as #{username}", :class => title_class)
      auth_logout_form(action)
      yield if block_given?
    else
      p("Log in new user", :class => title_class)
      (action, username, field_size)
    end
  end
end

#auth_login_form(action, username = "", field_size = 12) ⇒ Object

HTML helper to generate a login form. POSTs to the given action with a hidden field of ‘action’ => ACTION_LOGIN. If username is provided, its value is loaded into the username text field.



67
68
69
70
71
72
73
74
75
76
# File 'lib/tent_steak/auth.rb', line 67

def (action, username = "", field_size = 12)
  form :action => action, :method => "post" do
    label "Username:"; br
    input_text "username", username, :size => field_size; br
    label "Password:"; br
    input_password "password", :size => field_size; br
    input_hidden "action", ACTION_LOGIN
    input_submit "Login"
  end
end

#auth_logout_form(action) ⇒ Object

HTML helper to generate a logout form. POSTs to the given action with a hidden field of ‘action’ => ACTION_LOGOUT.



80
81
82
83
84
85
# File 'lib/tent_steak/auth.rb', line 80

def auth_logout_form(action)
  form :action => action, :method => "post" do
    input_hidden "action", ACTION_LOGOUT
    input_submit "Logout"
  end
end

#current_userObject

Authenticated user; should be nil if user is not logged in. By default, current_user returns @current_user. Customize with #set_current_user_proc.



45
46
47
# File 'lib/tent_steak/auth.rb', line 45

def current_user
  @user_proc ? @user_proc.call : @current_user
end

#logged_in?Boolean

true if user is authenticated, i.e. #current_user is non-blank.

Returns:

  • (Boolean)


60
61
62
# File 'lib/tent_steak/auth.rb', line 60

def logged_in?
  !current_user.blank?
end

#set_current_user_proc(user_proc) ⇒ Object

Customize the return value of current_user. Default proc is @current_user. Useful to redirect current_user to the database, or to a persisted session.

set_current_user_proc proc { @session['auth_user'] }


55
56
57
# File 'lib/tent_steak/auth.rb', line 55

def set_current_user_proc(user_proc)
  @user_proc = user_proc
end