--- loncom/interface/loncreateuser.pm 2001/02/15 00:57:41 1.1
+++ loncom/interface/loncreateuser.pm 2007/08/14 16:53:15 1.171
@@ -1,62 +1,2727 @@
-# The LearningOnline Network
+# The LearningOnline Network with CAPA
# Create a user
#
-# (Create a course
-# (My Desk
+# $Id: loncreateuser.pm,v 1.171 2007/08/14 16:53:15 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 LONCAPA qw(:DEFAULT :match);
+
+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 my $key (keys %rolehash) {
+ if ($key=~/^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;
+}
+
+# ====================================================
+
+sub portfolio_quota {
+ my ($ccuname,$ccdomain) = @_;
+ my %lt = &Apache::lonlocal::texthash(
+ 'disk' => "Disk space allocated to user's portfolio files",
+ 'cuqu' => "Current quota",
+ 'cust' => "Custom quota",
+ 'defa' => "Default",
+ 'chqu' => "Change quota",
+ );
+ my ($currquota,$quotatype,$inststatus,$defquota) =
+ &Apache::loncommon::get_user_quota($ccuname,$ccdomain);
+ my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($ccdomain);
+ my ($longinsttype,$showquota,$custom_on,$custom_off,$defaultinfo);
+ if ($inststatus ne '') {
+ if ($usertypes->{$inststatus} ne '') {
+ $longinsttype = $usertypes->{$inststatus};
+ }
+ }
+ $custom_on = ' ';
+ $custom_off = ' checked="checked" ';
+ my $quota_javascript = <<"END_SCRIPT";
+
+END_SCRIPT
+ if ($quotatype eq 'custom') {
+ $custom_on = $custom_off;
+ $custom_off = ' ';
+ $showquota = $currquota;
+ if ($longinsttype eq '') {
+ $defaultinfo = &mt('For this user, the default quota would be [_1]
+ Mb.',$defquota);
+ } else {
+ $defaultinfo = &mt("For this user, the default quota would be [_1]
+ Mb, as determined by the user's institutional
+ affiliation ([_2]).",$defquota,$longinsttype);
+ }
+ } else {
+ if ($longinsttype eq '') {
+ $defaultinfo = &mt('For this user, the default quota is [_1]
+ Mb.',$defquota);
+ } else {
+ $defaultinfo = &mt("For this user, the default quota of [_1]
+ Mb, is determined by the user's institutional
+ affiliation ([_2]).",$defquota,$longinsttype);
+ }
+ }
+ my $output = $quota_javascript.
+ '
'.$lt{'disk'}.'
'.
+ $lt{'cuqu'}.': '.$currquota.' Mb. '.
+ $defaultinfo.' '.$lt{'chqu'}.
+ ': '.
+ ' '.
+ ' Mb';
+ return $output;
+}
+
+# =================================================================== Phase one
+
+sub print_username_entry_form {
+ my ($r,$response,$srch,$forcenewuser) = @_;
+ my $defdom=$env{'request.role.domain'};
+ my $formtoset = 'crtuser';
+ if (exists($env{'form.startrolename'})) {
+ $formtoset = 'docustom';
+ $env{'form.rolename'} = $env{'form.startrolename'};
+ }
+
+ my ($jsback,$elements) = &crumb_utilities();
+
+ my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
+ ''."\n";
+
+ my %loaditems = (
+ 'onload' => "javascript:setFormElements(document.$formtoset)",
+ );
+ my $start_page =
+ &Apache::loncommon::start_page('Create Users, Change User Privileges',
+ $jscript,{'add_entries' => \%loaditems,});
+ &Apache::lonhtmlcommon::add_breadcrumb
+ ({href=>"javascript:backPage(document.crtuser)",
+ text=>"User modify/custom role",
+ faq=>282,bug=>'Instructor Interface',});
+
+ my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management');
+ 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(
+ 'srch' => "User Search",
+ or => "or",
+ 'siur' => "Set Individual User Roles",
+ 'usr' => "Username",
+ 'dom' => "Domain",
+ 'ecrp' => "Edit Custom Role Privileges",
+ 'nr' => "Name of Role",
+ 'cre' => "Custom Role Editor",
+ 'mod' => "to add/modify roles",
+ );
+ 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');
+ my $sellink=&Apache::loncommon::selectstudent_link('crtuser','srchterm','srchdomain');
+ if ($sellink) {
+ $sellink = "$lt{'or'} ".$sellink;
+ }
+ $r->print("
+$start_page
+$crumbs
+
');
+ $r->print('');
+ $r->print($response);
+ $r->print(&Apache::loncommon::end_page());
+}
+
+sub print_user_query_page {
+ my ($r) = @_;
+# FIXME - this is for a network-wide name search (similar to catalog search)
+# To use frames with similar behavior to catalog/portfolio search.
+# To be implemented.
+ return;
+}
+
+sub print_user_modification_page {
+ my ($r,$ccuname,$ccdomain,$srch,$response) = @_;
+ unless (($ccuname) && ($ccdomain)) {
+ &print_username_entry_form($r);
+ return;
+ }
+ if ($response) {
+ $response = ' '.$response
+ }
+ 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);
+
+ 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\./($match_domain)/$-) {
+ 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 coursename = "_$dcdom"+"_"+course+"_"+userrole
+ var numcourse = getIndex(document.cu.dccourse);
+ if (numcourse == "-1") {
+ alert("There was a problem with your course selection");
+ return
+ }
+ else {
+ document.cu.elements[numcourse].name = "act"+coursename;
+ var numnewsec = getIndex(document.cu.newsec);
+ if (numnewsec != "-1") {
+ document.cu.elements[numnewsec].name = "sec"+coursename;
+ document.cu.elements[numnewsec].value = section;
+ }
+ var numstart = getIndex(document.cu.start);
+ if (numstart != "-1") {
+ document.cu.elements[numstart].name = "start"+coursename;
+ }
+ var numend = getIndex(document.cu.end);
+ if (numend != "-1") {
+ document.cu.elements[numend].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'."\n".$jsback."\n".'';
+
+ my $start_page =
+ &Apache::loncommon::start_page('Create Users, Change User Privileges',
+ $js,{'add_entries' => \%loaditem,});
+ &Apache::lonhtmlcommon::add_breadcrumb
+ ({href=>"javascript:backPage(document.cu)",
+ text=>"User modify/custom role edit",
+ faq=>282,bug=>'Instructor Interface',});
+
+ if ($env{'form.phase'} eq 'userpicked') {
+ &Apache::lonhtmlcommon::add_breadcrumb
+ ({href=>"javascript:backPage(document.cu,'get_user_info','select')",
+ text=>"Select a user",
+ faq=>282,bug=>'Instructor Interface',});
+ }
+ &Apache::lonhtmlcommon::add_breadcrumb
+ ({href=>"javascript:backPage(document.cu,'$env{'form.phase'}','modify')",
+ text=>"Set user role",
+ faq=>282,bug=>'Instructor Interface',});
+ my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management');
+
+ my $forminfo =<<"ENDFORMINFO";
+
'.
+&Apache::loncommon::end_data_table_header_row());
+ foreach my $type ('Construction Space','Course','Group','Domain','System','Unknown') {
+ if ($output{$type}) {
+ $r->print($output{$type}."\n");
+ }
+ }
+ $r->print(&Apache::loncommon::end_data_table());
+ }
+ } # 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',$ccdomain)) {
+ &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 user's
+ # 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="
";
+ }
+ $authformcurrent.=' (will override current values) ';
+ if (&Apache::lonnet::allowed('mau',$ccdomain)) {
+ # Current user has login modification privileges
+ my %lt=&Apache::lonlocal::texthash(
+ 'ccld' => "Change Current Login Data",
+ 'enld' => "Enter New Login Data"
+ );
+ $r->print(<
+
+
$lt{'ccld'}
+
$generalrule
+
$authformnop
+
$authformcurrent
+
$lt{'enld'}
+$authform_other
+ENDOTHERAUTHS
+ } else {
+ if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
+ my %lt=&Apache::lonlocal::texthash(
+ 'ccld' => "Change Current Login Data",
+ 'yodo' => "You do not have privileges to modify the authentication configuration for this user.",
+ 'ifch' => "If a change is required, contact a domain coordinator for the domain",
+ );
+ $r->print(<
+
$lt{'ccld'}
+$lt{'yodo'} $lt{'ifch'}: $ccdomain
+ENDNOPRIV
+ }
+ }
+ if (&Apache::lonnet::allowed('mpq',$env{'request.role.domain'})) {
+ # Current user has quota modification privileges
+ $r->print(&portfolio_quota($ccuname,$ccdomain));
+ }
+ } ## End of "check for bad authentication type" logic
+ } ## End of new user/old user logic
+ $r->print('
'.&mt('Add Roles').'
');
+#
+# Co-Author
+#
+ if (&authorpriv($env{'user.name'},$env{'request.role.domain'}) &&
+ ($env{'user.name'} ne $ccuname || $env{'user.domain'} ne $ccdomain)) {
+ # No sense in assigning co-author role to yourself
+ my $cuname=$env{'user.name'};
+ my $cudom=$env{'request.role.domain'};
+ my %lt=&Apache::lonlocal::texthash(
+ 'cs' => "Construction Space",
+ 'act' => "Activate",
+ 'rol' => "Role",
+ 'ext' => "Extent",
+ 'sta' => "Start",
+ 'end' => "End",
+ 'cau' => "Co-Author",
+ 'caa' => "Assistant Co-Author",
+ 'ssd' => "Set Start Date",
+ 'sed' => "Set End Date"
+ );
+ $r->print('
");
+ }
+ } else { # End of if ($env ... ) logic
+ my $putresult;
+ if ($quotachanged) {
+ $putresult = &Apache::lonnet::put
+ ('environment',\%changeHash,
+ $env{'form.ccdomain'},$env{'form.ccuname'});
+ }
+ # They did not want to change the users name but we can
+ # still tell them what the name is
+ my %lt=&Apache::lonlocal::texthash(
+ 'mail' => "Permanent e-mail",
+ 'disk' => "Disk space allocated to user's portfolio files",
+ );
+ $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('