--- loncom/interface/loncreateuser.pm	2002/04/22 15:26:46	1.31
+++ loncom/interface/loncreateuser.pm	2002/08/21 17:18:08	1.40
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.31 2002/04/22 15:26:46 matthew Exp $
+# $Id: loncreateuser.pm,v 1.40 2002/08/21 17:18:08 www 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.31 2002/04/22 15:26:46 matthew Exp $
+# $Id: loncreateuser.pm,v 1.40 2002/08/21 17:18:08 www Exp $
 ###
 
 package Apache::loncreateuser;
@@ -85,17 +85,22 @@ BEGIN {
 sub phase_one {
     my $r=shift;
     my $defdom=$ENV{'user.domain'};
-    $r->print(<<ENDDOCUMENT);
+    my @domains = &Apache::loncommon::get_domains();
+    my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
+    my $bodytag =&Apache::loncommon::bodytag(
+                                  'Create Users, Change User Privileges');
+    $r->print(<<"ENDDOCUMENT");
 <html>
 <head>
 <title>The LearningOnline Network with CAPA</title>
 </head>
-<body bgcolor="#FFFFFF">
-<h1>Create User, Change User Privileges</h1>
+$bodytag
 <form action=/adm/createuser method=post>
 <input type=hidden name=phase value=two>
+<p>
 Username: <input type=text size=15 name=ccuname><br>
-Domain: <input type=text size=15 name=ccdomain value=$defdom><p>
+Domain: $domform 
+</p>
 <input type=submit value="Continue">
 </form>
 </body>
@@ -150,9 +155,9 @@ sub phase_two {
 
 </script>
 </head>
-<body bgcolor="#FFFFFF">
-<img align="right" src="/adm/lonIcons/lonlogos.gif">
 ENDDOCHEAD
+    $r->print(&Apache::loncommon::bodytag(
+                                     'Create Users, Change User Privileges'));
     my $forminfo =<<"ENDFORMINFO";
 <form action="/adm/createuser" method="post" name="cu">
 <input type="hidden" name="phase"       value="three">
@@ -165,7 +170,6 @@ 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;
     }
@@ -176,12 +180,9 @@ ENDFORMINFO
     }
     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";
-        }
+            '<option value="default" selected>default</option>'."\n".
+                &Apache::loncommon::home_server_option_list($ccdomain);
+        
 	$r->print(<<ENDNEWUSER);
 $dochead
 <h1>Create New User</h1>
@@ -250,65 +251,80 @@ END
         my ($tmp) = keys(%rolesdump);
         unless ($tmp =~ /^(con_lost|error)/i) {
            my $now=time;
-           $r->print('<hr /><h3>Revoke Existing Roles</h3>'.
-             '<table border=2><tr><th>Revoke</th><th>Role</th><th>Extent</th>'.
-	     '<th>Start</th><th>End</th>');
+           $r->print(<<END);
+<hr />
+<h3>Revoke Existing Roles</h3>
+<table border=2>
+<tr><th>Revoke</th><th>Role</th><th>Extent</th><th>Start</th><th>End</th>
+END
 	   foreach my $area (keys(%rolesdump)) {
-              if ($area!~/^rolesdef/) {
-                 my $role = $rolesdump{$area};
-                 my $thisrole=$area;
-                 $area=~s/\_\w\w$//;
-                 my ($role_code,$role_end_time,$role_start_time) =
-                     split(/_/,$role);
-                 my $bgcol='ffffff';
-                 my $allows=0;
-                 if ($area=~/^\/(\w+)\/(\d\w+)/) {
-                    my %coursedata=
-                        &Apache::lonnet::coursedescription($1.'_'.$2);
-                    my $carea='Course: '.$coursedata{'description'};
-                    $inccourses{$1.'_'.$2}=1;
-                    if (&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) {
-                        $allows=1;
-                    }
-                    # Compute the background color based on $area
-                    $bgcol=$1.'_'.$2;
-                    $bgcol=~s/[^8-9b-e]//g;
-                    $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',0,6);
-                    if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) {
+               next if ($area =~ /^rolesdef/);
+               my $role = $rolesdump{$area};
+               my $thisrole=$area;
+               $area =~ s/\_\w\w$//;
+               my ($role_code,$role_end_time,$role_start_time) = 
+                   split(/_/,$role);
+               my $bgcol='ffffff';
+               my $allowed=0;
+               if ($area =~ /^\/(\w+)\/(\d\w+)/ ) {
+                   my %coursedata=
+                       &Apache::lonnet::coursedescription($1.'_'.$2);
+                   my $carea='Course: '.$coursedata{'description'};
+                   $inccourses{$1.'_'.$2}=1;
+                   if (&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) {
+                       $allowed=1;
+                   }
+                   # Compute the background color based on $area
+                   $bgcol=$1.'_'.$2;
+                   $bgcol=~s/[^8-9b-e]//g;
+                   $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',0,6);
+                   if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) {
                        $carea.='<br>Section/Group: '.$3;
-                    }
-                    $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;
-                        }
-                     } else {
-                        if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
-                           $allows=1;
-                        }
-                     }
-                 }
-                 $r->print('<tr bgcolor=#"'.$bgcol.'"><td>');
-                 my $active=1;
-                 $active=0 if (($role_end_time) && ($now>$role_end_time));
-                 if (($active) && ($allows)) {
-                    $r->print('<input type="checkbox" name="rev:'
-                              .$thisrole.'">');
-                 } else {
-                    $r->print('&nbsp;');
-                 }
-                 $r->print('</td><td>'.
-                           &Apache::lonnet::plaintext($role_code).
-                           '</td><td>'.$area.'</td><td>'.
-                           ($role_start_time ? localtime($role_start_time)
-                                             : '&nbsp;' )
-                           .'</td><td>'.
-                           ($role_end_time   ? localtime($role_end_time)
-                                             : '&nbsp;' )
-                           ."</td></tr>\n");
-              }
+                   }
+                   $area=$carea;
+               } else {
+                   # Determine if current user is able to revoke privileges
+                   if ($area=~ /^\/(\w+)\//) {
+                       if (&Apache::lonnet::allowed('c'.$role_code,$1)) {
+                           $allowed=1;
+                       }
+                   } else {
+                       if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
+                           $allowed=1;
+                       }
+                   }
+               }
+               # I have no idea what the hell the above code does
+               # So the following is a check:
+               if ($allowed) {
+                   # If we are looking at a co-author role, make sure it is 
+                   # for the current users construction space before we let 
+                   # them revoke it.
+                   if (($role_code eq 'ca') && 
+                       ($ENV{'request.role'} !~ /^dc/)) {
+                       if ($area !~ 
+                           /^\/$ENV{'user.domain'}\/$ENV{'user.name'}/) {
+                           $allowed = 0;
+                       }
+                   }
+               }
+               my $row = '';
+               $row.='<tr bgcolor=#"'.$bgcol.'"><td>';
+               my $active=1;
+               $active=0 if (($role_end_time) && ($now>$role_end_time));
+               if (($active) && ($allowed)) {
+                   $row.= '<input type="checkbox" name="rev:'.$thisrole.'">';
+               } else {
+                   $row.='&nbsp;';
+               }
+               $row.= '</td><td>'.&Apache::lonnet::plaintext($role_code).
+                      '</td><td>'.$area.
+                      '</td><td>'.($role_start_time?localtime($role_start_time)
+                                                   : '&nbsp;' ).
+                      '</td><td>'.($role_end_time  ?localtime($role_end_time)
+                                                   : '&nbsp;' )
+                      ."</td></tr>\n";
+               $r->print($row);
            } # end of foreach        (table building loop)
 	   $r->print('</table>');
         }  # End of unless
