Annotation of loncom/interface/resetpw.pm, revision 1.48

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow access to password changing via a token sent to user's e-mail. 
                      3: #
1.48    ! raeburn     4: # $Id: resetpw.pm,v 1.47 2020/02/09 04:43:20 raeburn Exp $
1.14      bisitz      5: #
1.1       raeburn     6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: #
1.21      raeburn    29: 
                     30: =pod
                     31: 
                     32: =head1 NAME
                     33: 
1.22      raeburn    34: Apache::resetpw: reset user password.
1.21      raeburn    35: 
                     36: =head1 SYNOPSIS
                     37: 
                     38: Handles resetting of forgotten passwords.
                     39: 
                     40: This is part of the LearningOnline Network with CAPA project
                     41: described at http://www.lon-capa.org.
                     42:  
                     43: =head1 OVERVIEW
                     44: 
                     45: A user with an e-mail address associated with his/her LON-CAPA username
1.22      raeburn    46: can reset a forgotten password, using a link sent to the e-mail address
1.21      raeburn    47: if the authentication type for the account is "internal".
                     48: 
                     49: =cut
                     50: 
1.1       raeburn    51: package Apache::resetpw;
                     52: 
                     53: use strict;
                     54: use Apache::Constants qw(:common);
                     55: use Apache::lonacc;
                     56: use Apache::lonnet;
                     57: use Apache::loncommon;
1.44      raeburn    58: use Apache::lonpreferences;
1.1       raeburn    59: use Apache::lonlocal;
                     60: use LONCAPA;
1.38      raeburn    61: use HTML::Entities;
1.1       raeburn    62: 
                     63: sub handler {
                     64:     my $r = shift;
                     65:     &Apache::loncommon::content_type($r,'text/html');
                     66:     $r->send_http_header;
                     67:     if ($r->header_only) {
                     68:         return OK;
                     69:     }
                     70:     my $contact_name = &mt('LON-CAPA helpdesk');
1.27      raeburn    71:     my $origmail =  $r->dir_config('lonSupportEMail');
1.1       raeburn    72:     my $server = $r->dir_config('lonHostID');
1.19      raeburn    73:     my $defdom = &Apache::lonnet::default_login_domain();
1.27      raeburn    74:     my $contacts =
                     75:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
                     76:                                                  $defdom,$origmail);
                     77:     my ($contact_email) = split(',',$contacts);
1.26      raeburn    78:     my $handle = &Apache::lonnet::check_for_valid_session($r);
                     79:     my $lonidsdir=$r->dir_config('lonIDsDir');
                     80:     if ($handle ne '') {
                     81:         if ($handle=~/^publicuser\_/) {
                     82:             unlink($r->dir_config('lonIDsDir')."/$handle.id");
                     83:         } else {
                     84:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     85:         }
                     86:     }
1.1       raeburn    87:     &Apache::lonacc::get_posted_cgi($r);
                     88:     &Apache::lonlocal::get_language_handle($r);
                     89:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
                     90:     
                     91:     my @emailtypes = ('permanentemail','critnotification','notification');
1.38      raeburn    92:     my $uname = $env{'form.uname'};
                     93:     $uname =~ s/^\s+|\s+$//g;
                     94:     $uname = &LONCAPA::clean_username($uname);
                     95:     my $udom = &LONCAPA::clean_domain($env{'form.udom'});
1.44      raeburn    96:     my ($domdesc,$otherinst,$lookup);
1.38      raeburn    97:     if ($udom) {
                     98:         $domdesc = &Apache::lonnet::domain($udom,'description');
                     99:         if ($domdesc) {
1.40      raeburn   100:             $otherinst = 1;
                    101:             my @ids=&Apache::lonnet::current_machine_ids();
1.38      raeburn   102:             my %servers = &Apache::lonnet::internet_dom_servers($udom);
1.44      raeburn   103:             foreach my $hostid (keys(%servers)) {
                    104:                 if (grep(/^\Q$hostid\E$/,@ids)) {
1.40      raeburn   105:                     $otherinst = 0;
                    106:                     last;
                    107:                 }
1.38      raeburn   108:             }
                    109:         }
                    110:     }
1.44      raeburn   111:     my $dom_in_effect = $defdom;
                    112:     if (($udom ne '') && ($domdesc ne '')) {
                    113:         unless ($otherinst) {
                    114:             $dom_in_effect = $udom;
                    115:         }
                    116:     }
                    117:     my %passwdconf = &Apache::lonnet::get_passwdconf($dom_in_effect);
1.1       raeburn   118:     my $token = $env{'form.token'};
1.44      raeburn   119:     my $useremail = $env{'form.useremail'};
                    120:     if (($udom ne '') && (!$otherinst) && (!$token)) {
                    121:         if ($uname ne '') {
                    122:             my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                    123:             if ($uhome eq 'no_host') {
                    124:                 my %srch = (srchby     => 'uname_ci',
                    125:                             srchdomain => $udom,
                    126:                             srchterm   => $uname,
                    127:                             srchtype   => 'exact');
                    128:                 my %srch_results = &Apache::lonnet::usersearch(\%srch);
                    129:                 if (keys(%srch_results) > 1) {
                    130:                     $lookup = 'nonunique';
                    131:                     if ($useremail =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                    132:                         foreach my $key (keys(%srch_results)) {
                    133:                             if (ref($srch_results{$key}) eq 'HASH') {
                    134:                                 if ($srch_results{$key}{permanentemail} =~ /^\Q$useremail\E$/i) {
                    135:                                     ($uname) = split(/:/,$key);
                    136:                                     undef($lookup);
                    137:                                     last;
                    138:                                 }
                    139:                             }
                    140:                         }
                    141:                     }
                    142:                 } elsif (keys(%srch_results) == 1) {
                    143:                     my $match = (keys(%srch_results))[0];
                    144:                     ($uname) = split(/:/,$match);
                    145:                 } else {
                    146:                     $lookup = 'nomatch';
                    147:                 }
                    148:             }
                    149:         }
                    150:         if (($lookup eq 'nomatch') || ($uname eq '')) {
                    151:             if (($useremail =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) &&
                    152:                 ($passwdconf{'resetprelink'} eq 'either')) {
                    153:                 my %srch = (srchby     => 'email',
                    154:                             srchdomain => $udom,
                    155:                             srchterm   => $useremail,
                    156:                             srchtype   => 'exact');
                    157:                 my %srch_results = &Apache::lonnet::usersearch(\%srch);
                    158:                 if (keys(%srch_results) > 1) {
                    159:                     $lookup = 'nonunique';
                    160:                 } elsif (keys(%srch_results) == 1) {
                    161:                     my $match = (keys(%srch_results))[0];
                    162:                     ($uname) = split(/:/,$match);
                    163:                     undef($lookup);
                    164:                 } else {
                    165:                     $lookup = 'nomatch';
                    166:                 }
                    167:             }
                    168:         }
                    169:     }
