[LON-CAPA-cvs] cvs: loncom(version_2_11_X) /interface createaccount.pm

raeburn raeburn at source.lon-capa.org
Mon Jul 29 09:54:02 EDT 2019


raeburn		Mon Jul 29 13:54:02 2019 EDT

  Modified files:              (Branch: version_2_11_X)
    /loncom/interface	createaccount.pm 
  Log:
  - For 2.11
    Backport 1.73, 1.74, 1.75
  
  
-------------- next part --------------
Index: loncom/interface/createaccount.pm
diff -u loncom/interface/createaccount.pm:1.72.4.1 loncom/interface/createaccount.pm:1.72.4.2
--- loncom/interface/createaccount.pm:1.72.4.1	Tue Sep 11 18:55:57 2018
+++ loncom/interface/createaccount.pm	Mon Jul 29 13:54:02 2019
@@ -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.72.4.1 2018/09/11 18:55:57 raeburn Exp $
+# $Id: createaccount.pm,v 1.72.4.2 2019/07/29 13:54:02 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -124,7 +124,7 @@
 
         my %domconfig = 
             &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
-        my ($cancreate,$statustocreate,$emailusername) = 
+        my ($cancreate,$statustocreate) = 
             &get_creation_controls($domain,$domconfig{'usercreation'});
 
         my ($result,$output) =
@@ -146,17 +146,37 @@
             return OK;
         }
     }
-    $start_page = &Apache::loncommon::start_page($title,$js);
 
-    my %domconfig = 
+    my %domconfig =
         &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
-    my ($cancreate,$statustocreate,$emailusername) = 
+    my ($cancreate,$statustocreate,$statusforemail,$emailusername,
+        $emailoptions,$verification,$emaildomain,$types,$usertypes,$othertitle) =
         &get_creation_controls($domain,$domconfig{'usercreation'});
+    my ($additems,$pagetitle);
+    if (ref($cancreate) eq 'ARRAY') {
+        unless (($env{'form.token'}) || ($sso_username ne '') || ($env{'form.phase'}) ||
+                ($env{'form.create_with_email'})) {
+            if ((grep(/^email$/,@{$cancreate})) && (ref($statusforemail) eq 'ARRAY')) {
+                my $usertype = &get_usertype($domain);
+                if ((($usertype eq '') || (!grep(/^\Q$usertype\E$/,@{$statusforemail}))) && 
+                    (@{$statusforemail} > 0)) {
+                    $js .= &setelements_js($statusforemail,$types,$usertypes,$othertitle);
+                    $additems = {'add_entries' => { 'onload' => "setElements();"} };
+                    if ((@{$cancreate} == 1) && (@{$statusforemail} > 0)) {
+                        $pagetitle = 'Select affiliation';
+                    }
+                } else {
+                    $js .= &username_js();
+                }
+            }
+        }
+    }
+    $start_page = &Apache::loncommon::start_page($title,$js,$additems);
     if (@{$cancreate} == 0) {
-        &print_header($r,$start_page,$courseid);
+        &print_header($r,$start_page,$courseid,$pagetitle);
         my $output = '<h3>'.&mt('Account creation unavailable').'</h3>'.
                      '<span class="LC_warning">'.
-                     &mt('Creation of a new user account using an e-mail address or an institutional log-in ID as username is not permitted at this institution ([_1]).',$domdesc).
+                     &mt('Creation of a new user account using an institutional log-in ID or e-mail verification is not permitted for: [_1].',$domdesc).
                      '</span><br /><br />';
         $r->print($output);
         &print_footer($r);
@@ -178,7 +198,6 @@
                         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') {
@@ -215,11 +234,10 @@
     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'},$usertype);
+                               $domconfig{'usercreation'},$types);
         if ($redirect) {
             $r->internal_redirect('/adm/switchserver');
             return OK;
@@ -238,7 +256,35 @@
             return OK;
         }
     }
