File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.58: download - view: text, annotated - select for diffs
Thu Jul 17 18:14:41 2003 UTC (20 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Start work on bug #795: custom role editor

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

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