--- loncom/enrollment/Autoupdate.pl 2007/02/16 01:37:54 1.1 +++ loncom/enrollment/Autoupdate.pl 2007/05/11 17:25:28 1.5 @@ -1,6 +1,7 @@ #!/usr/bin/perl # # Automated Userinfo update script +# $Id: Autoupdate.pl,v 1.5 2007/05/11 17:25:28 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -27,15 +28,22 @@ use strict; use lib '/home/httpd/lib/perl'; use localenroll; - use LONCAPA::Configuration; - use LONCAPA::Enrollment; use Apache::lonnet; use Apache::loncommon; + use Apache::lonlocal; + use LONCAPA::Configuration; + use LONCAPA; + my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf'); + my $hostid = $perlvarref->{'lonHostID'}; my @info = ('inststatus','lockedname','lastname','firstname','id'); # find out which users we need to examine my @domains = sort(&Apache::lonnet::current_machine_domains()); foreach my $dom (@domains) { + my $primaryhost_id = &Apache::lonnet::domain($dom,'primary'); + if ($primaryhost_id ne $hostid) { + next; + } my %domconfig = &Apache::lonnet::get_dom('configuration',['autoupdate'], $dom); #only run if configured to @@ -43,12 +51,15 @@ my $settings; if (ref($domconfig{'autoupdate'}) eq 'HASH') { $settings = $domconfig{'autoupdate'}; - if ($settings->{'run'} eq 'yes') { + if ($settings->{'run'} eq '1') { $run_update = 1; } } next if (!$run_update); - my %users; + # get user information + my (%instusers,%instids); + next if (&localenroll::allusers_info($dom,\%instusers,\%instids) ne 'ok'); + my (%users,%unamechg,%possnames); my @types = ('active','future'); my @roles = ('st'); my @cdoms = ($dom); @@ -56,19 +67,38 @@ &descend_tree($dir,0,\%users); foreach my $uname (keys(%users)) { my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info); + my (@inststatuses); + if (!$userhash{'internalname'}) { + if (defined($instusers{$uname})) { + (my $insttypechg,@inststatuses) = + &affiliations_check(\%userhash,$instusers{$uname}); + if ($insttypechg) { + my $inststatusstr = join(':',&escape(@inststatuses)); + my %statushash = ( inststatus => $inststatusstr ); + my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname); + } + } + } if (!$userhash{'lockedname'} && !$userhash{'internalname'}) { - my %userinfo = &localenroll::get_userinfo($dom,$uname,%userhash); - if (keys(%userinfo) > 0) { - my @fields = @{$settings->{'default'}}; - if ($userhash{'inststatus'} ne '') { - if (ref($settings->{$userhash{'inststatus'}}) eq 'ARRAY') { - @fields = @{$settings->{$userhash{'inststatus'}}}; + if (defined($instusers{$uname})) { + my (@fields,%changes,$changed); + if (@inststatuses > 0) { + foreach my $type (@inststatuses) { + if (ref($settings->{fields}{$type}) eq 'ARRAY') { + foreach my $field (@{$settings->{fields}{$type}}) { + if (!grep(/^\Q$field\E$/,@fields)) { + push(@fields,$field); + } + } + } + } + } else { + if (ref($settings->{fields}{'default'}) eq 'ARRAY') { + @fields = @{$settings->{fields}{'default'}}; } } - my %changes; - my $changed; foreach my $field (@fields) { - if ($userhash{$field} ne $userinfo{$field}) { + if ($userhash{$field} ne $instusers{$uname}{$field}) { $changed = 1; if ($settings->{'classlists'} eq 'yes') { if ($field eq 'id') { @@ -83,16 +113,15 @@ if ($changed) { my %userupdate; foreach my $field (@fields) { - $userupdate{$field} = $userinfo{$field}; + $userupdate{$field} = $instusers{$uname}{$field}; } - my $putresult = &Apache::lonnet::put - ('environment',\%userupdate,$dom,$uname); - if ($putresult eq 'ok') { + my $modresult = &Apache::lonnet::modifyuser($dom,$uname,$userupdate{'id'},undef,undef,$userupdate{'firstname'},$userupdate{'middlename'},$userupdate{'lastname'},$userupdate{'generation'},1); + if ($modresult eq 'ok') { if ($settings->{'classlists'} eq 'yes') { if ($changes{'id'} || $changes{'fullname'}) { my %roleshash = &Apache::lonnet::get_my_roles($uname, - $dom,\@types,\@roles,\@cdoms); + $dom,'userroles',\@types,\@roles,\@cdoms); foreach my $item (%roleshash) { my ($cnum,$cdom,$role) = split(/:/,$item); my ($start,$end) = split(/:/,$roleshash{$item}); @@ -104,12 +133,33 @@ } } } + } else { + # check if the username has changed + if (defined($instids{$userhash{'id'}})) { + if (ref($instids{$userhash{'id'}}) eq 'ARRAY') { + foreach my $name (@{$instids{$userhash{'id'}}}) { + if (!exists($users{$name})) { + push(@{$possnames{$uname}},$name); + } + } + } else { + if (!exists($users{$instids{$userhash{'id'}}})) { + $unamechg{$uname} = $instids{$userhash{'id'}}; + print &mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n"; + } + } + } } } } + if (keys(%possnames) > 0) { + foreach my $uname (keys(%possnames)) { + my $altnames = join(' or ',@{$possnames{$uname}}); + print &mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n"; + } + } } - sub descend_tree { my ($dir,$depth,$alldomusers) = @_; if (-d $dir) { @@ -165,3 +215,27 @@ sub update_classlist { } } +sub affiliations_check { + my ($userhash,$insthashref) = @_; + my (@inststatuses,$insttypechg);; + if (ref($insthashref) eq 'HASH') { + if (ref($insthashref->{type}) eq 'ARRAY') { + @inststatuses = @{$insthashref->{type}}; + } + } + my @currstatuses = &unescape(split(/:/,$userhash->{'inststatus'})); + foreach my $status (@inststatuses) { + if (!grep/^\Q$status\E/,@currstatuses) { + $insttypechg = 1; + } + } + if (!$insttypechg) { + foreach my $status (@currstatuses) { + if (!grep/^\Q$status\E/,@inststatuses) { + $insttypechg = 1; + } + } + } + return ($insttypechg,@inststatuses); +} +