[LON-CAPA-cvs] cvs: loncom /auth lonroles.pm /interface domainprefs.pm

raeburn raeburn at source.lon-capa.org
Sun Nov 28 14:18:01 EST 2021


raeburn		Sun Nov 28 19:18:01 2021 EDT

  Modified files:              
    /loncom/interface	domainprefs.pm 
    /loncom/auth	lonroles.pm 
  Log:
  - Bug 6955. Domain configuration to limit selection of course role based on
    user's IP address.
  
  
-------------- next part --------------
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.393 loncom/interface/domainprefs.pm:1.394
--- loncom/interface/domainprefs.pm:1.393	Sun Nov 28 18:43:37 2021
+++ loncom/interface/domainprefs.pm	Sun Nov 28 19:17:59 2021
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.393 2021/11/28 18:43:37 raeburn Exp $
+# $Id: domainprefs.pm,v 1.394 2021/11/28 19:17:59 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -221,7 +221,7 @@
                 'coursedefaults','usersessions','loadbalancing',
                 'requestauthor','selfenrollment','inststatus',
                 'ltitools','ssl','trust','lti','privacy','passwords',
-                'proctoring','wafproxy'],$dom);
+                'proctoring','wafproxy','ipaccess'],$dom);
     my %encconfig =
         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring'],$dom,undef,1);
     if (ref($domconfig{'ltitools'}) eq 'HASH') {
@@ -260,8 +260,8 @@
             }
         }
     }
-    my @prefs_order = ('rolecolors','login','defaults','wafproxy','passwords','quotas',
-                       'autoenroll','autoupdate','autocreate','directorysrch',
+    my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
+                       'quotas','autoenroll','autoupdate','autocreate','directorysrch',
                        'contacts','privacy','usercreation','selfcreation',
                        'usermodification','scantron','requestcourses','requestauthor',
                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
@@ -624,6 +624,14 @@
                   print => \&print_lti,
                   modify => \&modify_lti,
                  },
+           'ipaccess' =>
+                       {text => 'IP-based access control',
+                        help => 'Domain_Configuration_IP_Access',
+                        header => [{col1 => 'Setting',
+                                    col2 => 'Value'},],
+                        print  => \&print_ipaccess,
+                        modify => \&modify_ipaccess,
+                       },
     );
     if (keys(%servers) > 1) {
         $prefs{'login'}  = { text   => 'Log-in page options',
@@ -679,6 +687,8 @@
 </script>
 $coursebrowserjs
 END
+        } elsif (grep(/^ipaccess$/, at actions)) {
+            $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
         }
         if (grep(/^selfcreation$/, at actions)) {
             $js .= &selfcreate_javascript();
@@ -825,6 +835,8 @@
         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
     } elsif ($action eq 'wafproxy') {
         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
+    } elsif ($action eq 'ipaccess') {
+        $output = &modify_ipaccess($dom,$lastactref,%domconfig);
     }
     return $output;
 }
@@ -867,6 +879,8 @@
         $output .= &autoupdate_javascript();
     } elsif ($action eq 'login') {
         $output .= &saml_javascript();
+    } elsif ($action eq 'ipaccess') {
+        $output .= &ipaccess_javascript($settings);
     }
     $output .=
          '<table class="LC_nested_outer">
@@ -1235,7 +1249,7 @@
         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
                  ($action eq 'ltitools') || ($action eq 'lti') ||
-                 ($action eq 'proctoring')) {
+                 ($action eq 'proctoring') || ($action eq 'ipaccess')) {
             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
         }
     }
@@ -1707,6 +1721,183 @@
       );
 }
 
