--- loncom/interface/lonuserutils.pm	2019/07/26 18:37:16	1.184.4.3
+++ loncom/interface/lonuserutils.pm	2017/08/11 18:58:16	1.189
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.184.4.3 2019/07/26 18:37:16 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.189 2017/08/11 18:58:16 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -943,8 +943,14 @@ sub print_upload_manager_footer {
                 &Apache::lonhtmlcommon::row_closure();
     }
 
+    my ($trusted,$untrusted);
+    if ($context eq 'course') {
+        ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('enroll',$defdom);
+    } elsif ($context eq 'author') {
+        ($trusted,$untrusted) = &Apache::lonnet::trusted_domains('othcoau',$defdom);
+    }
     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Default domain'))
-           .&Apache::loncommon::select_dom_form($defdom,'defaultdomain',undef,1)
+           .&Apache::loncommon::select_dom_form($defdom,'defaultdomain',undef,1,undef,$trusted,$untrusted)
            .&Apache::lonhtmlcommon::row_closure();
 
     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Starting and Ending Dates'))
@@ -1126,11 +1132,6 @@ sub print_upload_manager_form {
             &Apache::loncommon::load_tmp_file($r,$datatoken);
         }
     }
-    if ($datatoken eq '') {
-        $r->print('<p class="LC_error">'.&mt('Error').': '.
-                  &mt('Invalid datatoken').'</p>');
-        return 'missingdata';
-    }
     my @records=&Apache::loncommon::upfile_record_sep();
     if($env{'form.noFirstLine'}){
         $firstLine=shift(@records);
@@ -1213,7 +1214,6 @@ sub print_upload_manager_form {
     }
     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
                                  $context,$permission,$crstype,$showcredits);
