[LON-CAPA-cvs] cvs: loncom /interface domainprefs.pm loncommon.pm loncreateuser.pm /lonnet/perl lonnet.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Sat, 10 Nov 2007 03:51:49 -0000


This is a MIME encoded message

--raeburn1194666709
Content-Type: text/plain

raeburn		Fri Nov  9 22:51:49 2007 EDT

  Modified files:              
    /loncom/lonnet/perl	lonnet.pm 
    /loncom/interface	domainprefs.pm loncreateuser.pm loncommon.pm 
  Log:
  - ID rules can be set by DC via Domain Configuration
  - when creating a user, the proposed ID will checked against enabled ID rules
  - In loncommon.pm username_rule_check() replaced with &user_rule_check()
    - to allow messages from checking of multiple usernames to be aggregated
      (in readiness for adding checks for uploaded classlists).
    - to allow checking of IDs as well as usernames.
    - &user_rule_formats() added for creating of the listing of current rules for usernames and IDs when a proposed username or ID fails a check against any rules in operation. 
  
  
--raeburn1194666709
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20071109225149.txt"

Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.922 loncom/lonnet/perl/lonnet.pm:1.923
--- loncom/lonnet/perl/lonnet.pm:1.922	Tue Nov  6 08:05:00 2007
+++ loncom/lonnet/perl/lonnet.pm	Fri Nov  9 22:51:10 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.922 2007/11/06 13:05:00 raeburn Exp $
+# $Id: lonnet.pm,v 1.923 2007/11/10 03:51:10 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1047,7 +1047,7 @@
 }
 
 sub inst_rulecheck {
-    my ($udom,$uname,$rules) = @_;
+    my ($udom,$uname,$id,$item,$rules) = @_;
     my %returnhash;
     if ($udom ne '') {
         if (ref($rules) eq 'ARRAY') {
@@ -1055,9 +1055,16 @@
             my $rulestr = join(':',@{$rules});
             my $homeserver=&domain($udom,'primary');
             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
-                my $response=&unescape(&reply('instrulecheck:'.&escape($udom).':'.
-                                              &escape($uname).':'.$rulestr,
+                my $response;
+                if ($item eq 'username') {                
+                    $response=&unescape(&reply('instrulecheck:'.&escape($udom).
+                                              ':'.&escape($uname).':'.$rulestr,
                                               $homeserver));
+                } elsif ($item eq 'id') {
+                    $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
+                                              ':'.&escape($id).':'.$rulestr,
+                                              $homeserver));
+                }
                 if ($response ne 'refused') {
                     my @pairs=split(/\&/,$response);
                     foreach my $item (@pairs) {
@@ -1074,14 +1081,21 @@
 }
 
 sub inst_userrules {
-    my ($udom) = @_;
+    my ($udom,$check) = @_;
     my (%ruleshash,@ruleorder);
     if ($udom ne '') {
         my $homeserver=&domain($udom,'primary');
         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
-            my $response=&reply('instuserrules:'.&escape($udom),
+            my $response;
+            if ($check eq 'id') {
+                $response=&reply('instidrules:'.&escape($udom),
                                  $homeserver);
+            } else {
+                $response=&reply('instuserrules:'.&escape($udom),
+                                 $homeserver);
+            }
             if (($response ne 'refused') && ($response ne 'error') && 
+                ($response ne 'unknown_cmd') && 
                 ($response ne 'no_such_host')) {
                 my ($hashitems,$orderitems) = split(/:/,$response);
                 my @pairs=split(/\&/,$hashitems);
Index: loncom/interface/domainprefs.pm
diff -u loncom/interface/domainprefs.pm:1.31 loncom/interface/domainprefs.pm:1.32
--- loncom/interface/domainprefs.pm:1.31	Thu Sep 27 11:36:23 2007
+++ loncom/interface/domainprefs.pm	Fri Nov  9 22:51:46 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set domain-wide configuration settings
 #
-# $Id: domainprefs.pm,v 1.31 2007/09/27 15:36:23 raeburn Exp $
+# $Id: domainprefs.pm,v 1.32 2007/11/10 03:51:46 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1289,7 +1289,6 @@
 sub print_usercreation {
     my ($position,$dom,$settings,$rowtotal) = @_;
     my $numinrow = 4;
-    my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom);
     my $datatable;
     my %lt = &Apache::lonlocal::texthash (
                         nondc => 'User creation other than by Domain Coordinator: ',
@@ -1316,11 +1315,23 @@
         }
         $datatable .= '</table></td></tr>';
         $$rowtotal ++;
+        my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
+        my $rowcount = 0;
         if (ref($rules) eq 'HASH') {
             if (keys(%{$rules}) > 0) {
-                $datatable .= &username_formats_row($settings,$rules,
-                                                    $ruleorder,$numinrow);
+                $datatable .= &user_formats_row('username',$settings,$rules,
+                                                $ruleorder,$numinrow,$rowcount);
                 $$rowtotal ++;
+                $rowcount ++;
+            }
+        }
+        my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
+        if (ref($idrules) eq 'HASH') {
+            if (keys(%{$idrules}) > 0) {
+                $datatable .= &user_formats_row('id',$settings,$idrules,
+                                                $idruleorder,$numinrow,$rowcount);
+                $$rowtotal ++;
+                $rowcount ++;
             }
         }
     } else {
@@ -1369,13 +1380,19 @@
     return $datatable;
 }
 
-sub username_formats_row {
-    my ($settings,$rules,$ruleorder,$numinrow) = @_;
-    my $output =  '<tr>'.
-                  '<td><span class="LC_nobreak">'.
-                  &mt('Format rules to check for new usernames: ').
-                  '</span></td>'.
-                  '<td class="LC_left_item" colspan="2"><table>';
+sub user_formats_row {
+    my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
+    my $output;
+    my %text = (
+                   'username' => 'new usernames',
+                   'id'       => 'IDs',
+               );
+    my $css_class = $rowcount%2?' class="LC_odd_row"':'';
+    $output = '<tr '.$css_class.'>'.
+              '<td><span class="LC_nobreak">'.
+              &mt("Format rules to check for $text{$type}: ").
+              '</span></td>'.
+              '<td class="LC_left_item" colspan="2"><table>';
     my $rem;
     if (ref($ruleorder) eq 'ARRAY') {
         for (my $i=0; $i<@{$ruleorder}; $i++) {
@@ -1388,14 +1405,14 @@
                     $output .= '<tr>';
                 }
                 my $check = ' ';
-                if (ref($settings->{'username_rule'}) eq 'ARRAY') {
-                    if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{'username_rule'}})) {
+                if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
+                    if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
                         $check = ' checked="checked" ';
                     }
                 }
                 $output .= '<td class="LC_left_item">'.
                            '<span class="LC_nobreak"><label>'.
-                           '<input type="checkbox" name="username_rule" '.
+                           '<input type="checkbox" name="'.$type.'_rule" '.
                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
             }
@@ -2728,6 +2745,7 @@
                    course => 'adding users to a course',
                 );
     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
+    my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
     my @cancreate = &Apache::loncommon::get_env_multiple('form.can_createuser');
     if (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
         foreach my $type (@{$curr_usercreation{'cancreate'}}) {
@@ -2758,6 +2776,21 @@
         push(@{$changes{'username_rule'}},@username_rule);
     }
 
+    if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
+        foreach my $type (@{$curr_usercreation{'id_rule'}}) {
+            if (!grep(/^\Q$type\E$/,@id_rule)) {
+                push(@{$changes{'id_rule'}},$type);
+            }
+        }
+        foreach my $type (@id_rule) {
+            if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
+                push(@{$changes{'id_rule'}},$type);
+            }
+        }
+    } else {
+        push(@{$changes{'id_rule'}},@id_rule);
+    }
+
     my @contexts = ('author','course','domain');
     my @authtypes = ('int','krb4','krb5','loc');
     my %authhash;
@@ -2792,7 +2825,8 @@
             usercreation => {
                               cancreate     => \@cancreate,
                               username_rule => \@username_rule,
-                              authtypes      => \%authhash,
+                              id_rule       => \@id_rule,
+                              authtypes     => \%authhash,
                             }
             );
 
@@ -2814,7 +2848,8 @@
                 }
             }
             if (ref($changes{'username_rule'}) eq 'ARRAY') {
-                my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom);
+                my ($rules,$ruleorder) = 
+                    &Apache::lonnet::inst_userrules($dom,'username');
                 my $chgtext = '<ul>';
                 foreach my $type (@username_rule) {
                     if (ref($rules->{$type}) eq 'HASH') {
@@ -2828,6 +2863,22 @@
                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
                 }
             }
+            if (ref($changes{'id_rule'}) eq 'ARRAY') {
+                my ($idrules,$idruleorder) = 
+                    &Apache::lonnet::inst_userrules($dom,'id');
+                my $chgtext = '<ul>';
+                foreach my $type (@id_rule) {
+                    if (ref($idrules->{$type}) eq 'HASH') {
+                        $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
+                    }
+                }
+                $chgtext .= '</ul>';
+                if (@id_rule > 0) {
+                    $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
+                } else {
+                    $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
+                }
+            }
             my %authname = &authtype_names();
             my %context_title = &context_names();
             if (ref($changes{'authtypes'}) eq 'ARRAY') {
Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.192 loncom/interface/loncreateuser.pm:1.193
--- loncom/interface/loncreateuser.pm:1.192	Fri Nov  9 15:51:20 2007
+++ loncom/interface/loncreateuser.pm	Fri Nov  9 22:51:46 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.192 2007/11/09 20:51:20 albertel Exp $
+# $Id: loncreateuser.pm,v 1.193 2007/11/10 03:51:46 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -447,24 +447,32 @@
         return;
     }
     my %abv_auth = &auth_abbrev();
-    my ($curr_authtype,$instsrch,$rulematch,$rules,%inst_results,
-        $curr_kerb_ver,$newuser);
+    my ($curr_authtype,%rulematch,%inst_results,$curr_kerb_ver,$newuser,
+        %alerts,%curr_rules);
     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
     if ($uhome eq 'no_host') {
         $newuser = 1;
-        $instsrch =
-                      {
-                         srchin => 'instd',
-                         srchby => 'uname',
-                         srchtype => 'exact',
-                         srchterm => $ccuname,
-                         srchdomain => $ccdomain,
-                       };
-        (my $usercheckmsg,$rulematch,$rules,%inst_results) = 
-            &Apache::loncommon::username_rule_check($instsrch,'new');
-        if ($usercheckmsg) {
-            &print_username_entry_form($r,$usercheckmsg);
-            return;
+        my $checkhash;
+        my $checks = { 'username' => 1 };
+        $checkhash->{$ccuname.':'.$ccdomain} = { 'status' => 'new' };
+        &Apache::loncommon::user_rule_check($checkhash,$checks,
+                   \%alerts,\%rulematch,\%inst_results,\%curr_rules);
+        if (ref($alerts{$ccuname.':'.$ccdomain}) eq 'HASH') {
+            if ($alerts{$ccuname.':'.$ccdomain}{'username'}) {
+                my $domdesc = 
+                    &Apache::lonnet::domain($ccdomain,'description');
+                my $userchkmsg;
+                if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
+                    $userchkmsg = 
+                        &Apache::loncommon::instrule_disallow_msg('username',
+                                                                 $domdesc,1).
+                        &Apache::loncommon::user_rule_formats($ccdomain,
+                            $domdesc,$curr_rules{$ccdomain}{'username'},
+                            'username');
+                }
+                &print_username_entry_form($r,$userchkmsg);
+                return;
+            }
         }
     } else {
         $newuser = 0;
@@ -766,7 +774,7 @@
 $loginscript
 </script>
 <input type='hidden' name='makeuser' value='1' />
-<h3>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain</h3>
+<h2>$lt{'cnu'} "$ccuname" $lt{'ind'} $ccdomain</h2>
 ENDTITLE
         $r->print('<div class="LC_left_float">'.
                   &personal_data_display($ccuname,$ccdomain,$newuser,
@@ -785,14 +793,17 @@
         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   $lt{'lg'}.'</h3>');
         my ($fixedauth,$varauth,$authmsg); 
-        if ($rulematch) {
+        if (ref($rulematch{$ccuname.':'.$ccdomain}) eq 'HASH') {
+            my $matchedrule = $rulematch{$ccuname.':'.$ccdomain}{'username'};
+            my ($rules,$ruleorder) = 
+                &Apache::lonnet::inst_userrules($ccdomain,'username');
             if (ref($rules) eq 'HASH') {
-                if (ref($rules->{$rulematch}) eq 'HASH') {
-                    my $authtype = $rules->{$rulematch}{'authtype'};
+                if (ref($rules->{$matchedrule}) eq 'HASH') {
+                    my $authtype = $rules->{$matchedrule}{'authtype'};
                     if ($authtype !~ /^(krb4|krb5|int|fsys|loc)$/) {
                         $r->print(&Apache::lonuserutils::set_login($ccdomain,$authformkrb,$authformint,$authformloc));
                     } else { 
-                        my $authparm = $rules->{$rulematch}{'authparm'};
+                        my $authparm = $rules->{$matchedrule}{'authparm'};
                         if ($authtype =~ /^krb(4|5)$/) {
                             my $ver = $1;
                             if ($authparm ne '') {
@@ -801,12 +812,12 @@
 <input type="hidden" name="krbver" value="$ver" />
 <input type="hidden" name="krbarg" value="$authparm" />
 KERB
-                                $authmsg = $rules->{$rulematch}{'authmsg'};    
+                                $authmsg = $rules->{$matchedrule}{'authmsg'};    
                             }
                         } else {
                             $fixedauth = 
 '<input type="hidden" name="login" value="'.$authtype.'" />'."\n";
-                            if ($rules->{$rulematch}{'authparmfixed'}) {
+                            if ($rules->{$matchedrule}{'authparmfixed'}) {
                                 $fixedauth .=    
 '<input type="hidden" name="'.$authtype.'arg" value="'.$authparm.'" />'."\n";
                             } else {
@@ -1450,8 +1461,8 @@
                                           $env{'form.ccdomain'});
     # Error messages
     my $error     = '<span class="LC_error">'.&mt('Error').': ';
-    my $end       = '</span><br /><br />'.
-                    '<a href="javascript:backPage(document.userupdate,'.
+    my $end       = '</span><br /><br />';
+    my $rtnlink   = '<a href="javascript:backPage(document.userupdate,'.
                     "'$env{'form.prevphase'}','modify')".'" />'.
                     &mt('Return to previous page').'</a>'.&Apache::loncommon::end_page();
     my $title;
@@ -1489,25 +1500,25 @@
     $r->print(&update_result_form($uhome));
     # Check Inputs
     if (! $env{'form.ccuname'} ) {
-	$r->print($error.&mt('No login name specified').'.'.$end);
+	$r->print($error.&mt('No login name specified').'.'.$end.$rtnlink);
 	return;
     }
     if (  $env{'form.ccuname'} ne 
 	  &LONCAPA::clean_username($env{'form.ccuname'}) ) {
 	$r->print($error.&mt('Invalid login name').'.  '.
 		  &mt('Only letters, numbers, periods, dashes, @, and underscores are valid').'.'.
-		  $end);
+		  $end.$rtnlink);
 	return;
     }
     if (! $env{'form.ccdomain'}       ) {
-	$r->print($error.&mt('No domain specified').'.'.$end);
+	$r->print($error.&mt('No domain specified').'.'.$end.$rtnlink);
 	return;
     }
     if (  $env{'form.ccdomain'} ne
 	  &LONCAPA::clean_domain($env{'form.ccdomain'}) ) {
 	$r->print($error.&mt ('Invalid domain name').'.  '.
 		  &mt('Only letters, numbers, periods, dashes, and underscores are valid').'.'.
-		  $end);
+		  $end.$rtnlink);
 	return;
     }
     if (! exists($env{'form.makeuser'})) {
@@ -1543,19 +1554,19 @@
         # If they are creating a new user but have not specified login
         # information this will be caught below.
     } else {
-	    $r->print($error.&mt('Invalid login mode or password').$end);    
+	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
 	    return;
     }
 
 
     $r->print('<h3>'.&mt('User [_1] in domain [_2]',
 			 $env{'form.ccuname'}, $env{'form.ccdomain'}).'</h3>');
-
+    my (%alerts,%rulematch,%inst_results,%curr_rules);
     if ($env{'form.makeuser'}) {
 	$r->print('<h3>'.&mt('Creating new account.').'</h3>');
         # Check for the authentication mode and password
         if (! $amode || ! $genpwd) {
-	    $r->print($error.&mt('Invalid login mode or password').$end);    
+	    $r->print($error.&mt('Invalid login mode or password').$end.$rtnlink);    
 	    return;
 	}
         # Determine desired host
@@ -1566,13 +1577,39 @@
             my %home_servers = 
 		&Apache::lonnet::get_servers($env{'form.ccdomain'},'library');
             if (! exists($home_servers{$desiredhost})) {
-                $r->print($error.&mt('Invalid home server specified'));
+                $r->print($error.&mt('Invalid home server specified').$end.$rtnlink);
+                return;
+            }
+        }
+        # Check ID format
+        my %checkhash;
+        my %checks = ('id' => 1);
+        %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
+            'status' => 'new', 
+            'id' => $env{'form.cid'}
+        );
+        &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
+                                      \%rulematch,\%inst_results,\%curr_rules);
+        if (ref($alerts{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}) eq 'HASH') {
+            if ($alerts{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}{'id'}) {
+                my $domdesc =
+                    &Apache::lonnet::domain($env{'form.ccdomain'},'description');
+                my $userchkmsg;
+                if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
+                    $userchkmsg  = 
+                        &Apache::loncommon::instrule_disallow_msg('id',
+                                                                  $domdesc,1).
+                        &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
+                            $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
+                } 
+                $r->print($error.&mt('Invalid ID format').$end.
+                          $userchkmsg.$rtnlink);
                 return;
             }
         }
 	# Call modifyuser
 	my $result = &Apache::lonnet::modifyuser
-	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cstid'},
+	    ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cid'},
              $amode,$genpwd,$env{'form.cfirstname'},
              $env{'form.cmiddlename'},$env{'form.clastname'},
              $env{'form.cgeneration'},undef,$desiredhost,
@@ -1586,7 +1623,7 @@
              ($env{'form.login'} ne ''        )) {
 	# Modify user privileges
         if (! $amode || ! $genpwd) {
-	    $r->print($error.'Invalid login mode or password'.$end);    
+	    $r->print($error.'Invalid login mode or password'.$end.$rtnlink);    
 	    return;
 	}
 	# Only allow authentification modification if the person has authority
@@ -1599,7 +1636,7 @@
 		  ($env{'form.ccuname'},$env{'form.ccdomain'}));
 	} else {
 	    # Okay, this is a non-fatal error.
-	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.');    
+	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.'.$end);    
 	}
     }
     ##
@@ -1766,6 +1803,7 @@
     }
     ##
     my $now=time;
+    my $rolechanges = 0;
     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
     foreach my $key (keys (%env)) {
 	next if (! $env{$key});
@@ -1795,6 +1833,7 @@
 				  $env{'form.ccuname'},$1,$2,$3,$4).
 		'</b><br />');
 	    }
