2007-03-30 04:36:52 +00:00
|
|
|
class LoginController < ApplicationController
|
|
|
|
|
2007-04-16 02:19:20 +00:00
|
|
|
layout 'login'
|
2007-06-08 04:01:11 +00:00
|
|
|
filter_parameter_logging :user_password
|
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
|
|
|
|
|
|
|
protect_from_forgery :except => :check_expiry
|
|
|
|
|
2009-12-29 12:22:44 -08:00
|
|
|
if ( SITE_CONFIG['authentication_schemes'].include? 'cas')
|
|
|
|
# This will allow the user to view the index page without authentication
|
|
|
|
# but will process CAS authentication data if the user already
|
|
|
|
# has an SSO session open.
|
2010-01-12 17:30:51 -08:00
|
|
|
if defined? CASClient
|
2009-12-29 12:22:44 -08:00
|
|
|
# Only require sub-library if gem is installed and loaded
|
|
|
|
require 'casclient/frameworks/rails/filter'
|
|
|
|
before_filter CASClient::Frameworks::Rails::GatewayFilter, :only => :login_cas
|
|
|
|
|
|
|
|
# This requires the user to be authenticated for viewing all other pages.
|
|
|
|
before_filter CASClient::Frameworks::Rails::Filter, :only => [:login_cas ]
|
|
|
|
end
|
|
|
|
end
|
2009-11-22 14:39:39 -08:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def login
|
2009-11-22 14:39:39 -08:00
|
|
|
if cas_enabled?
|
|
|
|
@username = session[:cas_user]
|
|
|
|
@login_url = CASClient::Frameworks::Rails::Filter.login_url(self)
|
|
|
|
end
|
2008-12-08 00:52:57 -05:00
|
|
|
if openid_enabled? && using_open_id?
|
|
|
|
login_openid
|
2009-11-22 14:39:39 -08:00
|
|
|
elsif cas_enabled? && session[:cas_user]
|
2009-11-20 19:06:07 -08:00
|
|
|
login_cas
|
2008-12-08 00:52:57 -05:00
|
|
|
else
|
|
|
|
@page_title = "TRACKS::Login"
|
2009-12-29 12:22:44 -08:00
|
|
|
cookies[:preferred_auth] = prefered_auth? unless cookies[:preferred_auth]
|
2008-12-08 00:52:57 -05:00
|
|
|
case request.method
|
2010-11-10 23:48:56 +01:00
|
|
|
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'] }
|
2007-07-08 06:41:10 +00:00
|
|
|
end
|
2010-11-10 23:48:56 +01:00
|
|
|
redirect_back_or_home
|
|
|
|
return
|
|
|
|
else
|
|
|
|
@login = params['user_login']
|
|
|
|
notify :warning, t('login.unsuccessful')
|
|
|
|
end
|
|
|
|
when :get
|
|
|
|
if User.no_users_yet?
|
|
|
|
redirect_to signup_path
|
|
|
|
return
|
|
|
|
end
|
2008-12-08 00:52:57 -05:00
|
|
|
end
|
|
|
|
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
|
|
|
|
end
|
2008-12-08 00:52:57 -05:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def logout
|
2007-07-08 06:41:10 +00:00
|
|
|
@user.forget_me if logged_in?
|
|
|
|
cookies.delete :auth_token
|
2007-03-30 04:36:52 +00:00
|
|
|
session['user_id'] = nil
|
2009-12-29 12:22:44 -08:00
|
|
|
if ( SITE_CONFIG['authentication_schemes'].include? 'cas') && session[:cas_user]
|
2009-11-20 19:06:07 -08:00
|
|
|
CASClient::Frameworks::Rails::Filter.logout(self)
|
2009-11-24 13:09:29 -08:00
|
|
|
else
|
2009-12-29 12:22:44 -08:00
|
|
|
reset_session
|
2010-10-31 21:27:13 +08:00
|
|
|
notify :notice, t('login.logged_out')
|
2009-11-24 13:09:29 -08:00
|
|
|
redirect_to_login
|
2009-11-20 19:06:07 -08:00
|
|
|
end
|
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
|
|
|
|
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
|
|
|
|
|
2010-11-10 23:48:56 +01:00
|
|
|
def login_cas
|
2009-12-29 12:22:44 -08:00
|
|
|
# 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']
|
|
|
|
if session[:cas_user]
|
|
|
|
if @user = User.find_by_login(session[:cas_user])
|
|
|
|
session['user_id'] = @user.id
|
2010-10-31 21:27:13 +08:00
|
|
|
msg = (should_expire_sessions?) ? t('login.session_will_expire', :hours => 1) : t('login.session_will_not_expire')
|
|
|
|
notify :notice, (t('login.successful_with_session_info') + msg)
|
2009-12-29 12:22:44 -08:00
|
|
|
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'] }
|
|
|
|
end
|
|
|
|
else
|
2010-10-31 21:27:13 +08:00
|
|
|
notify :warning, t('login.cas_username_not_found', :username => session[:cas_user])
|
2009-12-29 12:22:44 -08:00
|
|
|
redirect_to signup_url ; return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
notify :warning, result.message
|
|
|
|
end
|
2010-11-10 23:48:56 +01:00
|
|
|
redirect_back_or_home
|
2009-12-29 12:22:44 -08:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2007-04-16 02:19:20 +00:00
|
|
|
def redirect_to_login
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to login_path }
|
2009-12-07 23:16:21 -05:00
|
|
|
format.m { redirect_to login_path(:format => 'm') }
|
2007-04-16 02:19:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def should_expire_sessions?
|
|
|
|
session['noexpiry'] != "on"
|
|
|
|
end
|
2008-12-08 00:52:57 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def login_openid
|
|
|
|
# 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']
|
|
|
|
authenticate_with_open_id do |result, identity_url|
|
|
|
|
if result.successful?
|
2008-12-08 18:51:33 -05:00
|
|
|
if @user = User.find_by_open_id_url(identity_url)
|
2008-12-08 00:52:57 -05:00
|
|
|
session['user_id'] = @user.id
|
2010-10-31 21:27:13 +08:00
|
|
|
msg = (should_expire_sessions?) ? t('login.session_will_expire', :hours => 1) : t('login.session_will_not_expire')
|
|
|
|
notify :notice, (t('login.successful_with_session_info') + msg)
|
2009-01-23 13:13:28 -05:00
|
|
|
cookies[:tracks_login] = { :value => @user.login, :expires => Time.now + 1.year, :secure => SITE_CONFIG['secure_cookies'] }
|
2008-12-08 00:52:57 -05:00
|
|
|
unless should_expire_sessions?
|
|
|
|
@user.remember_me
|
2009-01-23 13:13:28 -05:00
|
|
|
cookies[:auth_token] = { :value => @user.remember_token , :expires => @user.remember_token_expires_at, :secure => SITE_CONFIG['secure_cookies'] }
|
2008-12-08 00:52:57 -05:00
|
|
|
end
|
|
|
|
redirect_back_or_home
|
|
|
|
else
|
2010-10-31 21:27:13 +08:00
|
|
|
notify :warning, t('login.openid_identity_url_not_found', :identity_url => identity_url)
|
2008-12-08 00:52:57 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
notify :warning, result.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-11-20 19:06:07 -08:00
|
|
|
|
2009-12-29 12:22:44 -08:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|