--- loncom/interface/createaccount.pm 2014/02/12 20:47:40 1.58 +++ loncom/interface/createaccount.pm 2014/05/05 21:28:10 1.65 @@ -4,7 +4,7 @@ # kerberos, or SSO) or an e-mail address. Requests to use an e-mail address as # username may be processed automatically, or may be queued for approval. # -# $Id: createaccount.pm,v 1.58 2014/02/12 20:47:40 raeburn Exp $ +# $Id: createaccount.pm,v 1.65 2014/05/05 21:28:10 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -59,7 +59,7 @@ sub handler { my $sso_domain = $r->subprocess_env->get('SSOUserDomain'); &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, - ['token','courseid','domain']); + ['token','courseid','domain','type']); &Apache::lonacc::get_posted_cgi($r); &Apache::lonlocal::get_language_handle($r); @@ -170,10 +170,38 @@ sub handler { if (grep(/^sso$/,@{$cancreate})) { $msg = '

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

'. &mt("Although your username and password were authenticated by your institution's Single Sign On system, you do not currently have a LON-CAPA account at this institution.").'
'; - + my $shibenv; + if (($r->dir_config('lonOtherAuthen') eq 'yes') && + ($r->dir_config('lonOtherAuthenType') eq 'Shibboleth')) { + if (ref($domconfig{'usercreation'}) eq 'HASH') { + if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') { + if (ref($domconfig{'usercreation'}{'cancreate'}{'shibenv'}) eq 'HASH') { + my @possfields = ('firstname','middlename','lastname','generation', + 'permanentemail','id'); + my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($domain); + $shibenv= {}; + foreach my $key (keys(%{$domconfig{'usercreation'}{'cancreate'}{'shibenv'}})) { + if ($key eq 'inststatus') { + if (ref($usertypes) eq 'HASH') { + if ($domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key} ne '') { + if (exists($usertypes->{$domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key}})) { + $shibenv->{$key} = $domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key}; + } + } + } + } elsif (grep(/^\Q$key\E/,@possfields)) { + if ($domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key} ne '') { + $shibenv->{$key} = $domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key}; + } + } + } + } + } + } + } $msg .= &username_check($sso_username,$domain,$domdesc,$courseid, $lonhost,$contact_email,$contact_name, - $sso_logout,$statustocreate); + $sso_logout,$statustocreate,$shibenv); } else { $msg = '

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

'. ''.&mt("Although your username and password were authenticated by your institution's Single Sign On system, you do not currently have a LON-CAPA account at this institution, and you are not permitted to create one.").'

'.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email).'
'. @@ -187,10 +215,11 @@ sub handler { my ($output,$nostart,$noend,$redirect); my $token = $env{'form.token'}; if ($token) { + my $usertype = &get_usertype($domain); ($output,$nostart,$noend,$redirect) = &process_mailtoken($r,$token,$contact_name,$contact_email,$domain, $domdesc,$lonhost,$include,$start_page,$cancreate, - $domconfig{'usercreation'}); + $domconfig{'usercreation'},$usertype); if ($redirect) { $r->internal_redirect('/adm/switchserver'); return OK; @@ -240,10 +269,11 @@ sub handler { } } elsif ($env{'form.create_with_email'}) { &print_header($r,$start_page,$courseid); + my $usertype = &get_usertype($domain); $output = &process_email_request($env{'form.uname'},$domain,$domdesc, $contact_name,$contact_email,$cancreate, $lonhost,$domconfig{'usercreation'}, - $emailusername,$courseid); + $emailusername,$courseid,$usertype); } elsif (!$token) { &print_header($r,$start_page,$courseid); my $now=time; @@ -258,11 +288,13 @@ sub handler { $gotlondes = 1; } } - if (grep(/^email(|approval)$/,@{$cancreate})) { + if (grep(/^email$/,@{$cancreate})) { $r->print(&javascript_validmail()); } + my $usertype = &get_usertype($domain); $output = &print_username_form($r,$domain,$domdesc,$cancreate,$now,$lonhost, - $include,$courseid,$gotlondes,$emailusername); + $include,$courseid,$gotlondes,$emailusername, + $usertype); } $r->print($output); &print_footer($r); @@ -291,12 +323,28 @@ sub print_footer { &Apache::lonhtmlcommon::echo_form_input(['backto','logtoken', 'token','serverid','uname','upass','phase','create_with_email', 'code','crypt','cfirstname','clastname', + 'recaptcha_challenge_field','recaptcha_response_field', 'cmiddlename','cgeneration','cpermanentemail','cid']). ''); } $r->print(&Apache::loncommon::end_page()); } +sub get_usertype { + my ($domain) = @_; + my $usertype = 'default'; + my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($domain); + if (ref($types) eq 'ARRAY') { + push(@{$types},'default'); + my $posstype = $env{'form.type'}; + $posstype =~ s/^\s+|\s$//g; + if (grep(/^\Q$posstype\E$/,@{$types})) { + $usertype = $posstype; + } + } + return $usertype; +} + sub selfenroll_crumbs { my ($r,$courseid,$desc) = @_; &Apache::lonhtmlcommon::add_breadcrumb @@ -309,7 +357,7 @@ sub selfenroll_crumbs { } my $last_crumb; if ($desc ne '') { - $last_crumb = &mt('Self-enroll in [_1]',"$desc"); + $last_crumb = &mt("Self-enroll in [_1]","'$desc'"); } else { $last_crumb = &mt('Self-enroll'); } @@ -321,12 +369,50 @@ sub selfenroll_crumbs { } sub javascript_setforms { - my ($now,$emailusername) = @_; - my $setuserinfo; + my ($now,$emailusername,$captcha,$usertype) = @_; + my ($setuserinfo,@required,$requiredchk); if (ref($emailusername) eq 'HASH') { - foreach my $key (sort(keys(%{$emailusername}))) { - $setuserinfo .= ' server.elements.'.$key.'.value=client.elements.'.$key.'.value;'."\n"; - } + if (ref($emailusername->{$usertype}) eq 'HASH') { + foreach my $key (sort(keys(%{$emailusername->{$usertype}}))) { + if ($emailusername->{$usertype}{$key} eq 'required') { + push(@required,$key); + } + $setuserinfo .= ' server.elements.'.$key.'.value=client.elements.'.$key.'.value;'."\n"; + } + $setuserinfo .= ' server.elements.type.value=client.elements.type.value;'."\n"; + } + if ($captcha eq 'original') { + $setuserinfo .= ' server.elements.code.value=client.elements.code.value;'."\n". + ' server.elements.crypt.value=client.elements.crypt.value;'."\n"; + } elsif ($captcha eq 'recaptcha') { + $setuserinfo .= + ' server.elements.recaptcha_challenge_field.value=client.elements.recaptcha_challenge_field.value;'."\n". + ' server.elements.recaptcha_response_field.value=client.elements.recaptcha_response_field.value;'."\n"; + } + } + if (@required) { + my $missprompt = &mt('One or more required fields are currently blank.'); + my $reqstr = join("','",@required); + $requiredchk = <<"ENDCHK"; + var requiredfields = new Array('$reqstr'); + missing = 0; + for (var i=0; i 0) { + alert("$missprompt"); + return false; + } + +ENDCHK } my $js = < @@ -338,13 +424,9 @@ sub javascript_setforms { server = document.forms[one]; if (document.forms[two]) { client = document.forms[two]; +$requiredchk server.elements.uname.value = client.elements.uname.value; server.elements.udom.value = client.elements.udom.value; - if (context == 'email') { - $setuserinfo - } - server.elements.code.value=client.elements.code.value; - server.elements.crypt.value=client.elements.crypt.value; uextkey=client.elements.uextkey.value; lextkey=client.elements.lextkey.value; @@ -355,7 +437,10 @@ sub javascript_setforms { client.elements.uname.value=''; client.elements.upass$now.value=''; - client.elements.upasscheck$now.value=''; + if (context == 'email') { +$setuserinfo + client.elements.upasscheck$now.value=''; + } server.submit(); } } @@ -427,30 +512,37 @@ ENDSCRIPT } sub print_username_form { - my ($r,$domain,$domdesc,$cancreate,$now,$lonhost,$include,$courseid,$gotlondes,$emailusername) = @_; - my %lt = &Apache::lonlocal::texthash( + my ($r,$domain,$domdesc,$cancreate,$now,$lonhost,$include,$courseid,$gotlondes,$emailusername, + $usertype) = @_; + my %lt = &Apache::lonlocal::texthash ( unam => 'username', udom => 'domain', uemail => 'E-mail address in LON-CAPA', - proc => 'Proceed'); + proc => 'Proceed', + crac => 'Create account with a username provided by this institution', + clca => 'Create LON-CAPA account', + type => 'Type in your log-in ID and password to find out.', + plse => 'Please provide a password for your new account.', + info => 'Please provide user information and a password for your new account.', + yopw => 'Your password will be encrypted when sent (and stored).', + ); my $output; if (ref($cancreate) eq 'ARRAY') { if (grep(/^login$/,@{$cancreate})) { my %domdefaults = &Apache::lonnet::get_domain_defaults($domain); if ((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) { - $output = '

'.&mt('Create account with a username provided by this institution').'

'; - my $submit_text = &mt('Create LON-CAPA account'); - $output .= &mt('If you already have a log-in ID at this institution,[_1] you may be able to use it for LON-CAPA.','
'). + $output = '

'.$lt{'crac'}.'

'; + $output .= &mt('If you already have a log-in ID at this institution [_1]you may be able to use it for LON-CAPA.','
'). '

'. - &mt('Type in your log-in ID and password to find out.'). + $lt{'type'}. '

'; - $output .= &login_box($now,$lonhost,$courseid,$submit_text, + $output .= &login_box($now,$lonhost,$courseid,$lt{'clca'}, $domain,'createaccount').'
'; } } - if (grep(/^email(|approval)$/,@{$cancreate})) { + if (grep(/^email$/,@{$cancreate})) { $output .= '

'.&mt('Create account with an e-mail address as your username').'

'; - my ($captchaform,$error) = &Apache::loncommon::captcha_display('usercreation',$lonhost); + my ($captchaform,$error,$captcha) = &Apache::loncommon::captcha_display('usercreation',$lonhost); if ($error) { my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount'; if ($courseid ne '') { @@ -463,23 +555,24 @@ sub print_username_form { '','',''); } else { if (grep(/^login$/,@{$cancreate})) { - $output .= &mt('Provide your e-mail address to request a LON-CAPA account,[_1] if you do not have a log-in ID at your institution.','
').'

'; + $output .= &mt('If you do not have a log-in ID at your institution, [_1]provide your e-mail address to request a LON-CAPA account.','
').'

'. + $lt{'plse'}.'
'. + $lt{'yopw'}.'
'; } else { - $output .= '
'; - } - $output .= &mt('Please provide user information and a password for your new account.').'
'. - &mt('Your password, which must contain at least seven characters, will be sent to the LON-CAPA server in an encrypted form.').'
'; - if (grep(/^login$/,@{$cancreate})) { - $output .= &mt('Provide your e-mail address to request a LON-CAPA account,[_1] if you do not have a log-in ID at your institution.','
').'

'; - } else { - $output .= '
'; - } - $output .= &print_dataentry_form($r,$domain,$lonhost,$include,$now,$captchaform,$courseid,$gotlondes,$emailusername); - if (grep(/^login$/,@{$cancreate})) { - $output .= &mt('Provide your e-mail address to request a LON-CAPA account,[_1] if you do not have a log-in ID at your institution.','
').'

'; - } else { - $output .= '
'; + my $prompt = $lt{'plse'}; + if (ref($emailusername) eq 'HASH') { + if (ref($emailusername->{$usertype}) eq 'HASH') { + if (keys(%{$emailusername->{$usertype}}) > 0) { + $prompt = $lt{'info'}; + } + } + } + $output .= $prompt.'
'. + $lt{'yopw'}.'
'; } + $output .= &print_dataentry_form($r,$domain,$lonhost,$include,$now,$captchaform, + $courseid,$gotlondes,$emailusername,$captcha, + $usertype); } $output .= '
'; } @@ -504,8 +597,8 @@ sub login_box { my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount:createaccount', $lonhost); $output = &serverform($logtoken,$lonhost,undef,$courseid,$context); - my $unameform = ''; - my $upassform = ''; + my $unameform = ''; + my $upassform = ''; $output .= '
'."\n". &Apache::lonhtmlcommon::start_pick_box()."\n". &Apache::lonhtmlcommon::row_title($titles{$context}, @@ -545,10 +638,10 @@ sub login_box { sub process_email_request { my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate, - $server,$settings,$emailusername,$courseid) = @_; + $server,$settings,$emailusername,$courseid,$usertype) = @_; my $output; if (ref($cancreate) eq 'ARRAY') { - if (!grep(/^email(|approval)$/,@{$cancreate})) { + if (!grep(/^email$/,@{$cancreate})) { $output = &invalid_state('noemails',$domdesc, $contact_name,$contact_email); return $output; @@ -599,7 +692,7 @@ sub process_email_request { } } $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name, - $contact_email,$courseid,$emailusername); + $contact_email,$courseid,$emailusername,$usertype); } return $output; } @@ -619,7 +712,8 @@ sub call_rulecheck { } sub send_token { - my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid,$emailusername) = @_; + my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid,$emailusername, + $usertype) = @_; my $msg = '

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

'. &mt('Thank you for your request to create a new LON-CAPA account.'). '

'; @@ -647,9 +741,14 @@ sub send_token { 'tmpinfo' => $tmpinfo); if (ref($emailusername) eq 'HASH') { - foreach my $item (keys(%{$emailusername})) { - $info{$item} = $env{'form.'.$item}; - $info{$item} =~ s/(`)//g; + if (ref($emailusername->{$usertype}) eq 'HASH') { + foreach my $item (keys(%{$emailusername->{$usertype}})) { + $info{$item} = $env{'form.'.$item}; + $info{$item} =~ s/(`)//g; + } + unless ($usertype eq 'default') { + $info{'inststatus'} = $usertype; + } } } my $token = &Apache::lonnet::tmpput(\%info,$server,'createaccount'); @@ -685,7 +784,7 @@ sub send_token { sub process_mailtoken { my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost, - $include,$start_page,$cancreate,$settings) = @_; + $include,$start_page,$cancreate,$settings,$usertype) = @_; my ($msg,$nostart,$noend,$redirect); my %data = &Apache::lonnet::tmpget($token); my $now = time; @@ -702,36 +801,46 @@ sub process_mailtoken { if ($now - $data{'time'} < 7200) { # Check if request should be queued. if (ref($cancreate) eq 'ARRAY') { + my $disposition; if (grep(/^email$/,@{$cancreate})) { - my ($result,$output,$uhome) = - &create_account($r,$domain,$domdesc,\%data); - if ($result eq 'ok') { - $msg = $output; - my $shownow = &Apache::lonlocal::locallocaltime($now); - my $mailmsg = &mt('A LON-CAPA account for the institution: [_1] has been created [_2] from IP address: [_3]. If you did not perform this action or authorize it, please contact the [_4] ([_5]).',$domdesc,$shownow,$ENV{'REMOTE_ADDR'},$contact_name,$contact_email)."\n"; - my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'}, - $mailmsg,$contact_name, - $contact_email); - if ($mailresult eq 'ok') { - $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'}); - } else { - $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'}); + if (ref($settings) eq 'HASH') { + if (ref($settings->{'cancreate'}) eq 'HASH') { + if (ref($settings->{'cancreate'}{'selfcreateprocessing'}) eq 'HASH') { + $disposition = $settings->{'cancreate'}{'selfcreateprocessing'}{$usertype}; + } } - $redirect = &start_session($r,$data{'username'},$domain,$uhome, - $data{'courseid'},$token); - $nostart = 1; - $noend = 1; + } + if ($disposition eq 'approval') { + $msg = &store_request($domain,$data{'username'},'approval',\%data,$settings); + my $delete = &Apache::lonnet::tmpdel($token); } else { - $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account.') - .'
'.$output; - if (($contact_name ne '') && ($contact_email ne '')) { - $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email); + my ($result,$output,$uhome) = + &create_account($r,$domain,$domdesc,\%data); + if ($result eq 'ok') { + $msg = $output; + my $shownow = &Apache::lonlocal::locallocaltime($now); + my $mailmsg = &mt('A LON-CAPA account for the institution: [_1] has been created [_2] from IP address: [_3]. If you did not perform this action or authorize it, please contact the [_4] ([_5]).',$domdesc,$shownow,$ENV{'REMOTE_ADDR'},$contact_name,$contact_email)."\n"; + my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'}, + $mailmsg,$contact_name, + $contact_email); + if ($mailresult eq 'ok') { + $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'}); + } else { + $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'}); + } + $redirect = &start_session($r,$data{'username'},$domain,$uhome, + $data{'courseid'},$token); + $nostart = 1; + $noend = 1; + } else { + $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account.') + .'
'.$output; + if (($contact_name ne '') && ($contact_email ne '')) { + $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email); + } } + my $delete = &Apache::lonnet::tmpdel($token); } - my $delete = &Apache::lonnet::tmpdel($token); - } elsif (grep(/^emailapproval$/,@{$cancreate})) { - $msg = &store_request($domain,$data{'username'},'approval',\%data,$settings); - my $delete = &Apache::lonnet::tmpdel($token); } else { $msg = &invalid_state('noemails',$domdesc,$contact_name,$contact_email); } @@ -776,7 +885,8 @@ sub start_session { # Stores token to store DES-key and stage during creation session # sub print_dataentry_form { - my ($r,$domain,$lonhost,$include,$now,$captchaform,$courseid,$gotlondes,$emailusername) = @_; + my ($r,$domain,$lonhost,$include,$now,$captchaform,$courseid,$gotlondes,$emailusername,$captcha, + $usertype) = @_; my ($error,$output); unless ($gotlondes) { if (open(my $jsh,"<$include/londes.js")) { @@ -784,7 +894,7 @@ sub print_dataentry_form { $r->print($line); } close($jsh); - $output = &javascript_setforms($now,$emailusername)."\n"; + $output = &javascript_setforms($now,$emailusername,$captcha,$usertype)."\n"; $gotlondes = 1; } } @@ -800,18 +910,30 @@ sub print_dataentry_form { $output .= ''."\n"; } if (ref($emailusername) eq 'HASH') { - foreach my $field (sort(keys(%{$emailusername}))) { - $output .= ''."\n"; + if (ref($emailusername->{$usertype}) eq 'HASH') { + foreach my $field (sort(keys(%{$emailusername->{$usertype}}))) { + $output .= ''."\n"; + } + $output .= ''."\n"; } } + if ($captcha eq 'original') { + $output .= ' + + +'; + } elsif ($captcha eq 'recaptcha') { + $output .= ' + + +'; + } $output .= <<"ENDSERVERFORM"; - -
@@ -821,11 +943,12 @@ ENDSERVERFORM my $endclientform = ''."\n". ''."\n". ''."\n". - ''; + ''."\n". + '

'.&mt('Fields marked [_1]*[_2] are required.','','').'

'; my ($datatable,$rowcount) = &Apache::loncreateuser::personal_data_display('',$domain,'email','selfcreate', '','',$now,$captchaform, - $emailusername); + $emailusername,$usertype); if ($rowcount) { $output .= '
'.$beginclientform.$datatable.$endclientform; } else { @@ -879,6 +1002,8 @@ sub get_creation_controls { } if (ref($usercreation->{'cancreate'}{'emailusername'}) eq 'HASH') { $emailusername = $usercreation->{'cancreate'}{'emailusername'}; + } else { + $emailusername = {'lastname' => '1', 'firstname' => 1, }; } } } @@ -895,7 +1020,7 @@ sub create_account { my $output; if (ref($dataref) eq 'HASH') { my ($username,$encpass,$serverid,$courseid,$id,$firstname,$middlename,$lastname, - $generation); + $generation,$inststatus); $username = $dataref->{'username'}; $encpass = $dataref->{'upass'}; $serverid = $dataref->{'serverid'}; @@ -905,7 +1030,8 @@ sub create_account { $middlename = $dataref->{'middlename'}; $lastname = $dataref->{'lastname'}; $generation = $dataref->{'generation'}; - + $inststatus = $dataref->{'inststatus'}; + my $currhome = &Apache::lonnet::homeserver($username,$domain); unless ($currhome eq 'no_host') { $output = &mt('User account requested for username: [_1] in domain: [_2] already exists.',$username,$domain); @@ -943,6 +1069,9 @@ sub create_account { # Now that the user account exists, retrieve the homeserver, and include it in the output. my $uhome = &Apache::lonnet::homeserver($username,$domain); + unless (($inststatus eq 'default') || ($inststatus eq '')) { + &Apache::lonnet::put('environment',{inststatus => $inststatus},$domain,$username); + } $output .= '
'.&mt('Home server: [_1]',$uhome).' '. &Apache::lonnet::hostname($uhome).'

'; return ('ok',$output,$uhome); @@ -1063,7 +1192,7 @@ sub login_failure_msg { sub username_check { my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email, - $contact_name,$sso_logout,$statustocreate) = @_; + $contact_name,$sso_logout,$statustocreate,$shibenv) = @_; my (%rulematch,%inst_results,$checkfail,$rowcount,$editable,$output,$msg, %alerts,%curr_rules,%got_rules); &call_rulecheck($username,$domain,\%alerts,\%rulematch, @@ -1099,6 +1228,11 @@ sub username_check { } if (!$checkfail) { $output = '
'; + if (ref($shibenv) eq 'HASH') { + foreach my $key (keys(%{$shibenv})) { + $inst_results{$username.':'.$domain}{$key} = $shibenv->{$key}; + } + } (my $datatable,$rowcount,$editable) = &Apache::loncreateuser::personal_data_display($username,$domain,1,'selfcreate', $inst_results{$username.':'.$domain}); @@ -1507,7 +1641,7 @@ sub sso_logout_frag { sub catreturn_js { return <<"ENDSCRIPT"; ENDSCRIPT