--- loncom/interface/loncreateuser.pm 2001/02/15 00:57:41 1.1
+++ loncom/interface/loncreateuser.pm 2006/06/29 17:38:44 1.126
@@ -1,62 +1,1944 @@
-# The LearningOnline Network
+# The LearningOnline Network with CAPA
# Create a user
#
-# (Create a course
-# (My Desk
+# $Id: loncreateuser.pm,v 1.126 2006/06/29 17:38:44 albertel Exp $
#
-# (Internal Server Error Handler
+# Copyright Michigan State University Board of Trustees
#
-# (Login Screen
-# 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
-# 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
+# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
-# 3/1/1 Gerd Kortemeyer)
+# LON-CAPA is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
#
-# 3/1 Gerd Kortemeyer)
+# LON-CAPA is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
-# 2/14 Gerd Kortemeyer)
+# You should have received a copy of the GNU General Public License
+# along with LON-CAPA; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
-# 2/14 Gerd Kortemeyer
+# /home/httpd/html/adm/gpl.txt
#
+# http://www.lon-capa.org/
+#
+###
+
package Apache::loncreateuser;
+=pod
+
+=head1 NAME
+
+Apache::loncreateuser - handler to create users and custom roles
+
+=head1 SYNOPSIS
+
+Apache::loncreateuser provides an Apache handler for creating users,
+ editing their login parameters, roles, and removing roles, and
+ also creating and assigning custom roles.
+
+=head1 OVERVIEW
+
+=head2 Custom Roles
+
+In LON-CAPA, roles are actually collections of privileges. "Teaching
+Assistant", "Course Coordinator", and other such roles are really just
+collection of privileges that are useful in many circumstances.
+
+Creating custom roles can be done by the Domain Coordinator through
+the Create User functionality. That screen will show all privileges
+that can be assigned to users. For a complete list of privileges,
+please see C.
+
+Custom role definitions are stored in the C file of the role
+author.
+
+=cut
+
use strict;
use Apache::Constants qw(:common :http);
use Apache::lonnet;
+use Apache::loncommon;
+use Apache::lonlocal;
+use Apache::longroup;
+use lib '/home/httpd/lib/perl/';
+use LONCAPA;
+
+my $loginscript; # piece of javascript used in two separate instances
+my $generalrule;
+my $authformnop;
+my $authformkrb;
+my $authformint;
+my $authformfsys;
+my $authformloc;
+
+sub initialize_authen_forms {
+ my ($krbdefdom)=( $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/);
+ $krbdefdom= uc($krbdefdom);
+ my %param = ( formname => 'document.cu',
+ kerb_def_dom => $krbdefdom
+ );
+# no longer static due to configurable kerberos defaults
+# $loginscript = &Apache::loncommon::authform_header(%param);
+ $generalrule = &Apache::loncommon::authform_authorwarning(%param);
+ $authformnop = &Apache::loncommon::authform_nochange(%param);
+# no longer static due to configurable kerberos defaults
+# $authformkrb = &Apache::loncommon::authform_kerberos(%param);
+ $authformint = &Apache::loncommon::authform_internal(%param);
+ $authformfsys = &Apache::loncommon::authform_filesystem(%param);
+ $authformloc = &Apache::loncommon::authform_local(%param);
+}
+
+
+# ======================================================= Existing Custom Roles
+
+sub my_custom_roles {
+ my %returnhash=();
+ my %rolehash=&Apache::lonnet::dump('roles');
+ foreach (keys %rolehash) {
+ if ($_=~/^rolesdef\_(\w+)$/) {
+ $returnhash{$1}=$1;
+ }
+ }
+ return %returnhash;
+}
+
+# ==================================================== Figure out author access
+
+sub authorpriv {
+ my ($auname,$audom)=@_;
+ unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
+ || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }
+ return 1;
+}
+
+# =================================================================== Phase one
+
+sub print_username_entry_form {
+ my ($r) = @_;
+ my $defdom=$env{'request.role.domain'};
+ my @domains = &Apache::loncommon::get_domains();
+ my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain');
+ my $selscript=&Apache::loncommon::studentbrowser_javascript();
+ my $start_page =
+ &Apache::loncommon::start_page('Create Users, Change User Privileges',
+ $selscript);
+
+ my $sellink=&Apache::loncommon::selectstudent_link
+ ('crtuser','ccuname','ccdomain');
+ my %existingroles=&my_custom_roles();
+ my $choice=&Apache::loncommon::select_form('make new role','rolename',
+ ('make new role' => 'Generate new role ...',%existingroles));
+ my %lt=&Apache::lonlocal::texthash(
+ 'siur' => "Set Individual User Roles",
+ 'usr' => "Username",
+ 'dom' => "Domain",
+ 'usrr' => "User Roles",
+ 'ecrp' => "Edit Custom Role Privileges",
+ 'nr' => "Name of Role",
+ 'cre' => "Custom Role Editor"
+ );
+ my $help = &Apache::loncommon::help_open_menu(undef,undef,282,'Instructor Interface');
+ my $helpsiur=&Apache::loncommon::help_open_topic('Course_Change_Privileges');
+ my $helpecpr=&Apache::loncommon::help_open_topic('Course_Editing_Custom_Roles');
+ $r->print(<<"ENDDOCUMENT");
+$start_page
+
+ENDDOCUMENT
+ if (&Apache::lonnet::allowed('mcr','/')) {
+ $r->print(<
+
+
$lt{'ecrp'}$helpecpr
+$lt{'nr'}: $choice
+
+
+ENDCUSTOM
+ }
+ $r->print(&Apache::loncommon::end_page());
+}
+
+
+sub user_modification_js {
+ my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
+
+ return <
+
+ function pclose() {
+ parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
+ "height=350,width=350,scrollbars=no,menubar=no");
+ parmwin.close();
+ }
+
+ $pjump_def
+ $dc_setcourse_code
+
+ function dateset() {
+ eval("document.cu."+document.cu.pres_marker.value+
+ ".value=document.cu.pres_value.value");
+ pclose();
+ }
+
+ $nondc_setsection_code
+
+END
+}
+
+# =================================================================== Phase two
+sub print_user_modification_page {
+ my $r=shift;
+ my $ccuname=$env{'form.ccuname'};
+ my $ccdomain=$env{'form.ccdomain'};
+
+ $ccuname=~s/\W//g;
+ $ccdomain=~s/\W//g;
+
+ unless (($ccuname) && ($ccdomain)) {
+ &print_username_entry_form($r);
+ return;
+ }
+
+ my $defdom=$env{'request.role.domain'};
+
+ my ($krbdef,$krbdefdom) =
+ &Apache::loncommon::get_kerberos_defaults($defdom);
+
+ my %param = ( formname => 'document.cu',
+ kerb_def_dom => $krbdefdom,
+ kerb_def_auth => $krbdef
+ );
+ $loginscript = &Apache::loncommon::authform_header(%param);
+ $authformkrb = &Apache::loncommon::authform_kerberos(%param);
+
+ $ccuname=~s/\W//g;
+ $ccdomain=~s/\W//g;
+ my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
+ my $dc_setcourse_code = '';
+ my $nondc_setsection_code = '';
+
+ my %loaditem;
+
+ my $groupslist;
+ my %curr_groups = &Apache::longroup::coursegroups();
+ if (%curr_groups) {
+ $groupslist = join('","',sort(keys(%curr_groups)));
+ $groupslist = '"'.$groupslist.'"';
+ }
+
+ if ($env{'request.role'} =~ m-^dc\./(\w+)/$-) {
+ my $dcdom = $1;
+ $loaditem{'onload'} = "document.cu.coursedesc.value='';";
+ my @rolevals = ('st','ta','ep','in','cc');
+ my (@crsroles,@grproles);
+ for (my $i=0; $i<@rolevals; $i++) {
+ $crsroles[$i]=&Apache::lonnet::plaintext($rolevals[$i],'Course');
+ $grproles[$i]=&Apache::lonnet::plaintext($rolevals[$i],'Group');
+ }
+ my $rolevalslist = join('","',@rolevals);
+ my $crsrolenameslist = join('","',@crsroles);
+ my $grprolenameslist = join('","',@grproles);
+ my $pickcrsfirst = '<--'.&mt('Pick course first');
+ my $pickgrpfirst = '<--'.&mt('Pick group first');
+ $dc_setcourse_code = <<"ENDSCRIPT";
+ function setCourse() {
+ var course = document.cu.dccourse.value;
+ if (course != "") {
+ if (document.cu.dcdomain.value != document.cu.origdom.value) {
+ alert("You must select a course in the current domain");
+ return;
+ }
+ var userrole = document.cu.role.options[document.cu.role.selectedIndex].value
+ var section="";
+ var numsections = 0;
+ var newsecs = new Array();
+ for (var i=0; i 1)) {
+ alert("In each course, each user may only have one student role at a time. You had selected "+numsections+" sections.\\nPlease modify your selections so they include no more than one section.")
+ return;
+ }
+ for (var j=0; j 0)) {
+ alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections.");
+ section = "";
+ }
+ var numcourse = getIndex(document.cu.dccourse);
+ if (numcourse == "-1") {
+ alert("There was a problem with your course selection");
+ return
+ }
+ else {
+ var coursename = "_$dcdom"+"_"+course+"_"+userrole
+ document.cu.elements[numcourse].name = "act"+coursename
+ document.cu.elements[numcourse+5].name = "sec"+coursename
+ document.cu.elements[numcourse+5].value = section
+ document.cu.elements[numcourse+7].name = "start"+coursename
+ document.cu.elements[numcourse+8].name = "end"+coursename
+ }
+ }
+ document.cu.submit();
+ }
+
+ function getIndex(caller) {
+ for (var i=0;i 0) {
+ if (document.cu.elements[i+1].value != "" && document.cu.elements[i+1].value != null) {
+ sections = sections + "," + document.cu.elements[i+1].value;
+ }
+ }
+ else {
+ sections = document.cu.elements[i+1].value;
+ }
+ var newsecs = document.cu.elements[i+1].value;
+ var numsplit;
+ if (newsecs != null && newsecs != "") {
+ numsplit = newsecs.split(/,/g);
+ numsec = numsec + numsplit.length;
+ }
+
+ if ((role == 'st') && (numsec > 1)) {
+ alert("In each course, each user may only have one student role at a time. You had selected "+numsec+" sections.\\nPlease modify your selections so they include no more than one section.")
+ return;
+ }
+ else if (numsplit != null) {
+ for (var j=0; j \%loaditem,});
+
+ my $forminfo =<<"ENDFORMINFO";
+
+ENDCHANGEUSER
+ # Get the users information
+ my %userenv = &Apache::lonnet::get('environment',
+ ['firstname','middlename','lastname','generation'],
+ $ccdomain,$ccuname);
+ my %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
+ $r->print(<
+
+END
+ foreach my $type ('Construction Space','Course','Group','Domain','System','Unknown') {
+ if ($output{$type}) {
+ $r->print($output{$type}."\n");
+ }
+ }
+ $r->print('
');
+ }
+ } # End of unless
+ my $currentauth=&Apache::lonnet::queryauthenticate($ccuname,$ccdomain);
+ if ($currentauth=~/^krb(4|5):/) {
+ $currentauth=~/^krb(4|5):(.*)/;
+ my $krbdefdom=$2;
+ my %param = ( formname => 'document.cu',
+ kerb_def_dom => $krbdefdom
+ );
+ $loginscript = &Apache::loncommon::authform_header(%param);
+ }
+ # Check for a bad authentication type
+ unless ($currentauth=~/^krb(4|5):/ or
+ $currentauth=~/^unix:/ or
+ $currentauth=~/^internal:/ or
+ $currentauth=~/^localauth:/
+ ) { # bad authentication scheme
+ if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
+ &initialize_authen_forms();
+ my %lt=&Apache::lonlocal::texthash(
+ 'err' => "ERROR",
+ 'uuas' => "This user has an unrecognized authentication scheme",
+ 'sldb' => "Please specify login data below",
+ 'ld' => "Login Data"
+ );
+ $r->print(<
+
+$lt{'err'}:
+$lt{'uuas'} ($currentauth). $lt{'sldb'}.
+
$lt{'ld'}
+
$generalrule
+
$authformkrb
+
$authformint
+
$authformfsys
+
$authformloc
+ENDBADAUTH
+ } else {
+ # This user is not allowed to modify the users
+ # authentication scheme, so just notify them of the problem
+ my %lt=&Apache::lonlocal::texthash(
+ 'err' => "ERROR",
+ 'uuas' => "This user has an unrecognized authentication scheme",
+ 'adcs' => "Please alert a domain coordinator of this situation"
+ );
+ $r->print(<
+
+ $lt{'err'}:
+$lt{'uuas'} ($currentauth). $lt{'adcs'}.
+
+ENDBADAUTH
+ }
+ } else { # Authentication type is valid
+ my $authformcurrent='';
+ my $authform_other='';
+ &initialize_authen_forms();
+ if ($currentauth=~/^krb(4|5):/) {
+ $authformcurrent=$authformkrb;
+ $authform_other="
");
+ }
+ } else { # End of if ($env ... ) logic
+ # They did not want to change the users name but we can
+ # still tell them what the name is
+ my %lt=&Apache::lonlocal::texthash(
+ 'usr' => "User",
+ 'id' => "in domain",
+ 'gen' => "Generation"
+ );
+ $r->print(<<"END");
+
'.&mt('[_1] may not be used as the name for a section, as it is a reserved word.',$key));
+ } else {
+ $r->print('
'.&mt('[_1] may not be used as the name for a section, as it is the name of a course group.',$key));
+ }
+ $r->print(' '.&mt('Please go back and choose a different section name.').'
');
+ }
+ }
+ } # End of foreach (keys(%env))
+# Flush the course logs so reverse user roles immediately updated
+ &Apache::lonnet::flushcourselogs();
+ $r->print('