+sub print_ipaccess {
+    my ($dom,$settings,$rowtotal) = @_;
+    my $css_class;
+    my $itemcount = 0;
+    my $datatable;
+    my %ordered;
+    if (ref($settings) eq 'HASH') {
+        foreach my $item (keys(%{$settings})) {
+            if (ref($settings->{$item}) eq 'HASH') {
+                my $num = $settings->{$item}{'order'};
+                if ($num eq '') {
+                    $num = scalar(keys(%{$settings}));
+                }
+                $ordered{$num} = $item;
+            }
+        }
+    }
+    my $maxnum = scalar(keys(%ordered));
+    if (keys(%ordered)) {
+        my @items = sort { $a <=> $b } keys(%ordered);
+        for (my $i=0; $i<@items; $i++) {
+            $css_class = $itemcount%2?' class="LC_odd_row"':'';
+            my $item = $ordered{$items[$i]};
+            my ($name,$ipranges,%commblocks,%courses);
+            if (ref($settings->{$item}) eq 'HASH') {
+                $name = $settings->{$item}->{'name'};
+                $ipranges = $settings->{$item}->{'ip'};
+                if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
+                    %commblocks = %{$settings->{$item}->{'commblocks'}};
+                }
+                if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
+                    %courses = %{$settings->{$item}->{'courses'}};
+                }
+            }
+            my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
+            $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
+                         .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
+            for (my $k=0; $k<=$maxnum; $k++) {
+                my $vpos = $k+1;
+                my $selstr;
+                if ($k == $i) {
+                    $selstr = ' selected="selected" ';
+                }
+                $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
+            }
+            $datatable .= '</select>'.(' 'x2).
+                '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
+                &mt('Delete?').'</label></span></td>'.
+                '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
+                &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
+                '</td></tr>';
+            $itemcount ++;
+        }
+    }
+    $css_class = $itemcount%2?' class="LC_odd_row"':'';
+    my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
+    $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
+                  '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
+                  '<select name="ipaccess_pos_add"'.$chgstr.'>';
+    for (my $k=0; $k<$maxnum+1; $k++) {
+        my $vpos = $k+1;
+        my $selstr;
+        if ($k == $maxnum) {
+            $selstr = ' selected="selected" ';
+        }
+        $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
+    }
+    $datatable .= '</select> '."\n".
+                  '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
+                  '<td colspan="2">'.
+                  &ipaccess_options('add',$itemcount,$dom).
+                  '</td>'."\n".
+                  '</tr>'."\n";
+    $$rowtotal ++;
+    return $datatable;
+}
+
+sub ipaccess_options {
+    my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
+    my (%currblocks,%currcourses,$output);
+    if (ref($blocksref) eq 'HASH') {
+        %currblocks = %{$blocksref};
+    }
+    if (ref($coursesref) eq 'HASH') {
+        %currcourses = %{$coursesref};
+    }
+    $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
+              '<span class="LC_nobreak">'.&mt('Name').': '.
+              '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
+              '</span></fieldset>'.
+              '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
+              &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
+              &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
+              '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
+              $ipranges.'</textarea></fieldset>'.
+              '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
+              &blocker_checkboxes($num,$blocksref).'</fieldset>'.
+              '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
+              '<table>';
+    foreach my $cid (sort(keys(%currcourses))) {
+        my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
+        $output .= '<tr><td><span class="LC_nobreak">'.
+                   '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
+                   &mt('Delete?').' <span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
+                   ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
+    }
+    $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').': '.
+               '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
+                &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
+               '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
+               '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
+               '</span></td></tr></table>'."\n".
+               '</fieldset>';
+    return $output;
+}
+
+sub blocker_checkboxes {
+    my ($num,$blocks) = @_;
+    my ($typeorder,$types) = &commblocktype_text();
+    my $numinrow = 6;
+    my $output = '<table>';
+    for (my $i=0; $i<@{$typeorder}; $i++) {
+        my $block = $typeorder->[$i];
+        my $blockstatus;
+        if (ref($blocks) eq 'HASH') {
+            if ($blocks->{$block} eq 'on') {
+                $blockstatus = 'checked="checked"';
+            }
+        }
+        my $rem = $i%($numinrow);
+        if ($rem == 0) {
+            if ($i > 0) {
+                $output .= '</tr>';
+            }
+            $output .= '<tr>';
+        }
+        if ($i == scalar(@{$typeorder})-1) {
+            my $colsleft = $numinrow-$rem;
+            if ($colsleft > 1) {
+                $output .= '<td colspan="'.$colsleft.'">';
+            } else {
+                $output .= '<td>';
+            }
+        } else {
+            $output .= '<td>';
+        }
+        my $item = 'ipaccess_block_'.$num;
+        if ($blockstatus) {
+            $blockstatus = ' '.$blockstatus;
+        }
+        $output .= '<span class="LC_nobreak"><label>'."\n".
+                   '<input type="checkbox" name="'.$item.'"'.
+                   $blockstatus.' value="'.$block.'"'.' />'.
+                   $types->{$block}.'</label></span>'."\n".
+                   '<br /></td>';
+    }
+    $output .= '</tr></table>';
+    return $output;
+}
+
+sub commblocktype_text {
+    my %types = &Apache::lonlocal::texthash(
+        'com' => 'Messaging',
+        'chat' => 'Chat Room',
+        'boards' => 'Discussion',
+        'port' => 'Portfolio',
+        'groups' => 'Groups',
+        'blogs' => 'Blogs',
+        'about' => 'User Information',
+        'printout' => 'Printouts',
+        'passwd' => 'Change Password',
+        'grades' => 'Gradebook',
+    );
+    my $typeorder = ['com','chat','boards','port','groups','blogs','about','printout','grades','passwd'];
+    return ($typeorder,\%types);
+}
+
 sub print_rolecolors {
     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
     my %choices = &color_font_choices();
@@ -3536,6 +3727,74 @@
 ENDSCRIPT
 }
 
