--- loncom/auth/lonauth.pm 2006/10/10 21:57:12 1.85 +++ loncom/auth/lonauth.pm 2008/03/24 05:23:10 1.90 @@ -1,7 +1,7 @@ # The LearningOnline Network # User Authentication Module # -# $Id: lonauth.pm,v 1.85 2006/10/10 21:57:12 albertel Exp $ +# $Id: lonauth.pm,v 1.90 2008/03/24 05:23:10 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -32,12 +32,12 @@ use strict; use LONCAPA; use Apache::Constants qw(:common); use CGI qw(:standard); -use CGI::Cookie(); use DynaLoader; # for Crypt::DES version use Crypt::DES; use Apache::loncommon(); use Apache::lonnet; use Apache::lonmenu(); +use Apache::createaccount; use Fcntl qw(:flock); use Apache::lonlocal; @@ -50,7 +50,7 @@ sub success { my $cookie = &Apache::loncommon::init_user_environment($r, $username, $domain, $authhost, $form, - $extra_env); + {'extra_env' => $extra_env,}); my $public=($username eq 'public' && $domain eq 'public'); @@ -122,6 +122,7 @@ sub failed { {'no_inline_link' => 1,}); my $end_page = &Apache::loncommon::end_page(); + $message = &mt($message); my %lt=('sorry' => &mt('Sorry ...'), 'please' => &mt('Please [_1]log in again[_2].', @@ -169,22 +170,16 @@ sub handler { &Apache::lonlocal::get_language_handle($r); # -------------------------------- Prevent users from attempting to login twice - my %cookies=CGI::Cookie->parse($r->header_in('Cookie')); - my $lonid=$cookies{'lonID'}; - my $cookie; - if ($lonid) { - my $handle=$lonid->value; - $handle=~s/\W//g; - my $lonidsdir=$r->dir_config('lonIDsDir'); - if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) { + my $handle = &Apache::lonnet::check_for_valid_session($r); + if ($handle ne '') { # Indeed, a valid token is found - &Apache::loncommon::content_type($r,'text/html'); - $r->send_http_header; - my $start_page = - &Apache::loncommon::start_page('Already logged in'); - my $end_page = - &Apache::loncommon::end_page(); - $r->print(<send_http_header; + my $start_page = + &Apache::loncommon::start_page('Already logged in'); + my $end_page = + &Apache::loncommon::end_page(); + $r->print(<You are already logged in

Please either continue the current session or @@ -193,8 +188,7 @@ $start_page Problems?

$end_page ENDFAILED - return OK; - } + return OK; } # ---------------------------------------------------- No valid token, continue @@ -221,9 +215,9 @@ ENDFAILED # split user logging in and "su"-user ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'}); - $form{'uname'} =~ s/\W//g; - $form{'suname'} =~ s/\W//g; - $form{'udom'} =~ s/\W//g; + $form{'uname'} = &LONCAPA::clean_username($form{'uname'}); + $form{'suname'}= &LONCAPA::clean_username($form{'suname'}); + $form{'udom'} = &LONCAPA::clean_domain( $form{'udom'}); my $role = $r->dir_config('lonRole'); my $domain = $r->dir_config('lonDefDomain'); @@ -270,9 +264,21 @@ ENDFAILED } # ---------------------------------------------------------------- Authenticate - my $authhost=Apache::lonnet::authenticate($form{'uname'}, - $upass, - $form{'udom'}); + my $cancreate; + my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'}); + if (ref($domconfig{'usercreation'}) eq 'HASH') { + if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') { + if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') { + $cancreate = $domconfig{'usercreation'}{'cancreate'}{'selfcreate'}; + } + } + } + my $defaultauth; + if ($cancreate eq 'any' || $cancreate eq 'login') { + $defaultauth = 1; + } + my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass, + $form{'udom'},$defaultauth); # --------------------------------------------------------------------- Failed? @@ -280,6 +286,34 @@ ENDFAILED &failed($r,'Username and/or password could not be authenticated.', \%form); return OK; + } elsif ($authhost eq 'no_account_on_host') { + my $cancreate; + my %domconfig = + &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'}); + if (ref($domconfig{'usercreation'}) eq 'HASH') { + if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') { + if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') { + $cancreate = $domconfig{'usercreation'}{'cancreate'}{'selfcreate'}; + } + } + } + if ($cancreate eq 'any' || $cancreate eq 'login') { + my $start_page = + &Apache::loncommon::start_page('Create a user account in LON-CAPA', + '',{'no_inline_link' => 1,}); + my $domdesc = &Apache::lonnet::domain($form{'udom'},'description'); + my $output = &Apache::createaccount::username_check($form{'uname'}, + $form{'udom'},$domdesc); + &Apache::loncommon::content_type($r,'text/html'); + $r->send_http_header; + &Apache::createaccount::print_header($r,$start_page); + $r->print($output); + $r->print(&Apache::loncommon::end_page()); + return OK; + } else { + &failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form); + return OK; + } } if (($firsturl eq '') ||