Annotation of loncom/interface/loncreateuser.pm, revision 1.45

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

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