--- loncom/enrollment/Autoupdate.pl 2011/10/14 17:13:25 1.20 +++ loncom/enrollment/Autoupdate.pl 2023/12/06 23:38:45 1.26 @@ -1,7 +1,7 @@ #!/usr/bin/perl # # Automated Userinfo update script -# $Id: Autoupdate.pl,v 1.20 2011/10/14 17:13:25 raeburn Exp $ +# $Id: Autoupdate.pl,v 1.26 2023/12/06 23:38:45 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -28,6 +28,7 @@ use strict; use lib '/home/httpd/lib/perl'; use localenroll; + use GDBM_File; use Apache::lonnet; use Apache::loncommon; use Apache::lonlocal; @@ -53,11 +54,19 @@ #only run if configured to my $run_update = 0; my $settings; + my $allowed_inactivity; + my $check_unexpired; if (ref($domconfig{'autoupdate'}) eq 'HASH') { $settings = $domconfig{'autoupdate'}; if ($settings->{'run'} eq '1') { $run_update = 1; } + if ($settings->{'lastactive'} =~/^\d+$/) { + $allowed_inactivity = 86400 * $settings->{'lastactive'}; + } + if ($settings->{'unexpired'}) { + $check_unexpired = 1; + } } next if (!$run_update); open(my $fh,">>$logfile"); @@ -65,10 +74,26 @@ # get courseIDs for domain my %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.'); # get user information - my (%users,%instusers,%instids); + my (%users,%instusers,%instids,%counts); my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom; - &descend_tree($dir,0,\%users,\%courses); - next if (&localenroll::allusers_info($dom,\%instusers,\%instids,\%users) ne 'ok'); + &descend_tree($dom,$dir,0,\%users,\%courses,$allowed_inactivity,$check_unexpired); + my $resp = &localenroll::allusers_info($dom,\%instusers,\%instids,\%users,\%counts); + if ($resp ne 'ok') { + print $fh &mt('Problem retrieving institutional data for users in domain: [_1].',$dom)."\n". + &mt('Error: [_1].',$resp)."\n". + "-- \n".&Apache::lonlocal::locallocaltime(time).' '. + &mt('Autoupdate messages end')."\n*******************\n\n"; + close($fh); + next; + } else { + if (keys(%counts)) { + foreach my $key (sort(keys(%counts))) { + if ($counts{$key} ne '') { + print $fh "Count for $key = $counts{$key}\n"; + } + } + } + } my (%unamechg,%possnames); my @types = ('active','future'); my @roles = ('st'); @@ -78,7 +103,7 @@ my (@inststatuses); if (!$userhash{'internalname'}) { if (defined($instusers{$uname})) { - (my $insttypechg,@inststatuses) = + (my $insttypechg,@inststatuses) = &affiliations_check(\%userhash,$instusers{$uname}); if ($insttypechg) { my $inststatusstr = join(':',map { &escape($_) } (@inststatuses)); @@ -98,17 +123,17 @@ push(@fields,$field); } } - } + } } } else { if (ref($settings->{fields}{'default'}) eq 'ARRAY') { @fields = @{$settings->{fields}{'default'}}; } } - foreach my $field (@fields) { + foreach my $field (@fields) { if ($userhash{$field} ne $instusers{$uname}{$field}) { $changed = 1; - if ($settings->{'classlists'} == 1) { + if ($settings->{'classlists'} == 1) { if ($field eq 'id') { $changes{'id'} = 1; } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'generation') { @@ -132,14 +157,14 @@ print $fh "User change: $uname:$dom; New values: 1.Name: $userupdate{'firstname'} $userupdate{'middlename'} $userupdate{'lastname'} $userupdate{'generation'}; 2. StuID: $userupdate{'id'}; 3. Email: $userupdate{'permanentemail'}.\n"; if ($settings->{'classlists'} == 1) { if ($changes{'id'} || $changes{'fullname'}) { - my %roleshash = + my %roleshash = &Apache::lonnet::get_my_roles($uname, $dom,'userroles',\@types,\@roles,\@cdoms); foreach my $item (keys(%roleshash)) { my ($cnum,$cdom,$role) = split(/:/,$item); my ($start,$end) = split(/:/,$roleshash{$item}); if (&Apache::lonnet::is_course($cdom,$cnum)) { - my $result = + my $result = &Apache::lonuserutils::update_classlist($cdom,$cnum,$dom,$uname,\%userupdate); if ($result eq 'ok') { print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n"; @@ -184,18 +209,65 @@ } sub descend_tree { - my ($dir,$depth,$alldomusers,$coursesref) = @_; + my ($dom,$dir,$depth,$alldomusers,$coursesref,$allowed_inactivity,$check_unexpired) = @_; if (-d $dir) { opendir(DIR,$dir); my @contents = grep(!/^\./,readdir(DIR)); closedir(DIR); $depth ++; foreach my $item (@contents) { - if ($depth < 4) { - &descend_tree($dir.'/'.$item,$depth,$alldomusers,$coursesref); + if (($depth < 4) && (length($item) == 1)) { + &descend_tree($dom,$dir.'/'.$item,$depth,$alldomusers,$coursesref, + $allowed_inactivity,$check_unexpired); } elsif (-e $dir.'/'.$item.'/passwd') { if (ref($coursesref) eq 'HASH') { - next if (exists($coursesref->{$item})); + next if (exists($coursesref->{$dom.'_'.$item})); + } + if ($allowed_inactivity) { + my $now = time; + my $aclog = $dir.'/'.$item.'/activity.log'; + my $roledb = $dir.'/'.$item.'/roles.db'; + if (-e $aclog) { + my $lastac=(stat($aclog))[9]; + if (($now - $lastac) > $allowed_inactivity) { + if (-e $roledb) { + my $lastrolechg=(stat($roledb))[9]; + next if (($now - $lastrolechg) > $allowed_inactivity); + } else { + next; + } + } + } elsif (-e $roledb) { + my $lastrolechg=(stat($roledb))[9]; + next if (($now - $lastrolechg) > $allowed_inactivity); + } else { + next; + } + } + if ($check_unexpired) { + my $roledb = $dir.'/'.$item.'/roles.db'; + my $unexpired = 0; + my $now = time; + if (-e $roledb) { + my $roleshash = &LONCAPA::tie_user_hash($dom,$item,'roles',&GDBM_READER()) or next; + if (ref($roleshash)) { + while (my ($key,$value) = each(%$roleshash)) { + next if ($key =~ /^rolesdef/); + my ($role,$roleend,$rolestart) = split(/\_/,$value); + next if ($role =~ /^gr\//); + if (!$roleend || $roleend > $now) { + $unexpired = 1; + last; + } + } + &LONCAPA::untie_user_hash($roleshash); + next unless ($unexpired); + } else { + next; + } + } else { + next; + } } $$alldomusers{$item} = ''; }