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

1.20      harris41    1: # The LearningOnline Network with CAPA
1.1       www         2: # Create a user
                      3: #
1.88    ! raeburn     4: # $Id: loncreateuser.pm,v 1.87 2004/09/23 22:51:24 albertel Exp $
1.22      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.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.68      www        66: use Apache::lonlocal;
1.1       www        67: 
1.20      harris41   68: my $loginscript; # piece of javascript used in two separate instances
                     69: my $generalrule;
                     70: my $authformnop;
                     71: my $authformkrb;
                     72: my $authformint;
                     73: my $authformfsys;
                     74: my $authformloc;
                     75: 
1.23      harris41   76: BEGIN {
1.20      harris41   77:     $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                     78:     my $krbdefdom=$1;
                     79:     $krbdefdom=~tr/a-z/A-Z/;
1.31      matthew    80:     my %param = ( formname => 'document.cu',
                     81:                   kerb_def_dom => $krbdefdom 
                     82:                   );
1.48      albertel   83: # no longer static due to configurable kerberos defaults
                     84: #    $loginscript  = &Apache::loncommon::authform_header(%param);
1.31      matthew    85:     $generalrule  = &Apache::loncommon::authform_authorwarning(%param);
                     86:     $authformnop  = &Apache::loncommon::authform_nochange(%param);
1.48      albertel   87: # no longer static due to configurable kerberos defaults
                     88: #    $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.31      matthew    89:     $authformint  = &Apache::loncommon::authform_internal(%param);
                     90:     $authformfsys = &Apache::loncommon::authform_filesystem(%param);
                     91:     $authformloc  = &Apache::loncommon::authform_local(%param);
1.20      harris41   92: }
                     93: 
1.43      www        94: 
1.59      www        95: # ======================================================= Existing Custom Roles
                     96: 
                     97: sub my_custom_roles {
                     98:     my %returnhash=();
                     99:     my %rolehash=&Apache::lonnet::dump('roles');
                    100:     foreach (keys %rolehash) {
                    101: 	if ($_=~/^rolesdef\_(\w+)$/) {
1.61      www       102: 	    $returnhash{$1}=$1;
1.59      www       103: 	}
                    104:     }
                    105:     return %returnhash;
                    106: }
1.43      www       107: 
                    108: # ==================================================== Figure out author access
                    109: 
                    110: sub authorpriv {
                    111:     my ($auname,$audom)=@_;
                    112:     if (($auname ne $ENV{'user.name'}) ||
                    113:         (($audom ne $ENV{'user.domain'}) &&
                    114:          ($audom ne $ENV{'request.role.domain'}))) { return ''; }
                    115:     unless (&Apache::lonnet::allowed('cca',$audom)) { return ''; }
                    116:     return 1;
                    117: }
                    118: 
1.2       www       119: # =================================================================== Phase one
1.1       www       120: 
1.42      matthew   121: sub print_username_entry_form {
1.2       www       122:     my $r=shift;
1.42      matthew   123:     my $defdom=$ENV{'request.role.domain'};
1.33      matthew   124:     my @domains = &Apache::loncommon::get_domains();
                    125:     my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
1.84      albertel  126:     my $bodytag =&Apache::loncommon::bodytag('Create Users, Change User Privileges').&Apache::loncommon::help_open_menu('',undef,undef,'',282,'Instructor Interface');
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.71      sakharuk  133:     my %lt=&Apache::lonlocal::texthash(
                    134: 		    'siur'   => "Set Individual User Roles",
                    135: 		    'usr'  => "Username",
                    136:                     'dom'  => "Domain",
                    137:                     'usrr' => "User Roles",
                    138:                     'ecrp' => "Edit Custom Role Privileges",
1.72      sakharuk  139:                     'nr'   => "Name of Role",
1.71      sakharuk  140:                     'cre'  => "Custom Role Editor"
                    141: 				       );
1.76      www       142:     my $helpsiur=&Apache::loncommon::help_open_topic('Course_Change_Privileges');
                    143:     my $helpecpr=&Apache::loncommon::help_open_topic('Course_Editing_Custom_Roles');
1.33      matthew   144:     $r->print(<<"ENDDOCUMENT");
1.1       www       145: <html>
                    146: <head>
                    147: <title>The LearningOnline Network with CAPA</title>
1.46      www       148: $selscript
1.1       www       149: </head>
1.40      www       150: $bodytag
1.46      www       151: <form action="/adm/createuser" method="post" name="crtuser">
1.42      matthew   152: <input type="hidden" name="phase" value="get_user_info">
1.76      www       153: <h2>$lt{siur}$helpsiur</h2>
1.43      www       154: <table>
1.71      sakharuk  155: <tr><td>$lt{usr}:</td><td><input type="text" size="15" name="ccuname">
1.46      www       156: </td><td rowspan="2">$sellink</td></tr><tr><td>
1.71      sakharuk  157: $lt{'dom'}:</td><td>$domform</td></tr>
1.58      www       158: </table>
1.71      sakharuk  159: <input name="userrole" type="submit" value="$lt{usrr}" />
1.2       www       160: </form>
1.58      www       161: <form action="/adm/createuser" method="post" name="docustom">
                    162: <input type="hidden" name="phase" value="selected_custom_edit">
1.76      www       163: <h2>$lt{'ecrp'}$helpecpr</h2>
1.72      sakharuk  164: $lt{'nr'}: $choice <input type="text" size="15" name="newrolename" /><br />
1.71      sakharuk  165: <input name="customeditor" type="submit" value="$lt{'cre'}" />
1.1       www       166: </body>
                    167: </html>
                    168: ENDDOCUMENT
1.2       www       169: }
                    170: 
                    171: # =================================================================== Phase two
1.42      matthew   172: sub print_user_modification_page {
1.2       www       173:     my $r=shift;
                    174:     my $ccuname=$ENV{'form.ccuname'};
                    175:     my $ccdomain=$ENV{'form.ccdomain'};
1.4       www       176: 
1.85      albertel  177:     $ccuname=~s/[\W|_]//gs;
                    178:     $ccdomain=~s/[\W|_]//gs;
1.58      www       179: 
                    180:     unless (($ccuname) && ($ccdomain)) {
                    181: 	&print_username_entry_form($r);
                    182:         return;
                    183:     }
                    184: 
1.48      albertel  185:     my $defdom=$ENV{'request.role.domain'};
                    186: 
                    187:     my ($krbdef,$krbdefdom) =
                    188:        &Apache::loncommon::get_kerberos_defaults($defdom);
                    189: 
1.31      matthew   190:     my %param = ( formname => 'document.cu',
1.48      albertel  191:                   kerb_def_dom => $krbdefdom,
                    192:                   kerb_def_auth => $krbdef
1.31      matthew   193:                   );
                    194:     $loginscript  = &Apache::loncommon::authform_header(%param);
1.48      albertel  195:     $authformkrb  = &Apache::loncommon::authform_kerberos(%param);
1.4       www       196: 
1.2       www       197:     $ccuname=~s/\W//g;
                    198:     $ccdomain=~s/\W//g;
1.52      matthew   199:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.88    ! raeburn   200:     my $dc_setcourse_code = '';
        !           201:     my $loaditem;
        !           202:     if ($ENV{'request.role'} =~ m-^dc\./(\w+)/$-) {
        !           203:         my $dcdom = $1;
        !           204:         $loaditem = qq|OnLoad="document.cu.coursedesc.value=''"|;
        !           205:         $dc_setcourse_code = <<"ENDSCRIPT";
        !           206:     function setCourse() {
        !           207:         var course = document.cu.dccourse.value;
        !           208:         if (course != "") {
        !           209:             if (document.cu.dcdomain.value != document.cu.origdom.value) {
        !           210:                 alert("You must select a course in the current domain");
        !           211:                 return;
        !           212:             } 
        !           213:             var userrole = document.cu.role.options[document.cu.role.selectedIndex].value
        !           214:             var section;
        !           215:             var numsections = 0;
        !           216:             if (document.cu.role.value != "cc") {
        !           217:                 for (var i=0; i<document.cu.currsec.length; i++) {
        !           218:                     if (document.cu.currsec.options[i].selected == true ) {
        !           219:                         if (document.cu.currsec.options[i].value != "") { 
        !           220:                             if (numsections == 0) {
        !           221:                                 if (document.cu.currsec.options[i].value != "") {
        !           222:                                     section = document.cu.currsec.options[i].value
        !           223:                                     numsections = 1;
        !           224:                                 }
        !           225:                             }
        !           226:                             else {
        !           227:                                 section = section + "," +  document.cu.currsec.options[i].value
        !           228:                                 numsections ++;
        !           229:                             }
        !           230:                         }
        !           231:                     }
        !           232:                 }
        !           233:                 if (document.cu.newsec.value != "") {
        !           234:                     if (numsections == 0) {
        !           235:                         if (document.cu.newsec.value != "") {
        !           236:                             section = document.cu.newsec.value
        !           237:                             numsections = 1;
        !           238:                         }
        !           239:                     }
        !           240:                     else {
        !           241:                         section = section + "," +  document.cu.newsec.value
        !           242:                         numsections ++;
        !           243:                     }
        !           244:                 }
        !           245:             }
        !           246:             var numcourse = getIndex(document.cu.dccourse);
        !           247:             if (numcourse == "-1") {
        !           248:                 alert("There was a problem with your course selection");
        !           249:                 return
        !           250:             }
        !           251:             else { 
        !           252:                 var coursename = "_$dcdom"+"_"+course+"_"+userrole
        !           253:                 document.cu.elements[numcourse].name = "act"+coursename
        !           254:                 document.cu.elements[numcourse+4].name = "sec"+coursename
        !           255:                 document.cu.elements[numcourse+4].value = section
        !           256:                 document.cu.elements[numcourse+5].name = "start"+coursename
        !           257:                 document.cu.elements[numcourse+6].name = "end"+coursename
        !           258:             }
        !           259:         }
        !           260:         document.cu.submit();
        !           261:     }
        !           262: 
        !           263:     function getIndex(caller) {
        !           264:         for (var i=0;i<document.cu.elements.length;i++) {
        !           265:             if (document.cu.elements[i] == caller) {
        !           266:                 return i;
        !           267:             }
        !           268:         }
        !           269:         return -1;
        !           270:     }
        !           271: ENDSCRIPT
        !           272:     }
1.25      matthew   273:     my $dochead =<<"ENDDOCHEAD";
1.2       www       274: <html>
                    275: <head>
                    276: <title>The LearningOnline Network with CAPA</title>
1.31      matthew   277: <script type="text/javascript" language="Javascript">
1.3       www       278: 
                    279:     function pclose() {
                    280:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
                    281:                  "height=350,width=350,scrollbars=no,menubar=no");
                    282:         parmwin.close();
                    283:     }
                    284: 
1.52      matthew   285:     $pjump_def
1.88    ! raeburn   286:     $dc_setcourse_code
1.3       www       287: 
                    288:     function dateset() {
                    289:         eval("document.cu."+document.cu.pres_marker.value+
                    290:             ".value=document.cu.pres_value.value");
                    291:         pclose();
                    292:     }
                    293: 
                    294: </script>