+            $rolechanges ++;
 	} elsif ($key=~/^form\.del/) {
 	    if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) {
 # Delete standard role
@@ -1820,6 +1859,7 @@
                          $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now,
                          0,1).'</b><br />');
             }
+            $rolechanges ++;
 	} elsif ($key=~/^form\.ren/) {
             my $udom = $env{'form.ccdomain'};
             my $uname = $env{'form.ccuname'};
@@ -1858,6 +1898,7 @@
                 $r->print(&mt('Re-enabling custom role [_1] by [_2]@[_3] in [_4] : <b>[_5]</b>',
                           $rolename,$rnam,$rdom,$url,$result).'<br />');
             }
+            $rolechanges ++;
 	} elsif ($key=~/^form\.act/) {
             my $udom = $env{'form.ccdomain'};
             my $uname = $env{'form.ccuname'};
@@ -1971,10 +2012,14 @@
                 }
                 $r->print(' '.&mt('Please <a href="javascript:history.go(-1)">go back</a> and choose a different section name.').'</p><br />');
             }
+            $rolechanges ++;
 	}
     } # End of foreach (keys(%env))
 # Flush the course logs so reverse user roles immediately updated
     &Apache::lonnet::flushcourselogs();
+    if (!$rolechanges) {
+        $r->print(&mt('No roles to modify'));
+    }
     $r->print(&Apache::loncommon::end_page());
 }
 
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.611 loncom/interface/loncommon.pm:1.612
--- loncom/interface/loncommon.pm:1.611	Fri Nov  9 18:05:47 2007
+++ loncom/interface/loncommon.pm	Fri Nov  9 22:51:46 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.611 2007/11/09 23:05:47 albertel Exp $
+# $Id: loncommon.pm,v 1.612 2007/11/10 03:51:46 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -6430,57 +6430,127 @@
     return $output;
 }
 