+sub ipaccess_javascript {
+    my ($settings) = @_;
+    my (%ordered,$total,%jstext);
+    $total = 0;
+    if (ref($settings) eq 'HASH') {
+        foreach my $item (keys(%{$settings})) {
+            if (ref($settings->{$item}) eq 'HASH') {
+                my $num = $settings->{$item}{'order'};
+                $ordered{$num} = $item;
+            }
+        }
+        $total = scalar(keys(%{$settings}));
+    }
+    my @jsarray = ();
+    foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
+        push(@jsarray,$ordered{$item});
+    }
+    my $jstext = '    var ipaccess = Array('."'".join("','", at jsarray)."'".');'."\n";
+    return <<"ENDSCRIPT";
+<script type="text/javascript">
+// <![CDATA[
+function reorderIPaccess(form,item) {
+    var changedVal;
+$jstext
+    var newpos = 'ipaccess_pos_add';
+    var maxh = 1 + $total;
+    var current = new Array;
+    var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
+    if (item == newpos) {
+        changedVal = newitemVal;
+    } else {
+        changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
+        current[newitemVal] = newpos;
+    }
+    for (var i=0; i<ipaccess.length; i++) {
+        var elementName = 'ipaccess_pos_'+ipaccess[i];
+        if (elementName != item) {
+            if (form.elements[elementName]) {
+                var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
+                current[currVal] = elementName;
+            }
+        }
+    }
+    var oldVal;
+    for (var j=0; j<maxh; j++) {
+        if (current[j] == undefined) {
+            oldVal = j;
+        }
+    }
+    if (oldVal < changedVal) {
+        for (var k=oldVal+1; k<=changedVal ; k++) {
+           var elementName = current[k];
+           form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
+        }
+    } else {
+        for (var k=changedVal; k<oldVal; k++) {
+            var elementName = current[k];
+            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
+        }
+    }
+    return;
+}
+// ]]>
+</script>
+
+ENDSCRIPT
+}
+
 sub print_autoenroll {
     my ($dom,$settings,$rowtotal) = @_;
     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
@@ -12144,6 +12403,281 @@
     return %choices;
 }
 