1.26      raeburn   170:     my $brcrum = [];
                    171:     if ($token) {
                    172:         push (@{$brcrum},
                    173:             {href => '/adm/resetpw',
                    174:              text => 'Update Password'});
                    175:     } else {
                    176:         push (@{$brcrum},
                    177:             {href => '/adm/resetpw',
                    178:              text => 'Account Information'});
                    179:         if ($uname && $udom) {
                    180:             push (@{$brcrum},
                    181:                 {href => '/adm/resetpw',
                    182:                  text => 'Result'});
                    183:         }
                    184:     }
1.33      bisitz    185:     my $args = {bread_crumbs => $brcrum};
1.38      raeburn   186:     my $js;
                    187:     unless ($token || $otherinst || ($uname && $udom)) {
                    188:         my (@intdoms,@instdoms);
                    189:         my $internet_names = &Apache::lonnet::get_internet_names($server);
                    190:         if (ref($internet_names) eq 'ARRAY') {
                    191:             @intdoms = @{$internet_names};
                    192:         }
                    193:         if (@intdoms) {
                    194:             my %iphost = &Apache::lonnet::get_iphost();
                    195:             foreach my $ip (keys(%iphost)) {
                    196:                 if (ref($iphost{$ip}) eq 'ARRAY') {
                    197:                     foreach my $id (@{$iphost{$ip}}) {
                    198:                         my $location = &Apache::lonnet::internet_dom($id);
                    199:                         if ($location) {
                    200:                             if (grep(/^\Q$location\E$/,@intdoms)) {
                    201:                                 my $dom = &Apache::lonnet::host_domain($id);
                    202:                                 unless (grep(/^\Q$dom\E/,@instdoms)) {
                    203:                                     push(@instdoms,$dom);
                    204:                                 }
                    205:                             }
                    206:                         }
                    207:                     }
                    208:                 }
                    209:             }
                    210:         }
                    211:         my $instdomstr;
                    212:         if (@instdoms > 0) {
                    213:             $instdomstr = "'".join("','",@instdoms)."'";
                    214:         }
                    215:         my %js_lt = &Apache::lonlocal::texthash(
                    216:             thdo  => 'The domain you have selected is for another institution.',
                    217:             yowi  => 'You will be switched to the Forgot Password utility at that institution.',
                    218:             unam  => 'You must enter a username.',
1.44      raeburn   219:             mail  => 'You must enter an e-mail address.',
                    220:             eith  => 'Enter a username and/or an e-mail address.',
1.38      raeburn   221:         );
                    222:         &js_escape(\%js_lt);
                    223:         $js = <<"END";
                    224: function verifyDomain(caller,form) {
                    225:     var redirect = 1; 
                    226:     var instdoms = new Array($instdomstr);
                    227:     if (instdoms.length > 0) {
                    228:         for (var i=0; i<instdoms.length; i++) {
                    229:             if (caller.options[caller.selectedIndex].value == instdoms[i]) {
                    230:                 redirect = 0;
                    231:                 break;
                    232:             }
                    233:         }
                    234:     }
1.44      raeburn   235:     if (redirect == 0) {
                    236:         if (caller.options[caller.selectedIndex].value != '$dom_in_effect') {
                    237:             document.forgotpw.uname.value = '';
                    238:             document.forgotpw.useremail.value = '';
                    239:             form.submit();
                    240:         }
                    241:     }
1.38      raeburn   242:     if (redirect == 1) {
                    243:         if (confirm('$js_lt{thdo}\\n$js_lt{yowi}')) {
                    244:             form.submit();
1.44      raeburn   245:         } else {
                    246:             for (var i=0; i<caller.options.length; i++) { 
                    247:                 if (caller.option[i].value == '$dom_in_effect') {
                    248:                     caller.selectedIndex = i;
                    249:                     break;
                    250:                 }
                    251:             }
1.38      raeburn   252:         }
                    253:     }
                    254:     return;
                    255: }
                    256: 
1.44      raeburn   257: END
                    258:         if ($passwdconf{resetprelink} eq 'either') {
                    259:             $js .= <<"END";
                    260: function validInfo() {
                    261:     if ((document.forgotpw.uname.value == '') &&
                    262:         (document.forgotpw.useremail.value == '')) {
                    263:         alert("$js_lt{'eith'}");
                    264:         return false;
                    265:     }
                    266:     return true;
                    267: }
                    268: END
                    269:         } else {
                    270:             $js .= <<"END";
                    271: 
1.38      raeburn   272: function validInfo() {
                    273:     if (document.forgotpw.uname.value == '') {
                    274:         alert("$js_lt{'unam'}");
                    275:         return false;
                    276:     }
                    277:     if (document.forgotpw.useremail.value == '') {
                    278:         alert("$js_lt{'mail'}");
                    279:         return false;
                    280:     }
                    281:     return true;
                    282: }
                    283: END
1.44      raeburn   284:         }
                    285:     }
                    286:     $js = &Apache::lonhtmlcommon::scripttag($js);
                    287:     if (($passwdconf{'captcha'} eq 'recaptcha') && ($passwdconf{'recaptchaversion'} >=2)) {
                    288:         $js.= "\n".'<script src="https://www.google.com/recaptcha/api.js"></script>'."\n";
1.38      raeburn   289:     }
                    290:     my $header = &Apache::loncommon::start_page('Reset password',$js,$args).
                    291:                  '<h2>'.&mt('Reset forgotten LON-CAPA password').'</h2>';