1.2       www       295: </head>
1.25      matthew   296: ENDDOCHEAD
1.40      www       297:     $r->print(&Apache::loncommon::bodytag(
1.88    ! raeburn   298:                                      'Create Users, Change User Privileges',undef,$loaditem));
1.25      matthew   299:     my $forminfo =<<"ENDFORMINFO";
                    300: <form action="/adm/createuser" method="post" name="cu">
1.42      matthew   301: <input type="hidden" name="phase"       value="update_user_data">
1.25      matthew   302: <input type="hidden" name="ccuname"     value="$ccuname">
                    303: <input type="hidden" name="ccdomain"    value="$ccdomain">
                    304: <input type="hidden" name="pres_value"  value="" >
                    305: <input type="hidden" name="pres_type"   value="" >
                    306: <input type="hidden" name="pres_marker" value="" >
                    307: ENDFORMINFO
1.2       www       308:     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
                    309:     my %incdomains; 
                    310:     my %inccourses;
1.49      albertel  311:     foreach (values(%Apache::lonnet::hostdom)) {
1.13      www       312:        $incdomains{$_}=1;
1.24      matthew   313:     }
                    314:     foreach (keys(%ENV)) {
1.2       www       315: 	if ($_=~/^user\.priv\.cm\.\/(\w+)\/(\w+)/) {
                    316: 	    $inccourses{$1.'_'.$2}=1;
                    317:         }
1.24      matthew   318:     }
1.2       www       319:     if ($uhome eq 'no_host') {
1.29      matthew   320:         my $home_server_list=
1.32      matthew   321:             '<option value="default" selected>default</option>'."\n".
                    322:                 &Apache::loncommon::home_server_option_list($ccdomain);
                    323:         
1.79      albertel  324: 	my %lt=&Apache::lonlocal::texthash(
1.72      sakharuk  325:                     'cnu'  => "Create New User",
                    326:                     'nu'   => "New User",
                    327:                     'id'   => "in domain",
                    328:                     'pd'   => "Personal Data",
                    329:                     'fn'   => "First Name",
                    330:                     'mn'   => "Middle Name",
                    331:                     'ln'   => "Last Name",
                    332:                     'gen'  => "Generation",
                    333:                     'idsn' => "ID/Student Number",
                    334:                     'hs'   => "Home Server",
                    335:                     'lg'   => "Login Data"
                    336: 				       );
1.78      www       337: 	my $genhelp=&Apache::loncommon::help_open_topic('Generation');
1.26      matthew   338: 	$r->print(<<ENDNEWUSER);
1.25      matthew   339: $dochead
1.72      sakharuk  340: <h1>$lt{'cnu'}</h1>
1.25      matthew   341: $forminfo
1.72      sakharuk  342: <h2>$lt{'nu'} "$ccuname" $lt{'id'} $ccdomain</h2>
1.31      matthew   343: <script type="text/javascript" language="Javascript">
1.20      harris41  344: $loginscript
1.31      matthew   345: </script>
1.20      harris41  346: <input type='hidden' name='makeuser' value='1' />
1.72      sakharuk  347: <h3>$lt{'pd'}</h3>
1.25      matthew   348: <p>
                    349: <table>
1.72      sakharuk  350: <tr><td>$lt{'fn'}  </td>
1.25      matthew   351:     <td><input type='text' name='cfirst'  size='15' /></td></tr>
1.72      sakharuk  352: <tr><td>$lt{'mn'} </td> 
1.25      matthew   353:     <td><input type='text' name='cmiddle' size='15' /></td></tr>
1.72      sakharuk  354: <tr><td>$lt{'ln'}   </td>
1.25      matthew   355:     <td><input type='text' name='clast'   size='15' /></td></tr>
1.78      www       356: <tr><td>$lt{'gen'}$genhelp</td>
1.25      matthew   357:     <td><input type='text' name='cgen'    size='5'  /></td></tr>
                    358: </table>
1.72      sakharuk  359: $lt{'idsn'} <input type='text' name='cstid'   size='15' /></p>
1.74      sakharuk  360: $lt{'hs'}: <select name="hserver" size="1"> $home_server_list </select>
1.25      matthew   361: <hr />
1.72      sakharuk  362: <h3>$lt{'lg'}</h3>
1.31      matthew   363: <p>$generalrule </p>
                    364: <p>$authformkrb </p>
                    365: <p>$authformint </p>
                    366: <p>$authformfsys</p>
                    367: <p>$authformloc </p>
1.26      matthew   368: ENDNEWUSER
1.25      matthew   369:     } else { # user already exists
1.79      albertel  370: 	my %lt=&Apache::lonlocal::texthash(
1.72      sakharuk  371:                     'cup'  => "Change User Privileges",
                    372:                     'usr'  => "User",                    
                    373:                     'id'   => "in domain",
                    374:                     'fn'   => "first name",
                    375:                     'mn'   => "middle name",
                    376:                     'ln'   => "last name",
                    377:                     'gen'  => "generation"
                    378: 				       );
1.26      matthew   379: 	$r->print(<<ENDCHANGEUSER);
1.25      matthew   380: $dochead
1.72      sakharuk  381: <h1>$lt{'cup'}</h1>
1.25      matthew   382: $forminfo
1.72      sakharuk  383: <h2>$lt{'usr'} "$ccuname" $lt{'id'} "$ccdomain"</h2>
1.26      matthew   384: ENDCHANGEUSER
1.28      matthew   385:         # Get the users information
                    386:         my %userenv = &Apache::lonnet::get('environment',
                    387:                           ['firstname','middlename','lastname','generation'],
                    388:                           $ccdomain,$ccuname);
                    389:         my %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
                    390:         $r->print(<<END);
                    391: <hr />
                    392: <table border="2">
                    393: <tr>
1.72      sakharuk  394: <th>$lt{'fn'}</th><th>$lt{'mn'}</th><th>$lt{'ln'}</th><th>$lt{'gen'}</th>
1.28      matthew   395: </tr>
                    396: <tr>
                    397: END
                    398:         foreach ('firstname','middlename','lastname','generation') {
                    399:            if (&Apache::lonnet::allowed('mau',$ccdomain)) {
                    400:               $r->print(<<"END");            
1.53      www       401: <td><input type="text" name="c$_" value="$userenv{$_}" size="15" /></td>
1.28      matthew   402: END
                    403:            } else {
                    404:                $r->print('<td>'.$userenv{$_}.'</td>');
                    405:            }
                    406:         }
1.72      sakharuk  407:       $r->print(<<END);
1.28      matthew   408: </tr>
                    409: </table>
                    410: END
1.25      matthew   411:         # Build up table of user roles to allow revocation of a role.
1.28      matthew   412:         my ($tmp) = keys(%rolesdump);
                    413:         unless ($tmp =~ /^(con_lost|error)/i) {
1.2       www       414:            my $now=time;
1.72      sakharuk  415: 	   my %lt=&Apache::lonlocal::texthash(
                    416: 		    'rer'  => "Revoke Existing Roles",
                    417:                     'rev'  => "Revoke",                    
                    418:                     'del'  => "Delete",
1.81      albertel  419: 		    'ren'  => "Re-Enable",
1.72      sakharuk  420:                     'rol'  => "Role",
                    421:                     'ext'  => "Extent",
                    422:                     'sta'  => "Start",
                    423:                     'end'  => "End"
                    424: 				       );
1.37      matthew   425:            $r->print(<<END);
                    426: <hr />
1.72      sakharuk  427: <h3>$lt{'rer'}</h3>
1.79      albertel  428: <table>
1.81      albertel  429: <tr><th>$lt{'rev'}</th><th>$lt{'ren'}</th><th>$lt{'del'}</th><th>$lt{'rol'}</th><th>$lt{'ext'}</th><th>$lt{'sta'}</th><th>$lt{'end'}</th>
1.37      matthew   430: END
1.79      albertel  431:            my (%roletext,%sortrole,%roleclass);
1.67      albertel  432: 	   foreach my $area (sort { my $a1=join('_',(split('_',$a))[1,0]);
                    433: 				    my $b1=join('_',(split('_',$b))[1,0]);
                    434: 				    return $a1 cmp $b1;
                    435: 				} keys(%rolesdump)) {
1.37      matthew   436:                next if ($area =~ /^rolesdef/);
1.79      albertel  437: 	       my $envkey=$area;
1.37      matthew   438:                my $role = $rolesdump{$area};
                    439:                my $thisrole=$area;
                    440:                $area =~ s/\_\w\w$//;
                    441:                my ($role_code,$role_end_time,$role_start_time) = 
                    442:                    split(/_/,$role);
1.64      www       443: # Is this a custom role? Get role owner and title.
                    444: 	       my ($croleudom,$croleuname,$croletitle)=
                    445: 	           ($role_code=~/^cr\/(\w+)\/(\w+)\/(\w+)$/);
1.37      matthew   446:                my $bgcol='ffffff';
                    447:                my $allowed=0;
1.53      www       448:                my $delallowed=0;
1.79      albertel  449: 	       my $sortkey=$role_code;
                    450: 	       my $class='Unknown';
1.37      matthew   451:                if ($area =~ /^\/(\w+)\/(\d\w+)/ ) {
1.79      albertel  452: 		   $class='Course';
1.57      matthew   453:                    my ($coursedom,$coursedir) = ($1,$2);
1.79      albertel  454: 		   $sortkey.="\0$1";
1.57      matthew   455:                    # $1.'_'.$2 is the course id (eg. 103_12345abcef103l3).
1.37      matthew   456:                    my %coursedata=
                    457:                        &Apache::lonnet::coursedescription($1.'_'.$2);
1.51      albertel  458: 		   my $carea;
                    459: 		   if (defined($coursedata{'description'})) {
1.79      albertel  460: 		       $carea=$coursedata{'description'}.
1.72      sakharuk  461:                            '<br />'.&mt('Domain').': '.$coursedom.('&nbsp;'x8).
1.57      matthew   462:      &Apache::loncommon::syllabuswrapper('Syllabus',$coursedir,$coursedom);
1.79      albertel  463: 		       $sortkey.="\0".$coursedata{'description'};
1.51      albertel  464: 		   } else {
1.72      sakharuk  465: 		       $carea=&mt('Unavailable course').': '.$area;
1.86      albertel  466: 		       $sortkey.="\0".&mt('Unavailable course').': '.$area;
1.51      albertel  467: 		   }
1.37      matthew   468:                    $inccourses{$1.'_'.$2}=1;
1.53      www       469:                    if ((&Apache::lonnet::allowed('c'.$role_code,$1.'/'.$2)) ||
                    470:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
1.37      matthew   471:                        $allowed=1;
                    472:                    }
1.53      www       473:                    if ((&Apache::lonnet::allowed('dro',$1)) ||
                    474:                        (&Apache::lonnet::allowed('dro',$ccdomain))) {
                    475:                        $delallowed=1;
                    476:                    }
1.64      www       477: # - custom role. Needs more info, too
                    478: 		   if ($croletitle) {
                    479: 		       if (&Apache::lonnet::allowed('ccr',$1.'/'.$2)) {
                    480: 			   $allowed=1;
                    481: 			   $thisrole.='.'.$role_code;
                    482: 		       }
                    483: 		   }
1.37      matthew   484:                    # Compute the background color based on $area
                    485:                    $bgcol=$1.'_'.$2;
1.62      www       486:                    $bgcol=~s/[^7-9a-e]//g;
                    487:                    $bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
1.37      matthew   488:                    if ($area=~/^\/(\w+)\/(\d\w+)\/(\w+)/) {
1.28      matthew   489:                        $carea.='<br>Section/Group: '.$3;
1.87      albertel  490: 		       $sortkey.="\0$3";
1.37      matthew   491:                    }
                    492:                    $area=$carea;
                    493:                } else {
1.79      albertel  494: 		   $sortkey.="\0".$area;
1.37      matthew   495:                    # Determine if current user is able to revoke privileges
                    496:                    if ($area=~ /^\/(\w+)\//) {
1.53      www       497:                        if ((&Apache::lonnet::allowed('c'.$role_code,$1)) ||
                    498:                        (&Apache::lonnet::allowed('c'.$role_code,$ccdomain))) {
1.37      matthew   499:                            $allowed=1;
                    500:                        }
1.53      www       501:                        if (((&Apache::lonnet::allowed('dro',$1))  ||
                    502:                             (&Apache::lonnet::allowed('dro',$ccdomain))) &&
                    503:                            ($role_code ne 'dc')) {
                    504:                            $delallowed=1;
                    505:                        }
1.37      matthew   506:                    } else {
                    507:                        if (&Apache::lonnet::allowed('c'.$role_code,'/')) {
                    508:                            $allowed=1;
                    509:                        }
                    510:                    }
1.79      albertel  511: 		   if ($role_code eq 'ca' || $role_code eq 'au') {
                    512: 		       $class='Construction Space';
                    513: 		   } elsif ($role_code eq 'su') {
                    514: 		       $class='System';
                    515: 		   } else {
                    516: 		       $class='Domain';
                    517: 		   }
1.37      matthew   518:                }
1.43      www       519:                if ($role_code eq 'ca') {
                    520:                    $area=~/\/(\w+)\/(\w+)/;
                    521: 		   if (&authorpriv($2,$1)) {
                    522: 		       $allowed=1;
                    523:                    } else {
                    524:                        $allowed=0;
1.37      matthew   525:                    }
                    526:                }
1.79      albertel  527: 	       $bgcol='77FF77';
1.37      matthew   528:                my $row = '';
1.62      www       529:                $row.='<tr bgcolor="#'.$bgcol.'"><td>';
1.37      matthew   530:                my $active=1;
                    531:                $active=0 if (($role_end_time) && ($now>$role_end_time));
                    532:                if (($active) && ($allowed)) {
                    533:                    $row.= '<input type="checkbox" name="rev:'.$thisrole.'">';
                    534:                } else {
1.56      www       535:                    if ($active) {
                    536:                       $row.='&nbsp;';
                    537: 		   } else {
1.72      sakharuk  538:                       $row.=&mt('expired or revoked');
1.56      www       539: 		   }
1.37      matthew   540:                }
1.53      www       541: 	       $row.='</td><td>';
1.81      albertel  542:                if ($allowed && !$active) {
                    543:                    $row.= '<input type="checkbox" name="ren:'.$thisrole.'">';
                    544:                } else {
                    545:                    $row.='&nbsp;';
                    546:                }
                    547: 	       $row.='</td><td>';
1.53      www       548:                if ($delallowed) {
                    549:                    $row.= '<input type="checkbox" name="del:'.$thisrole.'">';
                    550:                } else {
                    551:                    $row.='&nbsp;';
                    552:                }
1.64      www       553: 	       my $plaintext='';
                    554: 	       unless ($croletitle) {
                    555: 		   $plaintext=&Apache::lonnet::plaintext($role_code);
                    556: 	       } else {
                    557: 	           $plaintext=
                    558: 		"Customrole '$croletitle' defined by $croleuname\@$croleudom";
                    559: 	       }
                    560:                $row.= '</td><td>'.$plaintext.
1.37      matthew   561:                       '</td><td>'.$area.
                    562:                       '</td><td>'.($role_start_time?localtime($role_start_time)
                    563:                                                    : '&nbsp;' ).
                    564:                       '</td><td>'.($role_end_time  ?localtime($role_end_time)
                    565:                                                    : '&nbsp;' )
                    566:                       ."</td></tr>\n";
1.79      albertel  567: 	       $sortrole{$sortkey}=$envkey;
                    568: 	       $roletext{$envkey}=$row;
                    569: 	       $roleclass{$envkey}=$class;
                    570:                #$r->print($row);
1.28      matthew   571:            } # end of foreach        (table building loop)
1.79      albertel  572: 	   foreach my $type ('Construction Space','Course','Domain','System','Unknown') {
                    573: 	       my $output;
                    574: 	       foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                    575: 		   if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
                    576: 		       $output.=$roletext{$sortrole{$which}};
                    577: 		   }
                    578: 	       }
                    579: 	       if (defined($output)) {
                    580: 		   $r->print("<tr bgcolor='#BBffBB'>".
                    581: 			     "<td align='center' colspan='7'>".&mt($type)."</td>");
                    582: 	       }
                    583: 	       $r->print($output);
                    584: 	   }
