--- loncom/interface/lonuserutils.pm 2015/06/09 21:22:57 1.170 +++ loncom/interface/lonuserutils.pm 2015/08/05 18:47:21 1.171 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Utility functions for managing LON-CAPA user accounts # -# $Id: lonuserutils.pm,v 1.170 2015/06/09 21:22:57 damieng Exp $ +# $Id: lonuserutils.pm,v 1.171 2015/08/05 18:47:21 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -4297,7 +4297,10 @@ sub upfile_drop_add { my $newuserdom = $env{'request.role.domain'}; map { $cancreate{$_} = &can_create_user($newuserdom,$context,$_); } keys(%longtypes); # Get new users list + my (%existinguser,%userinfo,%disallow,%rulematch,%inst_results,%idinst_results,%alerts,%checkuname); + my $counter = -1; foreach my $line (@userdata) { + $counter ++; my @secs; my %entries=&Apache::loncommon::record_sep($line); # Determine user name @@ -4329,23 +4332,20 @@ sub upfile_drop_add { if ($entries{$fields{'username'}} =~ /\s/) { $nowhitespace = ' - '.&mt('usernames may not contain spaces.'); } - $r->print( - '
'. + $disallow{$counter} = &mt('Unacceptable username [_1] for user [_2] [_3] [_4] [_5]', - '"'.$entries{$fields{'username'}}.'"', - $fname,$mname,$lname,$gen). - $nowhitespace); + '"'.$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( - '
'. + $disallow{$counter} = &mt('Unacceptable domain [_1] for user [_2] [_3] [_4] [_5]', - '"'.$entries{$fields{'domain'}}.'"', - $fname,$mname,$lname,$gen)); - next; + '"'.$entries{$fields{'domain'}}.'"', + $fname,$mname,$lname,$gen); + next; } my $username = $entries{$fields{'username'}}; my $userdomain = $entries{$fields{'domain'}}; @@ -4357,10 +4357,15 @@ sub upfile_drop_add { $entries{$fields{'sec'}} =~ s/\W//g; my $item = $entries{$fields{'sec'}}; if ($item eq "none" || $item eq 'all') { - $r->print('
'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.',''.$username.'',$fname,$mname,$lname,$gen,$item)); + $disallow{$counter} = + &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.', + ''.$username.'',$fname,$mname,$lname,$gen,$item); next; } elsif (exists($curr_groups{$item})) { - $r->print('
'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.',''.$username.'',$fname,$mname,$lname,$gen,$item).' '.&mt('Section names and group names must be distinct.')); + $disallow{$counter} = + &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.', + ''.$username.'',$fname,$mname,$lname,$gen,$item).' '. + &mt('Section names and group names must be distinct.'); next; } else { push(@secs,$item); @@ -4372,14 +4377,21 @@ sub upfile_drop_add { if (ref($userlist{$username.':'.$userdomain}) eq 'ARRAY') { my $currsec = $userlist{$username.':'.$userdomain}[$secidx]; if ($currsec ne $env{'request.course.sec'}) { - $r->print('
'.&mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".',''.$username.'',$fname,$mname,$lname,$gen,$secs[0]).'
'); + $disallow{$counter} = + &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".', + ''.$username.'',$fname,$mname,$lname,$gen,$secs[0]); if ($currsec eq '') { - $r->print(&mt('This user already has an active/future student role in the course, unaffiliated to any section.')); + $disallow{$counter} .= + &mt('This user already has an active/future student role in the course, unaffiliated to any section.'); } else { - $r->print(&mt('This user already has an active/future role in section "[_1]" of the course.',$currsec)); + $disallow{$counter} .= + &mt('This user already has an active/future role in section "[_1]" of the course.',$currsec); } - $r->print('
'.&mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.',$secs[0]).'
'); + $disallow{$counter} .= + '
'. + &mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.', + $secs[0]); next; } } @@ -4431,13 +4443,12 @@ sub upfile_drop_add { } if ($role eq '') { my $rolestr = join(', ',@permitted_roles); - $r->print('
' - .&mt('[_1]: You do not have permission to add the requested role [_2] for the user.' - ,''.$entries{$fields{'username'}}.'' - ,$entries{$fields{'role'}}) - .'
' - .&mt('Allowable role(s) is/are: [_1].',$rolestr)."\n" - ); + $disallow{$counter} = + &mt('[_1]: You do not have permission to add the requested role [_2] for the user.' + ,''.$entries{$fields{'username'}}.'' + ,$entries{$fields{'role'}}) + .'
' + .&mt('Allowable role(s) is/are: [_1].',$rolestr); next; } } @@ -4467,55 +4478,36 @@ sub upfile_drop_add { # check against rules my $checkid = 0; my $newuser = 0; - my (%rulematch,%inst_results,%idinst_results); my $uhome=&Apache::lonnet::homeserver($username,$userdomain); if ($uhome eq 'no_host') { 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).'
'); + $disallow{$counter} = + &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).'
'); + $disallow{$counter} = + &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).'
'); + $disallow{$counter} = + &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.')); + $disallow{$counter} .= + &mt('The user does not already exist, and you may not create a new user in a different domain.'); next; + } else { + unless ($password || $env{'form.login'} eq 'loc') { + $disallow{$counter} = + &mt('[_1]: This is a new user but no default password was provided, and the authentication type requires one.', + ''.$username.''); + next; + } } $checkid = 1; $newuser = 1; - my $user = $username.':'.$newuserdom; - my $checkhash; - my $checks = { 'username' => 1 }; - $checkhash->{$username.':'.$newuserdom} = { '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'}{$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.')); - next; - } - } - } - my $usertype = 'unofficial'; - if (ref($rulematch{$user}) eq 'HASH') { - if ($rulematch{$user}{'username'}) { - $usertype = 'official'; - } - } - 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: [_2].',''.$username.'',$showtype)); - next; - } + $checkuname{$username.':'.$newuserdom} = { 'newuser' => 1, 'id' => 1 }; } else { if ($context eq 'course' || $context eq 'author') { if ($userdomain eq $domain ) { @@ -4548,77 +4540,165 @@ sub upfile_drop_add { } } } + if ($id) { + $existinguser{$userdomain}{$username} = $id; + } } - if ($id ne '') { - if (!$newuser) { - my %idhash = &Apache::lonnet::idrget($userdomain,($username)); - if ($idhash{$username} ne $id) { - $checkid = 1; + $userinfo{$counter} = { + username => $username, + domain => $userdomain, + fname => $fname, + mname => $mname, + lname => $lname, + gen => $gen, + email => $email, + id => $id, + password => $password, + inststatus => $inststatus, + role => $role, + sections => \@secs, + credits => $credits, + newuser => $newuser, + checkid => $checkid, + }; + } + } + } # end of foreach (@userdata) + if ($counter > -1) { + my $total = $counter + 1; + my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,$total); + my %checkids; + if (keys(%existinguser)) { + foreach my $dom (keys(%existinguser)) { + if (ref($existinguser{$dom}) eq 'HASH') { + my %idhash = &Apache::lonnet::idrget($dom,keys(%{$existinguser{$dom}})); + foreach my $username (keys(%{$existinguser{$dom}})) { + if ($idhash{$username} ne $existinguser{$dom}{$username}) { + $checkids{$username.':'.$dom} = { 'id' => $existinguser{$dom}{$username} }; + } + } + if (keys(%checkids)) { + &Apache::loncommon::user_rule_check(\%checkids,{ 'id' => 1 }, + \%alerts,\%rulematch, + \%idinst_results,\%curr_rules, + \%got_rules); + } + } + } + } + if (keys(%checkuname)) { + &Apache::loncommon::user_rule_check(\%checkuname,{ 'username' => 1 }, + \%alerts,\%rulematch,\%inst_results, + \%curr_rules,\%got_rules); + } + $r->print(''); + } # end of loop + &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state); + } # Flush the course logs so reverse user roles immediately updated $r->register_cleanup(\&Apache::lonnet::flushcourselogs); $r->print("