1.1       raeburn   292:     my $output;
                    293:     if ($token) {
1.39      raeburn   294:         $r->print($header);
1.44      raeburn   295:         &reset_passwd($r,$token,$contact_name,$contact_email,\%passwdconf);
1.39      raeburn   296:         $r->print(&Apache::loncommon::end_page());
                    297:         return OK;
1.38      raeburn   298:     } elsif ($udom) {
                    299:         if (!$domdesc) {
                    300:             $output = &invalid_state('baddomain',$domdesc,
                    301:                                      $contact_name,$contact_email); 
                    302:         } elsif ($otherinst) {
                    303:             ($header,$output) = &homeserver_redirect($uname,$udom,$domdesc,$brcrum);
1.44      raeburn   304:         } elsif (($uname) || ($useremail)) {
                    305:             my $earlyout;
                    306:             unless ($passwdconf{'captcha'} eq 'unused') {
                    307:                 my ($captcha_chk,$captcha_error) =
1.46      raeburn   308:                     &Apache::loncommon::captcha_response('passwords',$server,$dom_in_effect);
1.44      raeburn   309:                 if ($captcha_chk != 1) {
                    310:                     my $error = 'captcha'; 
                    311:                     if ($passwdconf{'captcha'} eq 'recaptcha') {
                    312:                         $error = 'recaptcha';
                    313:                     }
                    314:                     $output = &invalid_state($error,$domdesc,
1.37      raeburn   315:                                              $contact_name,$contact_email);
1.44      raeburn   316:                     $earlyout = 1;
                    317:                 }
                    318:             }
                    319:             unless ($earlyout) {
                    320:                 if ($lookup) {
                    321:                     $output = &invalid_state($lookup,$domdesc,
                    322:                                              $contact_name,$contact_email);
                    323:                     $earlyout = 1;
                    324:                 }
                    325:             }
                    326:             unless ($earlyout) {
                    327:                 my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
                    328:                 if ($authtype =~ /^internal/) {
                    329:                     my ($blocked,$blocktext) =
                    330:                         &Apache::loncommon::blocking_status('passwd',$uname,$udom);
                    331:                     if ($blocked) {
                    332:                         $output = '<p class="LC_warning">'.$blocktext.'</p>'
                    333:                                   .&display_actions($contact_email,$domdesc);
                    334:                     } elsif (($passwdconf{'resetprelink'} ne 'either') && 
                    335:                              ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) {
                    336:                         $output = &invalid_state('baduseremail',$domdesc,
                    337:                                                  $contact_name,$contact_email);
                    338:                     } else {
                    339:                         my %userinfo = 
                    340: 	                    &Apache::lonnet::get('environment',\@emailtypes,
                    341: 					         $udom,$uname);
                    342:                         my @allemails;
                    343:                         foreach my $type (@emailtypes) {
                    344:                             if (ref($passwdconf{resetemail}) eq 'ARRAY') {
                    345:                                 if ($type eq 'permanentemail') {
                    346:                                     next unless (grep(/^permanent$/,@{$passwdconf{resetemail}}));
                    347:                                 } elsif ($type eq 'critnotification') {
                    348:                                     next unless (grep(/^critical$/,@{$passwdconf{resetemail}}));
                    349:                                 } elsif ($type eq 'notification') {
                    350:                                     next unless (grep(/^notify$/,@{$passwdconf{resetemail}}));
                    351:                                 }
                    352:                             }
                    353:                             my $email = $userinfo{$type};
                    354:                             my @items;
                    355:                             if ($email =~ /,/) {
                    356:                                 @items = split(',',$userinfo{$type});
                    357:                             } else {
                    358:                                 @items = ($email);
                    359:                             }
                    360:                             foreach my $item (@items) {
                    361:                                 if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
1.46      raeburn   362:                                     unless (grep(/^\Q$item\E$/i,@allemails)) {
1.44      raeburn   363:                                         push(@allemails,$item);
                    364:                                     }
                    365:                                 }
                    366:                             }
1.38      raeburn   367:                         }
1.44      raeburn   368:                         if (@allemails > 0) {
                    369:                             my ($sendto,$warning,$timelimit);
                    370:                             my $timelimit = 2;
                    371:                             if ($passwdconf{'resetlink'} =~ /^\d+(|\.\d*)$/) {
                    372:                                 $timelimit = $passwdconf{'resetlink'};
                    373:                             }
                    374:                             if ($passwdconf{'resetprelink'} eq 'either') {
                    375:                                 if ($useremail ne '') {
                    376:                                     if (grep(/^\Q$useremail\E$/i,@allemails)) {
                    377:                                         $sendto = $useremail;
                    378:                                     } else {
                    379:                                         $warning = &mt('The e-mail address you entered did not match the expected e-mail address.');
                    380:                                     }
                    381:                                 } elsif (@allemails > 1) {
                    382:                                     $warning = &mt('More than one e-mail address is associated with your username, and one has been selected to receive the message sent by LON-CAPA.');
                    383:                                 }
                    384:                                 unless ($sendto) {
                    385:                                     $sendto = $allemails[0];
                    386:                                 }
                    387:                             } else {
                    388:                                 if (grep(/^\Q$useremail\E$/i,@allemails)) {
                    389:                                     $sendto = $useremail;
                    390:                                 } else {
                    391:                                     $output = &invalid_state('mismatch',$domdesc,
                    392:                                                              $contact_name,
                    393:                                                              $contact_email);
1.38      raeburn   394:                                 }
1.18      raeburn   395:                             }
1.44      raeburn   396:                             if ($sendto) {
                    397:                                 $output = &send_token($uname,$udom,$sendto,$server,
                    398:                                                       $domdesc,$contact_name,
                    399:                                                       $contact_email,$timelimit,$warning);
                    400:                             }
1.38      raeburn   401:                         } else {
1.44      raeburn   402:                             $output = &invalid_state('missing',$domdesc,
                    403:                                                      $contact_name,$contact_email);
1.38      raeburn   404:                         }
1.3       raeburn   405:                     }
1.44      raeburn   406:                 } elsif ($authtype =~ /^(krb|unix|local)/) {
                    407:                     $output = &invalid_state('authentication',$domdesc,
                    408:                                          $contact_name,$contact_email);
                    409:                 } else {
                    410:                     $output = &invalid_state('invalid',$domdesc,
                    411:                                              $contact_name,$contact_email);
1.1       raeburn   412:                 }
                    413:             }
                    414:         } else {
1.44      raeburn   415:             $output = &get_uname($server,$dom_in_effect,\%passwdconf);
1.1       raeburn   416:         }
                    417:     } else {
1.44      raeburn   418:         $output = &get_uname($server,$defdom,\%passwdconf);
1.1       raeburn   419:     }
1.38      raeburn   420:     $r->print($header.$output);
1.1       raeburn   421:     $r->print(&Apache::loncommon::end_page());
                    422:     return OK;
                    423: }
                    424: 
                    425: sub get_uname {
1.44      raeburn   426:     my ($server,$defdom,$passwdconf) = @_;
                    427:     return unless (ref($passwdconf) eq 'HASH');
1.1       raeburn   428:     my %lt = &Apache::lonlocal::texthash(
1.44      raeburn   429:             unam => 'LON-CAPA username',
                    430:             udom => 'LON-CAPA domain',
                    431:             uemail => 'E-mail address in LON-CAPA',
                    432:             vali   => 'Validation',
                    433:             proc => 'Proceed');
                    434:     my $msg;
                    435:     unless ($passwdconf->{'resetremove'}) {
                    436:         $msg = '<p>'.&mt('If you use the same account for other campus services besides LON-CAPA, (e.g., e-mail, course registration, etc.), a separate centrally managed mechanism likely exists to reset a password. However, if your account is used for just LON-CAPA access you will probably be able to reset a password from this page.').'</p>';
                    437:     }
                    438:     if ($passwdconf->{'resetcustom'} eq "/res/$defdom/$defdom-domainconfig/customtext/resetpw/resetpw.html") {
                    439:         my $contents = &Apache::lonnet::getfile(&Apache::lonnet::filelocation('',$passwdconf->{'resetcustom'}));
                    440:         unless ($contents eq '-1') {
                    441:             $msg .= $contents;
                    442:         }
                    443:     }
                    444:     $msg .= '<p>'.&mt('Three conditions must be met:')
1.8       bisitz    445:            .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
                    446:            .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
                    447:            .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
1.44      raeburn   448:            .'</ul></p>';
1.38      raeburn   449:     my $onchange = 'javascript:verifyDomain(this,this.form);';
                    450:     $msg .= '<form name="forgotpw" method="post" action="/adm/resetpw" onsubmit="return validInfo();">'.
1.44      raeburn   451:             &Apache::lonhtmlcommon::start_pick_box().
1.32      bisitz    452:             &Apache::lonhtmlcommon::row_title($lt{'udom'}).
1.38      raeburn   453:             &Apache::loncommon::select_dom_form($defdom,'udom',undef,undef,$onchange).
1.26      raeburn   454:             &Apache::lonhtmlcommon::row_closure(1).
1.44      raeburn   455:             &Apache::lonhtmlcommon::row_title($lt{'unam'}).
                    456:             '<input type="text" name="uname" size="20" autocapitalize="off" autocorrect="off" />'.
                    457:             &Apache::lonhtmlcommon::row_closure(1).
1.26      raeburn   458:             &Apache::lonhtmlcommon::row_title($lt{'uemail'}).
1.44      raeburn   459:             '<input type="text" name="useremail" size="30" autocapitalize="off" autocorrect="off" />'.
                    460:             &Apache::lonhtmlcommon::row_closure(1);
                    461:     unless ($passwdconf->{'captcha'} eq 'notused') {
                    462:         my ($captcha_form,$captcha_error,$captcha,$recaptcha_version) =
                    463:             &Apache::loncommon::captcha_display('passwords',$server,$defdom);
                    464:         if ($captcha_form) {
                    465:             $msg .= &Apache::lonhtmlcommon::row_title($lt{'vali'}).
                    466:                     $captcha_form."\n".
                    467:                     &Apache::lonhtmlcommon::row_closure(1);
                    468:         }
                    469:     }
                    470:     $msg .= &Apache::lonhtmlcommon::end_pick_box().
1.26      raeburn   471:             '<br /><br /><input type="submit" name="resetter" value="'.$lt{'proc'}.'" /></form>';
1.1       raeburn   472:     return $msg;
                    473: }
                    474: 
                    475: sub send_token {
                    476:     my ($uname,$udom,$email,$server,$domdesc,$contact_name,
1.44      raeburn   477:         $contact_email,$timelimit,$warning) = @_;
1.29      bisitz    478:     my $msg =
                    479:         '<p class="LC_info">'
                    480:        .&mt('Thank you for your request to reset the password for your LON-CAPA account.')
                    481:        .'</p>';
1.1       raeburn   482: 
                    483:     my $now = time;
                    484:     my $temppasswd = &create_passwd();
1.48    ! raeburn   485:     my $ip = &Apache::lonnet::get_requestor_ip(); 
        !           486:     my %info = ('ip'         => $ip,
1.2       albertel  487: 		'time'       => $now,
                    488: 		'domain'     => $udom,
                    489: 		'username'   => $uname,
                    490: 		'email'      => $email,
                    491: 		'temppasswd' => $temppasswd);
1.1       raeburn   492: 
1.3       raeburn   493:     my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
                    494:     if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2       albertel  495:         my $esc_token = &escape($token);
1.15      raeburn   496:         my $showtime = &Apache::lonlocal::locallocaltime(time);
                    497:         my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
1.26      raeburn   498:         my $mailmsg = &mt('A request was submitted on [_1] for reset of the password for your LON-CAPA account.',$showtime)." \n".&mt('To complete this process please open a web browser and enter the following URL in the address/location box: [_1]',"\n\n".$reseturl);
1.1       raeburn   499:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
                    500:                                 $contact_email);
                    501:         if ($result eq 'ok') {
1.34      bisitz    502:             $msg .=
                    503:                 &mt('An e-mail sent to the e-mail address associated with your LON-CAPA account includes the web address for the link you should use to complete the reset process.')
                    504:                .'<br /><br />'
1.44      raeburn   505:                .&mt('The link included in the message will be valid for the next [_1][quant,_2,hour][_3].','<b>',$timelimit,'</b>');
1.1       raeburn   506:         } else {
1.28      bisitz    507:             $msg .=
1.29      bisitz    508:                 '<p class="LC_error">'
                    509:                .&mt('An error occurred when sending a message to the e-mail address'
                    510:                    .' associated with your LON-CAPA account.')
                    511:                .'</p>'
                    512:                .&display_actions($contact_email,$domdesc);
1.1       raeburn   513:         }
                    514:     } else {
1.28      bisitz    515:         $msg .=
1.29      bisitz    516:             '<p class="LC_error">'
                    517:            .&mt('An error occurred creating a token required for the'
                    518:                .' password reset process.')
                    519:            .'</p>'
                    520:            .&display_actions($contact_email,$domdesc);
1.1       raeburn   521:     }
                    522:     return $msg;
                    523: }
                    524: 
                    525: sub send_mail {
                    526:     my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
                    527:     my $outcome;
                    528:     my $requestmail = "To: $email\n".
                    529:                       "From: $contact_name <$contact_email>\n".
                    530:                       "Subject: ".&mt('Your LON-CAPA account')."\n".
1.25      bisitz    531:                       "Content-type: text/plain\;charset=UTF-8\n".
1.1       raeburn   532:                       "\n\n".$mailmsg."\n\n".
                    533:                       &mt('[_1] LON-CAPA support team',$domdesc)."\n".
                    534:                       "$contact_email\n";
                    535:     if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
                    536:         print MAIL $requestmail;
                    537:         close(MAIL);
                    538:         $outcome = 'ok';
                    539:     } else {
                    540:         $outcome = 'fail';
                    541:     }
                    542:     return $outcome;
                    543: }
                    544: 
                    545: sub invalid_state {
                    546:     my ($error,$domdesc,$contact_name,$contact_email) = @_;
                    547:     my $msg;
                    548:     if ($error eq 'invalid') {
1.29      bisitz    549:         $msg =
                    550:             '<p class="LC_warning">'
                    551:             .&mt('The username you provided was not verified as a valid username'
                    552:                 .' in the LON-CAPA system for the [_1] domain.','<i>'.$domdesc.'</i>')
                    553:               .'</p>';
                    554:         $msg .= &display_actions($contact_email,$domdesc);
1.1       raeburn   555:     } else {
1.44      raeburn   556:         if ($error eq 'captcha') {
                    557:             $msg = &mt('Validation of the code you entered failed');
                    558:         } elsif ($error eq 'recaptcha') {
                    559:             $msg = &mt('Validation of human, not robot, failed'); 
                    560:         } elsif ($error eq 'nonunique') {
                    561:             $msg = &mt('More than one username was identified from the information you provided; try providing both a username and e-mail address');
                    562:         } elsif ($error eq 'nomatch') {
                    563:             $msg = &mt('A valid user could not be identified from the username and/or e-mail address you provided');
                    564:         } elsif ($error eq 'baduseremail') {
1.3       raeburn   565:             $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                    566:         } elsif ($error eq 'mismatch') {
                    567:             $msg = &mt('The e-mail address you provided does not match the address recorded in the LON-CAPA system for the username and domain you provided.');  
                    568:         } elsif ($error eq 'missing') {
1.1       raeburn   569:             $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
                    570:         } elsif ($error eq 'authentication') {
                    571:             $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
1.38      raeburn   572:         } elsif ($error eq 'baddomain') {
                    573:             $msg = &mt('The domain you provided was not verified as a valid domain in the LON-CAPA system.');
1.1       raeburn   574:         }
1.29      bisitz    575:         $msg = '<p class="LC_warning">'.$msg.'</p>'
                    576:               .&display_actions($contact_email,$domdesc);
1.1       raeburn   577:     }
                    578:     return $msg;
                    579: }
                    580: 