1.2       www       585: 	   $r->print('</table>');
1.28      matthew   586:         }  # End of unless
1.20      harris41  587: 	my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
1.41      albertel  588: 	if ($currentauth=~/^krb(4|5):/) {
                    589: 	    $currentauth=~/^krb(4|5):(.*)/;
1.45      matthew   590: 	    my $krbdefdom=$1;
1.31      matthew   591:             my %param = ( formname => 'document.cu',
                    592:                           kerb_def_dom => $krbdefdom 
                    593:                           );
                    594:             $loginscript  = &Apache::loncommon::authform_header(%param);
1.20      harris41  595: 	}
1.26      matthew   596: 	# Check for a bad authentication type
1.41      albertel  597:         unless ($currentauth=~/^krb(4|5):/ or
1.20      harris41  598: 		$currentauth=~/^unix:/ or
                    599: 		$currentauth=~/^internal:/ or
                    600: 		$currentauth=~/^localauth:/
1.26      matthew   601: 		) { # bad authentication scheme
1.42      matthew   602: 	    if (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'})) {
1.73      sakharuk  603: 		my %lt=&Apache::lonlocal::texthash(
                    604:                                'err'   => "ERROR",
                    605: 			       'uuas'  => "This user has an unrecognized authentication scheme",
                    606:                                'sldb'  => "Please specify login data below",
                    607:                                'ld'    => "Login Data"
                    608: 						   );
1.26      matthew   609: 		$r->print(<<ENDBADAUTH);
1.21      harris41  610: <hr />
1.31      matthew   611: <script type="text/javascript" language="Javascript">
1.21      harris41  612: $loginscript
1.31      matthew   613: </script>
1.73      sakharuk  614: <font color='#ff0000'>$lt{'err'}:</font>
                    615: $lt{'uuas'} ($currentauth). $lt{'sldb'}.
                    616: <h3>$lt{'ld'}</h3>
1.31      matthew   617: <p>$generalrule</p>
                    618: <p>$authformkrb</p>
                    619: <p>$authformint</p>
                    620: <p>$authformfsys</p>
                    621: <p>$authformloc</p>
1.26      matthew   622: ENDBADAUTH
                    623:             } else { 
                    624:                 # This user is not allowed to modify the users 
                    625:                 # authentication scheme, so just notify them of the problem
1.73      sakharuk  626: 		my %lt=&Apache::lonlocal::texthash(
                    627:                                'err'   => "ERROR",
                    628: 			       'uuas'  => "This user has an unrecognized authentication scheme",
                    629:                                'adcs'  => "Please alert a domain coordinator of this situation"
                    630: 						   );
1.26      matthew   631: 		$r->print(<<ENDBADAUTH);
                    632: <hr />
1.31      matthew   633: <script type="text/javascript" language="Javascript">
1.26      matthew   634: $loginscript
1.31      matthew   635: </script>
1.73      sakharuk  636: <font color="#ff0000"> $lt{'err'}: </font>
                    637: $lt{'uuas'} ($currentauth). $lt{'adcs'}.
1.26      matthew   638: <hr />
                    639: ENDBADAUTH
                    640:             }
                    641:         } else { # Authentication type is valid
1.20      harris41  642: 	    my $authformcurrent='';
1.26      matthew   643: 	    my $authform_other='';
1.41      albertel  644: 	    if ($currentauth=~/^krb(4|5):/) {
1.20      harris41  645: 		$authformcurrent=$authformkrb;
1.31      matthew   646: 		$authform_other="<p>$authformint</p>\n".
                    647:                     "<p>$authformfsys</p><p>$authformloc</p>";
1.20      harris41  648: 	    }
                    649: 	    elsif ($currentauth=~/^internal:/) {
                    650: 		$authformcurrent=$authformint;
1.31      matthew   651: 		$authform_other="<p>$authformkrb</p>".
                    652:                     "<p>$authformfsys</p><p>$authformloc</p>";
1.20      harris41  653: 	    }
                    654: 	    elsif ($currentauth=~/^unix:/) {
                    655: 		$authformcurrent=$authformfsys;
1.31      matthew   656: 		$authform_other="<p>$authformkrb</p>".
                    657:                     "<p>$authformint</p><p>$authformloc;</p>";
1.20      harris41  658: 	    }
                    659: 	    elsif ($currentauth=~/^localauth:/) {
                    660: 		$authformcurrent=$authformloc;
1.31      matthew   661: 		$authform_other="<p>$authformkrb</p>".
                    662:                     "<p>$authformint</p><p>$authformfsys</p>";
1.20      harris41  663: 	    }
1.53      www       664:             $authformcurrent.=' <i>(will override current values)</i><br />';
1.42      matthew   665:             if (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'})) {
1.26      matthew   666: 		# Current user has login modification privileges
1.73      sakharuk  667: 		my %lt=&Apache::lonlocal::texthash(
                    668:                                'ccld'  => "Change Current Login Data",
                    669: 			       'enld'  => "Enter New Login Data"
                    670: 						   );
1.26      matthew   671: 		$r->print(<<ENDOTHERAUTHS);
1.21      harris41  672: <hr />
1.31      matthew   673: <script type="text/javascript" language="Javascript">
1.21      harris41  674: $loginscript
1.31      matthew   675: </script>
1.73      sakharuk  676: <h3>$lt{'ccld'}</h3>
1.31      matthew   677: <p>$generalrule</p>
                    678: <p>$authformnop</p>
                    679: <p>$authformcurrent</p>
1.73      sakharuk  680: <h3>$lt{'enld'}</h3>
1.26      matthew   681: $authform_other
                    682: ENDOTHERAUTHS
                    683:             }
                    684:         }  ## End of "check for bad authentication type" logic