\n

\n".&mt('Processed [quant,_1,user].',$counts{'user'}). @@ -4757,11 +4825,12 @@ sub user_change_result { my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc, $username,$userdomain,$userchg) = @_; my $okresult = 0; + my @status; if ($userresult ne 'ok') { if ($userresult =~ /^error:(.+)$/) { my $error = $1; - $r->print('
'. - &mt('[_1]: Unable to add/modify: [_2]',''.$username.':'.$userdomain.'',$error)); + push(@status, + &mt('[_1]: Unable to add/modify: [_2]',''.$username.':'.$userdomain.'',$error)); } } else { $counts->{'user'} ++; @@ -4770,8 +4839,8 @@ sub user_change_result { if ($authresult ne 'ok') { if ($authresult =~ /^error:(.+)$/) { my $error = $1; - $r->print('
'. - &mt('[_1]: Unable to modify authentication: [_2]',''.$username.':'.$userdomain.'',$error)); + push(@status, + &mt('[_1]: Unable to modify authentication: [_2]',''.$username.':'.$userdomain.'',$error)); } } else { $counts->{'auth'} ++; @@ -4780,8 +4849,8 @@ sub user_change_result { if ($roleresult ne 'ok') { if ($roleresult =~ /^error:(.+)$/) { my $error = $1; - $r->print('
'. - &mt('[_1]: Unable to add role: [_2]',''.$username.':'.$userdomain.'',$error)); + push(@status, + &mt('[_1]: Unable to add role: [_2]',''.$username.':'.$userdomain.'',$error)); } } else { $counts->{'role'} ++; @@ -4790,14 +4859,16 @@ sub user_change_result { if ($okresult) { $flushc++; $userchg->{$username.':'.$userdomain}=1; - $r->print('. '); if ($flushc>15) { $r->rflush; $flushc=0; } } if ($idresult) { - $r->print($idresult); + push(@status,$idresult); + } + if (@status) { + $r->print('

  • '.join('
    ',@status).'
  • '); } return $flushc; }