File:  [LON-CAPA] / loncom / interface / loncreateuser.pm
Revision 1.67: download - view: text, annotated - select for diffs
Wed Sep 17 17:30:10 2003 UTC (20 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- the top of the modify roles screen is now sorted in the same order as the roles screen

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

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