2007-03-30 04:36:52 +00:00
|
|
|
class LoginController < ApplicationController
|
|
|
|
|
2007-04-16 02:19:20 +00:00
|
|
|
layout 'login'
|
2007-03-30 04:36:52 +00:00
|
|
|
skip_before_filter :set_session_expiration
|
|
|
|
skip_before_filter :login_required
|
2007-07-08 06:41:10 +00:00
|
|
|
before_filter :login_optional
|
2007-03-30 04:36:52 +00:00
|
|
|
before_filter :get_current_user
|
2011-02-25 22:43:18 +01:00
|
|
|
|
2011-04-14 12:52:41 +02:00
|
|
|
protect_from_forgery :except => [:check_expiry, :login]
|
2011-02-25 22:43:18 +01:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def login
|
2012-04-30 13:51:42 +02:00
|
|
|
@page_title = "TRACKS::Login"
|
|
|
|
cookies[:preferred_auth] = prefered_auth? unless cookies[:preferred_auth]
|
|
|
|
case request.method
|
|
|
|
when 'POST'
|
|
|
|
if @user = User.authenticate(params['user_login'], params['user_password'])
|
|
|
|
session['user_id'] = @user.id
|
|
|
|
# If checkbox on login page checked, we don't expire the session after 1 hour
|
|
|
|
# of inactivity and we remember this user for future browser sessions
|
|
|
|
session['noexpiry'] = params['user_noexpiry']
|
|
|
|
msg = (should_expire_sessions?) ? "will expire after 1 hour of inactivity." : "will not expire."
|
|
|
|
notify :notice, "Login successful: session #{msg}"
|
|
|
|
cookies[:tracks_login] = { :value => @user.login, :expires => Time.now + 1.year, :secure => SITE_CONFIG['secure_cookies'] }
|
|
|
|
unless should_expire_sessions?
|
|
|
|
@user.remember_me
|
|
|
|
cookies[:auth_token] = { :value => @user.remember_token , :expires => @user.remember_token_expires_at, :secure => SITE_CONFIG['secure_cookies'] }
|
2010-11-10 23:48:56 +01:00
|
|
|
end
|
2012-04-30 13:51:42 +02:00
|
|
|
redirect_back_or_home
|
|
|
|
return
|
|
|
|
else
|
|
|
|
@login = params['user_login']
|
|
|
|
notify :warning, t('login.unsuccessful')
|
2008-12-08 00:52:57 -05:00
|
|
|
end
|
2012-04-30 13:51:42 +02:00
|
|
|
when 'GET'
|
|
|
|
if User.no_users_yet?
|
|
|
|
redirect_to signup_path
|
|
|
|
return
|
2008-12-08 00:52:57 -05:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2012-04-30 13:51:42 +02:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.m { render :action => 'login_mobile.html.erb', :layout => 'mobile' }
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-12-08 00:52:57 -05:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def logout
|
2011-10-24 21:47:15 +02:00
|
|
|
logout_user
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2010-11-10 23:48:56 +01:00
|
|
|
|
|
|
|
def expire_session
|
|
|
|
# this is a hack to enable cucumber to expire a session by calling this
|
|
|
|
# method. The method will be unavailable for production environment
|
2011-05-08 15:03:55 +02:00
|
|
|
|
|
|
|
@user.forget_me if logged_in?
|
|
|
|
cookies.delete :auth_token
|
|
|
|
session['user_id'] = nil
|
|
|
|
reset_session
|
|
|
|
|
2010-11-10 23:48:56 +01:00
|
|
|
unless Rails.env.production?
|
|
|
|
session['expiry_time'] = Time.now
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :text => "Session expired for test purposes"}
|
|
|
|
format.js { render :text => "" }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :text => "Not available for production use"}
|
|
|
|
format.js { render :text => "" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
def check_expiry
|
2010-11-10 23:48:56 +01:00
|
|
|
# Gets called by periodically_call_remote to check whether
|
2007-03-30 04:36:52 +00:00
|
|
|
# the session has timed out yet
|
|
|
|
unless session == nil
|
|
|
|
if session
|
|
|
|
return unless should_expire_sessions?
|
|
|
|
# Get expiry time (allow ten seconds window for the case where we have none)
|
|
|
|
expiry_time = session['expiry_time'] || Time.now + 10
|
2010-10-05 21:27:00 +02:00
|
|
|
time_left = expiry_time - Time.now
|
|
|
|
@session_expired = ( time_left < (10*60) ) # Session will time out before the next check
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-28 05:53:24 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
2009-12-29 12:22:44 -08:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def should_expire_sessions?
|
|
|
|
session['noexpiry'] != "on"
|
|
|
|
end
|
2008-12-08 00:52:57 -05:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|