+sub modify_ipaccess {
+    my ($dom,$lastactref,%domconfig) = @_;
+    my (@allpos,%changes,%confhash,$errors,$resulttext);
+    my (@items,%deletions,%itemids, at warnings);
+    my ($typeorder,$types) = &commblocktype_text();
+    if ($env{'form.ipaccess_add'}) {
+        my $name = $env{'form.ipaccess_name_add'};
+        my ($newid,$error) = &get_ipaccess_id($dom,$name);
+        if ($newid) {
+            $itemids{'add'} = $newid;
+            push(@items,'add');
+            $changes{$newid} = 1;
+        } else {
+            $error = &mt('Failed to acquire unique ID for new IP access control item');
+            $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
+        }
+    }
+    if (ref($domconfig{'ipaccess'}) eq 'HASH') {
+        my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
+        if (@todelete) {
+            map { $deletions{$_} = 1; } @todelete;
+        }
+        my $maxnum = $env{'form.ipaccess_maxnum'};
+        for (my $i=0; $i<$maxnum; $i++) {
+            my $itemid = $env{'form.ipaccess_id_'.$i};
+            $itemid =~ s/\D+//g;
+            if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
+                if ($deletions{$itemid}) {
+                    $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
+                } else {
+                    push(@items,$i);
+                    $itemids{$i} = $itemid;
+                }
+            }
+        }
+    }
+    foreach my $idx (@items) {
+        my $itemid = $itemids{$idx};
+        next unless ($itemid);
+        my %current;
+        unless ($idx eq 'add') {
+            if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
+                %current = %{$domconfig{'ipaccess'}{$itemid}};
+            }
+        }
+        my $position = $env{'form.ipaccess_pos_'.$itemid};
+        $position =~ s/\D+//g;
+        if ($position ne '') {
+            $allpos[$position] = $itemid;
+        }
+        my $name = $env{'form.ipaccess_name_'.$idx};
+        $name =~ s/^\s+|\s+$//g;
+        $confhash{$itemid}{'name'} = $name;
+        my $possrange = $env{'form.ipaccess_range_'.$idx};
+        $possrange =~ s/^\s+|\s+$//g;
+        unless ($possrange eq '') {
+            $possrange =~ s/[\r\n]+/\s/g;
+            $possrange =~ s/\s*-\s*/-/g;
+            $possrange =~ s/\s+/,/g;
+            $possrange =~ s/,+/,/g;
+            if ($possrange ne '') {
+                my (@ok,$count);
+                $count = 0; 
+                foreach my $poss (split(/\,/,$possrange)) {
+                    $count ++;
+                    $poss = &validate_ip_pattern($poss);
+                    if ($poss ne '') {
+                        push(@ok,$poss);
+                    }
+                }
+                my $diff = $count - scalar(@ok);
+                if ($diff) {
+                    $errors .= '<li><span class="LC_error">'.
+                               &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
+                                   $diff,$name).
+                               '</span></li>';
+                }
+                if (@ok) {
+                    my @cidr_list;
+                    foreach my $item (@ok) {
+                        @cidr_list = &Net::CIDR::cidradd($item, at cidr_list);
+                    }
+                    $confhash{$itemid}{'ip'} = join(',', at cidr_list);
+                }
+            }
+        }
+        foreach my $field ('name','ip') {
+            unless (($idx eq 'add') || ($changes{$itemid})) {
+                if ($current{$field} ne $confhash{$itemid}{$field}) {
+                    $changes{$itemid} = 1;
+                    last;
+                }
+            }
+        }
+        $confhash{$itemid}{'commblocks'} = {};
+         
+        my %commblocks;
+        map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);  
+        foreach my $type (@{$typeorder}) {
+            if ($commblocks{$type}) {
+                $confhash{$itemid}{'commblocks'}{$type} = 'on';
+            }
+            unless (($idx eq 'add') || ($changes{$itemid})) {
+                if (ref($current{'commblocks'}) eq 'HASH') {
+                    if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
+                        $changes{$itemid} = 1;
+                    }
+                } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
+                    $changes{$itemid} = 1;
+                }
+            }
+        }
+        $confhash{$itemid}{'courses'} = {};
+        my %crsdeletions;
+        my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
+        if (@delcrs) {
+            map { $crsdeletions{$_} = 1; } @delcrs;
+        }
+        if (ref($current{'courses'}) eq 'HASH') {
+            foreach my $cid (sort(keys(%{$current{'courses'}}))) {
+                if ($crsdeletions{$cid}) {
+                    $changes{$itemid} = 1;
+                } else {
+                    $confhash{$itemid}{'courses'}{$cid} = 1;
+                }
+            }
+        }
+        $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
+        $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
+        if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) && 
+            ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
+            if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
+                                            $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
+                $errors .= '<li><span class="LC_error">'.
+                           &mt('Invalid courseID [_1] omitted from list of allowed courses',
+                               $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
+                           '</span></li>';
+            } else {
+                $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
+                $changes{$itemid} = 1;
+            }
+        }
+    }
+    if (@allpos > 0) {
+        my $idx = 0;
+        foreach my $itemid (@allpos) {
+            if ($itemid ne '') {
+                $confhash{$itemid}{'order'} = $idx;
+                unless ($changes{$itemid}) {
+                    if (ref($domconfig{'ipaccess'}) eq 'HASH') {
+                        if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
+                            if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
+                                $changes{$itemid} = 1;
+                            }
+                        }
+                    }
+                }
+                $idx ++;
+            }
+        }
+    }
+    if (keys(%changes)) {
+        my %defaultshash = (
+                              ipaccess => \%confhash,
+                           );
+        my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
+                                                 $dom);
+        if ($putresult eq 'ok') {
+            my $cachetime = 1800;
+            &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
+            if (ref($lastactref) eq 'HASH') {
+                $lastactref->{'ipaccess'} = 1;
+            }
+            $resulttext = &mt('Changes made:').'<ul>';
+            my %bynum;
+            foreach my $itemid (sort(keys(%changes))) {
+                if (ref($confhash{$itemid}) eq 'HASH') {
+                    my $position = $confhash{$itemid}{'order'};
+                    if ($position =~ /^\d+$/) {
+                        $bynum{$position} = $itemid;
+                    }
+                }
+            }
+            if (keys(%deletions)) {
+                foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
+                    $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
+                }
+            }
+            foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
+                my $itemid = $bynum{$pos};
+                if (ref($confhash{$itemid}) eq 'HASH') {
+                    $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
+                    my $position = $pos + 1;
+                    $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
+                    if ($confhash{$itemid}{'ip'} eq '') {
+                        $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
+                    } else {
+                        $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
+                    }
+                    if (keys(%{$confhash{$itemid}{'commblocks'}})) {
+                        $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
+                                                  join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
+                                       '</li>';
+                    } else {
+                        $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
+                    }
+                    if (keys(%{$confhash{$itemid}{'courses'}})) {
+                        my @courses;
+                        foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
+                            my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1}); 
+                            push(@courses,$courseinfo{'description'}.' ('.$cid.')');
+                        }
+                        $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
+                                             join('</li><li>', at courses).'</li></ul>';
+                    } else {
+                        $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
+                    }
+                }
+            }
+        } else {
+            $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
+        }
+    } else {
+        $resulttext = &mt('No changes made');
+    }
+    if ($errors) {
+        $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
+                       $errors.'</ul></p>';
+    }
+    return $resulttext;
+}
+
+sub get_ipaccess_id {
+    my ($domain,$location) = @_;
+    # get lock on ipaccess db
+    my $lockhash = {
+                      lock => $env{'user.name'}.
+                              ':'.$env{'user.domain'},
+                   };
+    my $tries = 0;
+    my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
+    my ($id,$error);
+
+    while (($gotlock ne 'ok') && ($tries<10)) {
+        $tries ++;
+        sleep (0.1);
+        $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
+    }
+    if ($gotlock eq 'ok') {
+        my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
+        if ($currids{'lock'}) {
+            delete($currids{'lock'});
+            if (keys(%currids)) {
+                my @curr = sort { $a <=> $b } keys(%currids);
+                if ($curr[-1] =~ /^\d+$/) {
+                    $id = 1 + $curr[-1];
+                }
+            } else {
+                $id = 1;
+            }
+            if ($id) {
+                unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
+                    $error = 'nostore';
+                }
+            } else {
+                $error = 'nonumber';
+            }
+        }
+        my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
+    } else {
+        $error = 'nolock';
+    }
+    return ($id,$error);
+}
+
 sub modify_rolecolors {
     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
     my ($resulttext,%rolehash);
@@ -22458,7 +22992,8 @@
     my %thismachine;
     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
-                      'directorysrch','passwdconf','cats','proxyalias','proxysaml');
+                      'directorysrch','passwdconf','cats','proxyalias','proxysaml',
+                      'ipaccess');
     my %cache_by_lonhost;
     if (exists($cachekeys->{'samllanding'})) {
         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
Index: loncom/auth/lonroles.pm
diff -u loncom/auth/lonroles.pm:1.356 loncom/auth/lonroles.pm:1.357
--- loncom/auth/lonroles.pm:1.356	Fri Nov 19 19:19:35 2021
+++ loncom/auth/lonroles.pm	Sun Nov 28 19:18:00 2021
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # User Roles Screen
 #
-# $Id: lonroles.pm,v 1.356 2021/11/19 19:19:35 raeburn Exp $
+# $Id: lonroles.pm,v 1.357 2021/11/28 19:18:00 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -277,7 +277,7 @@
         $update = $then;
     }
 