1.25      matthew   685:     } ## End of new user/old user logic
1.72      sakharuk  686:     $r->print('<hr /><h3>'.&mt('Add Roles').'</h3>');
1.17      www       687: #
                    688: # Co-Author
                    689: # 
1.44      matthew   690:     if (&authorpriv($ENV{'user.name'},$ENV{'request.role.domain'}) &&
                    691:         ($ENV{'user.name'} ne $ccuname || $ENV{'user.domain'} ne $ccdomain)) {
                    692:         # No sense in assigning co-author role to yourself
1.17      www       693: 	my $cuname=$ENV{'user.name'};
1.42      matthew   694:         my $cudom=$ENV{'request.role.domain'};
1.72      sakharuk  695: 	   my %lt=&Apache::lonlocal::texthash(
                    696: 		    'cs'   => "Construction Space",
                    697:                     'act'  => "Activate",                    
                    698:                     'rol'  => "Role",
                    699:                     'ext'  => "Extent",
                    700:                     'sta'  => "Start",
1.80      www       701:                     'end'  => "End",
1.72      sakharuk  702:                     'cau'  => "Co-Author",
                    703:                     'ssd'  => "Set Start Date",
                    704:                     'sed'  => "Set End Date"
                    705: 				       );
1.17      www       706:        $r->print(<<ENDCOAUTH);
1.72      sakharuk  707: <h4>$lt{'cs'}</h4>
1.74      sakharuk  708: <table border=2><tr><th>$lt{'act'}</th><th>$lt{'rol'}</th><th>$lt{'ext'}</th>
1.72      sakharuk  709: <th>$lt{'sta'}</th><th>$lt{'end'}</th></tr>
1.17      www       710: <tr>
1.80      www       711: <td><input type=checkbox name="act_$cudom\_$cuname\_ca" /></td>
1.72      sakharuk  712: <td>$lt{'cau'}</td>
1.17      www       713: <td>$cudom\_$cuname</td>
1.80      www       714: <td><input type=hidden name="start_$cudom\_$cuname\_ca" value='' />
1.17      www       715: <a href=
1.72      sakharuk  716: "javascript:pjump('date_start','Start Date Co-Author',document.cu.start_$cudom\_$cuname\_ca.value,'start_$cudom\_$cuname\_ca','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.80      www       717: <td><input type=hidden name="end_$cudom\_$cuname\_ca" value='' />
1.17      www       718: <a href=
1.72      sakharuk  719: "javascript:pjump('date_end','End Date Co-Author',document.cu.end_$cudom\_$cuname\_ca.value,'end_$cudom\_$cuname\_ca','cu.pres','dateset')">$lt{'sed'}</a></td>
1.17      www       720: </tr>
                    721: </table>
                    722: ENDCOAUTH
                    723:     }
1.8       www       724: #
                    725: # Domain level
                    726: #
1.72      sakharuk  727:     $r->print('<h4>'.&mt('Domain Level').'</h4>'.
1.73      sakharuk  728:     '<table border=2><tr><th>'.&mt('Activate').'</th><th>'.&mt('Role').'</th><th>'.&mt('Extent').'</th>'.
                    729:     '<th>'.&mt('Start').'</th><th>'.&mt('End').'</th></tr>');
1.24      matthew   730:     foreach ( sort( keys(%incdomains))) {
1.2       www       731: 	my $thisdomain=$_;
1.69      albertel  732:         foreach ('dc','li','dg','au','sc') {
1.2       www       733:             if (&Apache::lonnet::allowed('c'.$_,$thisdomain)) {
1.8       www       734:                my $plrole=&Apache::lonnet::plaintext($_);
1.72      sakharuk  735: 	       my %lt=&Apache::lonlocal::texthash(
                    736:                     'ssd'  => "Set Start Date",
                    737:                     'sed'  => "Set End Date"
                    738: 				       );
1.8       www       739:                $r->print(<<ENDDROW);
                    740: <tr>
                    741: <td><input type=checkbox name="act_$thisdomain\_$_"></td>
                    742: <td>$plrole</td>
                    743: <td>$thisdomain</td>
                    744: <td><input type=hidden name="start_$thisdomain\_$_" value=''>
                    745: <a href=
1.72      sakharuk  746: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$thisdomain\_$_.value,'start_$thisdomain\_$_','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.8       www       747: <td><input type=hidden name="end_$thisdomain\_$_" value=''>
                    748: <a href=
1.72      sakharuk  749: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$thisdomain\_$_.value,'end_$thisdomain\_$_','cu.pres','dateset')">$lt{'sed'}</a></td>
1.8       www       750: </tr>
                    751: ENDDROW
1.2       www       752:             }
1.24      matthew   753:         } 
                    754:     }
1.8       www       755:     $r->print('</table>');
                    756: #
                    757: # Course level
                    758: #
1.88    ! raeburn   759: 
        !           760:     if ($ENV{'request.role'} =~ m-^dc\./(\w+)/$-) {
        !           761:         $r->print(&course_level_dc($1));
        !           762:         $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setCourse()">'."\n");
        !           763:     } else {
        !           764:         $r->print(&course_level_table(%inccourses));
        !           765:         $r->print("<hr /><input type=submit value=\"".&mt('Modify User')."\">\n");
        !           766:     }
