File:  [LON-CAPA] / loncom / enrollment / Autoupdate.pl
Revision 1.26: download - view: text, annotated - select for diffs
Wed Dec 6 23:38:45 2023 UTC (6 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Remove trailing whitespace.

    1: #!/usr/bin/perl
    2: #
    3: # Automated Userinfo update script
    4: # $Id: Autoupdate.pl,v 1.26 2023/12/06 23:38:45 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28:     use strict;
   29:     use lib '/home/httpd/lib/perl';
   30:     use localenroll;
   31:     use GDBM_File;
   32:     use Apache::lonnet;
   33:     use Apache::loncommon;
   34:     use Apache::lonlocal;
   35:     use Apache::lonuserutils();
   36:     use LONCAPA::Configuration;
   37:     use LONCAPA;
   38: 
   39:     my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   40:     exit if (ref($perlvarref) ne 'HASH');
   41:     my $logfile = $perlvarref->{'lonDaemons'}.'/logs/autoupdate.log';
   42:     exit if ($perlvarref->{'lonRole'} ne 'library');
   43:     my $hostid = $perlvarref->{'lonHostID'};
   44: 
   45:     my @info = ('inststatus','lockedname','internalname','lastname',
   46:                 'firstname','middlename','generation','id','permanentemail');
   47:     # Initialize language handler
   48:     &Apache::lonlocal::get_language_handle();
   49:     # find out which users we need to examine
   50:     my @domains = sort(&Apache::lonnet::current_machine_domains());
   51:     foreach my $dom (@domains) {
   52:         my %domconfig = &Apache::lonnet::get_dom('configuration',['autoupdate'],
   53:                                                  $dom);
   54:         #only run if configured to
   55:         my $run_update = 0;
   56:         my $settings;
   57:         my $allowed_inactivity;
   58:         my $check_unexpired;
   59:         if (ref($domconfig{'autoupdate'}) eq 'HASH') {
   60:             $settings = $domconfig{'autoupdate'};
   61:             if ($settings->{'run'} eq '1') {
   62:                 $run_update = 1;
   63:             }
   64:             if ($settings->{'lastactive'} =~/^\d+$/) {
   65:                 $allowed_inactivity = 86400 * $settings->{'lastactive'};
   66:             }
   67:             if ($settings->{'unexpired'}) {
   68:                 $check_unexpired = 1;
   69:             }
   70:         }
   71:         next if (!$run_update);
   72:         open(my $fh,">>$logfile");
   73:         print $fh ("********************\n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages start for domain: [_1]',$dom).' --'."\n");
   74:         # get courseIDs for domain
   75:         my %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.');
   76:         # get user information
   77:         my (%users,%instusers,%instids,%counts);
   78:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
   79:         &descend_tree($dom,$dir,0,\%users,\%courses,$allowed_inactivity,$check_unexpired);
   80:         my $resp = &localenroll::allusers_info($dom,\%instusers,\%instids,\%users,\%counts);
   81:         if ($resp ne 'ok') {
   82:             print $fh &mt('Problem retrieving institutional data for users in domain: [_1].',$dom)."\n".
   83:                       &mt('Error: [_1].',$resp)."\n".
   84:                       "-- \n".&Apache::lonlocal::locallocaltime(time).' '.
   85:                       &mt('Autoupdate messages end')."\n*******************\n\n";
   86:             close($fh);
   87:             next;
   88:         } else {
   89:             if (keys(%counts)) {
   90:                 foreach my $key (sort(keys(%counts))) {
   91:                     if ($counts{$key} ne '') {
   92:                         print $fh "Count for $key = $counts{$key}\n";
   93:                     }
   94:                 }
   95:             }
   96:         }
   97:         my (%unamechg,%possnames);
   98:         my @types = ('active','future');
   99:         my @roles = ('st');
  100:         my @cdoms = ($dom);
  101:         foreach my $uname (sort(keys(%users))) {
  102:             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
  103:             my (@inststatuses);
  104:             if (!$userhash{'internalname'}) {
  105:                 if (defined($instusers{$uname})) {
  106:                     (my $insttypechg,@inststatuses) =
  107:                        &affiliations_check(\%userhash,$instusers{$uname});
  108:                     if ($insttypechg) {
  109:                         my $inststatusstr = join(':',map { &escape($_) } (@inststatuses));
  110:                         my %statushash = ( inststatus => $inststatusstr );
  111:                         my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
  112:                     }
  113:                 }
  114:             }
  115:             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
  116:                 if (defined($instusers{$uname})) {
  117:                     my (@fields,%changes,$changed);
  118:                     if (@inststatuses > 0) {
  119:                         foreach my $type (@inststatuses) {
  120:                             if (ref($settings->{fields}{$type}) eq 'ARRAY') {
  121:                                 foreach my $field (@{$settings->{fields}{$type}}) {
  122:                                     if (!grep(/^\Q$field\E$/,@fields)) {
  123:                                         push(@fields,$field);
  124:                                     }
  125:                                 }
  126:                             }
  127:                         }
  128:                     } else {
  129:                         if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
  130:                             @fields = @{$settings->{fields}{'default'}};
  131:                         }
  132:                     }
  133:                     foreach my $field (@fields) {
  134:                         if ($userhash{$field} ne $instusers{$uname}{$field}) {
  135:                             $changed = 1;
  136:                             if ($settings->{'classlists'} == 1) {
  137:                                 if ($field eq 'id') {
  138:                                     $changes{'id'} = 1;
  139:                                 } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'generation') {
  140:                                     $changes{'fullname'} = 1;
  141:                                 }
  142:                             }
  143:                         }
  144:                     }
  145:                     # Make the change
  146:                     if ($changed) {
  147:                         my %userupdate;
  148:                         foreach my $item ('lastname','firstname','middlename','generation','id',
  149:                                           'permanentemail') {
  150:                             $userupdate{$item} = $userhash{$item};
  151:                         }
  152:                         foreach my $field (@fields) {
  153:                             $userupdate{$field} = $instusers{$uname}{$field};
  154:                         }
  155:                         my $modresult = &Apache::lonnet::modifyuser($dom,$uname,$userupdate{'id'},undef,undef,$userupdate{'firstname'},$userupdate{'middlename'},$userupdate{'lastname'},$userupdate{'generation'},1,$hostid,$userupdate{'permanentemail'},undef,\@fields);
  156:                         if ($modresult eq 'ok') {
  157:                             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";
  158:                             if ($settings->{'classlists'} == 1) {
  159:                                 if ($changes{'id'} || $changes{'fullname'}) {
  160:                                     my %roleshash =
  161:                                         &Apache::lonnet::get_my_roles($uname,
  162:                                                   $dom,'userroles',\@types,\@roles,\@cdoms);
  163:                                     foreach my $item (keys(%roleshash)) {
  164:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
  165:                                         my ($start,$end) = split(/:/,$roleshash{$item});
  166:                                         if (&Apache::lonnet::is_course($cdom,$cnum)) {
  167:                                             my $result =
  168:                                                 &Apache::lonuserutils::update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
  169:                                             if ($result eq 'ok') {
  170:                                                 print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n";
  171:                                             } else {
  172:                                                 print $fh "Error - $result -during classlist update for $uname:$dom in $cnum:$cdom\n";
  173:                                             }
  174:                                         }
  175:                                     }
  176:                                 }
  177:                             }
  178:                         } else {
  179:                             print $fh "User change detected for $uname:$dom. Error during update: $modresult\n";
  180:                         }
  181:                     }
  182:                 } else {
  183:                     # check if the username has changed
  184:                     if (defined($instids{$userhash{'id'}})) {
  185:                         if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
  186:                             foreach my $name (@{$instids{$userhash{'id'}}}) {
  187:                                 if (!exists($users{$name})) {
  188:                                     push(@{$possnames{$uname}},$name);
  189:                                 }
  190:                             }
  191:                         } else {
  192:                             if (!exists($users{$instids{$userhash{'id'}}})) {
  193:                                 $unamechg{$uname} = $instids{$userhash{'id'}};
  194:                                 print $fh (&mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n");
  195:                             }
  196:                         }
  197:                     }
  198:                 }
  199:             }
  200:         }
  201:         if (keys(%possnames) > 0) {
  202:             foreach my $uname (keys(%possnames)) {
  203:                 my $altnames = join(' or ',@{$possnames{$uname}});
  204:                 print $fh (&mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n");
  205:             }
  206:         }
  207:         print $fh ("-- \n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n");
  208:         close($fh);
  209:     }
  210: 
  211: sub descend_tree {
  212:     my ($dom,$dir,$depth,$alldomusers,$coursesref,$allowed_inactivity,$check_unexpired) = @_;
  213:     if (-d $dir) {
  214:         opendir(DIR,$dir);
  215:         my @contents = grep(!/^\./,readdir(DIR));
  216:         closedir(DIR);
  217:         $depth ++;
  218:         foreach my $item (@contents) {
  219:             if (($depth < 4) && (length($item) == 1)) {
  220:                 &descend_tree($dom,$dir.'/'.$item,$depth,$alldomusers,$coursesref,
  221:                               $allowed_inactivity,$check_unexpired);
  222:             } elsif (-e $dir.'/'.$item.'/passwd') {
  223:                 if (ref($coursesref) eq 'HASH') {
  224:                     next if (exists($coursesref->{$dom.'_'.$item}));
  225:                 }
  226:                 if ($allowed_inactivity) {
  227:                     my $now = time;
  228:                     my $aclog = $dir.'/'.$item.'/activity.log';
  229:                     my $roledb = $dir.'/'.$item.'/roles.db';
  230:                     if (-e $aclog) {
  231:                         my $lastac=(stat($aclog))[9];
  232:                         if (($now - $lastac) > $allowed_inactivity) {
  233:                             if (-e $roledb) {
  234:                                 my $lastrolechg=(stat($roledb))[9];
  235:                                 next if (($now - $lastrolechg) > $allowed_inactivity);
  236:                             } else {
  237:                                 next;
  238:                             }
  239:                         }
  240:                     } elsif (-e $roledb) {
  241:                         my $lastrolechg=(stat($roledb))[9];
  242:                         next if (($now - $lastrolechg) > $allowed_inactivity);
  243:                     } else {
  244:                         next;
  245:                     }
  246:                 }
  247:                 if ($check_unexpired) {
  248:                     my $roledb = $dir.'/'.$item.'/roles.db';
  249:                     my $unexpired = 0;
  250:                     my $now = time;
  251:                     if (-e $roledb) {
  252:                         my $roleshash = &LONCAPA::tie_user_hash($dom,$item,'roles',&GDBM_READER()) or next;
  253:                         if (ref($roleshash)) {
  254:                             while (my ($key,$value) = each(%$roleshash)) {
  255:                                 next if ($key =~ /^rolesdef/);
  256:                                 my ($role,$roleend,$rolestart) = split(/\_/,$value);
  257:                                 next if ($role =~ /^gr\//);
  258:                                 if (!$roleend || $roleend > $now) {
  259:                                     $unexpired = 1;
  260:                                     last;
  261:                                 }
  262:                             }
  263:                             &LONCAPA::untie_user_hash($roleshash);
  264:                             next unless ($unexpired);
  265:                         } else {
  266:                             next;
  267:                         }
  268:                     } else {
  269:                         next;
  270:                     }
  271:                 }
  272:                 $$alldomusers{$item} = '';
  273:             }
  274:         }
  275:     }
  276: }
  277: 
  278: sub affiliations_check {
  279:     my ($userhash,$insthashref) = @_;
  280:     my (@inststatuses,$insttypechg);;
  281:     if (ref($insthashref) eq 'HASH') {
  282:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
  283:             @inststatuses = @{$insthashref->{'inststatus'}};
  284:         }
  285:     }
  286:     my @currstatuses = map { &unescape($_); } (split(/:/,$userhash->{'inststatus'}));
  287:     foreach my $status (@inststatuses) {
  288:         if (!grep/^\Q$status\E/,@currstatuses) {
  289:             $insttypechg = 1;
  290:         }
  291:     }
  292:     if (!$insttypechg) {
  293:         foreach my $status (@currstatuses) {
  294:             if (!grep/^\Q$status\E/,@inststatuses) {
  295:                 $insttypechg = 1;
  296:             }
  297:         }
  298:     }
  299:     return ($insttypechg,@inststatuses);
  300: }
  301: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>