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

matthew lon-capa-cvs@mail.lon-capa.org
Tue, 12 Feb 2002 21:42:18 -0000


This is a MIME encoded message

--matthew1013550138
Content-Type: text/plain

matthew		Tue Feb 12 16:42:18 2002 EDT

  Modified files:              
    /loncom/interface	loncreateuser.pm 
  Log:
  Reworked &phase_three, cleaned up logic mostly.  Did a little to make it more
  intelligent.  Added check for MAU permissions before changing user permissions
  (it is a superfluous check, but I'll sleep a little easier with this in the
  code).  Fixed bug introduced in the last commit which reversed the logic on
  whether or not the user was able to revoke roles.  Many cleanups to indentation
  and a few added comments.
  
  
--matthew1013550138
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20020212164218.txt"

Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.26 loncom/interface/loncreateuser.pm:1.27
--- loncom/interface/loncreateuser.pm:1.26	Mon Feb 11 16:25:07 2002
+++ loncom/interface/loncreateuser.pm	Tue Feb 12 16:42:18 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.26 2002/02/11 21:25:07 matthew Exp $
+# $Id: loncreateuser.pm,v 1.27 2002/02/12 21:42:18 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.26 2002/02/11 21:25:07 matthew Exp $
+# $Id: loncreateuser.pm,v 1.27 2002/02/12 21:42:18 matthew Exp $
 ###
 
 package Apache::loncreateuser;
@@ -70,7 +70,7 @@
     $krbdefdom=~tr/a-z/A-Z/;
     $authformnop=(<<END);
 <p>
-<input type=radio name=login value=nop checked='checked'
+<input type="radio" name="login" value="" checked="checked"
 onClick="clicknop(this.form);">
 Do not change login data
 </p>