1.38      raeburn   581: sub homeserver_redirect {
                    582:     my ($uname,$udom,$domdesc,$brcrum) = @_;
1.42      raeburn   583:     my $uhome;
1.43      raeburn   584:     if (($uname ne '') && ($udom ne '')) {
1.42      raeburn   585:         $uhome = &Apache::lonnet::homeserver($uname,$udom);
                    586:     }
                    587:     if (($uhome eq 'no_host') || ($uhome eq '')) {
1.38      raeburn   588:         $uhome = &Apache::lonnet::domain($udom,'primary');
                    589:     }
1.42      raeburn   590:     my $hostname = &Apache::lonnet::hostname($uhome);
1.38      raeburn   591:     my $protocol = $Apache::lonnet::protocol{$uhome};
                    592:     $protocol = 'http' if ($protocol ne 'https');
1.42      raeburn   593:     my $url = $protocol.'://'.$hostname.'/adm/resetpw';
1.38      raeburn   594:     # Breadcrumbs
                    595:     my $start_page = &Apache::loncommon::start_page('Switching Server',undef,
                    596:                                                     {'redirect' => [0,$url],
                    597:                                                      'bread_crumbs' => $brcrum,});
                    598:     my $output = '<p>'.&mt('This LON-CAPA server belongs to a different domain.').' '.
                    599:                        &mt('You are being switched to your domain ([_1]), to use the "Forgot Password" tool.',$domdesc).
                    600:                  '</p>';
                    601:     return ($start_page,$output);
                    602: }
                    603: 
