[LON-CAPA-cvs] cvs: loncom /interface loncreateuser.pm

matthew lon-capa-cvs@mail.lon-capa.org
Thu, 04 Apr 2002 21:46:44 -0000


This is a MIME encoded message

--matthew1017956804
Content-Type: text/plain

matthew		Thu Apr  4 16:46:44 2002 EDT

  Modified files:              
    /loncom/interface	loncreateuser.pm 
  Log:
  Allow specification of server for new users.  This is somewhat experimental
  and has not been fully tested.  Checks are not in place to disallow creation
  of new users by existing users who should not be able to.
  
  
--matthew1017956804
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20020404164644.txt"

Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.28 loncom/interface/loncreateuser.pm:1.29
--- loncom/interface/loncreateuser.pm:1.28	Fri Mar 22 17:23:23 2002
+++ loncom/interface/loncreateuser.pm	Thu Apr  4 16:46:44 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.28 2002/03/22 22:23:23 matthew Exp $
+# $Id: loncreateuser.pm,v 1.29 2002/04/04 21:46:44 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -47,7 +47,7 @@
 # 11/12,11/13,11/15 Scott Harrison
 # 02/11/02 Matthew Hall
 #
-# $Id: loncreateuser.pm,v 1.28 2002/03/22 22:23:23 matthew Exp $
+# $Id: loncreateuser.pm,v 1.29 2002/04/04 21:46:44 matthew Exp $
 ###
 
 package Apache::loncreateuser;
@@ -260,12 +260,11 @@
 <input type="hidden" name="pres_value"  value="" >
 <input type="hidden" name="pres_type"   value="" >
 <input type="hidden" name="pres_marker" value="" >
-<input type="hidden" name="cuname"      value="$ccuname">
-<input type="hidden" name="cdomain"     value="$ccdomain">
 ENDFORMINFO
     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
     my %incdomains; 
     my %inccourses;
+    my %home_servers = &get_home_servers($ccdomain);  
     foreach (%Apache::lonnet::hostdom) {
        $incdomains{$_}=1;
     }