-
+    my ($usernameset,$condition,$excluded,$hascustom);
+    if ((grep(/^email$/,@{$cancreate})) && (($env{'form.create_with_email'}) ||
+                                            ((!$token) && ($env{'form.phase'} eq '')))) {
+        my $usertype = &get_usertype($domain);
+        if ($usertype eq '') {
+            $usertype = 'default';
+        }
+        if (ref($verification) eq 'HASH') {
+            if ($verification->{$usertype} =~ /^(free|first)$/) {
+                $usernameset = $verification->{$usertype};
+            }
+        }
+        if (ref($emailoptions) eq 'HASH') {
+            if ($emailoptions->{$usertype} =~ /^(inst|noninst)$/) {
+                my $chosen = $1;
+                if (ref($emaildomain) eq 'HASH') {
+                    if (ref($emaildomain->{$usertype}) eq 'HASH') {
+                        if ($chosen eq 'inst') {
+                            $condition = $emaildomain->{$usertype}->{$chosen};
+                        } else {
+                            $excluded = $emaildomain->{$usertype}->{$chosen};
+                        }
+                    }
+                }
+            } elsif ($emailoptions->{$usertype} eq 'custom') {
+                $hascustom = 1;
+            }
+        }
+    }
     if ($env{'form.phase'} eq 'username_activation') {
         (my $result,$output,$nostart) = 
             &username_activation($r,$env{'form.uname'},$domain,$domdesc,
@@ -270,12 +316,16 @@
     } elsif ($env{'form.create_with_email'}) {
         &print_header($r,$start_page,$courseid);
         my $usertype = &get_usertype($domain);
+        if ($usertype eq '') {
+            $usertype = 'default';
+        }
         $output = &process_email_request($env{'form.uname'},$domain,$domdesc,
                                          $contact_name,$contact_email,$cancreate,
                                          $lonhost,$domconfig{'usercreation'},
-                                         $emailusername,$courseid,$usertype);
+                                         $emailusername,$courseid,$usertype,
+                                         $usernameset,$condition,$excluded,$hascustom);
     } elsif (!$token) {
-        &print_header($r,$start_page,$courseid);
+        &print_header($r,$start_page,$courseid,$pagetitle);
         my $now=time;
         if ((grep(/^login$/,@{$cancreate})) && (!grep(/^email$/,@{$cancreate}))) {
             if (open(my $jsh,"<","$include/londes.js")) {
@@ -287,11 +337,13 @@
             }
         }
         if (grep(/^email$/,@{$cancreate})) {
-            $r->print(&javascript_validmail());
+            $r->print(&javascript_validmail($condition));
         }
         my $usertype = &get_usertype($domain);
         $output = &print_username_form($r,$domain,$domdesc,$cancreate,$now,$lonhost,
-                                       $include,$courseid,$emailusername,$usertype);
+                                       $include,$courseid,$emailusername,
+                                       $statusforemail,$usernameset,$condition,
+                                       $excluded,$usertype,$types,$usertypes,$othertitle);
     }
     $r->print($output);
     &print_footer($r);
@@ -299,16 +351,25 @@
 }
 
 sub print_header {
-    my ($r,$start_page,$courseid) = @_;
+    my ($r,$start_page,$courseid,$pagetitle) = @_;
     $r->print($start_page);
     &Apache::lonhtmlcommon::clear_breadcrumbs();
+    my $url = '/adm/createaccount';
+    if ($pagetitle eq '') {
+        $pagetitle = 'New username';
+    }
     if ($courseid ne '') {
         my %coursehash = &Apache::lonnet::coursedescription($courseid);
         &selfenroll_crumbs($r,$courseid,$coursehash{'description'});
     }
+    if ($env{'form.reportedtype'}) {
+        &Apache::lonhtmlcommon::add_breadcrumb
+        ({href=>$url,
+          text=>"Select affiliation"});
+    }
     &Apache::lonhtmlcommon::add_breadcrumb
-    ({href=>"/adm/createuser",
-      text=>"New username"});
+    ({href=>$url,
+      text=>$pagetitle});
     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account'));
     return;
 }