1.26      matthew   767:     $r->print("</form></body></html>");
1.2       www       768: }
1.1       www       769: 
1.4       www       770: # ================================================================= Phase Three
1.42      matthew   771: sub update_user_data {
1.4       www       772:     my $r=shift;
1.29      matthew   773:     my $uhome=&Apache::lonnet::homeserver($ENV{'form.ccuname'},
                    774:                                           $ENV{'form.ccdomain'});
1.27      matthew   775:     # Error messages
1.73      sakharuk  776:     my $error     = '<font color="#ff0000">'.&mt('Error').':</font>';
1.27      matthew   777:     my $end       = '</body></html>';
                    778:     # Print header
1.4       www       779:     $r->print(<<ENDTHREEHEAD);
                    780: <html>
                    781: <head>
                    782: <title>The LearningOnline Network with CAPA</title>
                    783: </head>
                    784: ENDTHREEHEAD
1.40      www       785:     my $title;
                    786:     if (exists($ENV{'form.makeuser'})) {
                    787: 	$title='Set Privileges for New User';
                    788:     } else {
                    789:         $title='Modify User Privileges';
                    790:     }
                    791:     $r->print(&Apache::loncommon::bodytag($title));
1.27      matthew   792:     # Check Inputs
1.29      matthew   793:     if (! $ENV{'form.ccuname'} ) {
1.73      sakharuk  794: 	$r->print($error.&mt('No login name specified').'.'.$end);
1.27      matthew   795: 	return;
                    796:     }
1.29      matthew   797:     if (  $ENV{'form.ccuname'}  =~/\W/) {
1.73      sakharuk  798: 	$r->print($error.&mt('Invalid login name').'.  '.
                    799: 		  &mt('Only letters, numbers, and underscores are valid').'.'.
1.27      matthew   800: 		  $end);
                    801: 	return;
                    802:     }
1.29      matthew   803:     if (! $ENV{'form.ccdomain'}       ) {
1.73      sakharuk  804: 	$r->print($error.&mt('No domain specified').'.'.$end);
1.27      matthew   805: 	return;
                    806:     }
1.29      matthew   807:     if (  $ENV{'form.ccdomain'} =~/\W/) {
1.73      sakharuk  808: 	$r->print($error.&mt ('Invalid domain name').'.  '.
                    809: 		  &mt('Only letters, numbers, and underscores are valid').'.'.
1.27      matthew   810: 		  $end);
                    811: 	return;
                    812:     }
1.29      matthew   813:     if (! exists($ENV{'form.makeuser'})) {
                    814:         # Modifying an existing user, so check the validity of the name
                    815:         if ($uhome eq 'no_host') {
1.73      sakharuk  816:             $r->print($error.&mt('Unable to determine home server for ').
                    817:                       $ENV{'form.ccuname'}.&mt(' in domain ').
1.29      matthew   818:                       $ENV{'form.ccdomain'}.'.');
                    819:             return;
                    820:         }
                    821:     }
1.27      matthew   822:     # Determine authentication method and password for the user being modified
                    823:     my $amode='';
                    824:     my $genpwd='';
                    825:     if ($ENV{'form.login'} eq 'krb') {
1.41      albertel  826: 	$amode='krb';
                    827: 	$amode.=$ENV{'form.krbver'};
1.30      matthew   828: 	$genpwd=$ENV{'form.krbarg'};
1.27      matthew   829:     } elsif ($ENV{'form.login'} eq 'int') {
                    830: 	$amode='internal';
1.30      matthew   831: 	$genpwd=$ENV{'form.intarg'};
1.27      matthew   832:     } elsif ($ENV{'form.login'} eq 'fsys') {
                    833: 	$amode='unix';
1.30      matthew   834: 	$genpwd=$ENV{'form.fsysarg'};
1.27      matthew   835:     } elsif ($ENV{'form.login'} eq 'loc') {
                    836: 	$amode='localauth';
                    837: 	$genpwd=$ENV{'form.locarg'};
                    838: 	$genpwd=" " if (!$genpwd);
1.35      matthew   839:     } elsif (($ENV{'form.login'} eq 'nochange') ||
                    840:              ($ENV{'form.login'} eq ''        )) { 
1.34      matthew   841:         # There is no need to tell the user we did not change what they
                    842:         # did not ask us to change.
1.35      matthew   843:         # If they are creating a new user but have not specified login
                    844:         # information this will be caught below.
1.30      matthew   845:     } else {
1.73      sakharuk  846: 	    $r->print($error.&mt('Invalid login mode or password').$end);    
1.30      matthew   847: 	    return;
1.27      matthew   848:     }
                    849:     if ($ENV{'form.makeuser'}) {
                    850:         # Create a new user
1.73      sakharuk  851: 	my %lt=&Apache::lonlocal::texthash(
                    852:                     'cru'  => "Creating user",                    
                    853:                     'id'   => "in domain"
                    854: 					   );
1.27      matthew   855: 	$r->print(<<ENDNEWUSERHEAD);
1.73      sakharuk  856: <h3>$lt{'cru'} "$ENV{'form.ccuname'}" $lt{'id'} "$ENV{'form.ccdomain'}"</h3>
1.27      matthew   857: ENDNEWUSERHEAD
                    858:         # Check for the authentication mode and password
                    859:         if (! $amode || ! $genpwd) {
1.73      sakharuk  860: 	    $r->print($error.&mt('Invalid login mode or password').$end);    
1.27      matthew   861: 	    return;
1.18      albertel  862: 	}
1.29      matthew   863:         # Determine desired host
                    864:         my $desiredhost = $ENV{'form.hserver'};
                    865:         if (lc($desiredhost) eq 'default') {
                    866:             $desiredhost = undef;
                    867:         } else {
1.39      matthew   868:             my %home_servers = &Apache::loncommon::get_library_servers
1.32      matthew   869:                 ($ENV{'form.ccdomain'});  
1.29      matthew   870:             if (! exists($home_servers{$desiredhost})) {
1.73      sakharuk  871:                 $r->print($error.&mt('Invalid home server specified'));
1.29      matthew   872:                 return;
                    873:             }
                    874:         }
1.27      matthew   875: 	# Call modifyuser
                    876: 	my $result = &Apache::lonnet::modifyuser
1.29      matthew   877: 	    ($ENV{'form.ccdomain'},$ENV{'form.ccuname'},$ENV{'form.cstid'},
                    878:              $amode,$genpwd,$ENV{'form.cfirst'},
                    879:              $ENV{'form.cmiddle'},$ENV{'form.clast'},$ENV{'form.cgen'},
                    880:              undef,$desiredhost
1.27      matthew   881: 	     );
1.77      www       882: 	$r->print(&mt('Generating user').': '.$result);
1.29      matthew   883:         my $home = &Apache::lonnet::homeserver($ENV{'form.ccuname'},
                    884:                                                $ENV{'form.ccdomain'});
1.77      www       885:         $r->print('<br />'.&mt('Home server').': '.$home.' '.
1.29      matthew   886:                   $Apache::lonnet::libserv{$home});
1.35      matthew   887:     } elsif (($ENV{'form.login'} ne 'nochange') &&
                    888:              ($ENV{'form.login'} ne ''        )) {
1.27      matthew   889: 	# Modify user privileges
1.73      sakharuk  890:     my %lt=&Apache::lonlocal::texthash(
                    891:                     'usr'  => "User",                    
                    892:                     'id'   => "in domain"
                    893: 				       );
1.27      matthew   894: 	$r->print(<<ENDMODIFYUSERHEAD);
1.73      sakharuk  895: <h2>$lt{'usr'} "$ENV{'form.ccuname'}" $lt{'id'} "$ENV{'form.ccdomain'}"</h2>
1.27      matthew   896: ENDMODIFYUSERHEAD
                    897:         if (! $amode || ! $genpwd) {
                    898: 	    $r->print($error.'Invalid login mode or password'.$end);    
                    899: 	    return;
1.20      harris41  900: 	}
1.27      matthew   901: 	# Only allow authentification modification if the person has authority
1.36      matthew   902: 	if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'})) {
1.20      harris41  903: 	    $r->print('Modifying authentication: '.
1.31      matthew   904:                       &Apache::lonnet::modifyuserauth(
1.29      matthew   905: 		       $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
1.21      harris41  906:                        $amode,$genpwd));
1.73      sakharuk  907:             $r->print('<br>'.&mt('Home server').': '.&Apache::lonnet::homeserver
1.29      matthew   908: 		  ($ENV{'form.ccuname'},$ENV{'form.ccdomain'}));
1.4       www       909: 	} else {
1.27      matthew   910: 	    # Okay, this is a non-fatal error.
1.73      sakharuk  911: 	    $r->print($error.&mt('You do not have the authority to modify this users authentification information').'.');    
1.27      matthew   912: 	}
1.28      matthew   913:     }
                    914:     ##
                    915:     if (! $ENV{'form.makeuser'} ) {
                    916:         # Check for need to change
                    917:         my %userenv = &Apache::lonnet::get
                    918:             ('environment',['firstname','middlename','lastname','generation'],
1.29      matthew   919:              $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
1.28      matthew   920:         my ($tmp) = keys(%userenv);
                    921:         if ($tmp =~ /^(con_lost|error)/i) { 
                    922:             %userenv = ();
                    923:         }
                    924:         # Check to see if we need to change user information
                    925:         foreach ('firstname','middlename','lastname','generation') {
                    926:             # Strip leading and trailing whitespace
                    927:             $ENV{'form.c'.$_} =~ s/(\s+$|^\s+)//g; 
                    928:         }
1.29      matthew   929:         if (&Apache::lonnet::allowed('mau',$ENV{'form.ccdomain'}) && 
1.28      matthew   930:             ($ENV{'form.cfirstname'}  ne $userenv{'firstname'}  ||
                    931:              $ENV{'form.cmiddlename'} ne $userenv{'middlename'} ||
                    932:              $ENV{'form.clastname'}   ne $userenv{'lastname'}   ||
                    933:              $ENV{'form.cgeneration'} ne $userenv{'generation'} )) {
                    934:             # Make the change
                    935:             my %changeHash;
                    936:             $changeHash{'firstname'}  = $ENV{'form.cfirstname'};
                    937:             $changeHash{'middlename'} = $ENV{'form.cmiddlename'};
                    938:             $changeHash{'lastname'}   = $ENV{'form.clastname'};
                    939:             $changeHash{'generation'} = $ENV{'form.cgeneration'};
                    940:             my $putresult = &Apache::lonnet::put
                    941:                 ('environment',\%changeHash,
1.29      matthew   942:                  $ENV{'form.ccdomain'},$ENV{'form.ccuname'});
1.28      matthew   943:             if ($putresult eq 'ok') {
                    944:             # Tell the user we changed the name
1.73      sakharuk  945: 		my %lt=&Apache::lonlocal::texthash(
                    946:                              'uic'  => "User Information Changed",             
                    947:                              'frst' => "first",
                    948:                              'mddl' => "middle",
                    949:                              'lst'  => "last",
                    950: 			     'gen'  => "generation",
                    951:                              'prvs' => "Previous",
                    952:                              'chto' => "Changed To"
                    953: 						   );
1.28      matthew   954:                 $r->print(<<"END");
                    955: <table border="2">
1.73      sakharuk  956: <caption>$lt{'uic'}</caption>
1.28      matthew   957: <tr><th>&nbsp;</th>
1.73      sakharuk  958:     <th>$lt{'frst'}</th>
                    959:     <th>$lt{'mddl'}</th>
                    960:     <th>$lt{'lst'}</th>
                    961:     <th>$lt{'gen'}</th></tr>
                    962: <tr><td>$lt{'prvs'}</td>
1.28      matthew   963:     <td>$userenv{'firstname'}  </td>
                    964:     <td>$userenv{'middlename'} </td>
                    965:     <td>$userenv{'lastname'}   </td>
                    966:     <td>$userenv{'generation'} </td></tr>
1.73      sakharuk  967: <tr><td>$lt{'chto'}</td>
1.28      matthew   968:     <td>$ENV{'form.cfirstname'}  </td>
                    969:     <td>$ENV{'form.cmiddlename'} </td>
                    970:     <td>$ENV{'form.clastname'}   </td>
                    971:     <td>$ENV{'form.cgeneration'} </td></tr>
                    972: </table>
                    973: END
                    974:             } else { # error occurred
1.73      sakharuk  975:                 $r->print("<h2>".&mt('Unable to successfully change environment for')." ".
                    976:                       $ENV{'form.ccuname'}." ".&mt('in domain')." ".
1.29      matthew   977:                       $ENV{'form.ccdomain'}."</h2>");
1.28      matthew   978:             }
                    979:         }  else { # End of if ($ENV ... ) logic
                    980:             # They did not want to change the users name but we can
                    981:             # still tell them what the name is
1.73      sakharuk  982: 	    my %lt=&Apache::lonlocal::texthash(
                    983:                            'usr'  => "User",                    
                    984:                            'id'   => "in domain",
                    985:                            'gen'  => "Generation"
                    986: 					       );
1.28      matthew   987:                 $r->print(<<"END");
1.74      sakharuk  988: <h2>$lt{'usr'} "$ENV{'form.ccuname'}" $lt{'id'} "$ENV{'form.ccdomain'}"</h2>
1.28      matthew   989: <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} </h4>
1.73      sakharuk  990: <h4>$lt{'gen'}: $userenv{'generation'}</h4>
1.28      matthew   991: END
                    992:         }
1.4       www       993:     }
1.27      matthew   994:     ##
1.4       www       995:     my $now=time;
1.73      sakharuk  996:     $r->print('<h3>'.&mt('Modifying Roles').'</h3>');
1.24      matthew   997:     foreach (keys (%ENV)) {
1.27      matthew   998: 	next if (! $ENV{$_});
                    999: 	# Revoke roles
                   1000: 	if ($_=~/^form\.rev/) {
1.64      www      1001: 	    if ($_=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) {
                   1002: # Revoke standard role
1.73      sakharuk 1003: 	        $r->print(&mt('Revoking').' '.$2.' in '.$1.': <b>'.
1.65      www      1004:                      &Apache::lonnet::revokerole($ENV{'form.ccdomain'},
                   1005:                      $ENV{'form.ccuname'},$1,$2).'</b><br>');
1.53      www      1006: 		if ($2 eq 'st') {
                   1007: 		    $1=~/^\/(\w+)\/(\w+)/;
                   1008: 		    my $cid=$1.'_'.$2;
1.73      sakharuk 1009: 		    $r->print(&mt('Drop from classlist').': <b>'.
1.53      www      1010: 			 &Apache::lonnet::critical('put:'.
                   1011:                              $ENV{'course.'.$cid.'.domain'}.':'.
                   1012: 	                     $ENV{'course.'.$cid.'.num'}.':classlist:'.
                   1013:                          &Apache::lonnet::escape($ENV{'form.ccuname'}.':'.
                   1014:                              $ENV{'form.ccdomain'}).'='.
                   1015:                          &Apache::lonnet::escape($now.':'),
1.56      www      1016: 	                     $ENV{'course.'.$cid.'.home'}).'</b><br>');
1.53      www      1017: 		}
                   1018: 	    } 
1.64      www      1019: 	    if ($_=~/^form\.rev\:([^\_]+)\_cr\.cr\/(\w+)\/(\w+)\/(\w+)$/) {
                   1020: # Revoke custom role
1.73      sakharuk 1021: 		$r->print(&mt('Revoking custom role').
                   1022:                       ' '.$4.' by '.$3.'@'.$2.' in '.$1.': <b>'.
                   1023:                       &Apache::lonnet::revokecustomrole($ENV{'form.ccdomain'},
1.65      www      1024: 				  $ENV{'form.ccuname'},$1,$2,$3,$4).
1.64      www      1025: 		'</b><br>');
                   1026: 	    }
1.53      www      1027: 	} elsif ($_=~/^form\.del/) {
                   1028: 	    if ($_=~/^form\.del\:([^\_]+)\_([^\_]+)$/) {
1.73      sakharuk 1029: 	        $r->print(&mt('Deleting').' '.$2.' in '.$1.': '.
1.53      www      1030:                      &Apache::lonnet::assignrole($ENV{'form.ccdomain'},
                   1031:                      $ENV{'form.ccuname'},$1,$2,$now,0,1).'<br>');
1.27      matthew  1032: 		if ($2 eq 'st') {
                   1033: 		    $1=~/^\/(\w+)\/(\w+)/;
                   1034: 		    my $cid=$1.'_'.$2;
1.73      sakharuk 1035: 		    $r->print(&mt('Drop from classlist').': <b>'.
1.27      matthew  1036: 			 &Apache::lonnet::critical('put:'.
                   1037:                              $ENV{'course.'.$cid.'.domain'}.':'.
                   1038: 	                     $ENV{'course.'.$cid.'.num'}.':classlist:'.
1.29      matthew  1039:                          &Apache::lonnet::escape($ENV{'form.ccuname'}.':'.
                   1040:                              $ENV{'form.ccdomain'}).'='.
1.27      matthew  1041:                          &Apache::lonnet::escape($now.':'),
1.56      www      1042: 	                     $ENV{'course.'.$cid.'.home'}).'</b><br>');
1.81      albertel 1043: 		}
                   1044: 	    } 
                   1045: 	} elsif ($_=~/^form\.ren/) {
                   1046: 	    if ($_=~/^form\.ren\:([^\_]+)\_([^\_]+)$/) {
                   1047: 		my $result=&Apache::lonnet::assignrole($ENV{'form.ccdomain'},
                   1048: 					 $ENV{'form.ccuname'},$1,$2,0,$now);
                   1049: 		$r->print(&mt('Re-Enabling [_1] in [_2]: [_3]',
                   1050: 			      $2,$1,$result).'<br />');
                   1051: 		if ($2 eq 'st') {
                   1052: 		    $1=~/^\/(\w+)\/(\w+)/;
                   1053: 		    my $cid=$1.'_'.$2;
                   1054: 		    $r->print(&mt('Add to classlist').': <b>'.
                   1055: 			      &Apache::lonnet::critical(
                   1056: 				  'put:'.$ENV{'course.'.$cid.'.domain'}.':'.
                   1057: 	                           $ENV{'course.'.$cid.'.num'}.':classlist:'.
                   1058:                                    &Apache::lonnet::escape(
                   1059:                                        $ENV{'form.ccuname'}.':'.
                   1060:                                        $ENV{'form.ccdomain'} ).'='.
                   1061:                                    &Apache::lonnet::escape(':'.$now),
                   1062: 				       $ENV{'course.'.$cid.'.home'})
                   1063: 			      .'</b><br>');
1.27      matthew  1064: 		}
                   1065: 	    } 
                   1066: 	} elsif ($_=~/^form\.act/) {
1.83      albertel 1067: 	    if ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_cr_cr_([^\_]+)_(\w+)_([^\_]+)$/) {
1.65      www      1068:                 # Activate a custom role
1.83      albertel 1069: 		my ($one,$two,$three,$four,$five)=($1,$2,$3,$4,$5);
                   1070: 		my $url='/'.$one.'/'.$two;
                   1071: 		my $full=$one.'_'.$two.'_cr_cr_'.$three.'_'.$four.'_'.$five;
1.65      www      1072: 
1.88    ! raeburn  1073:                 my $start = ( $ENV{'form.start_'.$full} ?
        !          1074:                               $ENV{'form.start_'.$full} :
        !          1075:                               $now );
        !          1076:                 my $end   = ( $ENV{'form.end_'.$full} ?
        !          1077:                               $ENV{'form.end_'.$full} :
        !          1078:                               0 );
        !          1079:                                                                                      
        !          1080:                 # split multiple sections
        !          1081:                 my %sections = ();
        !          1082:                 my $num_sections = &build_roles($ENV{'form.sec_'.$full},\%sections);
        !          1083:                 if ($num_sections == 0) {
        !          1084:                     $r->print(&commit_customrole($url,$three,$four,$five,$start,$end));
        !          1085:                 } else {
        !          1086:                     foreach (sort {$a cmp $b} keys %sections) {
        !          1087:                         my $securl = $url.'/'.$_;
        !          1088: 		        $r->print(&commit_customrole($securl,$three,$four,$five,$start,$end));
        !          1089:                     }
        !          1090:                 }
1.65      www      1091: 	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)\_([^\_]+)$/) {
1.27      matthew  1092: 		# Activate roles for sections with 3 id numbers
                   1093: 		# set start, end times, and the url for the class
1.83      albertel 1094: 		my ($one,$two,$three)=($1,$2,$3);
                   1095: 		my $start = ( $ENV{'form.start_'.$one.'_'.$two.'_'.$three} ? 
                   1096: 			      $ENV{'form.start_'.$one.'_'.$two.'_'.$three} : 
1.27      matthew  1097: 			      $now );
1.83      albertel 1098: 		my $end   = ( $ENV{'form.end_'.$one.'_'.$two.'_'.$three} ? 
                   1099: 			      $ENV{'form.end_'.$one.'_'.$two.'_'.$three} :
1.27      matthew  1100: 			      0 );
1.83      albertel 1101: 		my $url='/'.$one.'/'.$two;
1.88    ! raeburn  1102:                 my $type = 'three';
        !          1103:                 # split multiple sections
        !          1104:                 my %sections = ();
        !          1105:                 my $num_sections = &build_roles($ENV{'form.sec_'.$one.'_'.$two.'_'.$three},\%sections);
        !          1106:                 if ($num_sections == 0) {
        !          1107:                     $r->print(&commit_standardrole($url,$three,$start,$end,$one,$two));
        !          1108:                 } else {
        !          1109:                     my $emptysec = 0;
        !          1110:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
        !          1111:                         $sec =~ s/\W//g;
        !          1112:                         if ($sec ne '') {  
        !          1113:                             my $securl = $url.'/'.$sec;
        !          1114:                             $r->print(&commit_standardrole($securl,$three,$start,$end,$one,$two));
        !          1115:                         } else {
        !          1116:                             $emptysec = 1;
        !          1117:                         }
        !          1118:                     }
        !          1119:                     if ($emptysec) {
        !          1120:                         $r->print(&commit_standardrole($url,$three,$start,$end,$one,$two));
        !          1121:                     }
        !          1122:                 } 