@@ -275,6 +274,13 @@
         }
     }
     if ($uhome eq 'no_host') {
+        my $home_server_list=
+            '<option value="default" selected>default</option>'."\n";
+        foreach (sort keys(%home_servers)) {
+            $home_server_list.=
+                '<option value="'.$_.'">'.$_.' '.
+                    $home_servers{$_}."</option>\n";
+        }
 	$r->print(<<ENDNEWUSER);
 $dochead
 <h1>Create New User</h1>
@@ -295,6 +301,7 @@
     <td><input type='text' name='cgen'    size='5'  /></td></tr>
 </table>
 ID/Student Number <input type='text' name='cstid'   size='15' /></p>
+Home Server: <select name="hserver" size="1"> $home_server_list </select>
 <hr />
 <h3>Login Data</h3>
 $generalrule
@@ -570,6 +577,8 @@
 # ================================================================= Phase Three
 sub phase_three {
     my $r=shift;
+    my $uhome=&Apache::lonnet::homeserver($ENV{'form.ccuname'},
+                                          $ENV{'form.ccdomain'});
     # Error messages
     my $error     = '<font color="#ff0000">Error:</font>';
     my $end       = '</body></html>';
@@ -583,26 +592,35 @@
 <img align="right" src="/adm/lonIcons/lonlogos.gif">
 ENDTHREEHEAD
     # Check Inputs
-    if (! $ENV{'form.cuname'} ) {
+    if (! $ENV{'form.ccuname'} ) {
 	$r->print($error.'No login name specified.'.$end);
 	return;
     }
-    if (  $ENV{'form.cuname'}  =~/\W/) {
+    if (  $ENV{'form.ccuname'}  =~/\W/) {
 	$r->print($error.'Invalid login name.  '.
 		  'Only letters, numbers, and underscores are valid.'.
 		  $end);
 	return;
     }
-    if (! $ENV{'form.cdomain'}       ) {
+    if (! $ENV{'form.ccdomain'}       ) {
 	$r->print($error.'No domain specified.'.$end);
 	return;
     }
-    if (  $ENV{'form.cdomain'} =~/\W/) {
+    if (  $ENV{'form.ccdomain'} =~/\W/) {
 	$r->print($error.'Invalid domain name.  '.
 		  'Only letters, numbers, and underscores are valid.'.
 		  $end);
 	return;
     }
+    if (! exists($ENV{'form.makeuser'})) {
+        # Modifying an existing user, so check the validity of the name
+        if ($uhome eq 'no_host') {
+            $r->print($error.'Unable to determine home server for '.
+                      $ENV{'form.ccuname'}.' in domain '.
+                      $ENV{'form.ccdomain'}.'.');
+            return;
+        }
+    }
     # Determine authentication method and password for the user being modified
     my $amode='';
     my $genpwd='';
@@ -624,28 +642,41 @@
         # Create a new user
 	$r->print(<<ENDNEWUSERHEAD);
 <h1>Create User</h1>
-<h3>Creating user "$ENV{'form.cuname'}" in domain "$ENV{'form.cdomain'}"</h2>
+<h3>Creating user "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
 ENDNEWUSERHEAD
         # Check for the authentication mode and password
         if (! $amode || ! $genpwd) {
 	    $r->print($error.'Invalid login mode or password'.$end);    
 	    return;
 	}
+        # Determine desired host
+        my $desiredhost = $ENV{'form.hserver'};
+        if (lc($desiredhost) eq 'default') {
+            $desiredhost = undef;
+        } else {
+            my %home_servers = &get_home_servers($ENV{'form.ccdomain'});  
+            if (! exists($home_servers{$desiredhost})) {
+                $r->print($error.'Invalid home server specified');
+                return;
+            }
+        }
 	# Call modifyuser
 	my $result = &Apache::lonnet::modifyuser
-	    ($ENV{'form.cdomain'},$ENV{'form.cuname'},
-	     $ENV{'form.cstid'},$amode,$genpwd,
-	     $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
-	     $ENV{'form.clast'},$ENV{'form.cgen'}
+	    ($ENV{'form.ccdomain'},$ENV{'form.ccuname'},$ENV{'form.cstid'},
+             $amode,$genpwd,$ENV{'form.cfirst'},
+             $ENV{'form.cmiddle'},$ENV{'form.clast'},$ENV{'form.cgen'},
+             undef,$desiredhost
 	     );
 	$r->print('Generating user: '.$result);
-	$r->print('<br>Home server: '.&Apache::lonnet::homeserver
-		  ($ENV{'form.cuname'},$ENV{'form.cdomain'}));
+        my $home = &Apache::lonnet::homeserver($ENV{'form.ccuname'},
+                                               $ENV{'form.ccdomain'});
+        $r->print('<br>Home server: '.$home.' '.
+                  $Apache::lonnet::libserv{$home});
     } elsif ($ENV{'form.login'} ne '') {
 	# Modify user privileges
 	$r->print(<<ENDMODIFYUSERHEAD);
 <h1>Change User Privileges</h1>
-<h2>User "$ENV{'form.cuname'}" in domain "$ENV{'form.cdomain'}"</h2>
+<h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
 ENDMODIFYUSERHEAD
         if (! $amode || ! $genpwd) {
 	    $r->print($error.'Invalid login mode or password'.$end);    
@@ -655,10 +686,10 @@
 	if (&Apache::lonnet::allowed('mau',$ENV{'user.domain'})) {
 	    $r->print('Modifying authentication: '.
 		  &Apache::lonnet::modifyuserauth(
-		       $ENV{'form.cdomain'},$ENV{'form.cuname'},
+		       $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
                        $amode,$genpwd));
             $r->print('<br>Home server: '.&Apache::lonnet::homeserver
-		  ($ENV{'form.cuname'},$ENV{'form.cdomain'}));
+		  ($ENV{'form.ccuname'},$ENV{'form.ccdomain'}));
 	} else {
 	    # Okay, this is a non-fatal error.
 	    $r->print($error.'You do not have the authority to modify '.
@@ -670,7 +701,7 @@
         # Check for need to change
         my %userenv = &Apache::lonnet::get
             ('environment',['firstname','middlename','lastname','generation'],
-             $ENV{'form.cdomain'},$ENV{'form.cuname'});
+             $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
         my ($tmp) = keys(%userenv);
         if ($tmp =~ /^(con_lost|error)/i) { 
             %userenv = ();
@@ -680,7 +711,7 @@
             # Strip leading and trailing whitespace
             $ENV{'form.c'.$_} =~ s/(\s+$|^\s+)//g; 
         }
-        if (&Apache::lonnet::allowed('mau',$ENV{'form.cdomain'}) && 
+        if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'}) && 
             ($ENV{'form.cfirstname'}  ne $userenv{'firstname'}  ||
              $ENV{'form.cmiddlename'} ne $userenv{'middlename'} ||
              $ENV{'form.clastname'}   ne $userenv{'lastname'}   ||
@@ -693,7 +724,7 @@
             $changeHash{'generation'} = $ENV{'form.cgeneration'};
             my $putresult = &Apache::lonnet::put
                 ('environment',\%changeHash,
-                 $ENV{'form.cdomain'},$ENV{'form.cuname'});
+                 $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
             if ($putresult eq 'ok') {
             # Tell the user we changed the name
                 $r->print(<<"END");
@@ -718,14 +749,14 @@
 END
             } else { # error occurred
                 $r->print("<h2>Unable to successfully change environment for ".
-                      $ENV{'form.cuname'}." in domain ".
-                      $ENV{'form.cdomain'}."</h2>");
+                      $ENV{'form.ccuname'}." in domain ".
+                      $ENV{'form.ccdomain'}."</h2>");
             }
         }  else { # End of if ($ENV ... ) logic
             # They did not want to change the users name but we can
             # still tell them what the name is
                 $r->print(<<"END");
-<h2>User "$ENV{'form.cuname'}" in domain "$ENV{'form.cdomain'}"</h2>
+<h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
 <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} </h4>
 <h4>Generation: $userenv{'generation'}</h4>
 END
@@ -740,8 +771,8 @@
 	if ($_=~/^form\.rev/) {
 	    if ($_=~/^form\.rev\:([^\_]+)\_([^\_]+)$/) {
 	        $r->print('Revoking '.$2.' in '.$1.': '.
-                     &Apache::lonnet::assignrole($ENV{'form.cdomain'},
-                     $ENV{'form.cuname'},$1,$2,$now).'<br>');
+                     &Apache::lonnet::assignrole($ENV{'form.ccdomain'},
+                     $ENV{'form.ccuname'},$1,$2,$now).'<br>');
 		if ($2 eq 'st') {
 		    $1=~/^\/(\w+)\/(\w+)/;
 		    my $cid=$1.'_'.$2;
@@ -749,8 +780,8 @@
 			 &Apache::lonnet::critical('put:'.
                              $ENV{'course.'.$cid.'.domain'}.':'.
 	                     $ENV{'course.'.$cid.'.num'}.':classlist:'.
-                         &Apache::lonnet::escape($ENV{'form.cuname'}.':'.
-                             $ENV{'form.cdomain'}).'='.
+                         &Apache::lonnet::escape($ENV{'form.ccuname'}.':'.
+                             $ENV{'form.ccdomain'}).'='.
                          &Apache::lonnet::escape($now.':'),
 	                     $ENV{'course.'.$cid.'.home'}).'<br>');
 		}
@@ -772,7 +803,7 @@
 		# Assign the role and report it
 		$r->print('Assigning: '.$3.' in '.$url.': '.
                           &Apache::lonnet::assignrole(
-                              $ENV{'form.cdomain'},$ENV{'form.cuname'},
+                              $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
                               $url,$3,$end,$start).
 			  '<br>');
 		# Handle students differently
@@ -784,8 +815,8 @@
 				  'put:'.$ENV{'course.'.$cid.'.domain'}.':'.
 	                           $ENV{'course.'.$cid.'.num'}.':classlist:'.
                                    &Apache::lonnet::escape(
-                                       $ENV{'form.cuname'}.':'.
-                                       $ENV{'form.cdomain'} ).'='.
+                                       $ENV{'form.ccuname'}.':'.
+                                       $ENV{'form.ccdomain'} ).'='.
                                    &Apache::lonnet::escape($end.':'.$start),
 				       $ENV{'course.'.$cid.'.home'})
 			      .'<br>');
@@ -803,7 +834,7 @@
 		# Assign the role and report it.
 		$r->print('Assigning: '.$2.' in '.$url.': '.
                           &Apache::lonnet::assignrole(
-                              $ENV{'form.cdomain'},$ENV{'form.cuname'},
+                              $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
                               $url,$2,$end,$start)
 			  .'<br>');
 	    }
@@ -901,6 +932,20 @@
     return $result;
 }
 #---------------------------------------------- end functions for &phase_two
+
+#--------------------------------- functions for &phase_two and &phase_three
+sub get_home_servers {
+    my $domain = shift;
+    my %home_servers;
+    foreach (keys(%Apache::lonnet::libserv)) {
+        if ($Apache::lonnet::hostdom{$_} eq $domain) {
+            $home_servers{$_} = $Apache::lonnet::hostname{$_};
+        }
+    }
+    return %home_servers;
+}
+
+#--------------------------end of functions for &phase_two and &phase_three
 
 1;
 __END__

--matthew1017956804--