@@ -329,7 +390,7 @@
 
 sub get_usertype {
     my ($domain) = @_;
-    my $usertype = 'default';
+    my $usertype;
     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($domain);
     if (ref($types) eq 'ARRAY') {
         push(@{$types},'default');
@@ -366,7 +427,7 @@
 }
 
 sub javascript_setforms {
-    my ($now,$emailusername,$captcha,$usertype,$recaptchaversion) =  @_;
+    my ($now,$emailusername,$captcha,$usertype,$recaptchaversion,$usernameset,$condition,$excluded) =  @_;
     my ($setuserinfo, at required,$requiredchk);
     if (ref($emailusername) eq 'HASH') {
         if (ref($emailusername->{$usertype}) eq 'HASH') {  
@@ -388,6 +449,10 @@
                 '                    server.elements.recaptcha_response_field.value=client.elements.recaptcha_response_field.value;'."\n";
             }
         }
+        if ($usernameset eq 'free') {
+            $setuserinfo .=
+                '                    server.elements.username.value=client.elements.username.value;'."\n";
+        }
     }
     if (@required) {
         my $missprompt = &mt('One or more required fields are currently blank.');
@@ -431,10 +496,8 @@
                 uextkey=client.elements.uextkey.value;
                 lextkey=client.elements.lextkey.value;
                 initkeys();
-
                 server.elements.upass.value
                     = getCrypted(client.elements.upass$now.value);
-
                 client.elements.uname.value='';
                 client.elements.upass$now.value='';
                 if (context == 'email') {
@@ -496,9 +559,12 @@
 }
 
 sub javascript_validmail {
+    my ($condition) = @_;
     my %js_lt = &Apache::lonlocal::texthash (
                email => 'The e-mail address you entered',
                notv  => 'is not a valid e-mail address',
+               avae  => 'A valid e-mail address is not formed when the value you entered',
+               isco  => 'is combined with the required domain',
     );
     my $output =  "\n".'<script type="text/javascript">'."\n".
                   '// <![CDATA['."\n".
@@ -507,8 +573,13 @@
     $output .= <<"ENDSCRIPT";
 function validate_email(client) {
     field = client.uname;
-    if (validmail(field) == false) {
-        alert("$js_lt{'email'}: "+field.value+" $js_lt{'notv'}.");
+    var condition = '$condition';
+    if (validmail(field,condition) == false) {
+        if ((condition != undefined) && (condition != '')) {
+            alert("$js_lt{'avae'}: "+field.value+" $js_lt{'isco'}: "+condition);
+        } else {
+            alert("$js_lt{'email'}: "+field.value+" $js_lt{'notv'}.");
+        }
         return false;
     }
     return true;
@@ -520,18 +591,16 @@
 
 sub print_username_form {
     my ($r,$domain,$domdesc,$cancreate,$now,$lonhost,$include,$courseid,$emailusername,
-        $usertype) = @_;
+        $statusforemail,$usernameset,$condition,$excluded,$usertype,$types,$usertypes,
+        $othertitle) = @_;
     my %lt = &Apache::lonlocal::texthash (
-                                         unam => 'username',
-                                         udom => 'domain',
-                                         uemail => 'E-mail address in LON-CAPA',
-                                         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).',
+                                         crae => 'Create account using e-mail address verification',
                                          );
     my $output;
     if (ref($cancreate) eq 'ARRAY') {
@@ -548,45 +617,103 @@
             }
         }
         if (grep(/^email$/,@{$cancreate})) {
-            $output .= '<div class="LC_left_float"><h3>'.&mt('Create account with an e-mail address as your username').'</h3>';
-            my ($captchaform,$error,$captcha,$recaptchaversion) = 
-                &Apache::loncommon::captcha_display('usercreation',$lonhost);
-            if ($error) {
-                my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount';
-                if ($courseid ne '') {
-                    $helpdesk .= '&courseid='.$courseid;
-                }
-                $output .= '<span class="LC_error">'.
-                           &mt('An error occurred generating the validation code[_1] required for an e-mail address to be used as username.','<br />').
-                           '</span><br /><br />'.
-                           &mt('[_1]Contact the helpdesk[_2] or [_3]reload[_2] the page and try again.',
-                               '<a href="'.$helpdesk.'">','</a>','<a href="javascript:window.location.reload()">');
+            $output .= '<div class="LC_left_float"><h3>'.$lt{'crae'}.'</h3>';
+            if ($usertype ne '') {
+                if ((ref($statusforemail) eq 'ARRAY') && (@{$statusforemail} > 0)) {
+                    unless (grep(/^\Q$usertype\E$/,@{$statusforemail})) {
+                        undef($usertype);
+                    }
+                } elsif ($usertype ne 'default') {
+                    undef($usertype);
+                }
+            }
+            if (($usertype eq '') && (ref($statusforemail) eq 'ARRAY') && 
+                (@{$statusforemail} > 0) && (ref($types) eq 'ARRAY') && (@{$types} > 0)) {
+                my @posstypes = @{$types};
+                unless (grep(/^default$/, at posstypes)) {
+                    push(@posstypes,'default');
+                }
+                $output .= '<p>'.&mt('Choose your affiliation at [_1]',$domdesc).'</p>'."\n".
+                           '<form name="reportstatus" id="LC_reportstatus" action="/adm/createaccount" method="post" '.
+                           'onsubmit="return checkVerification();"><p>';
+                foreach my $type (@posstypes) {
+                    my $name;
+                    if ($type eq 'default') {
+                        $name = $othertitle;
+                    } else {
+                        $name = $type;
+                        if (ref($usertypes) eq 'HASH') {
+                            if (exists($usertypes->{$type})) {
+                                $name = $usertypes->{$type};
+                            }
+                        }
+                    }
+                    my $checked;
+                    if ($env{'form.type'} eq $type) {
+                        $checked = ' checked="checked"';
+                    }
+                    $output .= '<label><input type="radio" name="type" value="'.$type.'"'.$checked.' />'.
+                               $name.'</label>'.(' 'x2);
+                }
+                if ($env{'form.courseid'} =~ /^$match_domain\_$match_courseid$/) {
+                    $output .= "\n".'<input type="hidden" name="courseid" value="'.$env{'form.courseid'}.'" />';
+                }
+                $output .= '</p>'."\n".'<p><input type="submit" name="reportedtype" value="'.&mt('Submit').'" /></p></form>';
             } else {
-                if (grep(/^login$/,@{$cancreate})) {
-                    $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.','<br />').'<br /><br />'.
-                               $lt{'plse'}.'<br />'.
-                               $lt{'yopw'}.'<br />';
+                my ($captchaform,$error,$captcha,$recaptchaversion) = 
+                    &Apache::loncommon::captcha_display('usercreation',$lonhost);
+                if ($error) {
+                    my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount';
+                    if ($courseid ne '') {
+                        $helpdesk .= '&courseid='.$courseid;
+                    }
+                    $output .= '<span class="LC_error">'.
+                               &mt('An error occurred generating the validation code[_1] required for use of an e-mail address to request a LON-CAPA account.','<br />').
+                               '</span><br /><br />'.
+                               &mt('[_1]Contact the helpdesk[_2] or [_3]reload[_2] the page and try again.',
+                                   '<a href="'.$helpdesk.'">','</a>','<a href="javascript:window.location.reload()">');
                 } else {
-                    my $prompt = $lt{'plse'};
-                    if (ref($emailusername) eq 'HASH') {
-                        if (ref($emailusername->{$usertype}) eq 'HASH') {
-                            if (keys(%{$emailusername->{$usertype}}) > 0) {
-                                $prompt = $lt{'info'};
+                    if (grep(/^login$/,@{$cancreate})) {
+                        $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.','<br />').'<br /><br />'.
+                                   $lt{'plse'}.'<br />'.
+                                   $lt{'yopw'}.'<br />';
+                    } else {
+                        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.'<br />'.
+                                   $lt{'yopw'}.'<br />';
+                    }
+                    if ($usertype eq '') {
+                        $usertype = 'default';
+                    } elsif (ref($usertypes) eq 'HASH') {
+                        my $usertitle;
+                        if ($usertype eq 'default') {
+                            $usertitle = $othertitle;
+                        } elsif (exists($usertypes->{$usertype})) {
+                            $usertitle = $usertypes->{$usertype};
+                        }
+                        if ($usertitle ne '') {
+                            $output .= &mt('Self-reported affiliation: [_1]',
+                                           '<span style="font-style: italic;">'.$usertitle.'</span>').
+                                       '<br />';
+                        }
                     }
-                    $output .= $prompt.'<br />'.
-                               $lt{'yopw'}.'<br />';
+                    $output .= &print_dataentry_form($r,$domain,$lonhost,$include,$now,$captchaform,
+                                                     $courseid,$emailusername,$captcha,$usertype,
+                                                     $recaptchaversion,$usernameset,$condition,$excluded);
                 }
-                $output .= &print_dataentry_form($r,$domain,$lonhost,$include,$now,$captchaform,
-                                                 $courseid,$emailusername,$captcha,$usertype,
-                                                 $recaptchaversion);
             }
             $output .= '</div>';
         }
     }
     if ($output eq '') {
-        $output = &mt('Creation of a new LON-CAPA user account using an e-mail address or an institutional log-in ID as your username is not permitted at [_1].',$domdesc);
+        $output = &mt('Creation of a new LON-CAPA user account using an institutional log-in ID or verification by e-mail is not permitted at [_1].',$domdesc);
     } else {
         $output .= '<div class="LC_clear_float_footer"></div>';
     }
@@ -646,26 +773,61 @@
 
 sub process_email_request {
     my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate,
-        $server,$settings,$emailusername,$courseid,$usertype) = @_;
-    my $output;
+        $server,$settings,$emailusername,$courseid,$usertype,$usernameset,
+        $condition,$excluded,$hascustom) = @_;
+    my ($output,$uname);
     if (ref($cancreate) eq 'ARRAY') {
         if (!grep(/^email$/,@{$cancreate})) {
             $output = &invalid_state('noemails',$domdesc,
                                      $contact_name,$contact_email);
             return $output;
-        } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
+        } elsif ((($condition ne '') && ($useremail !~ /^[^\@]+$/)) ||
+                 (($condition eq '') && ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/))) {
             $output = &invalid_state('baduseremail',$domdesc,
                                      $contact_name,$contact_email);
             return $output;
         } else {
             $useremail =~ s/^\s+|\s+$//g;
-            my $uname=&LONCAPA::clean_username($useremail);
-            if ($useremail ne $uname) {
-                $output = &invalid_state('badusername',$domdesc,
-                                         $contact_name,$contact_email);
-                return $output;
+            my $possuname;
+            if ($condition ne '') {
+                if ($usernameset eq 'first') {
+                    $possuname = $useremail;
+                }
+                $useremail .= $condition;
+            } elsif ($excluded ne '') {
+                if ($useremail =~ /^[^\@]+\Q$excluded\E$/) {
+                    $output = &invalid_state('userrules',$domdesc,
+                                     $contact_name,$contact_email);
+                    return $output;
+                } 
+            }
+            if (($usernameset eq 'free') && ($env{'form.username'} ne '')) {
+                $possuname = $env{'form.username'};
+            } elsif (($usernameset eq 'first') && ($condition eq '')) {
+                if ($condition eq '') {
+                    ($possuname) = ($useremail =~ /^([^\@]+)\@/);
+                }
+            }
+            if ($possuname ne '') {
+                $possuname  =~ s/^\s+|\s+$//g;
+                if ($possuname ne '') {
+                    $uname=&LONCAPA::clean_username($possuname);
+                    if ($uname ne $possuname) {
+                        $output = &invalid_state('badusername',$domdesc,
+                                                 $contact_name,$contact_email);
+                        return $output;
+                    }
+                }
+            }
+            if ($possuname eq '') {
+                $uname=&LONCAPA::clean_username($useremail);
+                if ($useremail ne $uname) {
+                    $output = &invalid_state('badusername',$domdesc,
+                                             $contact_name,$contact_email);
+                    return $output;
+                }
             }
-            my $uhome = &Apache::lonnet::homeserver($useremail,$domain);
+            my $uhome = &Apache::lonnet::homeserver($uname,$domain);
             if ($uhome ne 'no_host') {
                 $output = &invalid_state('existinguser',$domdesc,
                                          $contact_name,$contact_email);
@@ -678,29 +840,32 @@
                     return $output;
                 }
                 my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
-                &call_rulecheck($useremail,$domain,\%alerts,\%rulematch,
+                &call_rulecheck($uname,$domain,\%alerts,\%rulematch,
                                 \%inst_results,\%curr_rules,\%got_rules,'username');
                 if (ref($alerts{'username'}) eq 'HASH') {
                     if (ref($alerts{'username'}{$domain}) eq 'HASH') {
-                        if ($alerts{'username'}{$domain}{$useremail}) {
+                        if ($alerts{'username'}{$domain}{$uname}) {
                             $output = &invalid_state('userrules',$domdesc,
                                                      $contact_name,$contact_email);
                             return $output;
                         }
                     }
                 }
-                my $format_msg = 
-                    &guest_format_check($useremail,$domain,$cancreate,
-                                        $settings);
-                if ($format_msg) {
-                    $output = &invalid_state('userformat',$domdesc,$contact_name,
-                                             $contact_email,$format_msg);
-                    return $output;
+                if ($hascustom) {
+                    my $format_msg = 
+                        &guest_format_check($useremail,$domain,$cancreate,
+                                            $settings,$usertype);
+                    if ($format_msg) {
+                        $output = &invalid_state('userformat',$domdesc,$contact_name,
+                                                 $contact_email,$format_msg);
+                        return $output;
+                    }
                 }
             }
         }
         $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name,
-                              $contact_email,$courseid,$emailusername,$usertype);
+                              $contact_email,$courseid,$emailusername,$usertype,
+                              $uname);
     }
     return $output;
 }
@@ -721,7 +886,7 @@
 
 sub send_token {
     my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid,$emailusername,
-        $usertype) = @_;
+        $usertype,$uname) = @_;
     my $msg = '<h3>'.&mt('Account creation status').'</h3>'.
               &mt('Thank you for your request to create a new LON-CAPA account.').
               '<br /><br />';
@@ -747,18 +912,21 @@
                     'upass'      => $env{'form.upass'},
                     'serverid'   => $env{'form.serverid'},
                     'tmpinfo'    => $tmpinfo);
-
+        if ($uname ne '') {
+            $info{'username'} = $uname;
+            $info{'email'} = $email;
+        }
         if (ref($emailusername) eq 'HASH') {
             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;
-                }
             }
         }