-    my $norolelist;
+    my ($norolelist,$blocked_by_ip,$blocked_type,$blocked_ipaddr);
     if (($env{'request.course.id'}) && ($env{'request.deeplink.login'})) {
         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
@@ -314,6 +314,93 @@
         }
     }
 
+    if ($env{'form.selectrole'}) {
+        my ($role,$cdom,$cnum,$rest);
+        if ($env{'form.switchrole'} =~ m{^(co|cc|in|ta|ep|ad|st|cr).*?\./($match_domain)/($match_courseid)(/(\w+)|$)}) {
+            ($role,$cdom,$cnum,$rest) = ($1,$2,$3,$4);
+        } elsif ($env{'form.newrole'} =~ m{^(co|cc|in|ta|ep|ad|st|cr).*?\./($match_domain)/($match_courseid)(/(\w+)|$)}) {
+            ($role,$cdom,$cnum,$rest) = ($1,$2,$3,$4);
+        }
+        if ($cdom ne '') {
+            my ($has_evb,$check_ipaccess,$showrole);
+            $showrole = 1;
+            my $checkrole = "cm./$cdom/$cnum";
+            if ($rest ne '') {
+                $checkrole .= "/$rest";
+            }
+            if ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
+                ($role ne 'st')) {
+                $has_evb = 1;
+            }
+            unless ($has_evb) {
+                my @machinedoms = &Apache::lonnet::current_machine_domains();
+                my $udom = $env{'user.domain'};
+                if ($udom eq $cdom) {
+                    $check_ipaccess = 1;
+                } elsif (($udom ne '') && (grep(/^\Q$udom\E$/, at machinedoms))) {
+                    $check_ipaccess = 1;
+                } else {
+                    my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
+                    my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
+                    my $cprim = &Apache::lonnet::domain($cdom,'primary');
+                    my $cintdom = &Apache::lonnet::internet_dom($cprim);
+                    if (($cintdom ne '') && (ref($internet_names) eq 'ARRAY')) {
+                        if (grep(/^\Q$cintdom\E$/,@{$internet_names})) {
+                            $check_ipaccess = 1;
+                        }
+                    }
+                }
+                if ($check_ipaccess) {
+                    my ($ipaccessref,$cached)=&Apache::lonnet::is_cached_new('ipaccess',$cdom);
+                    unless (defined($cached)) {
+                        my %domconfig =
+                            &Apache::lonnet::get_dom('configuration',['ipaccess'],$cdom);
+                        $ipaccessref = &do_cache_new('ipaccess',$cdom,$domconfig{'ipaccess'},1800);
+                    }
+                    if (ref($ipaccessref) eq 'HASH') {
+                        my $remote_ip = &Apache::lonnet::get_requestor_ip();
+                        foreach my $id (keys(%{$ipaccessref})) {
+                            if (ref($ipaccessref->{$id}) eq 'HASH') {
+                                my $range = $ipaccessref->{$id}->{'ip'};
+                                if ($range) {
+                                    my $type = 'exclude';
+                                    if (&Apache::lonnet::ip_match($remote_ip,$range)) {
+                                        $type = 'include';
+                                    }
+                                    if (ref($ipaccessref->{$id}->{'courses'}) eq 'HASH') {
+                                        if ($ipaccessref->{$id}->{'courses'}{$cdom.'_'.$cnum}) {
+                                            if ($type eq 'include') {
+                                                $showrole = 1;
+                                                last;
+                                            } else {
+                                                $showrole = 0;
+                                            }
+                                        } else {
+                                            if ($type eq 'include') {
+                                                $showrole = 0;
+                                            } else {
+                                                $showrole = 1;
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                        unless ($showrole) {
+                            $blocked_ipaddr = $remote_ip;
+                        }
+                    }
+                }
+            }
+            unless ($showrole) {
+                $blocked_by_ip = 1;
+                $blocked_type = &Apache::loncommon::course_type($cdom.'_'.$cnum);
+                delete($env{'form.selectrole'});
+                delete($env{'form.newrole'});
+            }
+        }
+    }
+
     $registered_cleanup=0;
     @{$rosterupdates}=();
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
@@ -1260,6 +1347,16 @@
         $r->print('<input type="hidden" name="newrole" value="" />');
         $r->print('<input type="hidden" name="display" value="'.$display.'" />');
         $r->print('<input type="hidden" name="state" value="" />');
+        if ($blocked_by_ip) {
+            my $blocked_role = 'student';
+            if ($blocked_type eq 'Community') {
+                $blocked_role = 'member';
+            }
+            $r->print('<h3><span class="LC_error">'.
+                      &mt('The [_1] you selected is not available for access with a [_2] role from your current IP address: [_3].',
+                          lc($blocked_type),$blocked_role,$blocked_ipaddr).
+                      '</span></h3>');
+        }
     }
     $r->rflush();
 


More information about the LON-CAPA-cvs mailing list