File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.60: download - view: text, annotated - select for diffs
Fri Jul 18 13:45:14 2003 UTC (20 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Toward bug #795

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

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