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

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

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