--- loncom/interface/createaccount.pm 2008/02/24 23:23:39 1.1 +++ loncom/interface/createaccount.pm 2008/03/08 04:17:11 1.3 @@ -3,7 +3,7 @@ # institutional log-in ID (institutional authentication required - localauth # or kerberos) or an e-mail address. # -# $Id: createaccount.pm,v 1.1 2008/02/24 23:23:39 raeburn Exp $ +# $Id: createaccount.pm,v 1.3 2008/03/08 04:17:11 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -36,11 +36,12 @@ use Apache::lonacc; use Apache::lonnet; use Apache::loncommon; use Apache::lonlocal; +use Apache::lonauth; use Apache::resetpw; use Authen::Captcha; use DynaLoader; # for Crypt::DES version use Crypt::DES; -use LONCAPA; +use LONCAPA qw(:DEFAULT :match); sub handler { my $r = shift; @@ -49,65 +50,140 @@ sub handler { if ($r->header_only) { return OK; } + my $domain = &Apache::lonnet::default_login_domain(); my $domdesc = &Apache::lonnet::domain($domain,'description'); - my $start_page = - &Apache::loncommon::start_page('Create a user account in LON-CAPA','', - { - 'no_inline_link' => 1,}); - $r->print($start_page); - &Apache::lonhtmlcommon::clear_breadcrumbs(); - &Apache::lonhtmlcommon::add_breadcrumb - ({href=>"/adm/createuser", - text=>"New username"}); my $contact_name = &mt('LON-CAPA helpdesk'); my $contact_email = $r->dir_config('lonSupportEMail'); my $lonhost = $r->dir_config('lonHostID'); my $include = $r->dir_config('lonIncludes'); + + &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token','courseid']); &Apache::lonacc::get_posted_cgi($r); &Apache::lonlocal::get_language_handle($r); - &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']); - my $token = $env{'form.token'}; - my $output; + + my $handle = &Apache::lonnet::check_for_valid_session($r); + if ($handle ne '') { + my $start_page = + &Apache::loncommon::start_page('Already logged in'); + my $end_page = + &Apache::loncommon::end_page(); + $r->print($start_page."\n".'
'.&mt('Please either [_1]continue the current session[_2] or [_3]logout[_4].','','','',''). + '
'.$end_page); + return OK; + } + my $cancreate; my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$domain); if (ref($domconfig{'usercreation'}) eq 'HASH') { - if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') { - if ($domconfig{'usercreation'}{'cancreate'}{'selfenroll'} ne 'none') { - $cancreate = $domconfig{'usercreation'}{'cancreate'}{'selfenroll'}; + if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') { + if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') { + $cancreate = $domconfig{'usercreation'}{'cancreate'}{'selfcreate'}; } } } - $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account')); + + my $start_page = + &Apache::loncommon::start_page('Create a user account in LON-CAPA','', + { + 'no_inline_link' => 1,}); if (!$cancreate) { - $output = &mt('Creation of a new user account using an e-mail address as username or a loginID from your institution is not permitted in the domain: [_1] ([_2])',$domain,$domdesc); - } elsif ($token) { - $output = &process_mailtoken($r,$token,$contact_name,$contact_email,$domain, - $domdesc,$lonhost,$include); - } elsif ($env{'form.create_with_email'}) { + &print_header($r,$start_page); + my $output = &mt('Creation of a new user account using an e-mail address as username or a loginID from your institution is not permitted in the domain: [_1] ([_2]).',$domain,$domdesc); + $r->print($output); + $r->print(&Apache::loncommon::end_page()); + return OK; + } + + my $token = $env{'form.token'}; + my ($output,$nostart,$noend); + if ($token) { + ($output,$nostart,$noend) = + &process_mailtoken($r,$token,$contact_name,$contact_email,$domain, + $domain,$domdesc,$lonhost,$include,$start_page); + if ($nostart) { + if ($noend) { + return OK; + } else { + $r->print($output); + $r->print(&Apache::loncommon::end_page()); + return OK; + } + } else { + &print_header($r,$start_page); + $r->print($output); + $r->print(&Apache::loncommon::end_page()); + return OK; + } + } + + my $courseid; + if (defined($env{'form.courseid'})) { + $courseid = &validate_course($env{'form.courseid'}); + } + + if ($env{'form.phase'} eq 'username_activation') { + (my $result,$output,$nostart) = + &username_activation($r,$env{'form.uname'},$domain,$domdesc, + $lonhost,$courseid); + if ($result eq 'ok') { + if ($nostart) { + return OK; + } + } + &print_header($r,$start_page); + $r->print($output); + $r->print(&Apache::loncommon::end_page()); + return OK; + } + + &print_header($r,$start_page); + if ($env{'form.create_with_email'}) { $output = &process_email_request($env{'form.useremail'},$domain,$domdesc, $contact_name,$contact_email,$cancreate, - $lonhost,$domconfig{'usercreation'}); + $lonhost,$domconfig{'usercreation'}, + $courseid); } elsif ($env{'form.phase'} eq 'username_validation') { $output = &username_validation($env{'form.uname'},$domain,$domdesc, - $contact_name,$contact_email); - } elsif ($env{'form.phase'} eq 'username_activation') { - (my $result,$output) = &username_activation($env{'form.uname'}, - $domain,$domdesc); - } else { + $contact_name,$contact_email,$courseid); + } elsif (!$token) { my $now=time; if ($cancreate eq 'any' || $cancreate eq 'login') { my $jsh=Apache::File->new($include."/londes.js"); $r->print(<$jsh>); $r->print(&javascript_setforms($now)); } - $output = &print_username_form($domain,$domdesc,$cancreate,$now,$lonhost); + $output = &print_username_form($domain,$domdesc,$cancreate,$now,$lonhost, + $courseid); } $r->print($output); $r->print(&Apache::loncommon::end_page()); return OK; } +sub print_header { + my ($r,$start_page) = @_; + $r->print($start_page); + &Apache::lonhtmlcommon::clear_breadcrumbs(); + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"/adm/createuser", + text=>"New username"}); + $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account')); + return; +} + +sub validate_course { + my ($courseid) = @_; + my ($cdom,$cnum) = ($courseid =~ /^($match_domain)_($match_courseid)$/); + if (($cdom ne '') && ($cnum ne '')) { + if (&Apache::lonnet::is_course($cdom,$cnum)) { + return ($courseid); + } + } + return; +} + sub javascript_setforms { my ($now) = @_; my $js = <