@@ -483,9 +499,14 @@ sub phase_three {
 <head>
 <title>The LearningOnline Network with CAPA</title>
 </head>
-<body bgcolor="#FFFFFF">
-<img align="right" src="/adm/lonIcons/lonlogos.gif">
 ENDTHREEHEAD
+    my $title;
+    if (exists($ENV{'form.makeuser'})) {
+	$title='Set Privileges for New User';
+    } else {
+        $title='Modify User Privileges';
+    }
+    $r->print(&Apache::loncommon::bodytag($title));
     # Check Inputs
     if (! $ENV{'form.ccuname'} ) {
 	$r->print($error.'No login name specified.'.$end);
@@ -532,6 +553,12 @@ ENDTHREEHEAD
 	$amode='localauth';
 	$genpwd=$ENV{'form.locarg'};
 	$genpwd=" " if (!$genpwd);
+    } elsif (($ENV{'form.login'} eq 'nochange') ||
+             ($ENV{'form.login'} eq ''        )) { 
+        # There is no need to tell the user we did not change what they
+        # did not ask us to change.
+        # If they are creating a new user but have not specified login
+        # information this will be caught below.
     } else {
 	    $r->print($error.'Invalid login mode or password'.$end);    
 	    return;
@@ -539,7 +566,6 @@ ENDTHREEHEAD
     if ($ENV{'form.makeuser'}) {
         # Create a new user
 	$r->print(<<ENDNEWUSERHEAD);
-<h1>Create User</h1>
 <h3>Creating user "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
 ENDNEWUSERHEAD
         # Check for the authentication mode and password
@@ -552,7 +578,8 @@ ENDNEWUSERHEAD
         if (lc($desiredhost) eq 'default') {
             $desiredhost = undef;
         } else {
-            my %home_servers = &get_home_servers($ENV{'form.ccdomain'});  
+            my %home_servers = &Apache::loncommon::get_library_servers
+                ($ENV{'form.ccdomain'});  
             if (! exists($home_servers{$desiredhost})) {
                 $r->print($error.'Invalid home server specified');
                 return;
@@ -570,10 +597,10 @@ ENDNEWUSERHEAD
                                                $ENV{'form.ccdomain'});
         $r->print('<br>Home server: '.$home.' '.
                   $Apache::lonnet::libserv{$home});
-    } elsif ($ENV{'form.login'} ne '') {
+    } elsif (($ENV{'form.login'} ne 'nochange') &&
+             ($ENV{'form.login'} ne ''        )) {
 	# Modify user privileges
 	$r->print(<<ENDMODIFYUSERHEAD);
-<h1>Change User Privileges</h1>
 <h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
 ENDMODIFYUSERHEAD
         if (! $amode || ! $genpwd) {
@@ -581,7 +608,7 @@ ENDMODIFYUSERHEAD
 	    return;
 	}
 	# Only allow authentification modification if the person has authority
-	if (&Apache::lonnet::allowed('mau',$ENV{'user.domain'})) {
+	if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'})) {
 	    $r->print('Modifying authentication: '.
                       &Apache::lonnet::modifyuserauth(
 		       $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
@@ -832,16 +859,6 @@ ENDTABLE
 #---------------------------------------------- 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