--- loncom/auth/lonauth.pm 2010/03/22 20:11:08 1.104 +++ loncom/auth/lonauth.pm 2010/07/17 20:02:02 1.105 @@ -1,7 +1,7 @@ # The LearningOnline Network # User Authentication Module # -# $Id: lonauth.pm,v 1.104 2010/03/22 20:11:08 droeschl Exp $ +# $Id: lonauth.pm,v 1.105 2010/07/17 20:02:02 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -211,11 +211,15 @@ sub handler { &Apache::loncommon::start_page('Already logged in'); my $end_page = &Apache::loncommon::end_page(); + my $dest = '/adm/roles'; + if ($env{'form.firsturl'} ne '') { + $dest = $env{'form.firsturl'}; + } $r->print( $start_page .'
'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].' - ,'','','','') + ,'','','','') .'
' .$end_page ); @@ -331,8 +335,10 @@ sub handler { if (grep(/^login$/,@cancreate)) { $defaultauth = 1; } + my $clientcancheckhost = 1; my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass, - $form{'udom'},$defaultauth); + $form{'udom'},$defaultauth, + $clientcancheckhost); # --------------------------------------------------------------------- Failed? @@ -344,9 +350,10 @@ sub handler { my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'}); if (grep(/^login$/,@cancreate)) { + my $domdesc = &Apache::lonnet::domain($form{'udom'},'description'); + &check_can_host($r,\%form,'no_account_on_host',$domdesc); my $start_page = &Apache::loncommon::start_page('Create a user account in LON-CAPA'); - my $domdesc = &Apache::lonnet::domain($form{'udom'},'description'); my $lonhost = $r->dir_config('lonHostID'); my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'}; my $contacts = @@ -396,10 +403,13 @@ sub handler { } } + &check_can_host($r,\%form,$authhost); + if ($r->dir_config("lonBalancer") eq 'yes') { &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef, \%form); - $r->internal_redirect('/adm/switchserver'); + my ($otherserver) = &choose_server($form{'udom'}); + $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver); } else { &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef, \%form); @@ -407,6 +417,75 @@ sub handler { return OK; } +sub check_can_host { + my ($r,$form,$authhost,$domdesc) = @_; + return unless (ref($form) eq 'HASH'); + my $canhost = 1; + my @machinedoms = &Apache::lonnet::current_machine_domains(); + my $udom = $form->{'udom'}; + unless (grep(/^\Q$udom\E/,@machinedoms)) { + my $defdom = &Apache::lonnet::default_login_domain(); + my %defdomdefaults = &Apache::lonnet::get_domain_defaults($defdom); + my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom); + my $loncaparev; + if ($authhost eq 'no_account_on_host') { + $loncaparev = &Apache::lonnet::get_server_loncaparev($defdom); + } else { + $loncaparev = &Apache::lonnet::get_server_loncaparev($defdom,$authhost); + } + $canhost = &Apache::lonnet::can_host_session($udom,$defdom,$loncaparev,$udomdefaults{'remotesessions'},$defdomdefaults{'hostedsessions'}); + } + unless ($canhost) { + if ($authhost eq 'no_account_on_host') { + my ($login_host,$hostname) = &choose_server($udom); + &Apache::loncommon::content_type($r,'text/html'); + $r->send_http_header; + if ($login_host ne '') { + my $protocol = $Apache::lonnet::protocol{$login_host}; + $protocol = 'http' if ($protocol ne 'https'); + my $newurl = $protocol.'://'.$hostname.'/adm/createaccount'; + $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA'). + ''.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'
'. + ''.&mt('[_1]Log in[_2]','',''). + &Apache::loncommon::end_page()); + } else { + $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable'). + '
'.&mt('Currently a LON-CAPA server is not available within the [_1] domain for you to log-in to, to create an account.',$domdesc).'
'. + &Apache::loncommon::end_page()); + } + return OK; + } else { + &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef, + $form); + my ($otherserver) = &choose_server($udom); + $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver); + } + } +} + +sub choose_server { + my ($udom) = @_; + my %domconfhash = &Apache::loncommon::get_domainconf($udom); + my %servers = &Apache::lonnet::get_servers($udom); + my $lowest_load = 30000; + my ($login_host,$hostname); + foreach my $lonhost (keys(%servers)) { + my $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost}; + if ($loginvia eq '') { + ($login_host, $lowest_load) = + &Apache::lonnet::compare_server_load($lonhost, $login_host, $lowest_load); + } + } + if ($login_host ne '') { + $hostname = $servers{$login_host}; + } + return ($login_host,$hostname); +} + 1; __END__