-sub username_rule_check {
-    my ($srch,$caller) = @_;
-    my ($response,@curr_rules,%inst_results,$rulematch);
-    my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($srch->{'srchdomain'});
-    if (ref($srch) eq 'HASH') {
-        (my $inst_response,%inst_results) = 
-            &Apache::lonnet::get_instuser($srch->{'srchdomain'},
-                                          $srch->{'srchterm'});
-        my %domconfig = &Apache::lonnet::get_dom('configuration',
-                              ['usercreation'],$srch->{'srchdomain'});
-        if (ref($domconfig{'usercreation'}) eq 'HASH') {
-            if (ref($domconfig{'usercreation'}{'username_rule'}) eq 'ARRAY') {
-                @curr_rules = @{$domconfig{'usercreation'}{'username_rule'}};
-            }
-        }
-        if (@curr_rules > 0) {
-            my $domdesc = &Apache::lonnet::domain($srch->{'srchdomain'},'description');
-            my $instuser_reqd;
-            my %rule_check = &Apache::lonnet::inst_rulecheck($srch->{'srchdomain'},$srch->{'srchterm'},\@curr_rules);
-            foreach my $rule (@curr_rules) {
-                if ($rule_check{$rule}) {
-                    $rulematch = $rule;
-                    if ($inst_response eq 'ok') {
-                        if (keys(%inst_results) == 0) {
-                            if ($caller eq 'new') {
-                                $response = &mt('The username you chose matches the format of usernames defined for <span class="LC_cusr_emph">[_1]</span>, but the user does not exist in the institutional directory.',$domdesc).'<br />'.&mt("You must choose a username with a different format -- one that will not conflict with 'official' institutional usernames.");
-                            }
+sub user_rule_check {
+    my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules) = @_;
+    my $response;
+    if (ref($usershash) eq 'HASH') {
+        my %got_rules;
+        foreach my $user (keys(%{$usershash})) {
+            my ($uname,$udom) = split(/:/,$user);
+            next if ($udom eq '' || $uname eq '');
+            my ($userstatus,$id);
+            if (ref($usershash->{$user}) eq 'HASH') {
+                $userstatus = $usershash->{$user}->{'status'};
+                $id = $usershash->{$user}->{'id'};
+            }
+            my $inst_response;
+            if (ref($checks) eq 'HASH') {
+                if (defined($checks->{'username'})) {
+                    ($inst_response,%{$inst_results}) = 
+                        &Apache::lonnet::get_instuser($udom,$uname);
+                } elsif (defined($checks->{'id'})) {
+                    ($inst_response,%{$inst_results}) =
+                        &Apache::lonnet::get_instuser($udom,undef,$id);
+                }
+            }
+            if (!$got_rules{$udom}) {
+                my %domconfig = &Apache::lonnet::get_dom('configuration',
+                                                  ['usercreation'],$udom);
+                if (ref($domconfig{'usercreation'}) eq 'HASH') {
+                    foreach my $item (keys(%{$checks})) {
+                        if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
+                            $$curr_rules{$udom}{$item} = 
+                                $domconfig{'usercreation'}{$item.'_rule'};
                         }
                     }
-                    last;
                 }
+                $got_rules{$udom} = 1;
             }
-            if ($response) {
-                if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
-                    if (@{$ruleorder} > 0) {
-                        $response .= '<br />'.&mt('Usernames with the following format(s) may <span class="LC_cusr_emph">only</span> be used for verified users at [_1]:',$domdesc).' <ul>';
-                        foreach my $rule (@{$ruleorder}) {
-                            if (grep(/^\Q$rule\E$/,@curr_rules)) {
-                                if (ref($rules->{$rule}) eq 'HASH') {
-                                    $response .= '<li>'.$rules->{$rule}{'name'}.': '.
-                                                 $rules->{$rule}{'desc'}.'</li>';
+            foreach my $item (keys(%{$checks})) {
+                if (ref($$curr_rules{$udom}) eq 'HASH') {
+                    if (ref($$curr_rules{$udom}{$item}) eq 'ARRAY') {
+                        if (@{$$curr_rules{$udom}{$item}} > 0) {
+                            my %rule_check;
+                            my %rule_check = &Apache::lonnet::inst_rulecheck($udom,$uname,$id,$item,$$curr_rules{$udom}{$item});
+                            foreach my $rule (@{$$curr_rules{$udom}{$item}}) {
+                                if ($rule_check{$rule}) {
+                                    $$rulematch{$user}{$item} = $rule;
+                                    if ($inst_response eq 'ok') {
+                                        if (keys(%{$inst_results}) == 0) {
+                                            if ($userstatus eq 'new') {
+                                                $$alerts{$user}{$item} = 1;
+                                            }
+                                        }
+                                    }  
                                 }
                             }
+                            last;
                         }
                     }
-                    $response .= '</ul>';
                 }
             }
         }
     }
-    return ($response,$rulematch,$rules,%inst_results);
+    return;
+}
+
+sub user_rule_formats {
+    my ($domain,$domdesc,$curr_rules,$check) = @_;
+    my %text = ( 
+                 'username' => 'Usernames',
+                 'id'       => 'IDs',
+               );
+    my $output;
+    my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check);
+    if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
+        if (@{$ruleorder} > 0) {
+            $output = '<br />'.&mt("$text{$check} with the following format(s) may <span class=\"LC_cusr_emph\">only</span> be used for verified users at [_1]:",$domdesc).' <ul>';
+            foreach my $rule (@{$ruleorder}) {
+                if (ref($curr_rules) eq 'ARRAY') {
+                    if (grep(/^\Q$rule\E$/,@{$curr_rules})) {
+                        if (ref($rules->{$rule}) eq 'HASH') {
+                            $output .= '<li>'.$rules->{$rule}{'name'}.': '.
+                                        $rules->{$rule}{'desc'}.'</li>';
+                        }
+                    }
+                }
+            }
+            $output .= '</ul>';
+        }
+    }
+    return $output;
+}
+
+sub instrule_disallow_msg {
+    my ($checkitem,$domdesc,$count) = @_;
+    my $response;
+    my %text = (
+                  item   => 'username',
+                  items  => 'usernames',
+                  match  => 'matches',
+                  do     => 'does',
+                  action => 'a username',
+                  one    => 'one',
+               );
+    if ($count > 1) {
+        $text{'item'} = 'usernames';
+        $text{'match'} ='match';
+        $text{'do'} = 'do';
+        $text{'action'} = 'usernames',
+        $text{'one'} = 'ones';
+    }
+    if ($checkitem eq 'id') {
+        $text{'items'} = 'IDs';
+        $text{'item'} = 'ID';
+        $text{'action'} = 'an ID';
+    }
+    $response = &mt("The $text{'item'} you chose $text{'match'} the format of $text{'items'} defined for <span class=\"LC_cusr_emph\">[_1]</span>, but the $text{'item'} $text{'do'} not exist in the institutional directory.",$domdesc).'<br />';
+    if ($checkitem eq 'username') {
+        $response .= &mt("You must choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}.");
+    } elsif ($checkitem eq 'id') {
+       $response .= &mt("You must either choose $text{'action'} with a different format --  $text{'one'} that will not conflict with 'official' institutional $text{'items'}, or leave this field blank.");
+    }
+    return $response;
 }
 
 =pod

--raeburn1194666709--