@@ -344,6 +344,7 @@
 		 }
                  $area=$carea;
 	      } else {
+		 # Determine if current user is able to revoke privileges
                  if ($area=~/^\/(\w+)\//) {
                      if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
 			 $allows=1;
@@ -358,8 +359,9 @@
               $r->print('<tr bgcolor=#"'.$bgcol.'"><td>');
               my $active=1;
               if (($role_end_time) && ($now>$role_end_time)) { $active=0; }
-              if (!($active) && ($allows)) {
-		  $r->print('<input type=checkbox name="rev:'.$thisrole.'">');
+              if (($active) && ($allows)) {
+		  $r->print('<input type="checkbox" name="rev:'
+			    .$thisrole.'">');
               } else {
                   $r->print('&nbsp;');
               }
@@ -543,150 +545,179 @@
 # ================================================================= Phase Three
 sub phase_three {
     my $r=shift;
+    # Error messages
+    my $error     = '<font color="#ff0000">Error:</font>';
+    my $end       = '</body></html>';
+    # Print header
     $r->print(<<ENDTHREEHEAD);
 <html>
 <head>
 <title>The LearningOnline Network with CAPA</title>
 </head>
 <body bgcolor="#FFFFFF">
-<img align=right src=/adm/lonIcons/lonlogos.gif>
-<h1>Create User, Change User Privileges</h1>
+<img align="right" src="/adm/lonIcons/lonlogos.gif">
 ENDTHREEHEAD
-   $r->print('<h2>'.$ENV{'form.cuname'}.' at '.$ENV{'form.cdomain'}.'</h2>');
-   if ($ENV{'form.makeuser'}) {
-    $r->print('<h3>Creating User</h3>');
-    if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
-        ($ENV{'form.cdomain'})&&($ENV{'form.cdomain'}!~/\W/)) {
-	my $amode='';
-        my $genpwd='';
-        if ($ENV{'form.login'} eq 'krb') {
-           $amode='krb4';
-           $genpwd=$ENV{'form.krbdom'};
-        } elsif ($ENV{'form.login'} eq 'int') {
-           $amode='internal';
-           $genpwd=$ENV{'form.intpwd'};
-        } elsif ($ENV{'form.login'} eq 'fsys') {
-           $amode='unix';
-           $genpwd=$ENV{'form.fsyspwd'};
-        } elsif ($ENV{'form.login'} eq 'loc') {
-	    $amode='localauth';
-	    $genpwd=$ENV{'form.locarg'};
-	    if (!$genpwd) { $genpwd=" "; }
+    # Check Inputs
+    if (! $ENV{'form.cuname'} ) {
+	$r->print($error.'No login name specified.'.$end);
+	return;
+    }
+    if (  $ENV{'form.cuname'}  =~/\W/) {
+	$r->print($error.'Invalid login name.  '.
+		  'Only letters, numbers, and underscores are valid.'.
+		  $end);
+	return;
+    }
+    if (! $ENV{'form.cdomain'}       ) {
+	$r->print($error.'No domain specified.'.$end);
+	return;
+    }
+    if (  $ENV{'form.cdomain'} =~/\W/) {
+	$r->print($error.'Invalid domain name.  '.
+		  'Only letters, numbers, and underscores are valid.'.
+		  $end);
+	return;
+    }
+    # Determine authentication method and password for the user being modified
+    my $amode='';
+    my $genpwd='';
+    if ($ENV{'form.login'} eq 'krb') {
+	$amode='krb4';
+	$genpwd=$ENV{'form.krbdom'};
+    } elsif ($ENV{'form.login'} eq 'int') {
+	$amode='internal';
+	$genpwd=$ENV{'form.intpwd'};
+    } elsif ($ENV{'form.login'} eq 'fsys') {
+	$amode='unix';
+	$genpwd=$ENV{'form.fsyspwd'};
+    } elsif ($ENV{'form.login'} eq 'loc') {
+	$amode='localauth';
+	$genpwd=$ENV{'form.locarg'};
+	$genpwd=" " if (!$genpwd);
+    }
+    if ($ENV{'form.makeuser'}) {
+        # Create a new user
+	$r->print(<<ENDNEWUSERHEAD);
+<h1>Create User</h1>
+<h3>Creating user "$ENV{'form.cuname'}" in domain "$ENV{'form.cdomain'}"</h2>
+ENDNEWUSERHEAD
+        # Check for the authentication mode and password
+        if (! $amode || ! $genpwd) {
+	    $r->print($error.'Invalid login mode or password'.$end);    
+	    return;
 	}
-        if (($amode) && ($genpwd)) {
-          $r->print('Generating user: '.&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'}));
-          $r->print('<br>Home server: '.&Apache::lonnet::homeserver
-                      ($ENV{'form.cuname'},$ENV{'form.cdomain'}));
-
-	} else {
-           $r->print('Invalid login mode or password');    
-        }          
-    } else {
-        $r->print('Invalid username or domain');
-    }
-   }
-   if (!$ENV{'form.makeuser'} and $ENV{'form.login'} ne 'nop') {
-    $r->print('<h3>Changing User Login Data</h3>');
-    if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
-        ($ENV{'form.cdomain'})&&($ENV{'form.cdomain'}!~/\W/)) {
-	my $amode='';
-        my $genpwd='';
-        if ($ENV{'form.login'} eq 'krb') {
-           $amode='krb4';
-           $genpwd=$ENV{'form.krbdom'};
-        } elsif ($ENV{'form.login'} eq 'int') {
-           $amode='internal';
-           $genpwd=$ENV{'form.intpwd'};
-        } elsif ($ENV{'form.login'} eq 'fsys') {
-           $amode='unix';
-           $genpwd=$ENV{'form.fsyspwd'};
-        } elsif ($ENV{'form.login'} eq 'loc') {
-	    $amode='localauth';
-	    $genpwd=$ENV{'form.locarg'};
-	    if (!$genpwd) { $genpwd=" "; }
+	# 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'}
+	     );
+	$r->print('Generating user: '.$result);
+	$r->print('<br>Home server: '.&Apache::lonnet::homeserver
+		  ($ENV{'form.cuname'},$ENV{'form.cdomain'}));
+    } 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>
+ENDMODIFYUSERHEAD
+        if (! $amode || ! $genpwd) {
+	    $r->print($error.'Invalid login mode or password'.$end);    
+	    return;
 	}
-        if (($amode) && ($genpwd)) {
+	# Only allow authentification modification if the person has authority
+	if (&Apache::lonnet::allowed('mau',$ENV{'user.domain'})) {
 	    $r->print('Modifying authentication: '.
-		 &Apache::lonnet::modifyuserauth(
+		  &Apache::lonnet::modifyuserauth(
 		       $ENV{'form.cdomain'},$ENV{'form.cuname'},
                        $amode,$genpwd));
             $r->print('<br>Home server: '.&Apache::lonnet::homeserver
-                      ($ENV{'form.cuname'},$ENV{'form.cdomain'}));
-
+		  ($ENV{'form.cuname'},$ENV{'form.cdomain'}));
 	} else {
-           $r->print('Invalid login mode or password');    
-        }          
-    } else {
-        $r->print('Invalid username or domain');
+	    # Okay, this is a non-fatal error.
+	    $r->print($error.'You do not have the authority to modify '.
+		      'this users authentification information.');    
+	}
     }
-   }
+    ##
     my $now=time;
     $r->print('<h3>Modifying Roles</h3>');
     foreach (keys (%ENV)) {
-	if (($_=~/^form\.rev\:([^\_]+)\_([^\_]+)$/) && ($ENV{$_})) {
-           $r->print('Revoking '.$2.' in '.$1.': '.
-          &Apache::lonnet::assignrole($ENV{'form.cdomain'},$ENV{'form.cuname'},
-                                      $1,$2,$now).'<br>');
-           if ($2 eq 'st') {
-               $1=~/^\/(\w+)\/(\w+)/;
-               my $cid=$1.'_'.$2;
-	       $r->print('Drop from classlist: '.
-          &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($now.':'),
-	              $ENV{'course.'.$cid.'.home'}).'<br>');
-           }
-	}
-    } 
-    foreach (keys(%ENV)) {
-	if (($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_([^\_]+)$/) && ($ENV{$_})) {
-            my $url='/'.$1.'/'.$2;
-            if ($ENV{'form.sec_'.$1.'_'.$2.'_'.$3}) {
-		$url.='/'.$ENV{'form.sec_'.$1.'_'.$2.'_'.$3};
-            }
-            my $start=$now;
-            if ($ENV{'form.start_'.$1.'_'.$2.'_'.$3}) {
-		$start=$ENV{'form.start_'.$1.'_'.$2.'_'.$3};
-            }
-            my $end=0;
-            if ($ENV{'form.end_'.$1.'_'.$2.'_'.$3}) {
-		$end=$ENV{'form.end_'.$1.'_'.$2.'_'.$3};
-            }
-            $r->print('Assigning: '.$3.' in '.$url.': '.
-          &Apache::lonnet::assignrole($ENV{'form.cdomain'},$ENV{'form.cuname'},
-                                      $url,$3,$end,$start).'<br>');
-            if ($3 eq 'st') {
-		$url=~/^\/(\w+)\/(\w+)/;
-                my $cid=$1.'_'.$2;
-               $r->print('Add to classlist: '.
-          &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($end.':'.$start),
-	              $ENV{'course.'.$cid.'.home'}).'<br>');
+	next if (! $ENV{$_});
+	# Revoke roles
+	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>');
+		if ($2 eq 'st') {
+		    $1=~/^\/(\w+)\/(\w+)/;
+		    my $cid=$1.'_'.$2;
+		    $r->print('Drop from classlist: '.
+			 &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($now.':'),
+	                     $ENV{'course.'.$cid.'.home'}).'<br>');
+		}
+	    } 
+	} elsif ($_=~/^form\.act/) {
+	    if ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_([^\_]+)$/) {
+		# Activate roles for sections with 3 id numbers
+		# set start, end times, and the url for the class
+		my $start = ( $ENV{'form.start_'.$1.'_'.$2} ? 
+			      $ENV{'form.start_'.$1.'_'.$2} : 
+			      $now );
+		my $end   = ( $ENV{'form.end_'.$1.'_'.$2} ? 
+			      $ENV{'form.end_'.$1.'_'.$2} :
+			      0 );
+		my $url='/'.$1.'/'.$2;
+		if ($ENV{'form.sec_'.$1.'_'.$2.'_'.$3}) {
+		    $url.='/'.$ENV{'form.sec_'.$1.'_'.$2.'_'.$3};
+		}
+		# Assign the role and report it
+		$r->print('Assigning: '.$3.' in '.$url.': '.
+                          &Apache::lonnet::assignrole(
+                              $ENV{'form.cdomain'},$ENV{'form.cuname'},
+                              $url,$3,$end,$start).
+			  '<br>');
+		# Handle students differently
+		if ($3 eq 'st') {
+		    $url=~/^\/(\w+)\/(\w+)/;
+		    my $cid=$1.'_'.$2;
+		    $r->print('Add to classlist: '.
+			      &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($end.':'.$start),
+				       $ENV{'course.'.$cid.'.home'})
+			      .'<br>');
+		}
+	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
+		# Activate roles for sections with two id numbers
+		# set start, end times, and the url for the class
+		my $start = ( $ENV{'form.start_'.$1.'_'.$2} ? 
+			      $ENV{'form.start_'.$1.'_'.$2} : 
+			      $now );
+		my $end   = ( $ENV{'form.end_'.$1.'_'.$2} ? 
+			      $ENV{'form.end_'.$1.'_'.$2} :
+			      0 );
+		my $url='/'.$1.'/';
+		# Assign the role and report it.
+		$r->print('Assigning: '.$2.' in '.$url.': '.
+                          &Apache::lonnet::assignrole(
+                              $ENV{'form.cdomain'},$ENV{'form.cuname'},
+                              $url,$2,$end,$start)
+			  .'<br>');
 	    }
-	} elsif (($_=~/^form\.act\_([^\_]+)\_([^\_]+)$/) && ($ENV{$_})) {
-            my $url='/'.$1.'/';
-            my $start=$now;
-            if ($ENV{'form.start_'.$1.'_'.$2}) {
-		$start=$ENV{'form.start_'.$1.'_'.$2};
-            }
-            my $end=0;
-            if ($ENV{'form.end_'.$1.'_'.$2}) {
-		$end=$ENV{'form.end_'.$1.'_'.$2};
-            }
-            $r->print('Assigning: '.$2.' in '.$url.': '.
-          &Apache::lonnet::assignrole($ENV{'form.cdomain'},$ENV{'form.cuname'},
-                                      $url,$2,$end,$start).'<br>');
-        }
-    }
+	} 
+    } # End of foreach (keys(%ENV))
     $r->print('</body></html>');
 }
 
@@ -724,7 +755,7 @@
    return OK;
 } 
 
-
+#-------------------------------------------------- functions for &phase_two
 sub course_level_table {
     my %inccourses = @_;
     my $table = '';
@@ -778,6 +809,7 @@
 ENDTABLE
     return $result;
 }
+#---------------------------------------------- end functions for &phase_two
 
 1;
 __END__

--matthew1013550138--