1.27      matthew  1123: 	    } elsif ($_=~/^form\.act\_([^\_]+)\_([^\_]+)$/) {
                   1124: 		# Activate roles for sections with two id numbers
                   1125: 		# set start, end times, and the url for the class
                   1126: 		my $start = ( $ENV{'form.start_'.$1.'_'.$2} ? 
                   1127: 			      $ENV{'form.start_'.$1.'_'.$2} : 
                   1128: 			      $now );
                   1129: 		my $end   = ( $ENV{'form.end_'.$1.'_'.$2} ? 
                   1130: 			      $ENV{'form.end_'.$1.'_'.$2} :
                   1131: 			      0 );
                   1132: 		my $url='/'.$1.'/';
1.88    ! raeburn  1133:                 # split multiple sections
        !          1134:                 my %sections = ();
        !          1135:                 my $num_sections = &build_roles($ENV{'form.sec_'.$1.'_'.$2},\%sections);
        !          1136:                 if ($num_sections == 0) {
        !          1137:                     $r->print(&commit_standardrole($url,$2,$start,$end,$1));
        !          1138:                 } else {
        !          1139:                     my $emptysec = 0;
        !          1140:                     foreach my $sec (sort {$a cmp $b} keys %sections) {
        !          1141:                         if ($sec ne '') {
        !          1142:                             my $securl = $url.'/'.$sec;
        !          1143:                             $r->print(&commit_standardrole($securl,$2,$start,$end,$1));
        !          1144:                         } else {
        !          1145:                             $emptysec = 1;
        !          1146:                         }
        !          1147:                     }
        !          1148:                     if ($emptysec) {
        !          1149:                         $r->print(&commit_standardrole($url,$2,$start,$end,$1));
        !          1150:                     }
        !          1151:                 }
1.27      matthew  1152: 		# Assign the role and report it.
1.73      sakharuk 1153: 		$r->print(&mt('Assigning').' '.$2.' in '.$url.': '.
                   1154:                          ($start?', '.&mt('starting').' '.localtime($start):'').
                   1155:                          ($end?', '.&mt('ending').' '.localtime($end):'').': <b>'.
1.27      matthew  1156:                           &Apache::lonnet::assignrole(
1.29      matthew  1157:                               $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
1.27      matthew  1158:                               $url,$2,$end,$start)
1.56      www      1159: 			  .'</b><br>');
1.64      www      1160: 	    } else {
1.73      sakharuk 1161: 		$r->print('<p>'.&mt('ERROR').': '.&mt('Unknown command').' <tt>'.$_.'</tt></p><br>');
1.64      www      1162:             }
1.27      matthew  1163: 	} 
                   1164:     } # End of foreach (keys(%ENV))
1.75      www      1165: # Flush the course logs so reverse user roles immediately updated
                   1166:     &Apache::lonnet::flushcourselogs();
1.5       www      1167:     $r->print('</body></html>');
1.4       www      1168: }
                   1169: 
1.88    ! raeburn  1170: sub commit_customrole {
        !          1171:     my ($url,$three,$four,$five,$end,$start) = @_;
        !          1172:     my $output = &mt('Assigning custom role').' "'.$five.'" by '.$four.'@'.$three.' in '.$url.
        !          1173:                          ($start?', '.&mt('starting').' '.localtime($start):'').
        !          1174:                          ($end?', ending '.localtime($end):'').': <b>'.
        !          1175:               &Apache::lonnet::assigncustomrole(
        !          1176:         $ENV{'form.ccdomain'},$ENV{'form.ccuname'},$url,$three,$four,$five,$end,$start).
        !          1177:               '</b><br>';
        !          1178:     return $output;
        !          1179: }
        !          1180: 
        !          1181: sub commit_standardrole {
        !          1182:     my ($url,$three,$start,$end,$one,$two) = @_;
        !          1183:     my $output = (&mt('Assigning').' '.$three.' in '.$url.
        !          1184:            ($start?', '.&mt('starting').' '.localtime($start):'').
        !          1185:            ($end?', '.&mt('ending').' '.localtime($end):'').': <b>'.
        !          1186:            &Apache::lonnet::assignrole(
        !          1187:                $ENV{'form.ccdomain'},$ENV{'form.ccuname'},
        !          1188:                $url,$three,$end,$start).
        !          1189:                '</b><br>');
        !          1190: 
        !          1191: # Handle students differently
        !          1192:     if (($three eq 'st') && defined($one) && defined($two)) {
        !          1193:         $url=~/^\/(\w+)\/(\w+)/;
        !          1194:         my $cid=$one.'_'.$two;
        !          1195:         $output .= (&mt('Add to classlist').': <b>'.
        !          1196:                 &Apache::lonnet::critical(
        !          1197:                 'put:'.$ENV{'course.'.$cid.'.domain'}.':'.
        !          1198:                 $ENV{'course.'.$cid.'.num'}.':classlist:'.
        !          1199:                 &Apache::lonnet::escape(
        !          1200:                 $ENV{'form.ccuname'}.':'.
        !          1201:                 $ENV{'form.ccdomain'} ).'='.
        !          1202:                 &Apache::lonnet::escape($end.':'.$start),
        !          1203:                 $ENV{'course.'.$cid.'.home'})
        !          1204:                .'</b><br>');
        !          1205:     }
        !          1206:     return $output;
        !          1207: }
        !          1208: 
        !          1209: 
        !          1210: sub build_roles {
        !          1211:     my ($sectionstr,$sections) = @_;
        !          1212:     my $num_sections = 0;
        !          1213:     if ($sectionstr=~ /,/) {
        !          1214:         my @secnums = split/,/,$sectionstr;
        !          1215:         foreach my $sec (@secnums) {
        !          1216:             $sec =~ ~s/\W//g;
        !          1217:             unless ($sec eq "") {
        !          1218:                 if (exists($$sections{$sec})) {
        !          1219:                     $$sections{$sec} ++;
        !          1220:                 } else {
        !          1221:                     $$sections{$sec} = 1;
        !          1222:                     $num_sections ++;
        !          1223:                 }
        !          1224:             }
        !          1225:         }
        !          1226:     } else {
        !          1227:         $sectionstr=~s/\W//g;
        !          1228:         unless ($sectionstr eq '') {
        !          1229:             $$sections{$sectionstr} = 1;
        !          1230:             $num_sections ++;
        !          1231:         }
        !          1232:     }
        !          1233:                                                                                      
        !          1234:     return $num_sections;
        !          1235: }
        !          1236: 