1.1       raeburn   604: sub reset_passwd {
1.44      raeburn   605:     my ($r,$token,$contact_name,$contact_email,$passwdconf) = @_;
                    606:     return unless (ref($passwdconf) eq 'HASH');
1.1       raeburn   607:     my %data = &Apache::lonnet::tmpget($token);
                    608:     my $now = time;
                    609:     if (keys(%data) == 0) {
1.39      raeburn   610:         $r->print(&mt('Sorry, the URL you provided to complete the reset of your password was invalid. Either the token included in the URL has been deleted or the URL you provided was invalid. Please submit a [_1]new request[_2] for a password reset, and follow the link to the new URL included in the e-mail that will be sent to you, to allow you to enter a new password.'
                    611:                   ,'<a href="/adm/resetpw">','</a>')
                    612:         );
                    613:         return;
1.1       raeburn   614:     }
                    615:     if (($data{'time'} =~ /^\d+$/) && 
                    616:         ($data{'username'} ne '') && 
                    617:         ($data{'domain'} ne '') && 
1.3       raeburn   618:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && 
1.1       raeburn   619:         ($data{'temppasswd'} =~/^\w+$/)) {
1.44      raeburn   620:         my $timelimit = 7200;
                    621:         if ($passwdconf->{resetlink} =~ /^\d+(|\.\d*)$/) {
                    622:             $timelimit = 3600 * $passwdconf->{resetlink};
                    623:         }
1.9       raeburn   624:         my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.37      raeburn   625:         my ($blocked,$blocktext) =
                    626:             &Apache::loncommon::blocking_status('passwd',$data{'username'},$data{'domain'});
                    627:         if ($blocked) {
1.39      raeburn   628:             $r->print('<p class="LC_warning">'.$blocktext.'</p>');
                    629:             return;
1.44      raeburn   630:         } elsif ($now - $data{'time'} < $timelimit) {
                    631:             my ($needscase,%formfields) = &reset_requires($data{'username'},$data{'domain'},
                    632:                                                           $passwdconf);
1.1       raeburn   633:             if ($env{'form.action'} eq 'verify_and_change_pass') {
1.44      raeburn   634:                 my $invalidinfo;
                    635:                 if ($formfields{'username'}) {
                    636:                     $env{'form.uname'} =~ s/^\s+|\s+$//g;
                    637:                     $env{'form.udom'} =~ s/^\s+|\s+$//g;
                    638:                     if ($needscase) {
                    639:                         unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'})) {
                    640:                             $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| ";
                    641:                         }
                    642:                     } else {
1.47      raeburn   643:                         if ((lc($env{'form.uname'}) eq lc($data{'username'})) && (lc($env{'form.udom'}) eq lc($data{'domain'}))) {
                    644:                             $env{'form.uname'} = $data{'username'};
                    645:                         } else {
1.44      raeburn   646:                             $invalidinfo = "||$env{'form.uname'}|| ||$env{'form.udom'}|| ";
                    647:                         }
                    648:                     }
                    649:                 } else {
                    650:                     $env{'form.uname'} = $data{'username'};
                    651:                     $env{'form.udom'} = $data{'domain'};
                    652:                 }
                    653:                 if ($formfields{'email'}) {
                    654:                     $env{'form.email'} =~ s/^\s+|\s+$//g;
                    655:                     if ($needscase) {
                    656:                         unless ($env{'form.email'} eq $data{'email'}) {
                    657:                             $invalidinfo .= "||$env{'form.email'}||";
                    658:                         }
                    659:                     } else {
                    660:                         unless (lc($env{'form.email'}) eq lc($data{'email'})) {
                    661:                             $invalidinfo = "||$env{'form.email'}||";
                    662:                         }
                    663:                     }
                    664:                 }
                    665:                 if ($invalidinfo) {
                    666:                     &Apache::lonnet::logthis("Forgot Password -- token data: ||$data{'username'}|| ||$data{'domain'}|| ||$data{'email'}|| differs from form: $invalidinfo");
1.47      raeburn   667:                     my $retry;
                    668:                     $r->print(
                    669:                               '<p class="LC_warning">'
                    670:                              .&mt('A problem occurred when attempting to reset'
                    671:                              .' the password for your account.').'</p>');
                    672:                     if (($formfields{'username'}) && ($formfields{'email'})) {
                    673:                         if ($needscase) {
                    674:                             $r->print('<p>'
                    675:                                      .&mt('Please verify you entered the correct username and e-mail address, '
                    676:                                      .'including the correct lower and/or upper case letters')
                    677:                                      .'</p>');
                    678:                         } else {
                    679:                             $r->print('<p>'
                    680:                                      .&mt('Please verify you entered the correct username and e-mail address.')
                    681:                                      .'</p>');
                    682:                         }
                    683:                         $retry = 1;
                    684:                     } elsif ($formfields{'username'}) {
                    685:                         if ($needscase) {
                    686:                             $r->print('<p>'
                    687:                                      .&mt('Please verify you entered the correct username, '
                    688:                                      .'including the correct lower and/or upper case letters')
                    689:                                      .'</p>');
                    690:                         } else {
                    691:                             $r->print('<p>'
                    692:                                      .&mt('Please verify you entered the correct username.')
                    693:                                      .'</p>');
                    694:                         }
                    695:                         $retry = 1;
                    696:                     } elsif ($formfields{'email'}) {
                    697:                         if ($needscase) {
                    698:                             $r->print('<p>'
                    699:                                      .&mt('Please verify you entered the correct e-mail address, '
                    700:                                      .'including the correct lower and/or upper case letters')
                    701:                                      .'</p>');
                    702:                         } else {
                    703:                             $r->print('<p>'
                    704:                                      .&mt('Please verify you entered the correct e-mail address.')
                    705:                                      .'</p>');
                    706:                         }
                    707:                         $retry = 1;
                    708:                     }
                    709:                     if ($retry) {
                    710:                          &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token,$timelimit,\%formfields);
                    711:                     } else {
                    712:                         $r->print(&generic_failure_msg($contact_name,$contact_email));
                    713:                     }
1.44      raeburn   714:                     unless ($formfields{'username'}) {
                    715:                         delete($env{'form.uname'});
                    716:                         delete($env{'form.udom'});
                    717:                     }
1.39      raeburn   718:                     return;
1.22      raeburn   719:                 }
1.44      raeburn   720:                 my $change_failed =
1.47      raeburn   721: 		    &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token,$timelimit,\%formfields);
1.1       raeburn   722:                 if (!$change_failed) {
                    723:                     my $delete = &Apache::lonnet::tmpdel($token);
1.9       raeburn   724:                     my $now = &Apache::lonlocal::locallocaltime(time);
1.1       raeburn   725:                     my $domdesc = 
1.6       albertel  726: 			&Apache::lonnet::domain($data{'domain'},'description');
1.48    ! raeburn   727:                     my $ip = &Apache::lonnet::get_requestor_ip();
        !           728:                     my $mailmsg = &mt('The password for your LON-CAPA account in the [_1] domain was changed [_2] from IP address: [_3].  If you did not perform this change or authorize it, please contact the [_4] ([_5]).',$domdesc,$now,$ip,$contact_name,$contact_email)."\n";
1.1       raeburn   729:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
                    730:                                             $contact_name,$contact_email);
1.33      bisitz    731:                     my $confirm_msg;
1.1       raeburn   732:                     if ($result eq 'ok') {
1.33      bisitz    733:                         $confirm_msg =
                    734:                             &Apache::lonhtmlcommon::confirm_success(
                    735:                                 &mt('An e-mail confirming setting of the password'
                    736:                                    .' for your LON-CAPA account has been sent to [_1].'
                    737:                                     ,'<span class="LC_filename">'.$data{'email'}.'</span>'));
1.1       raeburn   738:                     } else {
1.33      bisitz    739:                         $confirm_msg =
                    740:                             &Apache::lonhtmlcommon::confirm_success(
                    741:                                 &mt('An error occurred when sending e-mail to [_1]'
                    742:                                    .' confirming setting of your new password.'
                    743:                                     ,'<span class="LC_filename">'.$data{'email'}.'</span>'),1);
1.1       raeburn   744:                     }
1.39      raeburn   745:                     $r->print(
1.33      bisitz    746:                         &Apache::loncommon::confirmwrapper($confirm_msg)
                    747:                        .&Apache::lonhtmlcommon::actionbox([
1.39      raeburn   748:                             '<a href="/adm/login">'.&mt('Go to the login page').'</a>'])
                    749:                     );
1.20      raeburn   750:                 } elsif ($change_failed eq 'invalid_client') {
                    751:                     my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
                    752:                     if ($homeserver eq 'no_host') {
1.39      raeburn   753:                         $r->print(&generic_failure_msg($contact_name,$contact_email));
1.20      raeburn   754:                     } else {
1.42      raeburn   755:                         my $hostname = &Apache::lonnet::hostname($homeserver);
1.20      raeburn   756:                         my $protocol = $Apache::lonnet::protocol{$homeserver};
                    757:                         $protocol = 'http' if ($protocol ne 'https');
1.42      raeburn   758:                         my $url = $protocol.'://'.$hostname.'/adm/resetpw';
1.20      raeburn   759:                         my ($opentag,$closetag);
                    760:                         if ($url) {
                    761:                            $opentag = '<a href="'.$url.'">';
                    762:                            $closetag = '</a>';
                    763:                         }
1.39      raeburn   764:                         $r->print(
1.28      bisitz    765:                             '<p class="LC_warning">'
                    766:                            .&mt('A problem occurred when attempting to reset'
                    767:                                .' the password for your account.'
                    768:                                .' Please try again from your [_1]home server[_2].'
                    769:                                 ,$opentag,$closetag)
1.39      raeburn   770:                            .'</p>'
                    771:                         );
1.20      raeburn   772:                     }
1.45      raeburn   773:                 } elsif (($change_failed eq 'prioruse') && ($passwdconf->{'numsaved'})) {
                    774:                     my $domdesc =
                    775:                         &Apache::lonnet::domain($data{'domain'},'description');
                    776:                     $r->print(
                    777:                           '<p class="LC_warning">'
                    778:                          .&mt('Please enter a password that you have not used recently.')
                    779:                          .'</p>'
1.46      raeburn   780:                          .&display_actions($contact_email,$domdesc,$token)
1.45      raeburn   781:                     );
1.47      raeburn   782:                 } elsif (($change_failed eq 'internalerror') || ($change_failed eq 'missingtemp') ||
                    783:                          ($change_failed eq 'error')) {
1.39      raeburn   784:                     $r->print(&generic_failure_msg($contact_name,$contact_email));
1.1       raeburn   785:                 }
1.44      raeburn   786:                 unless ($formfields{'username'}) {
                    787:                     delete($env{'form.uname'});
                    788:                     delete($env{'form.udom'});
                    789:                 }
