2007-03-30 04:36:52 +00:00
|
|
|
class UsersController < ApplicationController
|
|
|
|
before_filter :admin_login_required, :only => [ :index, :show, :destroy ]
|
|
|
|
skip_before_filter :login_required, :only => [ :new, :create ]
|
2011-09-05 22:06:37 +02:00
|
|
|
skip_before_filter :check_for_deprecated_password_hash,
|
2011-09-05 22:45:52 +02:00
|
|
|
:only => [ :change_password, :update_password ]
|
2007-03-30 04:36:52 +00:00
|
|
|
prepend_before_filter :login_optional, :only => [ :new, :create ]
|
|
|
|
|
2009-06-30 12:08:34 +02:00
|
|
|
# GET /users GET /users.xml
|
2007-03-30 04:36:52 +00:00
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
@page_title = "TRACKS::Manage Users"
|
2008-07-14 13:10:55 -04:00
|
|
|
@users = User.paginate :page => params[:page], :order => 'login ASC'
|
2007-03-30 04:36:52 +00:00
|
|
|
@total_users = User.count
|
2009-06-30 12:08:34 +02:00
|
|
|
# When we call users/signup from the admin page we store the URL so that
|
|
|
|
# we get returned here when signup is successful
|
2007-03-30 04:36:52 +00:00
|
|
|
store_location
|
|
|
|
end
|
2011-05-03 19:14:30 +02:00
|
|
|
format.xml do
|
|
|
|
@users = User.find(:all, :order => 'login')
|
|
|
|
render :xml => @users.to_xml(:except => [ :password ])
|
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-30 12:08:34 +02:00
|
|
|
# GET /users/id GET /users/id.xml
|
2007-03-30 04:36:52 +00:00
|
|
|
def show
|
2009-06-30 12:08:34 +02:00
|
|
|
@user = User.find_by_id(params[:id])
|
2007-03-30 04:36:52 +00:00
|
|
|
render :xml => @user.to_xml(:except => [ :password ])
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /users/new
|
|
|
|
def new
|
2009-12-29 12:22:44 -08:00
|
|
|
@auth_types = []
|
|
|
|
unless session[:cas_user]
|
|
|
|
Tracks::Config.auth_schemes.each {|auth| @auth_types << [auth,auth]}
|
|
|
|
else
|
|
|
|
@auth_types << ['cas','cas']
|
|
|
|
end
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
if User.no_users_yet?
|
2011-01-16 18:14:07 +01:00
|
|
|
@page_title = t('users.new_user_title')
|
|
|
|
@heading = t('users.first_user_heading')
|
2007-03-30 04:36:52 +00:00
|
|
|
@user = get_new_user
|
2009-03-31 22:32:29 +02:00
|
|
|
elsif (@user && @user.is_admin?) || SITE_CONFIG['open_signups']
|
2011-01-16 18:14:07 +01:00
|
|
|
@page_title = t('users.new_user_title')
|
|
|
|
@heading = t('users.new_user_heading')
|
2007-03-30 04:36:52 +00:00
|
|
|
@user = get_new_user
|
|
|
|
else # all other situations (i.e. a non-admin is logged in, or no one is logged in, but we have some users)
|
2011-01-16 18:14:07 +01:00
|
|
|
@page_title = t('users.no_signups_title')
|
2007-03-30 04:36:52 +00:00
|
|
|
@admin_email = User.find_admin.preference.admin_email
|
|
|
|
render :action => "nosignup", :layout => "login"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
render :layout => "login"
|
|
|
|
end
|
|
|
|
|
2009-06-30 12:08:34 +02:00
|
|
|
# Example usage: curl -H 'Accept: application/xml' -H 'Content-Type:
|
|
|
|
# application/xml'
|
2007-03-30 04:36:52 +00:00
|
|
|
# -u admin:up2n0g00d
|
|
|
|
# -d '<request><login>username</login><password>abc123</password></request>'
|
|
|
|
# http://our.tracks.host/users
|
|
|
|
#
|
2009-06-30 12:08:34 +02:00
|
|
|
# POST /users POST /users.xml
|
2007-03-30 04:36:52 +00:00
|
|
|
def create
|
|
|
|
if params['exception']
|
|
|
|
render_failure "Expected post format is valid xml like so: <request><login>username</login><password>abc123</password></request>."
|
|
|
|
return
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2009-03-31 22:32:29 +02:00
|
|
|
unless User.no_users_yet? || (@user && @user.is_admin?) || SITE_CONFIG['open_signups']
|
2011-01-16 18:14:07 +01:00
|
|
|
@page_title = t('users.no_signups_title')
|
2007-03-30 04:36:52 +00:00
|
|
|
@admin_email = User.find_admin.preference.admin_email
|
|
|
|
render :action => "nosignup", :layout => "login"
|
|
|
|
return
|
|
|
|
end
|
2010-04-16 04:23:14 +08:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
user = User.new(params['user'])
|
2009-11-24 13:09:29 -08:00
|
|
|
|
2010-04-16 04:23:14 +08:00
|
|
|
if Tracks::Config.auth_schemes.include?('ldap') &&
|
|
|
|
user.auth_type == 'ldap' &&
|
|
|
|
!SimpleLdapAuthenticator.valid?(user.login, params['user']['password'])
|
|
|
|
notify :warning, "Incorrect password"
|
|
|
|
redirect_to :action => 'new'
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2009-11-24 13:09:29 -08:00
|
|
|
if Tracks::Config.auth_schemes.include?('cas')
|
2009-12-29 12:22:44 -08:00
|
|
|
if user.auth_type.eql? "cas"
|
|
|
|
user.crypted_password = "cas"
|
|
|
|
end
|
2009-11-24 13:09:29 -08:00
|
|
|
end
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
unless user.valid?
|
|
|
|
session['new_user'] = user
|
2010-05-03 21:26:48 +02:00
|
|
|
redirect_to signup_path
|
2007-03-30 04:36:52 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
first_user_signing_up = User.no_users_yet?
|
|
|
|
user.is_admin = true if first_user_signing_up
|
|
|
|
if user.save
|
|
|
|
@user = User.authenticate(user.login, params['user']['password'])
|
2011-01-16 18:14:07 +01:00
|
|
|
@user.create_preference({:locale => I18n.locale})
|
2007-03-30 04:36:52 +00:00
|
|
|
@user.save
|
|
|
|
session['user_id'] = @user.id if first_user_signing_up
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :notice, t('users.signup_successful', :username => @user.login)
|
2007-03-30 04:36:52 +00:00
|
|
|
redirect_back_or_home
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
format.xml do
|
|
|
|
unless User.find_by_id_and_is_admin(session['user_id'], true)
|
|
|
|
render :text => "401 Unauthorized: Only admin users are allowed access to this function.", :status => 401
|
|
|
|
return
|
|
|
|
end
|
|
|
|
unless check_create_user_params
|
|
|
|
render_failure "Expected post format is valid xml like so: <request><login>username</login><password>abc123</password></request>."
|
|
|
|
return
|
|
|
|
end
|
|
|
|
user = User.new(params[:request])
|
2009-12-29 12:22:44 -08:00
|
|
|
if Tracks::Config.auth_schemes.include?('cas') && session[:cas_user]
|
|
|
|
user.auth_type = "cas" #if they area cas user
|
2009-11-24 13:09:29 -08:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
user.password_confirmation = params[:request][:password]
|
|
|
|
if user.save
|
2011-01-16 18:14:07 +01:00
|
|
|
render :text => t('users.user_created'), :status => 200
|
2007-03-30 04:36:52 +00:00
|
|
|
else
|
|
|
|
render_failure user.errors.to_xml
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-30 12:08:34 +02:00
|
|
|
# DELETE /users/id DELETE /users/id.xml
|
2007-03-30 04:36:52 +00:00
|
|
|
def destroy
|
2009-06-30 12:08:34 +02:00
|
|
|
@deleted_user = User.find_by_id(params[:id])
|
2007-03-30 04:36:52 +00:00
|
|
|
@saved = @deleted_user.destroy
|
|
|
|
@total_users = User.find(:all).size
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
if @saved
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :notice, t('users.successfully_deleted_user', :username => @deleted_user.login), 2.0
|
2007-03-30 04:36:52 +00:00
|
|
|
else
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :error, t('users.failed_to_delete_user', :username => @deleted_user.login), 2.0
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
redirect_to users_url
|
|
|
|
end
|
|
|
|
format.js
|
|
|
|
format.xml { head :ok }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def change_password
|
2011-01-16 18:14:07 +01:00
|
|
|
@page_title = t('users.change_password_title')
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_password
|
|
|
|
@user.change_password(params[:updateuser][:password], params[:updateuser][:password_confirmation])
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :notice, t('users.password_updated')
|
2007-07-17 04:47:35 +00:00
|
|
|
redirect_to preferences_path
|
2007-03-30 04:36:52 +00:00
|
|
|
rescue Exception => error
|
|
|
|
notify :error, error.message
|
|
|
|
redirect_to :action => 'change_password'
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_auth_type
|
2011-01-16 18:14:07 +01:00
|
|
|
@page_title = t('users.change_auth_type_title')
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_auth_type
|
2008-12-08 00:52:57 -05:00
|
|
|
if (params[:open_id_complete] || (params[:user][:auth_type] == 'open_id')) && openid_enabled?
|
|
|
|
authenticate_with_open_id do |result, identity_url|
|
|
|
|
if result.successful?
|
2009-06-30 12:08:34 +02:00
|
|
|
# Success means that the transaction completed without error. If info
|
|
|
|
# is nil, it means that the user cancelled the verification.
|
2008-12-08 00:52:57 -05:00
|
|
|
@user.auth_type = 'open_id'
|
2008-12-08 18:51:33 -05:00
|
|
|
@user.open_id_url = identity_url
|
2008-12-08 00:52:57 -05:00
|
|
|
if @user.save
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :notice, t('users.openid_url_verified', :url => identity_url)
|
2008-12-08 00:52:57 -05:00
|
|
|
else
|
|
|
|
debugger
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :warning, t('users.openid_ok_pref_failed', :url => identity_url)
|
2008-12-08 00:52:57 -05:00
|
|
|
end
|
|
|
|
redirect_to preferences_path
|
2007-03-30 04:36:52 +00:00
|
|
|
else
|
2008-12-08 00:52:57 -05:00
|
|
|
notify :warning, result.message
|
2007-03-30 04:36:52 +00:00
|
|
|
redirect_to :action => 'change_auth_type'
|
2008-12-08 00:52:57 -05:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@user.auth_type = params[:user][:auth_type]
|
|
|
|
if @user.save
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :notice, t('users.auth_type_updated')
|
2007-07-17 04:47:35 +00:00
|
|
|
redirect_to preferences_path
|
2007-03-30 04:36:52 +00:00
|
|
|
else
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :warning, t('users.auth_type_update_error', :error_messages => @user.errors.full_messages.join(', '))
|
2007-03-30 04:36:52 +00:00
|
|
|
redirect_to :action => 'change_auth_type'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_token
|
2007-07-17 04:47:35 +00:00
|
|
|
@user.generate_token
|
|
|
|
@user.save!
|
2011-01-16 18:14:07 +01:00
|
|
|
notify :notice, t('users.new_token_generated')
|
2007-07-17 04:47:35 +00:00
|
|
|
redirect_to preferences_path
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_new_user
|
|
|
|
if session['new_user']
|
|
|
|
user = session['new_user']
|
|
|
|
session['new_user'] = nil
|
|
|
|
else
|
|
|
|
user = User.new
|
|
|
|
end
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_create_user_params
|
|
|
|
return false unless params.has_key?(:request)
|
|
|
|
return false unless params[:request].has_key?(:login)
|
|
|
|
return false if params[:request][:login].empty?
|
|
|
|
return false unless params[:request].has_key?(:password)
|
|
|
|
return false if params[:request][:password].empty?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2009-11-24 13:09:29 -08:00
|
|
|
end
|