Module: DohMerb::Login

Defined in:
lib/doh/merb/login.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#index_pageObject

Returns the value of attribute index_page.



6
7
8
# File 'lib/doh/merb/login.rb', line 6

def index_page
  @index_page
end

#login_db_nameObject

Returns the value of attribute login_db_name.



6
7
8
# File 'lib/doh/merb/login.rb', line 6

def 
  @login_db_name
end

#login_pageObject

Returns the value of attribute login_page.



6
7
8
# File 'lib/doh/merb/login.rb', line 6

def 
  @login_page
end

#login_tableObject

Returns the value of attribute login_table.



6
7
8
# File 'lib/doh/merb/login.rb', line 6

def 
  @login_table
end

#password_db_nameObject

Returns the value of attribute password_db_name.



6
7
8
# File 'lib/doh/merb/login.rb', line 6

def password_db_name
  @password_db_name
end

#password_fieldnameObject

Returns the value of attribute password_fieldname.



6
7
8
# File 'lib/doh/merb/login.rb', line 6

def password_fieldname
  @password_fieldname
end

#username_fieldnameObject

set the attr_accessors or override them



9
10
11
# File 'lib/doh/merb/login.rb', line 9

def username_fieldname
  @username_fieldname
end

Instance Method Details

#authenticate_username_key(username, key) ⇒ Object

override this to have custom behavior



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/doh/merb/login.rb', line 53

def authenticate_username_key(username, key)
  begin
    row = DohDb::select_optional_row("SELECT * FROM #{} WHERE #{} = #{username.to_sql}")
    if row
      if key == get_session_key(username, row[password_db_name])
        self.instance_variable_set("@#{}_row", row)
        return true
      end
    end
  rescue Exception => e
    dohlog.warn("got error: #{e.inspect} (called from authenticate_username_key) -- are you missing the table: #{.inspect}?")
  end
  return nil
end

#authenticated?Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/doh/merb/login.rb', line 47

def authenticated?
  dohlog.debug("authenticated? called -- session[:dohmerb_login_username] = #{session[:dohmerb_login_username]}, session[:dohmerb_login_key] = #{session[:dohmerb_login_key]}")
  authenticate_username_key(session[:dohmerb_login_username], session[:dohmerb_login_key]) if !session[:dohmerb_login_username].to_s.strip.empty? && !session[:dohmerb_login_key].to_s.strip.empty?
end

#get_session_key(username, password) ⇒ Object



68
69
70
# File 'lib/doh/merb/login.rb', line 68

def get_session_key(username, password)
  Digest::SHA1.hexdigest("#{username}**#{password}")
end

#logged_in?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/doh/merb/login.rb', line 72

def logged_in?
  authenticated?
end

#login_helperObject

kjmtodobb: figure out how to make it so login / logout can just exist here instead of having to get called as helpers – merb doesn’t recognize them as actions if they’re included via a module in regular fashion…



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/doh/merb/login.rb', line 77

def 
  if request.post?
    session[:dohmerb_login_username] = params[username_fieldname]
    session[:dohmerb_login_key] = get_session_key(params[username_fieldname], params[password_fieldname])
    if authenticated?
      if session[:dohmerb_original_uri]
        dohlog.debug("redirecting to original uri: #{session[:dohmerb_original_uri].inspect}")
        redirect session[:dohmerb_original_uri]
      else
        dohlog.debug("no original uri, redirecting to index")
        redirect index_page
      end
    else
      dohlog.debug("login_helper called, not authenticated -- redirecting to login page")
      redirect 
    end
  end
  render
end

#login_requiredObject



37
38
39
40
41
42
43
44
45
# File 'lib/doh/merb/login.rb', line 37

def 
  if !authenticated?
    dohlog.debug "not authenticated, redirecting to #{}"
    session[:dohmerb_original_uri] = request.uri
    dohlog.debug("storing original uri #{request.uri.inspect}")
    redirect 
    throw :halt
  end
end

#logout_helperObject



97
98
99
100
101
102
# File 'lib/doh/merb/login.rb', line 97

def logout_helper
  session[:dohmerb_login_username] = ''
  session[:dohmerb_login_key] = ''
  redirect 
  render
end