1.1       raeburn   790:             } else {
1.13      schafran  791:                 $r->print(&mt('The token included in an e-mail sent to you [_1] has been verified, so you may now proceed to reset the password for your LON-CAPA account.',$reqtime).'<br /><br />');
1.44      raeburn   792:                 if (keys(%formfields)) {
                    793:                     if (($formfields{'username'}) && ($formfields{'email'})) {
                    794:                         $r->print(&mt('Please enter the username and domain of the LON-CAPA account, and the associated e-mail address, for which you are setting a password.'));
                    795:                     } elsif ($formfields{'username'}) {
                    796:                         $r->print(&mt('Please enter the username and domain of the LON-CAPA account for which you are setting a password.'));
                    797:                     } elsif ($formfields{'email'}) {
                    798:                         $r->print(&mt('Please enter the e-mail address associated with the LON-CAPA account for which you are setting a password.'));
                    799:                     }
                    800:                     if ($needscase) {
                    801:                         $r->print(' '.&mt('User data entered must match LON-CAPA account information (including case).'));
                    802:                     }
1.47      raeburn   803:                     $r->print('<br />');
                    804:                 }
                    805:                 my ($min,$max,$minrule,$maxrule);
                    806:                 if ($passwdconf->{min}) {
                    807:                     $min = $passwdconf->{min};
                    808:                 } else {
                    809:                     $min = $Apache::lonnet::passwdmin;
                    810:                 }
                    811:                 if ($min) {
                    812:                     $minrule = &mt('Minimum password length: [_1]',$min);
                    813:                 }
                    814:                 if ($passwdconf->{max}) {
                    815:                     $max = $passwdconf->{max};
                    816:                     $maxrule = &mt('Maximum password length: [_1]',$max);
1.44      raeburn   817:                 }
                    818:                 if (ref($passwdconf->{chars}) eq 'ARRAY') {
                    819:                     my %rules;
                    820:                     map { $rules{$_} = 1; } @{$passwdconf->{chars}};
                    821:                     my %rulenames = &Apache::lonlocal::texthash(
                    822:                                                      uc => 'At least one upper case letter',
                    823:                                                      lc => 'At least one lower case letter',
                    824:                                                      num => 'At least one number',
                    825:                                                      spec => 'At least one non-alphanumeric',
                    826:                                                    );
                    827:                     $r->print(&mt('The new password must satisfy the following:').'<ul>');
                    828:                     foreach my $poss ('uc','lc','num','spec') {
                    829:                         if ($rules{$poss}) {
                    830:                             $r->print('<li>'.$rulenames{$poss}.'</li>');
                    831:                         }
                    832:                     }
1.47      raeburn   833:                     if ($min) {
                    834:                         $r->print('<li>'.$minrule.'</li>');
                    835:                     }
                    836:                     if ($max) {
                    837:                         $r->print('<li>'.$maxrule.'</li>');
                    838:                     }
1.44      raeburn   839:                     $r->print('</ul>');
                    840:                 } else {
1.47      raeburn   841:                     if ($min && $max) {
                    842:                         $r->print(&mt('The new password must satisfy the following:').'<ul>'."\n".
                    843:                                   '<li>'.$minrule.'</li>'."\n".
                    844:                                   '<li>'.$maxrule.'</li>'."\n".
                    845:                                   '</ul>'."\n");
                    846:                     } elsif ($min) {
                    847:                         $r->print($minrule.'<br />');
                    848:                     } elsif ($max) {
                    849:                         $r->print($maxrule.'<br />');
                    850:                     }
1.44      raeburn   851:                 }
                    852:                 $r->print(&mt('Your new password will be sent to the LON-CAPA server in an encrypted form.').'<br />');
                    853:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token,$timelimit,\%formfields);
