--- loncom/auth/lonauth.pm 2006/10/10 21:57:12 1.85 +++ loncom/auth/lonauth.pm 2009/07/22 20:24:07 1.100 @@ -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.100 2009/07/22 20:24:07 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'); @@ -73,20 +73,58 @@ sub success { # ------------------------------------------------------------ Get cookie ready $cookie="lonID=$cookie; path=/"; # -------------------------------------------------------- Menu script and info + my $destination = $lowerurl; + + if (defined($form->{role})) { + my $envkey = 'user.role.'.$form->{role}; + my $now=time; + my $then=$env{'user.login.time'}; + my $refresh=$env{'user.refresh.time'}; + if (exists($env{$envkey})) { + my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus); + &Apache::lonnet::role_status($envkey,$then,$refresh,$now,\$role,\$where, + \$trolecode,\$tstatus,\$tstart,\$tend); + if ($tstatus eq 'is') { + if ($destination =~ /\?/) { + $destination .= '&'; + } else { + $destination .= '?'; + } + $destination .= 'selectrole=1&'.$form->{role}.'=1'; + if (defined($form->{symb})) { + my $destsymb = $form->{symb}; + if ($destsymb =~ /___/) { + # FIXME Need to deal with encrypted symbs and urls as needed. + my ($map,$resid,$desturl)=split(/___/,$destsymb); + unless ($desturl=~/^(adm|uploaded|editupload|public)/) { + $desturl = &Apache::lonnet::clutter($desturl); + } + $destination .= '&destinationurl='.$desturl. + '&destsymb='.$destsymb; + } else { + $destination .= '&destinationurl='.$destsymb; + } + } + } + } + } + my $windowinfo=&Apache::lonmenu::open($env{'browser.os'}); - my $startupremote=&Apache::lonmenu::startupremote($lowerurl); + my $startupremote=&Apache::lonmenu::startupremote($destination); my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl); my $setflags=&Apache::lonmenu::setflags(); my $maincall=&Apache::lonmenu::maincall(); + my $brcrum = [{'href' => '', + 'text' => 'Successful Login'},]; my $start_page=&Apache::loncommon::start_page('Successful Login', - $startupremote, - {'no_inline_link' => 1,}); + $startupremote, + {'no_inline_link' => 1, + 'bread_crumbs' => $brcrum,}); my $end_page =&Apache::loncommon::end_page(); my $continuelink; - if (($env{'browser.interface'} eq 'textual') || - ($env{'environment.remote'} eq 'off')) { - $continuelink="".&mt('Continue').""; + if ($env{'environment.remote'} eq 'off') { + $continuelink=''.&mt('Continue').''; } # ------------------------------------------------- Output for successful login @@ -96,8 +134,8 @@ sub success { my %lt=&Apache::lonlocal::texthash( 'wel' => 'Welcome', - 'mes' => 'Welcome to the LearningOnline Network with CAPA. Please wait while your session is being set up', - 'pro' => 'Problems', + 'mes' => 'Welcome to the LearningOnline Network with CAPA. Please wait while your session is being set up.', + 'pro' => 'Login problems?', 'log' => 'loginproblems.html', ); $r->print(<$lt{'wel'} -$lt{'mes'}.

-$lt{'pro'}?

+$lt{'mes'}

+$lt{'pro'}

$remoteinfo $maincall $continuelink @@ -120,29 +158,27 @@ sub failed { my ($r,$message,$form) = @_; my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef, {'no_inline_link' => 1,}); + my $retry = '/adm/login?username='.$form->{'uname'}. + '&domain='.$form->{'udom'}; + if (exists($form->{role})) { + $retry .= '&role='.$form->{role}; + } + if (exists($form->{symb})) { + $retry .= '&symb='.$form->{symb}; + } my $end_page = &Apache::loncommon::end_page(); - - my %lt=('sorry' => &mt('Sorry ...'), - 'please' => - &mt('Please [_1]log in again[_2].', - "{'uname'}&domain=$form->{'udom'}\">", - ''), - 'problemspage' => &mt('loginproblems.html'), - 'problems' => 'Problems', - ); &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; - - $r->print(<$lt{'sorry'} -

$message

-

$lt{'please'}

-

-$lt{'problems'}

-$end_page -ENDFAILED -} + $r->print( + $start_page + .'

'.&mt('Sorry ...').'

' + .'

'.&mt($message).'

' + .'

'.&mt('Please [_1]log in again[_2].','','') + .'

' + .'

'.&mt('Login problems?').'

' + .$end_page + ); + } # ------------------------------------------------------------------ Rerouting! @@ -150,8 +186,8 @@ sub reroute { my ($r) = @_; &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; - my $msg='

Sorry ...

- Please log in again.'; + my $msg='

'.&mt('Sorry ...').'

' + .&mt('Please [_1]log in again[_2].'); &Apache::loncommon::simple_error_page($r,'Rerouting',$msg); } @@ -169,32 +205,25 @@ 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(<You are already logged in -

Please either continue the current session or -logout.

-

-Problems?

-$end_page -ENDFAILED - return OK; - } + &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( + $start_page + .'

'.&mt('You are already logged in!').'

' + .'

'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].' + ,'','','','') + .'

' + .'

'.&mt('Login problems?').'

' + .$end_page + ); + return OK; } # ---------------------------------------------------- No valid token, continue @@ -221,13 +250,14 @@ 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'); my $prodir = $r->dir_config('lonUsersDir'); + my $contact_name = &mt('LON-CAPA helpdesk'); # ---------------------------------------- Get the information from login token @@ -246,7 +276,25 @@ ENDFAILED return OK; } } - my ($key,$firsturl)=split(/&/,$tmpinfo); + + if (!&Apache::lonnet::domain($form{'udom'})) { + &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form); + return OK; + } + + my ($key,$firsturl,$rolestr,$symbstr)=split(/&/,$tmpinfo); + if ($rolestr) { + $rolestr = &unescape($rolestr); + } + if ($symbstr) { + $symbstr= &unescape($symbstr); + } + if ($rolestr =~ /^role=/) { + (undef,$form{'role'}) = split('=',$rolestr); + } + if ($symbstr =~ /^symb=/) { + (undef,$form{'symb'}) = split('=',$symbstr); + } my $keybin=pack("H16",$key); @@ -270,9 +318,24 @@ 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 (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') { + @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}}; + } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') && + ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) { + @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}); + } + } + } + my $defaultauth; + if (grep(/^login$/,@cancreate)) { + $defaultauth = 1; + } + my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass, + $form{'udom'},$defaultauth); # --------------------------------------------------------------------- Failed? @@ -280,6 +343,34 @@ ENDFAILED &failed($r,'Username and/or password could not be authenticated.', \%form); return OK; + } elsif ($authhost eq 'no_account_on_host') { + my %domconfig = + &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'}); + if (grep(/^login$/,@cancreate)) { + 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 $lonhost = $r->dir_config('lonHostID'); + my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'}; + my $contacts = + &Apache::loncommon::build_recipient_list(undef,'helpdeskmail', + $form{'udom'},$origmail); + my ($contact_email) = split(',',$contacts); + my $output = &Apache::createaccount::username_check($form{'uname'}, + $form{'udom'},$domdesc,'', + $lonhost,$contact_email,$contact_name); + &Apache::loncommon::content_type($r,'text/html'); + $r->send_http_header; + &Apache::createaccount::print_header($r,$start_page); + $r->print('

'.&mt('Account creation').'

'. + &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'
'. + $output.&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 '') ||