1.58      www      1237: # ========================================================== Custom Role Editor
                   1238: 
                   1239: sub custom_role_editor {
                   1240:     my $r=shift;
                   1241:     my $rolename=$ENV{'form.rolename'};
                   1242: 
1.59      www      1243:     if ($rolename eq 'make new role') {
                   1244: 	$rolename=$ENV{'form.newrolename'};
                   1245:     }
                   1246: 
1.63      www      1247:     $rolename=~s/[^A-Za-z0-9]//gs;
1.58      www      1248: 
                   1249:     unless ($rolename) {
                   1250: 	&print_username_entry_form($r);
                   1251:         return;
                   1252:     }
                   1253: 
                   1254:     $r->print(&Apache::loncommon::bodytag(
1.59      www      1255:                      'Create Users, Change User Privileges').'<h2>');
1.61      www      1256:     my $syspriv='';
                   1257:     my $dompriv='';
                   1258:     my $coursepriv='';
1.59      www      1259:     my ($rdummy,$roledef)=
                   1260: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
1.60      www      1261: # ------------------------------------------------------- Does this role exist?
1.59      www      1262:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 1263: 	$r->print(&mt('Existing Role').' "');
1.61      www      1264: # ------------------------------------------------- Get current role privileges
                   1265: 	($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
1.59      www      1266:     } else {
1.73      sakharuk 1267: 	$r->print(&mt('New Role').' "');
1.59      www      1268: 	$roledef='';
                   1269:     }
                   1270:     $r->print($rolename.'"</h2>');
1.60      www      1271: # ------------------------------------------------------- What can be assigned?
                   1272:     my %full=();
                   1273:     my %courselevel=();
1.61      www      1274:     my %courselevelcurrent=();
1.60      www      1275:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   1276: 	my ($priv,$restrict)=split(/\&/,$_);
                   1277:         unless ($restrict) { $restrict='F'; }
                   1278:         $courselevel{$priv}=$restrict;
1.61      www      1279:         if ($coursepriv=~/\:$priv/) {
                   1280: 	    $courselevelcurrent{$priv}=1;
                   1281: 	}
1.60      www      1282: 	$full{$priv}=1;
                   1283:     }
                   1284:     my %domainlevel=();
1.61      www      1285:     my %domainlevelcurrent=();
1.60      www      1286:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   1287: 	my ($priv,$restrict)=split(/\&/,$_);
                   1288:         unless ($restrict) { $restrict='F'; }
                   1289:         $domainlevel{$priv}=$restrict;
1.61      www      1290:         if ($dompriv=~/\:$priv/) {
                   1291: 	    $domainlevelcurrent{$priv}=1;
                   1292: 	}
1.60      www      1293: 	$full{$priv}=1;
                   1294:     }
1.61      www      1295:     my %systemlevel=();
                   1296:     my %systemlevelcurrent=();
                   1297:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   1298: 	my ($priv,$restrict)=split(/\&/,$_);
                   1299:         unless ($restrict) { $restrict='F'; }
                   1300:         $systemlevel{$priv}=$restrict;
                   1301:         if ($syspriv=~/\:$priv/) {
                   1302: 	    $systemlevelcurrent{$priv}=1;
                   1303: 	}
                   1304: 	$full{$priv}=1;
                   1305:     }
1.73      sakharuk 1306:     my %lt=&Apache::lonlocal::texthash(
                   1307: 		    'prv'  => "Privilege",
                   1308: 		    'crl'  => "Course Level",
                   1309:                     'dml'  => "Domain Level",
                   1310:                     'ssl'  => "System Level"
                   1311: 				       );
1.61      www      1312:     $r->print(<<ENDCCF);
                   1313: <form method="post">
                   1314: <input type="hidden" name="phase" value="set_custom_roles" />
                   1315: <input type="hidden" name="rolename" value="$rolename" />
                   1316: <table border="2">
1.73      sakharuk 1317: <tr><th>$lt{'prv'}</th><th>$lt{'crl'}</th><th>$lt{'dml'}</th>
                   1318: <th>$lt{'ssl'}</th></tr>
1.61      www      1319: ENDCCF
1.60      www      1320:     foreach (sort keys %full) {
                   1321: 	$r->print('<tr><td>'.&Apache::lonnet::plaintext($_).'</td><td>'.
1.61      www      1322:     ($courselevel{$_}?'<input type="checkbox" name="'.$_.':c" '.
                   1323:     ($courselevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
                   1324:     '</td><td>'.
                   1325:     ($domainlevel{$_}?'<input type="checkbox" name="'.$_.':d" '.
                   1326:     ($domainlevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
                   1327:     '</td><td>'.
                   1328:     ($systemlevel{$_}?'<input type="checkbox" name="'.$_.':s" '.
                   1329:     ($systemlevelcurrent{$_}?'checked="1"':'').' />':'&nbsp;').
                   1330:     '</td></tr>');
1.60      www      1331:     }
1.61      www      1332:     $r->print(
1.73      sakharuk 1333:    '<table><input type="submit" value="'.&mt('Define Role').'" /></form></body></html>');
1.61      www      1334: }
                   1335: 
                   1336: # ---------------------------------------------------------- Call to definerole
                   1337: sub set_custom_role {
                   1338:     my $r=shift;
                   1339: 
                   1340:     my $rolename=$ENV{'form.rolename'};
                   1341: 
1.63      www      1342:     $rolename=~s/[^A-Za-z0-9]//gs;
1.61      www      1343: 
                   1344:     unless ($rolename) {
                   1345: 	&print_username_entry_form($r);
                   1346:         return;
                   1347:     }
                   1348: 
                   1349:     $r->print(&Apache::loncommon::bodytag(
                   1350:                      'Create Users, Change User Privileges').'<h2>');
                   1351:     my ($rdummy,$roledef)=
                   1352: 			 &Apache::lonnet::get('roles',["rolesdef_$rolename"]);
                   1353: # ------------------------------------------------------- Does this role exist?
                   1354:     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
1.73      sakharuk 1355: 	$r->print(&mt('Existing Role').' "');
1.61      www      1356:     } else {
1.73      sakharuk 1357: 	$r->print(&mt('New Role').' "');
1.61      www      1358: 	$roledef='';
                   1359:     }
                   1360:     $r->print($rolename.'"</h2>');
                   1361: # ------------------------------------------------------- What can be assigned?
                   1362:     my $sysrole='';
                   1363:     my $domrole='';
                   1364:     my $courole='';
                   1365: 
                   1366:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   1367: 	my ($priv,$restrict)=split(/\&/,$_);
                   1368:         unless ($restrict) { $restrict=''; }
                   1369:         if ($ENV{'form.'.$priv.':c'}) {
                   1370: 	    $courole.=':'.$_;
                   1371: 	}
                   1372:     }
                   1373: 
                   1374:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:d'})) {
                   1375: 	my ($priv,$restrict)=split(/\&/,$_);
                   1376:         unless ($restrict) { $restrict=''; }
                   1377:         if ($ENV{'form.'.$priv.':d'}) {
                   1378: 	    $domrole.=':'.$_;
                   1379: 	}
                   1380:     }
                   1381: 
                   1382:     foreach (split(/\:/,$Apache::lonnet::pr{'cr:s'})) {
                   1383: 	my ($priv,$restrict)=split(/\&/,$_);
                   1384:         unless ($restrict) { $restrict=''; }
                   1385:         if ($ENV{'form.'.$priv.':s'}) {
                   1386: 	    $sysrole.=':'.$_;
                   1387: 	}
                   1388:     }
1.63      www      1389:     $r->print('<br />Defining Role: '.
1.61      www      1390: 	   &Apache::lonnet::definerole($rolename,$sysrole,$domrole,$courole));
1.63      www      1391:     if ($ENV{'request.course.id'}) {
                   1392:         my $url='/'.$ENV{'request.course.id'};
                   1393:         $url=~s/\_/\//g;
1.73      sakharuk 1394: 	$r->print('<br />'.&mt('Assigning Role to Self').': '.
1.63      www      1395: 	      &Apache::lonnet::assigncustomrole($ENV{'user.domain'},
                   1396: 						$ENV{'user.name'},
                   1397: 						$url,
                   1398: 						$ENV{'user.domain'},
                   1399: 						$ENV{'user.name'},
                   1400: 						$rolename));
                   1401:     }
1.61      www      1402:     $r->print('</body></html>');
1.58      www      1403: }
                   1404: 
1.2       www      1405: # ================================================================ Main Handler
                   1406: sub handler {
                   1407:     my $r = shift;
                   1408: 
                   1409:     if ($r->header_only) {
1.68      www      1410:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      1411:        $r->send_http_header;
                   1412:        return OK;
                   1413:     }
                   1414: 
                   1415:     if ((&Apache::lonnet::allowed('cta',$ENV{'request.course.id'})) ||
                   1416:         (&Apache::lonnet::allowed('cin',$ENV{'request.course.id'})) || 
                   1417:         (&Apache::lonnet::allowed('ccr',$ENV{'request.course.id'})) || 
                   1418:         (&Apache::lonnet::allowed('cep',$ENV{'request.course.id'})) ||
1.42      matthew  1419:         (&Apache::lonnet::allowed('cca',$ENV{'request.role.domain'})) ||
                   1420:         (&Apache::lonnet::allowed('mau',$ENV{'request.role.domain'}))) {
1.68      www      1421:        &Apache::loncommon::content_type($r,'text/html');
1.2       www      1422:        $r->send_http_header;
                   1423:        unless ($ENV{'form.phase'}) {
1.42      matthew  1424: 	   &print_username_entry_form($r);
1.2       www      1425:        }
1.42      matthew  1426:        if ($ENV{'form.phase'} eq 'get_user_info') {
                   1427:            &print_user_modification_page($r);
                   1428:        } elsif ($ENV{'form.phase'} eq 'update_user_data') {
                   1429:            &update_user_data($r);
1.58      www      1430:        } elsif ($ENV{'form.phase'} eq 'selected_custom_edit') {
                   1431:            &custom_role_editor($r);
1.61      www      1432:        } elsif ($ENV{'form.phase'} eq 'set_custom_roles') {
                   1433: 	   &set_custom_role($r);
1.2       www      1434:        }
1.1       www      1435:    } else {
                   1436:       $ENV{'user.error.msg'}=
1.9       albertel 1437:         "/adm/createuser:mau:0:0:Cannot modify user data";
1.1       www      1438:       return HTTP_NOT_ACCEPTABLE; 
                   1439:    }
                   1440:    return OK;
                   1441: } 