1.1       raeburn   854:             }
                    855:         } else {
1.39      raeburn   856:             $r->print(
1.28      bisitz    857:                 '<p class="LC_warning">'
1.47      raeburn   858:                .&mt('Sorry, the token generated when you requested a password reset has expired.').'<br />'
                    859:                .&mt('Please submit a [_1]new request[_2], and follow the link to the web page included in the new e-mail that will be sent to you, to allow you to enter a new password.'
1.28      bisitz    860:                     ,'<a href="/adm/resetpw">','</a>')
1.39      raeburn   861:                .'</p>'
                    862:             );
1.1       raeburn   863:         }
                    864:     } else {
1.39      raeburn   865:         $r->print(
1.28      bisitz    866:             '<p class="LC_warning">'
                    867:            .&mt('Sorry, the URL generated when you requested reset of your password contained incomplete information. Please submit a [_1]new request[_2] for a password reset, and use the new URL that will be sent to your e-mail account to complete the process.'
                    868:                 ,'<a href="/adm/resetpw">','</a>')
1.39      raeburn   869:            .'</p>'
                    870:         );
1.1       raeburn   871:     }
1.39      raeburn   872:     return;
1.1       raeburn   873: }
                    874: 
1.20      raeburn   875: sub generic_failure_msg {
                    876:     my ($contact_name,$contact_email) = @_;
1.28      bisitz    877:     return
1.29      bisitz    878:         '<p class="LC_error">'
                    879:        .&mt('A problem occurred when attempting to reset the password for your account.')
                    880:        .'<br />'
1.36      raeburn   881:        .&mt('Please contact the [_1] ([_2]) for assistance.',
1.28      bisitz    882:               $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>')
                    883:        .'</p>';
1.20      raeburn   884: }
                    885: 
1.1       raeburn   886: sub create_passwd {
                    887:     my $passwd = '';
1.2       albertel  888:     my @letts = ("a".."z");
1.1       raeburn   889:     for (my $i=0; $i<8; $i++) {
1.2       albertel  890:         my $lettnum = int(rand(2));
1.1       raeburn   891:         my $item = '';
                    892:         if ($lettnum) {
1.2       albertel  893:             $item = $letts[int(rand(26))];
                    894:             my $uppercase = int(rand(2));
1.1       raeburn   895:             if ($uppercase) {
                    896:                 $item =~ tr/a-z/A-Z/;
                    897:             }
                    898:         } else {
1.2       albertel  899:             $item = int(rand(10));
1.1       raeburn   900:         }
                    901:         $passwd .= $item;
                    902:     }
                    903:     return ($passwd);
                    904: }
                    905: 
1.29      bisitz    906: sub display_actions {
1.46      raeburn   907:     my ($contact_email,$domdesc,$token) = @_;
                    908:     my $url = '/adm/resetpw';
                    909:     if ($token) {
                    910:         $url .= '?token='.&escape($token);
                    911:     }
1.29      bisitz    912:     my @msg = (&mt('[_1]Go back[_2] and try again',
1.46      raeburn   913:                    '<a href="'.$url.'">','</a>'));
1.29      bisitz    914:     my $msg2 = '';
                    915:     if ($contact_email ne '') {
1.30      raeburn   916:         my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
                    917:         push(@msg, &mt('Contact the [_1]LON-CAPA helpdesk[_2] for the institution: [_3]',
                    918:                        '<a href="/adm/helpdesk?origurl='.$escuri.'">',
                    919:                        '</a>','<i>'.$domdesc.'</i>'));
                    920:     } else {
                    921:         $msg2 =
                    922:             '<p>'
                    923:            .&mt('You may wish to send an e-mail to the'
                    924:            .' server administrator: [_1] for the [_2] domain.',
1.31      raeburn   925:                 '<i>'.$Apache::lonnet::perlvar{'AdmEMail'}.'</i>',
1.30      raeburn   926:                 '<i>'.$domdesc.'</i>')
                    927:            .'</p>';
                    928:     }
1.29      bisitz    929:     return &Apache::lonhtmlcommon::actionbox(\@msg).$msg2;
1.44      raeburn   930: }
1.29      bisitz    931: 
1.44      raeburn   932: sub reset_requires {
                    933:     my ($username,$domain,$passwdconf) = @_;
                    934:     my (%fields,$needscase);
                    935:     return ($needscase,%fields) unless (ref($passwdconf) eq 'HASH');
                    936:     my (%postlink,%resetcase);
                    937:     if (ref($passwdconf->{resetpostlink}) eq 'HASH') {
                    938:         %postlink = %{$passwdconf->{resetpostlink}};
                    939:     }
                    940:     if (ref($passwdconf->{resetcase}) eq 'ARRAY') {
                    941:         map { $resetcase{$_} = 1; } (@{$passwdconf->{resetcase}});
                    942:     } else {
                    943:         $needscase = 1;
                    944:     }
                    945:     my %userenv =
                    946:         &Apache::lonnet::get('environment',['inststatus'],$domain,$username);
                    947:     my @inststatuses;
                    948:     if ($userenv{'inststatus'} ne '') {
                    949:         @inststatuses = split(/:/,$userenv{'inststatus'});
                    950:     } else {
                    951:         @inststatuses = ('default');
                    952:     }
                    953:     foreach my $status (@inststatuses) {
                    954:         if (ref($postlink{$status}) eq 'ARRAY') {
                    955:             map { $fields{$_} = 1; } (@{$postlink{$status}});
                    956:         } else {
                    957:             map { $fields{$_} = 1; } ('username','email');
                    958:         }
                    959:         if ($resetcase{$status}) {
                    960:             $needscase = 1;
                    961:         }
                    962:     }
                    963:     return ($needscase,%fields);
1.29      bisitz    964: }
                    965: 
1.1       raeburn   966: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>