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

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

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