Module: DohRails::Login
- Defined in:
- lib/doh/rails/login.rb
Instance Attribute Summary collapse
-
#index_page ⇒ Object
Returns the value of attribute index_page.
-
#login_controller ⇒ Object
set the attr_accessors or override them.
-
#login_db_name ⇒ Object
Returns the value of attribute login_db_name.
-
#login_model ⇒ Object
Returns the value of attribute login_model.
-
#login_page ⇒ Object
Returns the value of attribute login_page.
-
#login_table ⇒ Object
Returns the value of attribute login_table.
-
#password_db_name ⇒ Object
Returns the value of attribute password_db_name.
-
#password_fieldname ⇒ Object
Returns the value of attribute password_fieldname.
-
#username_fieldname ⇒ Object
Returns the value of attribute username_fieldname.
Instance Method Summary collapse
-
#authenticate_username_key(username, key) ⇒ Object
override this to have custom behavior.
- #authenticated? ⇒ Boolean
- #get_session_key(username, password) ⇒ Object
- #logged_in? ⇒ Boolean
- #logged_out ⇒ Object
- #login ⇒ Object
- #login_failed ⇒ Object
- #login_page_hit ⇒ Object
- #login_redirect ⇒ Object
- #login_required ⇒ Object
- #login_succeeded ⇒ Object
- #logout ⇒ Object
Instance Attribute Details
#index_page ⇒ Object
Returns the value of attribute index_page.
6 7 8 |
# File 'lib/doh/rails/login.rb', line 6 def index_page @index_page end |
#login_controller ⇒ Object
set the attr_accessors or override them
10 11 12 |
# File 'lib/doh/rails/login.rb', line 10 def login_controller @login_controller end |
#login_db_name ⇒ Object
Returns the value of attribute login_db_name.
6 7 8 |
# File 'lib/doh/rails/login.rb', line 6 def login_db_name @login_db_name end |
#login_model ⇒ Object
Returns the value of attribute login_model.
6 7 8 |
# File 'lib/doh/rails/login.rb', line 6 def login_model @login_model end |
#login_page ⇒ Object
Returns the value of attribute login_page.
6 7 8 |
# File 'lib/doh/rails/login.rb', line 6 def login_page @login_page end |
#login_table ⇒ Object
Returns the value of attribute login_table.
6 7 8 |
# File 'lib/doh/rails/login.rb', line 6 def login_table @login_table end |
#password_db_name ⇒ Object
Returns the value of attribute password_db_name.
6 7 8 |
# File 'lib/doh/rails/login.rb', line 6 def password_db_name @password_db_name end |
#password_fieldname ⇒ Object
Returns the value of attribute password_fieldname.
6 7 8 |
# File 'lib/doh/rails/login.rb', line 6 def password_fieldname @password_fieldname end |
#username_fieldname ⇒ Object
Returns the value of attribute username_fieldname.
6 7 8 |
# File 'lib/doh/rails/login.rb', line 6 def username_fieldname @username_fieldname end |
Instance Method Details
#authenticate_username_key(username, key) ⇒ Object
override this to have custom behavior
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/doh/rails/login.rb', line 82 def authenticate_username_key(username, key) begin row = DohDb::select_optional_row("SELECT * FROM #{login_table} WHERE #{login_db_name} = #{username.to_sql}") if row if key == get_session_key(username, row[password_db_name]) self.instance_variable_set("@#{login_table}_row", row) dohlog.debug("authenticate_username returning true") return true end end rescue Exception => e dohlog.warn("got error: #{e.inspect} (called from authenticate_username_key) -- are you missing the table: #{login_table.inspect}?") end dohlog.debug("authenticate_username returning nil") return nil end |
#authenticated? ⇒ Boolean
71 72 73 74 75 76 77 78 79 |
# File 'lib/doh/rails/login.rb', line 71 def authenticated? if @authentication_checked return @authenticated end dohlog.debug("authenticated? called -- session[:dohrails_login_username] = #{session[:dohrails_login_username]}") @authenticated = authenticate_username_key(session[:dohrails_login_username], session[:dohrails_login_key]) if !session[:dohrails_login_username].to_s.strip.empty? && !session[:dohrails_login_key].to_s.strip.empty? @authentication_checked = true return @authenticated end |
#get_session_key(username, password) ⇒ Object
99 100 101 |
# File 'lib/doh/rails/login.rb', line 99 def get_session_key(username, password) Digest::SHA1.hexdigest("#{username}**#{password}") end |
#logged_in? ⇒ Boolean
103 104 105 |
# File 'lib/doh/rails/login.rb', line 103 def logged_in? authenticated? end |
#logged_out ⇒ Object
131 132 133 |
# File 'lib/doh/rails/login.rb', line 131 def logged_out flash[:notice] = 'You are now logged out' end |
#login ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/doh/rails/login.rb', line 107 def login login_page_hit if request.post? login_params = params[login_model] session[:dohrails_login_username] = login_params[username_fieldname] session[:dohrails_login_key] = get_session_key(login_params[username_fieldname], login_params[password_fieldname]) if authenticated? login_succeeded if session[:dohrails_original_uri] dohlog.debug("redirecting to original uri: #{session[:dohrails_original_uri].inspect}") redirect_to session[:dohrails_original_uri] session[:dohrails_original_uri] = nil else dohlog.debug("no original uri, redirecting to index") redirect_to :controller => login_controller, :action => index_page end else dohlog.debug("login_helper called, not authenticated -- redirecting to login page") login_failed redirect_to :controller => login_controller, :action => login_page end end end |
#login_failed ⇒ Object
49 50 51 |
# File 'lib/doh/rails/login.rb', line 49 def login_failed flash[:notice] = 'Please enter a valid username / password' end |
#login_page_hit ⇒ Object
46 47 |
# File 'lib/doh/rails/login.rb', line 46 def login_page_hit end |
#login_redirect ⇒ Object
56 57 58 |
# File 'lib/doh/rails/login.rb', line 56 def login_redirect flash[:notice] = 'Please log in' end |
#login_required ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/doh/rails/login.rb', line 60 def login_required if !authenticated? dohlog.debug "not authenticated, redirecting to #{login_page}" session[:dohrails_original_uri] = request.request_uri dohlog.debug("storing original uri #{request.request_uri.inspect}") login_redirect redirect_to :controller => login_controller, :action => login_page return false end end |
#login_succeeded ⇒ Object
53 54 |
# File 'lib/doh/rails/login.rb', line 53 def login_succeeded end |
#logout ⇒ Object
135 136 137 138 139 140 |
# File 'lib/doh/rails/login.rb', line 135 def logout session[:dohrails_login_username] = '' session[:dohrails_login_key] = '' logged_out redirect_to :controller => login_controller, :action => login_page end |