File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.71: download - view: text, annotated - select for diffs
Wed Dec 10 19:34:53 2003 UTC (20 years, 6 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Localization is added for part of module.

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

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