-    return 'ok';
 }
 
 sub setup_date_selectors {
@@ -2287,6 +2287,7 @@ sub build_user_record {
 
 sub courses_selector {
     my ($cdom,$formname) = @_;
+    my %coursecodes = ();
     my %codes = ();
     my @codetitles = ();
     my %cat_titles = ();
@@ -2299,15 +2300,14 @@ sub courses_selector {
     my $jscript = '';
 
     my $totcodes = 0;
-    my $instcats = &Apache::lonnet::get_dom_instcats($cdom);
-    if (ref($instcats) eq 'HASH') {
-        if ((ref($instcats->{'codetitles'}) eq 'ARRAY') && (ref($instcats->{'codes'}) eq 'HASH') &&
-            (ref($instcats->{'cat_titles'}) eq 'HASH') && (ref($instcats->{'cat_order'}) eq 'HASH')) {
-            %codes = %{$instcats->{'codes'}};
-            @codetitles = @{$instcats->{'codetitles'}};
-            %cat_titles = %{$instcats->{'cat_titles'}};
-            %cat_order = %{$instcats->{'cat_order'}};
-            $totcodes = scalar(keys(%codes));
+    $totcodes =
+        &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
+                                                      $cdom,$totcodes);
+    if ($totcodes > 0) {
+        $format_reply =
+             &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
+                                \%codes,\@codetitles,\%cat_titles,\%cat_order);
+        if ($format_reply eq 'ok') {
             my $numtypes = @codetitles;
             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
@@ -4105,7 +4105,7 @@ sub print_first_users_upload_form {
            .&Apache::lonhtmlcommon::end_pick_box();
 
     $str .= '<p>'
-           .'<input type="button" name="fileupload" value="'.&mt('Next').'"'
+           .'<input type="submit" name="fileupload" value="'.&mt('Next').'"'
            .' onclick="javascript:checkUpload(this.form);" />'
            .'</p>';
 
@@ -4133,6 +4133,10 @@ sub upfile_drop_add {
             $fields{$env{'form.f'.$i}}=$keyfields[$i];
         }
     }
+    if ($env{'form.fullup'} ne 'yes') {
+        $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
+                  '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
+    }
     #
     # Store the field choices away
     my @storefields = qw/username names fname mname lname gen id 
@@ -4146,17 +4150,19 @@ sub upfile_drop_add {
         $fieldstype{$field.'_choice'} = 'scalar';
     }
     &Apache::loncommon::store_course_settings('enrollment_upload',\%fieldstype);
-    my ($cid,$crstype,$setting);
+    my ($cid,$crstype,$setting,$crsdom);
     if ($context eq 'domain') {
         $setting = $env{'form.roleaction'};
     }
     if ($env{'request.course.id'} ne '') {
         $cid = $env{'request.course.id'};
         $crstype = &Apache::loncommon::course_type();
+        $crsdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
     } elsif ($setting eq 'course') {
         if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
             $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
             $crstype = &Apache::loncommon::course_type($cid);
+            $crsdom = $env{'form.dcdomain'};
         }
     }
     my ($startdate,$enddate) = &get_dates_from_form();
@@ -4167,7 +4173,43 @@ sub upfile_drop_add {
     my $defdom=$env{'request.role.domain'};
     my $domain;
     if ($env{'form.defaultdomain'} ne '') {
-        $domain = $env{'form.defaultdomain'};
+        if (($context eq 'course') || ($setting eq 'course')) {
+            unless ($env{'form.defaultdomain'} eq $crsdom) {
+                if (&Apache::lonnet::will_trust('enroll',$crsdom,$env{'form.defaultdomain'})) {
+                    $domain = $env{'form.defaultdomain'};
+                } else {
+                    $r->print('<span class="LC_error">'.&mt('Error').
+                              &mt('Enrollment of users not permitted for specified default domain: [_1].',
+                                  &Apache::lonnet::domain($env{'form.defaultdomain'},'description')).'</span>');
+                    $r->print(&Apache::loncommon::end_page());
+                }
+                return;
+            }
+        } elsif ($context eq 'author') {
+            unless ($env{'form.defaultdomain'} eq $defdom) {
+                if ((&Apache::lonnet::will_trust('othcoau',$defdom,$env{'form.defaultdomain'})) &&
+                    (&Apache::lonnet::will_trust('coaurem',$env{'form.defaultdomain'},$defdom))) {
+                    $domain = $env{'form.defaultdomain'};
+                } else {
+                    $r->print('<span class="LC_error">'.&mt('Error').
+                              &mt('Addition of users not permitted for specified default domain: [_1].',
+                                  &Apache::lonnet::domain($env{'form.defaultdomain'},'description')).'</span>');
+                    $r->print(&Apache::loncommon::end_page());
+                }
+                return; 
+            }
+        } elsif (($context eq 'domain') && ($setting eq 'domain')) {
+            unless ($env{'form.defaultdomain'} eq $defdom) {
+                if (&Apache::lonnet::will_trust('domroles',$defdom,$env{'form.defaultdomain'})) {
+                    $domain = $env{'form.defaultdomain'};
+                } else {
+                    $r->print('<span class="LC_error">'.&mt('Error').
+                              &mt('Addition of users not permitted for specified default domain: [_1].',
+                                  &Apache::lonnet::domain($env{'form.defaultdomain'},'description')).'</span>');
+                    $r->print(&Apache::loncommon::end_page());
+                }
+            }
+        }
     } else {
         $domain = $defdom;
     }
@@ -4177,10 +4219,10 @@ sub upfile_drop_add {
     } else {
         my %home_servers = &Apache::lonnet::get_servers($defdom,'library');
         if (! exists($home_servers{$desiredhost})) {
-            $r->print('<p class="LC_error">'.&mt('Error').': '.
-                      &mt('Invalid home server specified').'</p>');
+            $r->print('<span class="LC_error">'.&mt('Error').
+                      &mt('Invalid home server specified').'</span>');
             $r->print(&Apache::loncommon::end_page());
-            return 'invalidhome';
+            return;
         }
     }
     # Determine authentication mechanism
@@ -4280,11 +4322,6 @@ sub upfile_drop_add {
             }
         }
     }
-    if ($datatoken eq '') {
-        $r->print('<p class="LC_error">'.&mt('Error').': '.
-                  &mt('Invalid datatoken').'</p>');
-        return 'missingdata';
-    }
     if ( $domain eq &LONCAPA::clean_domain($domain)
         && ($amode ne '')) {
         #######################################
@@ -4356,6 +4393,7 @@ sub upfile_drop_add {
         # Get new users list
         my (%existinguser,%userinfo,%disallow,%rulematch,%inst_results,%alerts,%checkuname);
         my $counter = -1;
+        my (%willtrust,%trustchecked);
         foreach my $line (@userdata) {
             $counter ++;
             my @secs;
@@ -4403,6 +4441,28 @@ sub upfile_drop_add {
                                 '"<b>'.$entries{$fields{'domain'}}.'</b>"',
                                 $fname,$mname,$lname,$gen);
                         next;
+                    } elsif ($entries{$fields{'domain'}} ne $domain) {
+                        my $possdom = $entries{$fields{'domain'}};
+                        if ($context eq 'course' || $setting eq 'course') {
+                            unless ($trustchecked{$possdom}) {
+                                $willtrust{$possdom} = &Apache::lonnet::will_trust('enroll',$domain,$possdom);
+                                $trustchecked{$possdom} = 1;
+                            }
+                        } elsif ($context eq 'author') {
+                            unless ($trustchecked{$possdom}) {
+                                $willtrust{$possdom} = &Apache::lonnet::will_trust('othcoau',$domain,$possdom);
+                            }
+                            if ($willtrust{$possdom}) {
+                                $willtrust{$possdom} = &Apache::lonnet::will_trust('coaurem',$possdom,$domain); 
+                            }
+                        }
+                        unless ($willtrust{$possdom}) {
+                            $disallow{$counter} =
+                                &mt('Unacceptable domain [_1] for user [_2] [_3] [_4] [_5]',
+                                    '"<b>'.$possdom.'</b>"',
+                                    $fname,$mname,$lname,$gen);
+                            next;
+                        }
                     }
                     my $username = $entries{$fields{'username'}};
                     my $userdomain = $entries{$fields{'domain'}};
@@ -4837,9 +4897,7 @@ sub upfile_drop_add {
                   "</p>\n");
         if ($counts{'role'} > 0) {
             $r->print("<p>\n".
-                      &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.
-                      &mt('If a user is currently logged-in to LON-CAPA, any new roles which are active will be available when the user next logs in.').
-                      "</p>\n");
+                      &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.&mt('If a user is currently logged-in to LON-CAPA, any new roles which are active will be available when the user next logs in.')."</p>\n");
         } else {
             $r->print('<p>'.&mt('No roles added').'</p>');
         }
@@ -4857,9 +4915,10 @@ sub upfile_drop_add {
             #  Get current classlist
             my $classlist = &Apache::loncoursedata::get_classlist();
             if (! defined($classlist)) {
-                $r->print('<p class="LC_info">'.
-                          &mt('There are no students with current/future access to the course.').
-                          '</p>'."\n");
+                $r->print('<form name="studentform" method="post" action="/adm/createuser">'.
+                          '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
+                          '<p class="LC_info">'.&mt('There are no students with current/future access to the course.').'</p>'.
+                          '</form>'."\n");
             } elsif (ref($classlist) eq 'HASH') {
                 # Remove the students we just added from the list of students.
                 foreach my $line (@userdata) {
@@ -4875,7 +4934,9 @@ sub upfile_drop_add {
             }
         }
     } # end of unless
-    return 'ok';
+    if ($env{'form.fullup'} ne 'yes') {
+        $r->print('</form>');
+    }
 }
 
 sub print_namespacing_alerts {