File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.54: download - view: text, annotated - select for diffs
Wed Jun 4 19:48:19 2003 UTC (21 years ago) by bowersj2
Branches: MAIN
CVS tags: version_0_99_2, HEAD
loncreateuser uses routines from loncommon but did not 'use
Apache::loncommon'; didn't seem to bother Perl inside Apache but was
causing errors with loncreateuser when used outside of the server.

    1: # The LearningOnline Network with CAPA
    2: # Create a user
    3: #
    4: # $Id: loncreateuser.pm,v 1.54 2003/06/04 19:48:19 bowersj2 Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # (Create a course
   29: # (My Desk
   30: #
   31: # (Internal Server Error Handler
   32: #
   33: # (Login Screen
   34: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
   35: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
   36: #
   37: # YEAR=2001
   38: # 3/1/1 Gerd Kortemeyer)
   39: #
   40: # 3/1 Gerd Kortemeyer)
   41: #
   42: # 2/14 Gerd Kortemeyer)
   43: #
   44: # 2/14,2/17,2/19,2/20,2/21,2/22,2/23,3/2,3/17,3/24,04/12 Gerd Kortemeyer
   45: # April Guy Albertelli
   46: # 05/10,10/16 Gerd Kortemeyer 
   47: # 02/11/02 Matthew Hall
   48: #
   49: # $Id: loncreateuser.pm,v 1.54 2003/06/04 19:48:19 bowersj2 Exp $
   50: ###
   51: 
   52: package Apache::loncreateuser;
   53: 
   54: use strict;
   55: use Apache::Constants qw(:common :http);
   56: use Apache::lonnet;
   57: use Apache::loncommon;
   58: 
   59: my $loginscript; # piece of javascript used in two separate instances
   60: my $generalrule;
   61: my $authformnop;
   62: my $authformkrb;
   63: my $authformint;
   64: my $authformfsys;
   65: my $authformloc;
   66: 
   67: BEGIN {
   68:     $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
   69:     my $krbdefdom=$1;
   70:     $krbdefdom=~tr/a-z/A-Z/;
   71:     my %param = ( formname => 'document.cu',
   72:                   kerb_def_dom => $krbdefdom 
   73:                   );
   74: # no longer static due to configurable kerberos defaults
   75: #    $loginscript  = &Apache::loncommon::authform_header(%param);
   76:     $generalrule  = &Apache::loncommon::authform_authorwarning(%param);
   77:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
   78: # no longer static due to configurable kerberos defaults
   79: #    $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
   80:     $authformint  = &Apache::loncommon::authform_internal(%param);
   81:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
   82:     $authformloc  = &Apache::loncommon::authform_local(%param);
   83: }
   84: 
   85: 
   86: 
   87: # ==================================================== Figure out author access
   88: 
   89: sub authorpriv {
   90:     my ($auname,$audom)=@_;
   91:     if (($auname ne $ENV{'user.name'}) ||
   92:         (($audom ne $ENV{'user.domain'}) &&
   93:          ($audom ne $ENV{'request.role.domain'}))) { return ''; }
   94:     unless (&Apache::lonnet::allowed('cca',$audom)) { return ''; }
   95:     return 1;
   96: }
   97: 
   98: # =================================================================== Phase one
   99: 
  100: sub print_username_entry_form {
  101:     my $r=shift;
  102:     my $defdom=$ENV{'request.role.domain'};
  103:     my @domains = &Apache::loncommon::get_domains();
  104:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
  105:     my $bodytag =&Apache::loncommon::bodytag(
  106:                                   'Create Users, Change User Privileges');
  107:     my $selscript=&Apache::loncommon::studentbrowser_javascript();
  108:     my $sellink=&Apache::loncommon::selectstudent_link
  109:                                         ('crtuser','ccuname','ccdomain');
  110:     $r->print(<<"ENDDOCUMENT");
  111: <html>
  112: <head>
  113: <title>The LearningOnline Network with CAPA</title>
  114: $selscript
  115: </head>
  116: $bodytag
  117: <form action="/adm/createuser" method="post" name="crtuser">
  118: <input type="hidden" name="phase" value="get_user_info">
  119: <p>
  120: <table>
  121: <tr><td>Username:</td><td><input type="text" size="15" name="ccuname">
  122: </td><td rowspan="2">$sellink</td></tr><tr><td>
  123: Domain:</td><td>$domform</td></tr>
  124: </table> 
  125: </p>
  126: <input type="submit" value="Continue">
  127: </form>
  128: </body>
  129: </html>
  130: ENDDOCUMENT
  131: }
  132: 
  133: # =================================================================== Phase two
  134: sub print_user_modification_page {
  135:     my $r=shift;
  136:     my $ccuname=$ENV{'form.ccuname'};
  137:     my $ccdomain=$ENV{'form.ccdomain'};
  138: 
  139:     my $defdom=$ENV{'request.role.domain'};
  140: 
  141:     my ($krbdef,$krbdefdom) =
  142:        &Apache::loncommon::get_kerberos_defaults($defdom);
  143: 
  144:     my %param = ( formname => 'document.cu',
  145:                   kerb_def_dom => $krbdefdom,
  146:                   kerb_def_auth => $krbdef
  147:                   );
  148:     $loginscript  = &Apache::loncommon::authform_header(%param);
  149:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
  150: 
  151:     $ccuname=~s/\W//g;
  152:     $ccdomain=~s/\W//g;
  153:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  154:     my $dochead =<<"ENDDOCHEAD";
  155: <html>
  156: <head>
  157: <title>The LearningOnline Network with CAPA</title>
  158: <script type="text/javascript" language="Javascript">
  159: 
  160:     function pclose() {
  161:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
  162:                  "height=350,width=350,scrollbars=no,menubar=no");
  163:         parmwin.close();
  164:     }
  165: 
  166:     $pjump_def
  167: 
  168:     function dateset() {
  169:         eval("document.cu."+document.cu.pres_marker.value+
  170:             ".value=document.cu.pres_value.value");
  171:         pclose();
  172:     }
  173: 
  174: </script>
  175: </head>
  176: ENDDOCHEAD
  177:     $r->print(&Apache::loncommon::bodytag(
  178:                                      'Create Users, Change User Privileges'));
  179:     my $forminfo =<<"ENDFORMINFO";
  180: <form action="/adm/createuser" method="post" name="cu">
  181: <input type="hidden" name="phase"       value="update_user_data">
  182: <input type="hidden" name="ccuname"     value="$ccuname">
  183: <input type="hidden" name="ccdomain"    value="$ccdomain">
  184: <input type="hidden" name="pres_value"  value="" >
  185: <input type="hidden" name="pres_type"   value="" >
  186: <input type="hidden" name="pres_marker" value="" >
  187: ENDFORMINFO
  188:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
  189:     my %incdomains; 
  190:     my %inccourses;
  191:     foreach (values(%Apache::lonnet::hostdom)) {
  192:        $incdomains{$_}=1;
  193:     }
  194:     foreach (keys(%ENV)) {
  195: 	if ($_=~/^user\.priv\.cm\.\/(\w+)\/(\w+)/) {
  196: 	    $inccourses{$1.'_'.$2}=1;
  197:         }
  198:     }
  199:     if ($uhome eq 'no_host') {
  200:         my $home_server_list=
  201:             '<option value="default" selected>default</option>'."\n".
  202:                 &Apache::loncommon::home_server_option_list($ccdomain);
  203:         
  204: 	$r->print(<<ENDNEWUSER);
  205: $dochead
  206: <h1>Create New User</h1>
  207: $forminfo
  208: <h2>New user "$ccuname" in domain $ccdomain</h2>
  209: <script type="text/javascript" language="Javascript">
  210: $loginscript
  211: </script>
  212: <input type='hidden' name='makeuser' value='1' />
  213: <h3>Personal Data</h3>
  214: <p>
  215: <table>
  216: <tr><td>First Name  </td>
  217:     <td><input type='text' name='cfirst'  size='15' /></td></tr>
  218: <tr><td>Middle Name </td> 
  219:     <td><input type='text' name='cmiddle' size='15' /></td></tr>
  220: <tr><td>Last Name   </td>
  221:     <td><input type='text' name='clast'   size='15' /></td></tr>
  222: <tr><td>Generation  </td>
  223:     <td><input type='text' name='cgen'    size='5'  /></td></tr>
  224: </table>
  225: ID/Student Number <input type='text' name='cstid'   size='15' /></p>
  226: Home Server: <select name="hserver" size="1"> $home_server_list </select>
  227: <hr />
  228: <h3>Login Data</h3>
  229: <p>$generalrule </p>
  230: <p>$authformkrb </p>
  231: <p>$authformint </p>
  232: <p>$authformfsys</p>
  233: <p>$authformloc </p>
  234: ENDNEWUSER
  235:     } else { # user already exists
  236: 	$r->print(<<ENDCHANGEUSER);
  237: $dochead
  238: <h1>Change User Privileges</h1>
  239: $forminfo
  240: <h2>User "$ccuname" in domain $ccdomain </h2>
  241: ENDCHANGEUSER
  242:         # Get the users information
  243:         my %userenv = &Apache::lonnet::get('environment',
  244:                           ['firstname','middlename','lastname','generation'],
  245:                           $ccdomain,$ccuname);
  246:         my %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
  247:         $r->print(<<END);
  248: <hr />
  249: <table border="2">
  250: <tr>
  251: <th>first name</th><th>middle name</th><th>last name</th><th>generation</th>
  252: </tr>
  253: <tr>
  254: END
  255:         foreach ('firstname','middlename','lastname','generation') {
  256:            if (&Apache::lonnet::allowed('mau',$ccdomain)) {
  257:               $r->print(<<"END");            
  258: <td><input type="text" name="c$_" value="$userenv{$_}" size="15" /></td>
  259: END
  260:            } else {
  261:                $r->print('<td>'.$userenv{$_}.'</td>');
  262:            }
  263:         }
  264:         $r->print(<<END);
  265: </tr>
  266: </table>
  267: END
  268:         # Build up table of user roles to allow revocation of a role.
  269:         my ($tmp) = keys(%rolesdump);
  270:         unless ($tmp =~ /^(con_lost|error)/i) {
  271:            my $now=time;
  272:            $r->print(<<END);
  273: <hr />
  274: <h3>Revoke Existing Roles</h3>
  275: <table border=2>
  276: <tr><th>Revoke</th><th>Delete</th><th>Role</th><th>Extent</th><th>Start</th><th>End</th>
  277: END
  278: 	   foreach my $area (keys(%rolesdump)) {
  279:                next if ($area =~ /^rolesdef/);
  280:                my $role = $rolesdump{$area};
  281:                my $thisrole=$area;
  282:                $area =~ s/\_\w\w$//;
  283:                my ($role_code,$role_end_time,$role_start_time) = 
  284:                    split(/_/,$role);
  285:                my $bgcol='ffffff';
  286:                my $allowed=0;
  287:                my $delallowed=0;
  288:                if ($area =~ /^\/(\w+)\/(\d\w+)/ ) {
  289:                    my %coursedata=
  290:                        &Apache::lonnet::coursedescription($1.'_'.$2);
  291: 		   my $carea;
  292: 		   if (defined($coursedata{'description'})) {
  293: 		       $carea='Course: '.$coursedata{'description'}.
  294:                               '<br />Domain: '.$1;
  295: 		   } else {
  296: 		       $carea='Unavailable course: '.$area;
  297: 		   }
  298:                    $inccourses{$1.'_'.$2}=1;
  299:                    if ((&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) ||
  300:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
  301:                        $allowed=1;
  302:                    }
  303:                    if ((&Apache::lonnet::allowed('dro',$1)) ||
  304:                        (&Apache::lonnet::allowed('dro',$ccdomain))) {
  305:                        $delallowed=1;
  306:                    }
  307:                    # Compute the background color based on $area
  308:                    $bgcol=$1.'_'.$2;
  309:                    $bgcol=~s/[^8-9b-e]//g;
  310:                    $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',0,6);
  311:                    if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) {
  312:                        $carea.='<br>Section/Group: '.$3;
  313:                    }
  314:                    $area=$carea;
  315:                } else {
  316:                    # Determine if current user is able to revoke privileges
  317:                    if ($area=~ /^\/(\w+)\//) {
  318:                        if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
  319:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
  320:                            $allowed=1;
  321:                        }
  322:                        if (((&Apache::lonnet::allowed('dro',$1))  ||
  323:                             (&Apache::lonnet::allowed('dro',$ccdomain))) &&
  324:                            ($role_code ne 'dc')) {
  325:                            $delallowed=1;
  326:                        }
  327:                    } else {
  328:                        if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
  329:                            $allowed=1;
  330:                        }
  331:                    }
  332:                }
  333:                if ($role_code eq 'ca') {
  334:                    $area=~/\/(\w+)\/(\w+)/;
  335: 		   if (&authorpriv($2,$1)) {
  336: 		       $allowed=1;
  337:                    } else {
  338:                        $allowed=0;
  339:                    }
  340:                }
  341:                my $row = '';
  342:                $row.='<tr bgcolor=#"'.$bgcol.'"><td>';
  343:                my $active=1;
  344:                $active=0 if (($role_end_time) && ($now>$role_end_time));
  345:                if (($active) && ($allowed)) {
  346:                    $row.= '<input type="checkbox" name="rev:'.$thisrole.'">';
  347:                } else {
  348:                    $row.='&nbsp;';
  349:                }
  350: 	       $row.='</td><td>';
  351:                if ($delallowed) {
  352:                    $row.= '<input type="checkbox" name="del:'.$thisrole.'">';
  353:                } else {
  354:                    $row.='&nbsp;';
  355:                }
  356:                $row.= '</td><td>'.&Apache::lonnet::plaintext($role_code).
  357:                       '</td><td>'.$area.
  358:                       '</td><td>'.($role_start_time?localtime($role_start_time)
  359:                                                    : '&nbsp;' ).
  360:                       '</td><td>'.($role_end_time  ?localtime($role_end_time)
  361:                                                    : '&nbsp;' )
  362:                       ."</td></tr>\n";
  363:                $r->print($row);
  364:            } # end of foreach        (table building loop)
  365: 	   $r->print('</table>');
  366:         }  # End of unless
  367: 	my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
  368: 	if ($currentauth=~/^krb(4|5):/) {
  369: 	    $currentauth=~/^krb(4|5):(.*)/;
  370: 	    my $krbdefdom=$1;
  371:             my %param = ( formname => 'document.cu',
  372:                           kerb_def_dom => $krbdefdom 
  373:                           );
  374:             $loginscript  = &Apache::loncommon::authform_header(%param);
  375: 	}
  376: 	# Check for a bad authentication type
  377:         unless ($currentauth=~/^krb(4|5):/ or
  378: 		$currentauth=~/^unix:/ or
  379: 		$currentauth=~/^internal:/ or
  380: 		$currentauth=~/^localauth:/
  381: 		) { # bad authentication scheme
  382: 	    if (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'})) {
  383: 		$r->print(<<ENDBADAUTH);
  384: <hr />
  385: <script type="text/javascript" language="Javascript">
  386: $loginscript
  387: </script>
  388: <font color='#ff0000'>ERROR:</font>
  389: This user has an unrecognized authentication scheme ($currentauth).
  390: Please specify login data below.
  391: <h3>Login Data</h3>
  392: <p>$generalrule</p>
  393: <p>$authformkrb</p>
  394: <p>$authformint</p>
  395: <p>$authformfsys</p>
  396: <p>$authformloc</p>
  397: ENDBADAUTH
  398:             } else { 
  399:                 # This user is not allowed to modify the users 
  400:                 # authentication scheme, so just notify them of the problem
  401: 		$r->print(<<ENDBADAUTH);
  402: <hr />
  403: <script type="text/javascript" language="Javascript">
  404: $loginscript
  405: </script>
  406: <font color="#ff0000"> ERROR: </font>
  407: This user has an unrecognized authentication scheme ($currentauth).
  408: Please alert a domain coordinator of this situation.
  409: <hr />
  410: ENDBADAUTH
  411:             }
  412:         } else { # Authentication type is valid
  413: 	    my $authformcurrent='';
  414: 	    my $authform_other='';
  415: 	    if ($currentauth=~/^krb(4|5):/) {
  416: 		$authformcurrent=$authformkrb;
  417: 		$authform_other="<p>$authformint</p>\n".
  418:                     "<p>$authformfsys</p><p>$authformloc</p>";
  419: 	    }
  420: 	    elsif ($currentauth=~/^internal:/) {
  421: 		$authformcurrent=$authformint;
  422: 		$authform_other="<p>$authformkrb</p>".
  423:                     "<p>$authformfsys</p><p>$authformloc</p>";
  424: 	    }
  425: 	    elsif ($currentauth=~/^unix:/) {
  426: 		$authformcurrent=$authformfsys;
  427: 		$authform_other="<p>$authformkrb</p>".
  428:                     "<p>$authformint</p><p>$authformloc;</p>";
  429: 	    }
  430: 	    elsif ($currentauth=~/^localauth:/) {
  431: 		$authformcurrent=$authformloc;
  432: 		$authform_other="<p>$authformkrb</p>".
  433:                     "<p>$authformint</p><p>$authformfsys</p>";
  434: 	    }
  435:             $authformcurrent.=' <i>(will override current values)</i><br />';
  436:             if (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'})) {
  437: 		# Current user has login modification privileges
  438: 		$r->print(<<ENDOTHERAUTHS);
  439: <hr />
  440: <script type="text/javascript" language="Javascript">
  441: $loginscript
  442: </script>
  443: <h3>Change Current Login Data</h3>
  444: <p>$generalrule</p>
  445: <p>$authformnop</p>
  446: <p>$authformcurrent</p>
  447: <h3>Enter New Login Data</h3>
  448: $authform_other
  449: ENDOTHERAUTHS
  450:             }
  451:         }  ## End of "check for bad authentication type" logic
  452:     } ## End of new user/old user logic
  453:     $r->print('<hr /><h3>Add Roles</h3>');
  454: #
  455: # Co-Author
  456: # 
  457:     if (&authorpriv($ENV{'user.name'},$ENV{'request.role.domain'}) &&
  458:         ($ENV{'user.name'} ne $ccuname || $ENV{'user.domain'} ne $ccdomain)) {
  459:         # No sense in assigning co-author role to yourself
  460: 	my $cuname=$ENV{'user.name'};
  461:         my $cudom=$ENV{'request.role.domain'};
  462:        $r->print(<<ENDCOAUTH);
  463: <h4>Construction Space</h4>
  464: <table border=2><tr><th>Activate</th><th>Role</th><th>Extent</th>
  465: <th>Start</th><th>End</th></tr>
  466: <tr>
  467: <td><input type=checkbox name="act_$cudom\_$cuname\_ca"></td>
  468: <td>Co-Author</td>
  469: <td>$cudom\_$cuname</td>
  470: <td><input type=hidden name="start_$cudom\_$cuname\_ca" value=''>
  471: <a href=
  472: "javascript:pjump('date_start','Start Date Co-Author',document.cu.start_$cudom\_$cuname\_ca.value,'start_$cudom\_$cuname\_ca','cu.pres','dateset')">Set Start Date</a></td>
  473: <td><input type=hidden name="end_$cudom\_$cuname\_ca" value=''>
  474: <a href=
  475: "javascript:pjump('date_end','End Date Co-Author',document.cu.end_$cudom\_$cuname\_ca.value,'end_$cudom\_$cuname\_ca','cu.pres','dateset')">Set End Date</a></td>
  476: </tr>
  477: </table>
  478: ENDCOAUTH
  479:     }
  480: #
  481: # Domain level
  482: #
  483:     $r->print('<h4>Domain Level</h4>'.
  484:     '<table border=2><tr><th>Activate</th><th>Role</th><th>Extent</th>'.
  485:     '<th>Start</th><th>End</th></tr>');
  486:     foreach ( sort( keys(%incdomains))) {
  487: 	my $thisdomain=$_;
  488:         foreach ('dc','li','dg','au') {
  489:             if (&Apache::lonnet::allowed('c'.$_,$thisdomain)) {
  490:                my $plrole=&Apache::lonnet::plaintext($_);
  491:                $r->print(<<ENDDROW);
  492: <tr>
  493: <td><input type=checkbox name="act_$thisdomain\_$_"></td>
  494: <td>$plrole</td>
  495: <td>$thisdomain</td>
  496: <td><input type=hidden name="start_$thisdomain\_$_" value=''>
  497: <a href=
  498: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$thisdomain\_$_.value,'start_$thisdomain\_$_','cu.pres','dateset')">Set Start Date</a></td>
  499: <td><input type=hidden name="end_$thisdomain\_$_" value=''>
  500: <a href=
  501: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$thisdomain\_$_.value,'end_$thisdomain\_$_','cu.pres','dateset')">Set End Date</a></td>
  502: </tr>
  503: ENDDROW
  504:             }
  505:         } 
  506:     }
  507:     $r->print('</table>');
  508: #
  509: # Course level
  510: #
  511:     $r->print(&course_level_table(%inccourses));
  512:     $r->print("<hr /><input type=submit value=\"Modify User\">\n");
  513:     $r->print("</form></body></html>");
  514: }
  515: 
  516: # ================================================================= Phase Three
  517: sub update_user_data {
  518:     my $r=shift;
  519:     my $uhome=&Apache::lonnet::homeserver($ENV{'form.ccuname'},
  520:                                           $ENV{'form.ccdomain'});
  521:     # Error messages
  522:     my $error     = '<font color="#ff0000">Error:</font>';
  523:     my $end       = '</body></html>';
  524:     # Print header
  525:     $r->print(<<ENDTHREEHEAD);
  526: <html>
  527: <head>
  528: <title>The LearningOnline Network with CAPA</title>
  529: </head>
  530: ENDTHREEHEAD
  531:     my $title;
  532:     if (exists($ENV{'form.makeuser'})) {
  533: 	$title='Set Privileges for New User';
  534:     } else {
  535:         $title='Modify User Privileges';
  536:     }
  537:     $r->print(&Apache::loncommon::bodytag($title));
  538:     # Check Inputs
  539:     if (! $ENV{'form.ccuname'} ) {
  540: 	$r->print($error.'No login name specified.'.$end);
  541: 	return;
  542:     }
  543:     if (  $ENV{'form.ccuname'}  =~/\W/) {
  544: 	$r->print($error.'Invalid login name.  '.
  545: 		  'Only letters, numbers, and underscores are valid.'.
  546: 		  $end);
  547: 	return;
  548:     }
  549:     if (! $ENV{'form.ccdomain'}       ) {
  550: 	$r->print($error.'No domain specified.'.$end);
  551: 	return;
  552:     }
  553:     if (  $ENV{'form.ccdomain'} =~/\W/) {
  554: 	$r->print($error.'Invalid domain name.  '.
  555: 		  'Only letters, numbers, and underscores are valid.'.
  556: 		  $end);
  557: 	return;
  558:     }
  559:     if (! exists($ENV{'form.makeuser'})) {
  560:         # Modifying an existing user, so check the validity of the name
  561:         if ($uhome eq 'no_host') {
  562:             $r->print($error.'Unable to determine home server for '.
  563:                       $ENV{'form.ccuname'}.' in domain '.
  564:                       $ENV{'form.ccdomain'}.'.');
  565:             return;
  566:         }
  567:     }
  568:     # Determine authentication method and password for the user being modified
  569:     my $amode='';
  570:     my $genpwd='';
  571:     if ($ENV{'form.login'} eq 'krb') {
  572: 	$amode='krb';
  573: 	$amode.=$ENV{'form.krbver'};
  574: 	$genpwd=$ENV{'form.krbarg'};
  575:     } elsif ($ENV{'form.login'} eq 'int') {
  576: 	$amode='internal';
  577: 	$genpwd=$ENV{'form.intarg'};
  578:     } elsif ($ENV{'form.login'} eq 'fsys') {
  579: 	$amode='unix';
  580: 	$genpwd=$ENV{'form.fsysarg'};
  581:     } elsif ($ENV{'form.login'} eq 'loc') {
  582: 	$amode='localauth';
  583: 	$genpwd=$ENV{'form.locarg'};
  584: 	$genpwd=" " if (!$genpwd);
  585:     } elsif (($ENV{'form.login'} eq 'nochange') ||
  586:              ($ENV{'form.login'} eq ''        )) { 
  587:         # There is no need to tell the user we did not change what they
  588:         # did not ask us to change.
  589:         # If they are creating a new user but have not specified login
  590:         # information this will be caught below.
  591:     } else {
  592: 	    $r->print($error.'Invalid login mode or password'.$end);    
  593: 	    return;
  594:     }
  595:     if ($ENV{'form.makeuser'}) {
  596:         # Create a new user
  597: 	$r->print(<<ENDNEWUSERHEAD);
  598: <h3>Creating user "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
  599: ENDNEWUSERHEAD
  600:         # Check for the authentication mode and password
  601:         if (! $amode || ! $genpwd) {
  602: 	    $r->print($error.'Invalid login mode or password'.$end);    
  603: 	    return;
  604: 	}
  605:         # Determine desired host
  606:         my $desiredhost = $ENV{'form.hserver'};
  607:         if (lc($desiredhost) eq 'default') {
  608:             $desiredhost = undef;
  609:         } else {
  610:             my %home_servers = &Apache::loncommon::get_library_servers
  611:                 ($ENV{'form.ccdomain'});  
  612:             if (! exists($home_servers{$desiredhost})) {
  613:                 $r->print($error.'Invalid home server specified');
  614:                 return;
  615:             }
  616:         }
  617: 	# Call modifyuser
  618: 	my $result = &Apache::lonnet::modifyuser
  619: 	    ($ENV{'form.ccdomain'},$ENV{'form.ccuname'},$ENV{'form.cstid'},
  620:              $amode,$genpwd,$ENV{'form.cfirst'},
  621:              $ENV{'form.cmiddle'},$ENV{'form.clast'},$ENV{'form.cgen'},
  622:              undef,$desiredhost
  623: 	     );
  624: 	$r->print('Generating user: '.$result);
  625:         my $home = &Apache::lonnet::homeserver($ENV{'form.ccuname'},
  626:                                                $ENV{'form.ccdomain'});
  627:         $r->print('<br>Home server: '.$home.' '.
  628:                   $Apache::lonnet::libserv{$home});
  629:     } elsif (($ENV{'form.login'} ne 'nochange') &&
  630:              ($ENV{'form.login'} ne ''        )) {
  631: 	# Modify user privileges
  632: 	$r->print(<<ENDMODIFYUSERHEAD);
  633: <h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
  634: ENDMODIFYUSERHEAD
  635:         if (! $amode || ! $genpwd) {
  636: 	    $r->print($error.'Invalid login mode or password'.$end);    
  637: 	    return;
  638: 	}
  639: 	# Only allow authentification modification if the person has authority
  640: 	if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'})) {
  641: 	    $r->print('Modifying authentication: '.
  642:                       &Apache::lonnet::modifyuserauth(
  643: 		       $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
  644:                        $amode,$genpwd));
  645:             $r->print('<br>Home server: '.&Apache::lonnet::homeserver
  646: 		  ($ENV{'form.ccuname'},$ENV{'form.ccdomain'}));
  647: 	} else {
  648: 	    # Okay, this is a non-fatal error.
  649: 	    $r->print($error.'You do not have the authority to modify '.
  650: 		      'this users authentification information.');    
  651: 	}
  652:     }
  653:     ##
  654:     if (! $ENV{'form.makeuser'} ) {
  655:         # Check for need to change
  656:         my %userenv = &Apache::lonnet::get
  657:             ('environment',['firstname','middlename','lastname','generation'],
  658:              $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
  659:         my ($tmp) = keys(%userenv);
  660:         if ($tmp =~ /^(con_lost|error)/i) { 
  661:             %userenv = ();
  662:         }
  663:         # Check to see if we need to change user information
  664:         foreach ('firstname','middlename','lastname','generation') {
  665:             # Strip leading and trailing whitespace
  666:             $ENV{'form.c'.$_} =~ s/(\s+$|^\s+)//g; 
  667:         }
  668:         if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'}) && 
  669:             ($ENV{'form.cfirstname'}  ne $userenv{'firstname'}  ||
  670:              $ENV{'form.cmiddlename'} ne $userenv{'middlename'} ||
  671:              $ENV{'form.clastname'}   ne $userenv{'lastname'}   ||
  672:              $ENV{'form.cgeneration'} ne $userenv{'generation'} )) {
  673:             # Make the change
  674:             my %changeHash;
  675:             $changeHash{'firstname'}  = $ENV{'form.cfirstname'};
  676:             $changeHash{'middlename'} = $ENV{'form.cmiddlename'};
  677:             $changeHash{'lastname'}   = $ENV{'form.clastname'};
  678:             $changeHash{'generation'} = $ENV{'form.cgeneration'};
  679:             my $putresult = &Apache::lonnet::put
  680:                 ('environment',\%changeHash,
  681:                  $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
  682:             if ($putresult eq 'ok') {
  683:             # Tell the user we changed the name
  684:                 $r->print(<<"END");
  685: <table border="2">
  686: <caption>User Information Changed</caption>
  687: <tr><th>&nbsp;</th>
  688:     <th>first</th>
  689:     <th>middle</th>
  690:     <th>last</th>
  691:     <th>generation</th></tr>
  692: <tr><td>Previous</td>
  693:     <td>$userenv{'firstname'}  </td>
  694:     <td>$userenv{'middlename'} </td>
  695:     <td>$userenv{'lastname'}   </td>
  696:     <td>$userenv{'generation'} </td></tr>
  697: <tr><td>Changed To</td>
  698:     <td>$ENV{'form.cfirstname'}  </td>
  699:     <td>$ENV{'form.cmiddlename'} </td>
  700:     <td>$ENV{'form.clastname'}   </td>
  701:     <td>$ENV{'form.cgeneration'} </td></tr>
  702: </table>
  703: END
  704:             } else { # error occurred
  705:                 $r->print("<h2>Unable to successfully change environment for ".
  706:                       $ENV{'form.ccuname'}." in domain ".
  707:                       $ENV{'form.ccdomain'}."</h2>");
  708:             }
  709:         }  else { # End of if ($ENV ... ) logic
  710:             # They did not want to change the users name but we can
  711:             # still tell them what the name is
  712:                 $r->print(<<"END");
  713: <h2>User "$ENV{'form.ccuname'}" in domain "$ENV{'form.ccdomain'}"</h2>
  714: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} </h4>
  715: <h4>Generation: $userenv{'generation'}</h4>
  716: END
  717:         }
  718:     }
  719:     ##
  720:     my $now=time;
  721:     $r->print('<h3>Modifying Roles</h3>');
  722:     foreach (keys (%ENV)) {
  723: 	next if (! $ENV{$_});
  724: 	# Revoke roles
  725: 	if ($_=~/^form\.rev/) {
  726: 	    if ($_=~/^form\.rev\:([^\_]+)\_([^\_]+)$/) {
  727: 	        $r->print('Revoking '.$2.' in '.$1.': '.
  728:                      &Apache::lonnet::assignrole($ENV{'form.ccdomain'},
  729:                      $ENV{'form.ccuname'},$1,$2,$now).'<br>');
  730: 		if ($2 eq 'st') {
  731: 		    $1=~/^\/(\w+)\/(\w+)/;
  732: 		    my $cid=$1.'_'.$2;
  733: 		    $r->print('Drop from classlist: '.
  734: 			 &Apache::lonnet::critical('put:'.
  735:                              $ENV{'course.'.$cid.'.domain'}.':'.
  736: 	                     $ENV{'course.'.$cid.'.num'}.':classlist:'.
  737:                          &Apache::lonnet::escape($ENV{'form.ccuname'}.':'.
  738:                              $ENV{'form.ccdomain'}).'='.
  739:                          &Apache::lonnet::escape($now.':'),
  740: 	                     $ENV{'course.'.$cid.'.home'}).'<br>');
  741: 		}
  742: 	    } 
  743: 	} elsif ($_=~/^form\.del/) {
  744: 	    if ($_=~/^form\.del\:([^\_]+)\_([^\_]+)$/) {
  745: 	        $r->print('Deleting '.$2.' in '.$1.': '.
  746:                      &Apache::lonnet::assignrole($ENV{'form.ccdomain'},
  747:                      $ENV{'form.ccuname'},$1,$2,$now,0,1).'<br>');
  748: 		if ($2 eq 'st') {
  749: 		    $1=~/^\/(\w+)\/(\w+)/;
  750: 		    my $cid=$1.'_'.$2;
  751: 		    $r->print('Drop from classlist: '.
  752: 			 &Apache::lonnet::critical('put:'.
  753:                              $ENV{'course.'.$cid.'.domain'}.':'.
  754: 	                     $ENV{'course.'.$cid.'.num'}.':classlist:'.
  755:                          &Apache::lonnet::escape($ENV{'form.ccuname'}.':'.
  756:                              $ENV{'form.ccdomain'}).'='.
  757:                          &Apache::lonnet::escape($now.':'),
  758: 	                     $ENV{'course.'.$cid.'.home'}).'<br>');
  759: 		}
  760: 	    } 
  761: 	} elsif ($_=~/^form\.act/) {
  762: 	    if ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_([^\_]+)$/) {
  763: 		# Activate roles for sections with 3 id numbers
  764: 		# set start, end times, and the url for the class
  765: 		my $start = ( $ENV{'form.start_'.$1.'_'.$2} ? 
  766: 			      $ENV{'form.start_'.$1.'_'.$2} : 
  767: 			      $now );
  768: 		my $end   = ( $ENV{'form.end_'.$1.'_'.$2} ? 
  769: 			      $ENV{'form.end_'.$1.'_'.$2} :
  770: 			      0 );
  771: 		my $url='/'.$1.'/'.$2;
  772: 		if ($ENV{'form.sec_'.$1.'_'.$2.'_'.$3}) {
  773: 		    $url.='/'.$ENV{'form.sec_'.$1.'_'.$2.'_'.$3};
  774: 		}
  775: 		# Assign the role and report it
  776: 		$r->print('Assigning: '.$3.' in '.$url.': '.
  777:                           &Apache::lonnet::assignrole(
  778:                               $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
  779:                               $url,$3,$end,$start).
  780: 			  '<br>');
  781: 		# Handle students differently
  782: 		if ($3 eq 'st') {
  783: 		    $url=~/^\/(\w+)\/(\w+)/;
  784: 		    my $cid=$1.'_'.$2;
  785: 		    $r->print('Add to classlist: '.
  786: 			      &Apache::lonnet::critical(
  787: 				  'put:'.$ENV{'course.'.$cid.'.domain'}.':'.
  788: 	                           $ENV{'course.'.$cid.'.num'}.':classlist:'.
  789:                                    &Apache::lonnet::escape(
  790:                                        $ENV{'form.ccuname'}.':'.
  791:                                        $ENV{'form.ccdomain'} ).'='.
  792:                                    &Apache::lonnet::escape($end.':'.$start),
  793: 				       $ENV{'course.'.$cid.'.home'})
  794: 			      .'<br>');
  795: 		}
  796: 	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
  797: 		# Activate roles for sections with two id numbers
  798: 		# set start, end times, and the url for the class
  799: 		my $start = ( $ENV{'form.start_'.$1.'_'.$2} ? 
  800: 			      $ENV{'form.start_'.$1.'_'.$2} : 
  801: 			      $now );
  802: 		my $end   = ( $ENV{'form.end_'.$1.'_'.$2} ? 
  803: 			      $ENV{'form.end_'.$1.'_'.$2} :
  804: 			      0 );
  805: 		my $url='/'.$1.'/';
  806: 		# Assign the role and report it.
  807: 		$r->print('Assigning: '.$2.' in '.$url.': '.
  808:                           &Apache::lonnet::assignrole(
  809:                               $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
  810:                               $url,$2,$end,$start)
  811: 			  .'<br>');
  812: 	    }
  813: 	} 
  814:     } # End of foreach (keys(%ENV))
  815:     $r->print('</body></html>');
  816: }
  817: 
  818: # ================================================================ Main Handler
  819: sub handler {
  820:     my $r = shift;
  821: 
  822:     if ($r->header_only) {
  823:        $r->content_type('text/html');
  824:        $r->send_http_header;
  825:        return OK;
  826:     }
  827: 
  828:     if ((&Apache::lonnet::allowed('cta',$ENV{'request.course.id'})) ||
  829:         (&Apache::lonnet::allowed('cin',$ENV{'request.course.id'})) || 
  830:         (&Apache::lonnet::allowed('ccr',$ENV{'request.course.id'})) || 
  831:         (&Apache::lonnet::allowed('cep',$ENV{'request.course.id'})) ||
  832:         (&Apache::lonnet::allowed('cca',$ENV{'request.role.domain'})) ||
  833:         (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'}))) {
  834:        $r->content_type('text/html');
  835:        $r->send_http_header;
  836:        unless ($ENV{'form.phase'}) {
  837: 	   &print_username_entry_form($r);
  838:        }
  839:        if ($ENV{'form.phase'} eq 'get_user_info') {
  840:            &print_user_modification_page($r);
  841:        } elsif ($ENV{'form.phase'} eq 'update_user_data') {
  842:            &update_user_data($r);
  843:        }
  844:    } else {
  845:       $ENV{'user.error.msg'}=
  846:         "/adm/createuser:mau:0:0:Cannot modify user data";
  847:       return HTTP_NOT_ACCEPTABLE; 
  848:    }
  849:    return OK;
  850: } 
  851: 
  852: #-------------------------------------------------- functions for &phase_two
  853: sub course_level_table {
  854:     my %inccourses = @_;
  855:     my $table = '';
  856:     foreach (sort( keys(%inccourses))) {
  857: 	my $thiscourse=$_;
  858: 	my $protectedcourse=$_;
  859: 	$thiscourse=~s:_:/:g;
  860: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
  861: 	my $area=$coursedata{'description'};
  862: 	if (!defined($area)) { $area='Unavailable course: '.$_; }
  863: 	my $bgcol=$thiscourse;
  864: 	$bgcol=~s/[^8-9b-e]//g;
  865: 	$bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',0,6);
  866: 	foreach  ('st','ta','ep','ad','in','cc') {
  867: 	    if (&Apache::lonnet::allowed('c'.$_,$thiscourse)) {
  868: 		my $plrole=&Apache::lonnet::plaintext($_);
  869: 		$table .= <<ENDEXTENT;
  870: <tr bgcolor="#$bgcol">
  871: <td><input type="checkbox" name="act_$protectedcourse\_$_"></td>
  872: <td>$plrole</td>
  873: <td>$area</td>
  874: ENDEXTENT
  875: 	        if ($_ ne 'cc') {
  876: 		    $table .= <<ENDSECTION;
  877: <td><input type="text" size="5" name="sec_$protectedcourse\_$_"></td>
  878: ENDSECTION
  879:                 } else { 
  880: 		    $table .= <<ENDSECTION;
  881: <td>&nbsp</td> 
  882: ENDSECTION
  883:                 }
  884: 		$table .= <<ENDTIMEENTRY;
  885: <td><input type=hidden name="start_$protectedcourse\_$_" value=''>
  886: <a href=
  887: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$_.value,'start_$protectedcourse\_$_','cu.pres','dateset')">Set Start Date</a></td>
  888: <td><input type=hidden name="end_$protectedcourse\_$_" value=''>
  889: <a href=
  890: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$_.value,'end_$protectedcourse\_$_','cu.pres','dateset')">Set End Date</a></td>
  891: ENDTIMEENTRY
  892:                 $table.= "</tr>\n";
  893:             }
  894:         }
  895:     }
  896:     return '' if ($table eq ''); # return nothing if there is nothing 
  897:                                  # in the table
  898:     my $result = <<ENDTABLE;
  899: <h4>Course Level</h4>
  900: <table border=2><tr><th>Activate</th><th>Role</th><th>Extent</th>
  901: <th>Group/Section</th><th>Start</th><th>End</th></tr>
  902: $table
  903: </table>
  904: ENDTABLE
  905:     return $result;
  906: }
  907: #---------------------------------------------- end functions for &phase_two
  908: 
  909: #--------------------------------- functions for &phase_two and &phase_three
  910: 
  911: #--------------------------end of functions for &phase_two and &phase_three
  912: 
  913: 1;
  914: __END__
  915: 
  916: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>