1.26      matthew  1442: 
1.27      matthew  1443: #-------------------------------------------------- functions for &phase_two
1.26      matthew  1444: sub course_level_table {
                   1445:     my %inccourses = @_;
                   1446:     my $table = '';
1.62      www      1447: # Custom Roles?
                   1448: 
                   1449:     my %customroles=&my_custom_roles();
                   1450: 
1.26      matthew  1451:     foreach (sort( keys(%inccourses))) {
                   1452: 	my $thiscourse=$_;
                   1453: 	my $protectedcourse=$_;
                   1454: 	$thiscourse=~s:_:/:g;
                   1455: 	my %coursedata=&Apache::lonnet::coursedescription($thiscourse);
                   1456: 	my $area=$coursedata{'description'};
1.72      sakharuk 1457: 	if (!defined($area)) { $area=&mt('Unavailable course').': '.$_; }
1.26      matthew  1458: 	my $bgcol=$thiscourse;
1.62      www      1459: 	$bgcol=~s/[^7-9a-e]//g;
                   1460: 	$bgcol=substr($bgcol.$bgcol.$bgcol.'ffffff',2,6);
1.82      albertel 1461: 	my ($domain)=split(/\//,$thiscourse);
1.26      matthew  1462: 	foreach  ('st','ta','ep','ad','in','cc') {
                   1463: 	    if (&Apache::lonnet::allowed('c'.$_,$thiscourse)) {
                   1464: 		my $plrole=&Apache::lonnet::plaintext($_);
                   1465: 		$table .= <<ENDEXTENT;
                   1466: <tr bgcolor="#$bgcol">
                   1467: <td><input type="checkbox" name="act_$protectedcourse\_$_"></td>
                   1468: <td>$plrole</td>
1.82      albertel 1469: <td>$area<br />Domain: $domain</td>
1.26      matthew  1470: ENDEXTENT
                   1471: 	        if ($_ ne 'cc') {
                   1472: 		    $table .= <<ENDSECTION;
                   1473: <td><input type="text" size="5" name="sec_$protectedcourse\_$_"></td>
                   1474: ENDSECTION
                   1475:                 } else { 
                   1476: 		    $table .= <<ENDSECTION;
                   1477: <td>&nbsp</td> 
                   1478: ENDSECTION
                   1479:                 }
1.73      sakharuk 1480: 		my %lt=&Apache::lonlocal::texthash(
                   1481:                                'ssd'  => "Set Start Date",
                   1482:                                'sed'  => "Set End Date"
                   1483: 						   );
1.26      matthew  1484: 		$table .= <<ENDTIMEENTRY;
                   1485: <td><input type=hidden name="start_$protectedcourse\_$_" value=''>
                   1486: <a href=
1.73      sakharuk 1487: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$_.value,'start_$protectedcourse\_$_','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.26      matthew  1488: <td><input type=hidden name="end_$protectedcourse\_$_" value=''>
                   1489: <a href=
1.73      sakharuk 1490: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$_.value,'end_$protectedcourse\_$_','cu.pres','dateset')">$lt{'sed'}</a></td>
1.26      matthew  1491: ENDTIMEENTRY
                   1492:                 $table.= "</tr>\n";
                   1493:             }
                   1494:         }
1.62      www      1495:         foreach (sort keys %customroles) {
1.65      www      1496: 	    if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
                   1497: 		my $plrole=$_;
                   1498:                 my $customrole=$protectedcourse.'_cr_cr_'.$ENV{'user.domain'}.
                   1499: 		    '_'.$ENV{'user.name'}.'_'.$plrole;
1.73      sakharuk 1500: 		my %lt=&Apache::lonlocal::texthash(
                   1501:                                'ssd'  => "Set Start Date",
                   1502:                                'sed'  => "Set End Date"
                   1503: 						   );
1.65      www      1504: 		$table .= <<ENDENTRY;
1.62      www      1505: <tr bgcolor="#$bgcol">
1.65      www      1506: <td><input type="checkbox" name="act_$customrole"></td>
1.62      www      1507: <td>$plrole</td>
                   1508: <td>$area</td>
1.65      www      1509: <td><input type="text" size="5" name="sec_$customrole"></td>
                   1510: <td><input type=hidden name="start_$customrole" value=''>
1.62      www      1511: <a href=
1.73      sakharuk 1512: "javascript:pjump('date_start','Start Date $plrole',document.cu.start_$customrole.value,'start_$customrole','cu.pres','dateset')">$lt{'ssd'}</a></td>
1.65      www      1513: <td><input type=hidden name="end_$customrole" value=''>
1.62      www      1514: <a href=
1.73      sakharuk 1515: "javascript:pjump('date_end','End Date $plrole',document.cu.end_$customrole.value,'end_$customrole','cu.pres','dateset')">$lt{'sed'}</a></td></tr>
1.62      www      1516: ENDENTRY
1.65      www      1517:            }
1.62      www      1518: 	}
1.26      matthew  1519:     }
                   1520:     return '' if ($table eq ''); # return nothing if there is nothing 
                   1521:                                  # in the table
1.73      sakharuk 1522:     my %lt=&Apache::lonlocal::texthash(
                   1523: 		    'crl'  => "Course Level",
                   1524:                     'act'  => "Activate",
                   1525:                     'rol'  => "Role",
                   1526:                     'ext'  => "Extent",
                   1527:                     'grs'  => "Group/Section",
                   1528:                     'sta'  => "Start",
                   1529:                     'end'  => "End"
                   1530: 				       );
1.26      matthew  1531:     my $result = <<ENDTABLE;
1.73      sakharuk 1532: <h4>$lt{'crl'}</h4>
                   1533: <table border=2><tr><th>$lt{'act'}</th><th>$lt{'rol'}</th><th>$lt{'ext'}</th>
                   1534: <th>$lt{'grs'}</th><th>$lt{'sta'}</th><th>$lt{'end'}</th></tr>
1.26      matthew  1535: $table
                   1536: </table>
                   1537: ENDTABLE
                   1538:     return $result;
                   1539: }
1.88    ! raeburn  1540: 
        !          1541: sub course_level_dc {
        !          1542:     my ($dcdom) = @_;
        !          1543:     my %customroles=&my_custom_roles();
        !          1544:     my $hiddenitems = '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
        !          1545:                       '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
        !          1546:                       '<input type="hidden" name="dccourse" value="" />';
        !          1547:     my $courseform='<b>'.&Apache::loncommon::selectcourse_link
        !          1548:                      ('cu','dccourse','dcdomain','coursedesc').'</b>';
        !          1549:                                                                                       
        !          1550:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,$dcdom);
        !          1551:     my %lt=&Apache::lonlocal::texthash(
        !          1552:                     'crl'  => "Course Level",
        !          1553:                     'crt'  => "Course Title",
        !          1554:                     'rol'  => "Role",
        !          1555:                     'grs'  => "Group/Section",
        !          1556:                     'exs'  => "Existing sections",
        !          1557:                     'new'  => "Define new section", 
        !          1558:                     'sta'  => "Start",
        !          1559:                     'end'  => "End",
        !          1560:                     'ssd'  => "Set Start Date",
        !          1561:                     'sed'  => "Set End Date"
        !          1562:                   );
        !          1563:     my $header = '<h4>'.$lt{'crl'}.'</h4>'.
        !          1564:                  '<table border="2"><tr><th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th><th>'.$lt{'grs'}.'</th><th>'.$lt{'sta'}.'</th><th>'.$lt{'end'}.'</th></tr>';
        !          1565:     my $otheritems = '<tr><td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'".'cu'."'".','."'".'dccourse'."'".','."'".'dcdomain'."'".','."'".'coursedesc'."'".')" /></td>'.
        !          1566:                      '<td><select name="role">'."\n";
        !          1567:     foreach  ('st','ta','ep','ad','in','cc') {
        !          1568:         my $plrole=&Apache::lonnet::plaintext($_);
        !          1569:         $otheritems .= '  <option value="'.$_.'">'.$plrole;
        !          1570:     }
        !          1571:     if ( keys %customroles > 0) {
        !          1572:         foreach (sort keys %customroles) {
        !          1573:             my $custrole='cr_cr_'.$ENV{'user.domain'}.
        !          1574:                     '_'.$ENV{'user.name'}.'_'.$_;
        !          1575:             $otheritems .= '  <option value="'.$custrole.'">'.$_;
        !          1576:         }
        !          1577:     }
        !          1578:     $otheritems .= '</select></td><td>'.
        !          1579:                      '<table border="0" cellspacing="0" cellpadding="0">'.
        !          1580:                      '<tr><td valign="top"><b>'.$lt{'exs'}.'</b><br /><select name="currsec">'.
        !          1581:                      ' <option value=""><--'.&mt('Pick course first').'</select></td>'.
        !          1582:                      '<td>&nbsp;&nbsp;</td>'.
        !          1583:                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
        !          1584:                      '<input type="text" name="newsec" value="" /></td>'.
        !          1585:                      '</tr></table></td>';
        !          1586:     $otheritems .= <<ENDTIMEENTRY;
        !          1587: <td><input type=hidden name="start" value=''>
        !          1588: <a href=
        !          1589: "javascript:pjump('date_start','Start Date',document.cu.start.value,'start','cu.pres','dateset')">$lt{'ssd'}</a></td>
        !          1590: <td><input type=hidden name="end" value=''>
        !          1591: <a href=
        !          1592: "javascript:pjump('date_end','End Date',document.cu.end.value,'end','cu.pres','dateset')">$lt{'sed'}</a></td>
        !          1593: ENDTIMEENTRY
        !          1594:     $otheritems .= "</tr></table>\n";
        !          1595:     return $cb_jscript.$header.$hiddenitems.$otheritems;
        !          1596: }
        !          1597: 
1.27      matthew  1598: #---------------------------------------------- end functions for &phase_two
1.29      matthew  1599: 
                   1600: #--------------------------------- functions for &phase_two and &phase_three
                   1601: 
                   1602: #--------------------------end of functions for &phase_two and &phase_three
1.1       www      1603: 
                   1604: 1;
                   1605: __END__
1.2       www      1606: 
                   1607: 

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