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

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

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