--- loncom/interface/lonuserutils.pm 2010/09/20 00:21:08 1.125
+++ loncom/interface/lonuserutils.pm 2012/02/08 19:35:20 1.136.6.2
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Utility functions for managing LON-CAPA user accounts
#
-# $Id: lonuserutils.pm,v 1.125 2010/09/20 00:21:08 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.136.6.2 2012/02/08 19:35:20 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -47,7 +47,8 @@ sub modifystudent {
# this one. If $csec is defined, drop them from all other sections of
# this course and add them to section $csec
my ($cnum,$cdom) = &get_course_identity($courseid);
- my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
+ my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
+ my %roles = &Apache::lonnet::dump('roles',$udom,$unam,'.',undef,$extra);
my ($tmp) = keys(%roles);
# Bail out if we were unable to get the students roles
return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
@@ -341,7 +342,8 @@ sub print_upload_manager_header {
."
\n");
$r->print(''.
&mt('Identify fields in uploaded list')."
\n");
- $r->print(&mt('Enter as many fields as you can.
The system will inform you and bring you back to this page,
if the data selected are insufficient to add users.')."
\n");
+ $r->print(&mt('Enter as many fields as you can.').'
'.
+ &mt('The system will inform you and bring you back to this page, [_1]if the data selected are insufficient to add users.','
')."
\n");
$r->print(&hidden_input('action','upload').
&hidden_input('state','got_file').
&hidden_input('associate','').
@@ -533,6 +535,7 @@ END
if (message!='') {
message+='\\n';
}
+ message+='$alert{'section'}';
}
if (foundemail==0) {
if (message!='') {
@@ -587,28 +590,43 @@ END
###############################################################
sub upload_manager_javascript_forward_associate {
my ($can_assign) = @_;
- my $auth_update;
+ my ($auth_update,$numbuttons,$argreset);
if (ref($can_assign) eq 'HASH') {
- if (keys(%{$can_assign}) > 1) {
+ if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
+ $argreset .= " vf.krbarg.value='';\n";
+ $numbuttons ++ ;
+ }
+ if ($can_assign->{'int'}) {
+ $argreset .= " vf.intarg.value='';\n";
+ $numbuttons ++;
+ }
+ if ($can_assign->{'loc'}) {
+ $argreset .= " vf.locarg.value='';\n";
+ $numbuttons ++;
+ }
+ if (!$can_assign->{'int'}) {
+ my $warning = &mt('You may not specify an initial password for each user, as this is only available when new users use LON-CAPA internal authentication.\n').
+ &mt('Your current role does not have rights to create users with that authentication type.');
+ $auth_update = <<"END";
+ // Currently the initial password field is only supported for internal auth
+ // (see bug 6368).
+ if (nw==9) {
+ eval('vf.f'+tf+'.selectedIndex=0;')
+ alert('$warning');
+ }
+END
+ } elsif ($numbuttons > 1) {
$auth_update = <<"END";
// If we set the password, make the password form below correspond to
// the new value.
if (nw==9) {
changed_radio('int',document.studentform);
set_auth_radio_buttons('int',document.studentform);
+$argreset
+ }
+
END
}
- if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
- $auth_update .= " vf.krbarg.value='';\n";
- }
- if ($can_assign->{'int'}) {
- $auth_update .= " vf.intarg.value='';\n";
- }
- if ($can_assign->{'loc'}) {
- $auth_update .= " vf.locarg.value='';\n";
- }
- $auth_update .= "
- }\n";
}
return(<
1) {
+ if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
+ $argreset .= " vf.krbarg.value='';\n";
+ $numbuttons ++ ;
+ }
+ if ($can_assign->{'int'}) {
+ $argreset .= " vf.intarg.value='';\n";
+ $numbuttons ++;
+ }
+ if ($can_assign->{'loc'}) {
+ $argreset .= " vf.locarg.value='';\n";
+ $numbuttons ++;
+ }
+ if (!$can_assign->{'int'}) {
+ my $warning = &mt('You may not specify an initial password, as this is only available when new users use LON-CAPA internal authentication.\n').
+ &mt('Your current role does not have rights to create users with that authentication type.');
+ $auth_update = <<"END";
+ // Currently the initial password field is only supported for internal auth
+ // (see bug 6368).
+ if (tf==8 && nw!=0) {
+ eval('vf.f'+tf+'.selectedIndex=0;')
+ alert('$warning');
+ }
+END
+ } elsif ($numbuttons > 1) {
$auth_update = <<"END";
// initial password specified, pick internal authentication
if (tf==8 && nw!=0) {
changed_radio('int',document.studentform);
set_auth_radio_buttons('int',document.studentform);
+$argreset
+ }
+
END
}
- if ($can_assign->{'krb'}) {
- $auth_update .= " vf.krbarg.value='';\n";
- }
- if ($can_assign->{'int'}) {
- $auth_update .= " vf.intarg.value='';\n";
- }
- if ($can_assign->{'loc'}) {
- $auth_update .= " vf.locarg.value='';\n";
- }
- $auth_update .= "
- }\n";
}
+
return(< "enroll type/action",
'email' => "e-mail address",
'photo' => "photo",
+ 'lastlogin' => "last login"
'extent' => "extent",
'pr' => "Proceed",
'ca' => "check all",
@@ -2334,6 +2371,9 @@ END
push(@cols,'groups');
}
push(@cols,'email');
+ if ($context eq 'course') {
+ push(@cols,'lastlogin');
+ }
}
my $rolefilter = $env{'form.showrole'};
@@ -2558,6 +2598,11 @@ END
Future => 'Future',
Expired => 'Expired',
);
+ # If this is for a single course get last course "log-in".
+ my %crslogins;
+ if ($context eq 'course') {
+ %crslogins=&Apache::lonnet::dump('nohist_crslastlogin',$cdom,$cnum);
+ }
# Get groups, role, permanent e-mail so we can sort on them if
# necessary.
foreach my $user (keys(%{$userlist})) {
@@ -2694,15 +2739,23 @@ END
$in{'clicker'} = $clickers;
my $role = $in{'role'};
$in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype);
- if (! defined($in{'start'}) || $in{'start'} == 0) {
- $in{'start'} = &mt('none');
- } else {
- $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
+ unless ($mode eq 'excel') {
+ if (! defined($in{'start'}) || $in{'start'} == 0) {
+ $in{'start'} = &mt('none');
+ } else {
+ $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
+ }
+ if (! defined($in{'end'}) || $in{'end'} == 0) {
+ $in{'end'} = &mt('none');
+ } else {
+ $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
+ }
}
- if (! defined($in{'end'}) || $in{'end'} == 0) {
- $in{'end'} = &mt('none');
- } else {
- $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
+ if ($context eq 'course') {
+ my $lastlogin = $crslogins{$in{'username'}.':'.$in{'domain'}.':'.$in{'section'}.':'.$role};
+ if ($lastlogin ne '') {
+ $in{'lastlogin'} = &Apache::lonlocal::locallocaltime($lastlogin);
+ }
}
if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
$r->print(&Apache::loncommon::start_data_table_row());
@@ -2804,16 +2857,6 @@ END
} elsif ($mode eq 'csv') {
next if (! defined($CSVfile));
# no need to bother with $linkto
- if (! defined($in{'start'}) || $in{'start'} == 0) {
- $in{'start'} = &mt('none');
- } else {
- $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
- }
- if (! defined($in{'end'}) || $in{'end'} == 0) {
- $in{'end'} = &mt('none');
- } else {
- $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
- }
my @line = ();
foreach my $item (@cols) {
push @line,&Apache::loncommon::csv_translate($in{$item});
@@ -2823,9 +2866,9 @@ END
my $col = 0;
foreach my $item (@cols) {
if ($item eq 'start' || $item eq 'end') {
- if (defined($item) && $item != 0) {
+ if ((defined($in{$item})) && ($in{$item} != 0)) {
$excel_sheet->write($row,$col++,
- &Apache::lonstathelpers::calc_serial($in{item}),
+ &Apache::lonstathelpers::calc_serial($in{$item}),
$format->{'date'});
} else {
$excel_sheet->write($row,$col++,'none');
@@ -3965,12 +4008,14 @@ sub upfile_drop_add {
official => 'Institutional',
unofficial => 'Non-institutional',
);
- map { $cancreate{$_} = &can_create_user($domain,$context,$_); } keys(%longtypes);
+ my $newuserdom = $env{'request.role.domain'};
+ map { $cancreate{$_} = &can_create_user($newuserdom,$context,$_); } keys(%longtypes);
# Get new users list
foreach my $line (@userdata) {
my @secs;
my %entries=&Apache::loncommon::record_sep($line);
# Determine user name
+ $entries{$fields{'username'}} =~ s/^\s+|\s+$//g;
unless (($entries{$fields{'username'}} eq '') ||
(!defined($entries{$fields{'username'}}))) {
my ($fname, $mname, $lname,$gen) = ('','','','');
@@ -3991,13 +4036,20 @@ sub upfile_drop_add {
$gen=$entries{$fields{'gen'}};
}
}
+
if ($entries{$fields{'username'}}
ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
+ my $nowhitespace;
+ if ($entries{$fields{'username'}} =~ /\s/) {
+ $nowhitespace = ' - '.&mt('usernames may not contain spaces.');
+ }
$r->print('
'.
&mt('[_1]: Unacceptable username for user [_2] [_3] [_4] [_5]',
- ''.$entries{$fields{'username'}}.'',$fname,$mname,$lname,$gen));
+ ''.$entries{$fields{'username'}}.'',$fname,$mname,$lname,$gen).
+ $nowhitespace);
next;
} else {
+ $entries{$fields{'domain'}} =~ s/^\s+|\s+$//g;
if ($entries{$fields{'domain'}}
ne &LONCAPA::clean_domain($entries{$fields{'domain'}})) {
$r->print('
'. ''.$entries{$fields{'domain'}}.
@@ -4056,6 +4108,7 @@ sub upfile_drop_add {
# determine email address
my $email='';
if (defined($fields{'email'})) {
+ $entries{$fields{'email'}} =~ s/^\s+|\s+$//g;
if (defined($entries{$fields{'email'}})) {
$email=$entries{$fields{'email'}};
unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
@@ -4102,7 +4155,7 @@ sub upfile_drop_add {
$role = $defaultrole;
}
# Clean up whitespace
- foreach (\$id,\$fname,\$mname,\$lname,\$gen) {
+ foreach (\$id,\$fname,\$mname,\$lname,\$gen,\$inststatus) {
$$_ =~ s/(\s+$|^\s+)//g;
}
# check against rules
@@ -4111,25 +4164,55 @@ sub upfile_drop_add {
my (%rulematch,%inst_results,%idinst_results);
my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
if ($uhome eq 'no_host') {
- if ($userdomain ne $domain) {
- $r->print('
'.
- &mt('[_1]: The domain specified ([_2]) is different to that of the course.',
- ''.$username.'',$userdomain).'
'.
- &mt('The user does not already exist, and you may not create a new user in a different domain.'));
+ if ($userdomain ne $newuserdom) {
+ if ($context eq 'course') {
+ $r->print('
'.
+ &mt('[_1]: The domain specified ([_2]) is different to that of the course.',
+ ''.$username.'',$userdomain).'
');
+ } elsif ($context eq 'author') {
+ $r->print(&mt('[_1]: The domain specified ([_2]) is different to that of the author.',
+ ''.$username.'',$userdomain).'
');
+ } else {
+ $r->print(&mt('[_1]: The domain specified ([_2]) is different to that of your current role.',
+ ''.$username.'',$userdomain).'
');
+ }
+ $r->print(&mt('The user does not already exist, and you may not create a new user in a different domain.'));
next;
}
$checkid = 1;
$newuser = 1;
- my $user = $username.':'.$domain;
+ if ($username =~/^[^\@]+\@[^\@]+$/) {
+ if ($email eq '') {
+ $email = $username;
+ }
+ my $lc_email;
+ if ($username eq $email) {
+ $lc_email = lc($email);
+ }
+ my $lc_username = lc($username);
+ if ($lc_username ne $username) {
+ if ($username eq $email) {
+ $email = $lc_username;
+ }
+ $username = $lc_username;
+ $uhome=&Apache::lonnet::homeserver($username,$userdomain);
+ if ($uhome ne 'no_host') {
+ $newuser = 0;
+ }
+ }
+ }
+ }
+ my $user = $username.':'.$newuserdom;
+ if ($newuser) {
my $checkhash;
my $checks = { 'username' => 1 };
- $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
+ $checkhash->{$user} = { 'newuser' => 1, };
&Apache::loncommon::user_rule_check($checkhash,$checks,
\%alerts,\%rulematch,\%inst_results,\%curr_rules,
\%got_rules);
if (ref($alerts{'username'}) eq 'HASH') {
- if (ref($alerts{'username'}{$domain}) eq 'HASH') {
- if ($alerts{'username'}{$domain}{$username}) {
+ if (ref($alerts{'username'}{$newuserdom}) eq 'HASH') {
+ if ($alerts{'username'}{$newuserdom}{$username}) {
$r->print('
'.
&mt('[_1]: matches the username format at your institution, but is not known to your directory service.',''.$username.'').'
'.
&mt('Consequently, the user was not created.'));
@@ -4145,8 +4228,13 @@ sub upfile_drop_add {
}
unless ($cancreate{$usertype}) {
my $showtype = $longtypes{$usertype};
- $r->print('
'.
- &mt("[_1]: The user does not exist, and you are not permitted to create users of type: $showtype.",''.$username.''));
+ if ($usertype eq 'unofficial') {
+ $r->print('
'.
+ &mt("[_1]: The user does not exist, and the new user's username must be an e-mail address.",''.$username.''));
+ } else {
+ $r->print('
'.
+ &mt("[_1]: The user does not exist, and you are not permitted to create users of type: [_2].",''.$username.'',$showtype));
+ }
next;
}
} else {
@@ -4299,7 +4387,7 @@ sub upfile_drop_add {
}
} # end of foreach (@userdata)
# Flush the course logs so reverse user roles immediately updated
- $r->register_cleanup(\&Apache::lonnet::flushcourselogs());
+ $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
$r->print("\n\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
"
\n");
if ($counts{'role'} > 0) {
@@ -4791,7 +4879,7 @@ sub update_user_list {
$r->print(''.&mt('Re-enabling will re-activate data for the role.').'
');
}
# Flush the course logs so reverse user roles immediately updated
- &Apache::lonnet::flushcourselogs();
+ $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
}
if ($env{'form.makedatesdefault'}) {
if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
@@ -5227,18 +5315,40 @@ sub can_modify_userinfo {
}
sub check_usertype {
- my ($dom,$uname,$rules) = @_;
+ my ($dom,$uname,$rules,$curr_rules,$got_rules) = @_;
my $usertype;
- if (ref($rules) eq 'HASH') {
- my @user_rules = keys(%{$rules});
- if (@user_rules > 0) {
- my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
- if (keys(%rule_check) > 0) {
- $usertype = 'unofficial';
- foreach my $item (keys(%rule_check)) {
- if ($rule_check{$item}) {
- $usertype = 'official';
- last;
+ if ((ref($got_rules) eq 'HASH') && (ref($curr_rules) eq 'HASH')) {
+ if (!$got_rules->{$dom}) {
+ my %domconfig = &Apache::lonnet::get_dom('configuration',
+ ['usercreation'],$dom);
+ if (ref($domconfig{'usercreation'}) eq 'HASH') {
+ foreach my $item ('username','id') {
+ if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
+ $curr_rules->{$dom}{$item} =
+ $domconfig{'usercreation'}{$item.'_rule'};
+ }
+ }
+ }
+ $got_rules->{$dom} = 1;
+ }
+ if (ref($rules) eq 'HASH') {
+ my @user_rules;
+ if (ref($curr_rules->{$dom}{'username'}) eq 'ARRAY') {
+ foreach my $rule (keys(%{$rules})) {
+ if (grep(/^\Q$rule\E/,@{$curr_rules->{$dom}{'username'}})) {
+ push(@user_rules,$rule);
+ }
+ }
+ }
+ if (@user_rules > 0) {
+ my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
+ if (keys(%rule_check) > 0) {
+ $usertype = 'unofficial';
+ foreach my $item (keys(%rule_check)) {
+ if ($rule_check{$item}) {
+ $usertype = 'official';
+ last;
+ }
}
}
}