+        if ($usertype ne '') {
+           $info{'usertype'} = $usertype;
+        }
         my $token = &Apache::lonnet::tmpput(\%info,$server,'createaccount');
         if ($token !~ /^error/ && $token ne 'no_such_host') {
             my $esc_token = &escape($token);
@@ -792,7 +960,7 @@
 
 sub process_mailtoken {
     my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost,
-        $include,$start_page,$cancreate,$settings,$usertype) = @_;
+        $include,$start_page,$cancreate,$settings,$types) = @_;
     my ($msg,$nostart,$noend,$redirect);
     my %data = &Apache::lonnet::tmpget($token);
     my $now = time;
@@ -805,16 +973,42 @@
     }
     if (($data{'time'} =~ /^\d+$/) &&
         ($data{'domain'} ne '') &&
-        ($data{'username'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) {
+        ((($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && ($data{'username'} =~ /^$match_username$/)) ||
+          ($data{'username'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/))) {
         if ($now - $data{'time'} < 7200) {
 # Check if request should be queued.
             if (ref($cancreate) eq 'ARRAY') {
-                my $disposition;
+                my ($disposition,$usertype);
                 if (grep(/^email$/,@{$cancreate})) {
+                    if (exists($data{'usertype'})) {
+                        $usertype = $data{'usertype'};
+                        my @posstypes;
+                        if (ref($types) eq 'ARRAY') {
+                            @posstypes = @{$types};
+                            if (@posstypes) {
+                                unless (grep(/^default$/, at posstypes)) {
+                                    push(@posstypes,'default');
+                                }
+                            }
+                            if (grep(/\Q$usertype\E/, at posstypes)) {
+                                unless ($usertype eq 'default') {
+                                    $data{'inststatus'} = $usertype;
+                                }
+                            } else {
+                                $disposition = 'approval';
+                            }
+                        }
+                        delete($data{'usertype'});
+                    }
                     if (ref($settings) eq 'HASH') {
                         if (ref($settings->{'cancreate'}) eq 'HASH') {
                             if (ref($settings->{'cancreate'}{'selfcreateprocessing'}) eq 'HASH') {
-                                $disposition = $settings->{'cancreate'}{'selfcreateprocessing'}{$usertype}; 
+                                if ($usertype ne '') {
+                                    $disposition = $settings->{'cancreate'}{'selfcreateprocessing'}{$usertype};
+                                    unless ($disposition =~ /^(approval|automatic)$/) {
+                                        $disposition = 'approval';
+                                    }
+                                }
                             }
                         }
                     }
@@ -893,14 +1087,15 @@
 #
 sub print_dataentry_form {
     my ($r,$domain,$lonhost,$include,$now,$captchaform,$courseid,$emailusername,$captcha,
-        $usertype,$recaptchaversion) = @_;
+        $usertype,$recaptchaversion,$usernameset,$condition,$excluded) = @_;
     my ($error,$output);
     if (open(my $jsh,"<","$include/londes.js")) {
         while(my $line = <$jsh>) {
             $r->print($line);
         }
         close($jsh);
-        $output = &javascript_setforms($now,$emailusername,$captcha,$usertype,$recaptchaversion).
+        $output = &javascript_setforms($now,$emailusername,$captcha,$usertype,$recaptchaversion,
+                                       $usernameset,$condition,$excluded).
                   "\n".&javascript_checkpass($now,'email');
         my ($lkey,$ukey) = &Apache::loncommon::des_keys();
         my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
@@ -916,7 +1111,6 @@
                 foreach my $field (sort(keys(%{$emailusername->{$usertype}}))) {
                     $output .= '<input type="hidden" name="'.$field.'" value="" />'."\n";
                 }
-                $output .= '<input type="hidden" name="type" value="" />'."\n";
             }
         }
         if ($captcha eq 'original') {
@@ -935,6 +1129,13 @@
 ';
             }
         }
+        if ($usertype ne '') {
+            $output .= '<input type="hidden" name="type" value="'.
+                       &HTML::Entities::encode($usertype,'\'<>"&').'" />'."\n";
+        }
+        if ($usernameset eq 'free') {
+            $output .= '<input type="hidden" name="username" value="" />'."\n"; 
+        }
         $output .= <<"ENDSERVERFORM";
    <input type="hidden" name="logtoken" value="$logtoken" />
    <input type="hidden" name="serverid" value="$lonhost" />
@@ -955,7 +1156,8 @@
         my ($datatable,$rowcount) =
             &Apache::loncreateuser::personal_data_display('',$domain,'email','selfcreate',
                                                           '','',$now,$captchaform,
-                                                          $emailusername,$usertype);
+                                                          $emailusername,$usertype,
+                                                          $usernameset,$condition,$excluded);
         if ($rowcount) {
             $output .= '<div class="LC_left_float">'.$beginclientform.$datatable.$endclientform;
         } else {
@@ -977,14 +1179,15 @@
 
 sub get_creation_controls {
     my ($domain,$usercreation) = @_;
-    my (@cancreate, at statustocreate,$emailusername);
+    my (@cancreate, at statustocreate, at statusforemail,$emailusername,$processing,
+        $emailoptions,$verification,$emaildomain,$othertitle,$usertypes,$types);
     if (ref($usercreation) eq 'HASH') {
         if (ref($usercreation->{'cancreate'}) eq 'HASH') {
+            ($othertitle,$usertypes,$types) =
+                &Apache::loncommon::sorted_inst_types($domain);
             if (ref($usercreation->{'cancreate'}{'statustocreate'}) eq 'ARRAY') {
                 @statustocreate = @{$usercreation->{'cancreate'}{'statustocreate'}};
                 if (@statustocreate == 0) {
-                    my ($othertitle,$usertypes,$types) =
-                        &Apache::loncommon::sorted_inst_types($domain);
                     if (ref($types) eq 'ARRAY') {
                         if (@{$types} == 0) {
                             @statustocreate = ('default');
@@ -995,8 +1198,6 @@
                 }
             } else {
                 @statustocreate = ('default');
-                my ($othertitle,$usertypes,$types) =
-                    &Apache::loncommon::sorted_inst_types($domain);
                 if (ref($types) eq 'ARRAY') {
                     push(@statustocreate,@{$types});
                 }
@@ -1007,6 +1208,36 @@
                      ($usercreation->{'cancreate'}{'selfcreate'} ne '')) {
                 @cancreate = ($usercreation->{'cancreate'}{'selfcreate'});
             }
+            if (grep(/^email$/, at cancreate)) {
+                if (ref($usercreation->{'cancreate'}{'selfcreateprocessing'}) eq 'HASH') {
+                    $processing = $usercreation->{'cancreate'}{'selfcreateprocessing'};
+                }
+                if (ref($usercreation->{'cancreate'}{'emailoptions'}) eq 'HASH') {
+                    $emailoptions = $usercreation->{'cancreate'}{'emailoptions'};
+                }
+                if (ref($usercreation->{'cancreate'}{'emailverified'}) eq 'HASH') {
+                    $verification = $usercreation->{'cancreate'}{'emailverified'};
+                }
+                if (ref($usercreation->{'cancreate'}{'emaildomain'}) eq 'HASH') {
+                    $emaildomain = $usercreation->{'cancreate'}{'emaildomain'};
+                }
+                if (ref($processing)) {
+                    my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
+                    my @emailtypes;
+                    if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
+                        @statusforemail = @{$domdefaults{'inststatusguest'}};
+                        unless (@statusforemail) {
+                            my @okcreate;
+                            foreach my $poss (@cancreate) {
+                                unless ($poss eq 'email') {
+                                     push(@okcreate,$poss);
+                                }
+                            }
+                            @cancreate = @okcreate;
+                        }
+                    }
+                }  
+            }
             if (ref($usercreation->{'cancreate'}{'emailusername'}) eq 'HASH') {
                 $emailusername = $usercreation->{'cancreate'}{'emailusername'};
             } else {
@@ -1019,7 +1250,8 @@
             }
         }
     }
-    return (\@cancreate,\@statustocreate,$emailusername);
+    return (\@cancreate,\@statustocreate,\@statusforemail,$emailusername,
+            $emailoptions,$verification,$emaildomain,$types,$usertypes,$othertitle);
 }
 
 sub create_account {
@@ -1032,7 +1264,7 @@
     my $output;
     if (ref($dataref) eq 'HASH') {
         my ($username,$encpass,$serverid,$courseid,$id,$firstname,$middlename,$lastname,
-            $generation,$inststatus);
+            $generation,$inststatus,$permanentemail);
         $username   = $dataref->{'username'};
         $encpass    = $dataref->{'upass'};
         $serverid   = $dataref->{'serverid'};
@@ -1044,6 +1276,11 @@
         $generation = $dataref->{'generation'};
         $inststatus = $dataref->{'inststatus'};
 
+        if ($dataref->{'email'} ne '') {
+            $permanentemail = $dataref->{'email'};
+        } else {
+            $permanentemail = $dataref->{'username'};
+        }
         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);
@@ -1054,7 +1291,13 @@
 
         my ($key,$caller)=split(/&/,$dataref->{'tmpinfo'});
         if ($caller eq 'createaccount') {
-            my $upass = &Apache::loncommon::des_decrypt($key,$encpass);
+            my $upass;
+            if ($encpass eq '') {
+                $output = &mt('Password retrieved was blank.');
+                return ('fail',$error.$output.$end.$rtnlink);
+            } else {
+                $upass = &Apache::loncommon::des_decrypt($key,$encpass);
+            }
 
 # See if we are allowed to use the proposed student/employee ID,
 # as per domain rules; if not, student/employee will be left blank.
@@ -1075,7 +1318,7 @@
             my $result =
                 &Apache::lonnet::modifyuser($domain,$username,$id,
                                             'internal',$upass,$firstname,$middlename,
-                                            $lastname,$generation,undef,undef,$username);
+                                            $lastname,$generation,undef,undef,$permanentemail);
             $output = &mt('Generating user: [_1]',$result);
 
 # Now that the user account exists, retrieve the homeserver, and include it in the output.
@@ -1465,6 +1708,8 @@
         $msg .= &mt('Validation of the code you entered failed.');
     } elsif ($error eq 'noemails') {
         $msg .= &mt('Creation of a new user account using an e-mail address as username is not permitted at this LON-CAPA institution.');
+    } elsif ($error eq 'emailfail') {
+        $msg .= &mt('Creation of a new user account with verification by e-mail is not permitted with the e-mail address you provided');
     }
     $msg .= '</span>';
     if ($msgtext) {
@@ -1597,11 +1842,15 @@
 }
 
 sub guest_format_check {
-    my ($useremail,$domain,$cancreate,$settings) = @_;
+    my ($useremail,$domain,$cancreate,$settings,$usertype) = @_;
     my ($login,$format_match,$format_msg, at user_rules);
     if (ref($settings) eq 'HASH') {
         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
             push(@user_rules,@{$settings->{'email_rule'}});
+        } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
+            if (ref($settings->{'email_rule'}->{$usertype}) eq 'ARRAY') {
+                push(@user_rules,@{$settings->{'email_rule'}->{$usertype}});
+            }
         }
     }
     if (@user_rules > 0) {
@@ -1709,4 +1958,99 @@
 
 }
 
+sub setelements_js {
+    my ($statusforemail,$types,$usertypes,$othertitle) = @_;
+    my ($posstypes,$posstypesnames,$availabletypes);
+    if ((ref($statusforemail) eq 'ARRAY') && (ref($types) eq 'ARRAY') && 
+        (ref($usertypes) eq 'HASH')) {
+        $posstypes = join("','",@{$types},'default');
+        $posstypesnames = join("','",(map {$usertypes->{$_};} @{$types}),$othertitle);
+        $availabletypes = join("','", @{$statusforemail});
+    }
+    return  <<"ENDSCRIPT";
+<script type="text/javascript">
+// <![CDATA[
+
+function setElements() {
+    if (document.getElementById('LC_reportstatus')) {
+        var reportnum = document.reportstatus.type.length;
+        if ((reportnum != 'undefined') && (typeof(document.reportstatus.type) != 'undefined')) {
+            for (var i=0; i<reportnum; i++) {
+                document.reportstatus.type[i].checked = false;
+            }
+        }
+    }
+}
+
+function checkVerification() {
+    var curr;
+    var cancreate = false;
+    var reportnum = document.reportstatus.type.length;
+    if ((reportnum == 'undefined') && (typeof(document.reportstatus.type) != 'undefined')) {
+        curr = document.reportstatus.type.value; 
+    } else if (document.reportstatus.type.length) {
+        for (var i=0; i<document.reportstatus.type.length; i++) {
+            if (document.reportstatus.type[i].checked) {
+                curr = document.reportstatus.type[i].value;
+                break;
+            }
+        }
+    }
+    var types = Array('$posstypes');
+    var names = Array('$posstypesnames');
+    var available = Array('$availabletypes');
+    if (available.length) {
+        for (var i=0; i<available.length; i++) {
+            if (curr == available[i]) {
+                cancreate = true;   
+                break;
+            }
+        }
+    }
+    if (types.length > 0) {
+        for (var j=0; j<types.length; j++) {
+            if (curr == types[j]) {
+                if (!cancreate) {
+                    alert('Creation of an account via verification by e-mail unavailable for user type: "'+names[j]+'"');
+                    setElements();
+                }
+                break;
+            }
+        }
+    }
+    if (cancreate) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+// ]]>
+</script>
+ENDSCRIPT
+
+}
+
+sub username_js {
+    return  <<"ENDSCRIPT";
+<script type="text/javascript">
+// <![CDATA[
+
+function toggleUsernameDisp(caller,divid) {
+    if (document.getElementById(divid)) {
+        if (caller.checked) {
+            if (caller.value == '1') {
+                document.getElementById(divid).style.display = 'none';
+            } else {
+                document.getElementById(divid).style.display = 'inline';
+            }
+        }
+    }
+}
+// ]]>
+</script>
+ENDSCRIPT
+
+}
+
 1;


More information about the LON-CAPA-cvs mailing list