File:  [LON-CAPA] / loncom / enrollment / Autoupdate.pl
Revision 1.28: download - view: text, annotated - select for diffs
Sun Dec 10 17:43:06 2023 UTC (5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_4_msu, HEAD
- Fix path to id_check() routine in localenroll.pm

    1: #!/usr/bin/perl
    2: #
    3: # Automated Userinfo update script
    4: # $Id: Autoupdate.pl,v 1.28 2023/12/10 17:43:06 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','usercreation'],
   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,@idrules);
   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:         if (ref($domconfig{'usercreation'}) eq 'HASH') {
  102:             if (ref($domconfig{'usercreation'}{'id_rule'}) eq 'ARRAY') {
  103:                 @idrules = @{$domconfig{'usercreation'}{'id_rule'}};
  104:             }
  105:         }
  106:         foreach my $uname (sort(keys(%users))) {
  107:             my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
  108:             my (@inststatuses);
  109:             if (!$userhash{'internalname'}) {
  110:                 if (defined($instusers{$uname})) {
  111:                     (my $insttypechg,@inststatuses) =
  112:                        &affiliations_check(\%userhash,$instusers{$uname});
  113:                     if ($insttypechg) {
  114:                         my $inststatusstr = join(':',map { &escape($_) } (@inststatuses));
  115:                         my %statushash = ( inststatus => $inststatusstr );
  116:                         my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
  117:                     }
  118:                 }
  119:             }
  120:             if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
  121:                 if (defined($instusers{$uname})) {
  122:                     my (@fields,%changes,$changed);
  123:                     if (@inststatuses > 0) {
  124:                         foreach my $type (@inststatuses) {
  125:                             if (ref($settings->{fields}{$type}) eq 'ARRAY') {
  126:                                 foreach my $field (@{$settings->{fields}{$type}}) {
  127:                                     if (!grep(/^\Q$field\E$/,@fields)) {
  128:                                         push(@fields,$field);
  129:                                     }
  130:                                 }
  131:                             }
  132:                         }
  133:                     } else {
  134:                         if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
  135:                             @fields = @{$settings->{fields}{'default'}};
  136:                         }
  137:                     }
  138:                     foreach my $field (@fields) {
  139:                         if ($userhash{$field} ne $instusers{$uname}{$field}) {
  140:                             # Do not change student/employee ID when field is modifiable
  141:                             # but institutional data is absent or does not match rules
  142:                             # defined for IDs for institutional users in domain
  143:                             if ($field eq 'id') {
  144:                                 if ($instusers{$uname}{$field} eq '') {
  145:                                     $instusers{$uname}{$field} = $userhash{$field};
  146:                                     next;
  147:                                 } elsif (@idrules > 0) {
  148:                                     my %idchecks;
  149:                                     if (&localenroll::id_check($dom,$instusers{$uname}{$field},
  150:                                                                \@idrules,\%idchecks) eq 'ok') {
  151:                                         my $matches;
  152:                                         foreach my $result (values(%idchecks)) {
  153:                                             if ($result) {
  154:                                                 $matches = 1;
  155:                                                 last;
  156:                                             }
  157:                                         }
  158:                                         if ($matches) {
  159:                                             $changed = 1;
  160:                                         } else {
  161:                                             $instusers{$uname}{$field} = $userhash{$field}; 
  162:                                             next;
  163:                                         }
  164:                                     }
  165:                                 }
  166:                             } else {
  167:                                 $changed = 1;
  168:                             }
  169:                             if ($field eq 'id') {
  170:                                 $changes{'id'} = 1;
  171:                             } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'generation') {
  172:                                 $changes{'fullname'} = 1;
  173:                             }
  174:                         }
  175:                     }
  176:                     # Do not change name information when lastname and firstname are modifiable
  177:                     # but institutional data is absent for both components of a user's name.
  178:                     if ($changed) {
  179:                         if ($changes{'fullname'}) {
  180:                             my %modifiable;
  181:                             map { $modifiable{$_} = 1; } @fields;
  182:                             if (($modifiable{'lastname'}) && ($modifiable{'firstname'}) &&
  183:                                 ($instusers{$uname}{'lastname'} eq '') && ($instusers{$uname}{'firstname'} eq '')) {
  184:                                 if ($changes{'fullname'}) {
  185:                                     undef($changes{'fullname'});
  186:                                 }
  187:                                 if ($changes{'id'}) {
  188:                                     foreach my $field ('lastname','firstname','middlename','generation') {
  189:                                         $instusers{$uname}{$field} = $userhash{$field};
  190:                                     }
  191:                                 } else {
  192:                                     undef($changed);
  193:                                 }
  194:                             }
  195:                         }
  196:                     }
  197:                     # Make the change
  198:                     if ($changed) {
  199:                         my %userupdate;
  200:                         foreach my $item ('lastname','firstname','middlename','generation','id',
  201:                                           'permanentemail') {
  202:                             $userupdate{$item} = $userhash{$item};
  203:                         }
  204:                         foreach my $field (@fields) {
  205:                             $userupdate{$field} = $instusers{$uname}{$field};
  206:                         }
  207:                         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);
  208:                         if ($modresult eq 'ok') {
  209:                             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";
  210:                             if ($settings->{'classlists'} == 1) {
  211:                                 if ($changes{'id'} || $changes{'fullname'}) {
  212:                                     my %roleshash =
  213:                                         &Apache::lonnet::get_my_roles($uname,
  214:                                                   $dom,'userroles',\@types,\@roles,\@cdoms);
  215:                                     foreach my $item (keys(%roleshash)) {
  216:                                         my ($cnum,$cdom,$role) = split(/:/,$item);
  217:                                         my ($start,$end) = split(/:/,$roleshash{$item});
  218:                                         if (&Apache::lonnet::is_course($cdom,$cnum)) {
  219:                                             my $result =
  220:                                                 &Apache::lonuserutils::update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
  221:                                             if ($result eq 'ok') {
  222:                                                 print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n";
  223:                                             } else {
  224:                                                 print $fh "Error - $result -during classlist update for $uname:$dom in $cnum:$cdom\n";
  225:                                             }
  226:                                         }
  227:                                     }
  228:                                 }
  229:                             }
  230:                         } else {
  231:                             print $fh "User change detected for $uname:$dom. Error during update: $modresult\n";
  232:                         }
  233:                     }
  234:                 } else {
  235:                     # check if the username has changed
  236:                     if (defined($instids{$userhash{'id'}})) {
  237:                         if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
  238:                             foreach my $name (@{$instids{$userhash{'id'}}}) {
  239:                                 if (!exists($users{$name})) {
  240:                                     push(@{$possnames{$uname}},$name);
  241:                                 }
  242:                             }
  243:                         } else {
  244:                             if (!exists($users{$instids{$userhash{'id'}}})) {
  245:                                 $unamechg{$uname} = $instids{$userhash{'id'}};
  246:                                 print $fh (&mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n");
  247:                             }
  248:                         }
  249:                     }
  250:                 }
  251:             }
  252:         }
  253:         if (keys(%possnames) > 0) {
  254:             foreach my $uname (keys(%possnames)) {
  255:                 my $altnames = join(' or ',@{$possnames{$uname}});
  256:                 print $fh (&mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n");
  257:             }
  258:         }
  259:         print $fh ("-- \n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n");
  260:         close($fh);
  261:     }
  262: 
  263: sub descend_tree {
  264:     my ($dom,$dir,$depth,$alldomusers,$coursesref,$allowed_inactivity,$check_unexpired) = @_;
  265:     if (-d $dir) {
  266:         opendir(DIR,$dir);
  267:         my @contents = grep(!/^\./,readdir(DIR));
  268:         closedir(DIR);
  269:         $depth ++;
  270:         foreach my $item (@contents) {
  271:             if (($depth < 4) && (length($item) == 1)) {
  272:                 &descend_tree($dom,$dir.'/'.$item,$depth,$alldomusers,$coursesref,
  273:                               $allowed_inactivity,$check_unexpired);
  274:             } elsif (-e $dir.'/'.$item.'/passwd') {
  275:                 if (ref($coursesref) eq 'HASH') {
  276:                     next if (exists($coursesref->{$dom.'_'.$item}));
  277:                 }
  278:                 if ($allowed_inactivity) {
  279:                     my $now = time;
  280:                     my $aclog = $dir.'/'.$item.'/activity.log';
  281:                     my $roledb = $dir.'/'.$item.'/roles.db';
  282:                     if (-e $aclog) {
  283:                         my $lastac=(stat($aclog))[9];
  284:                         if (($now - $lastac) > $allowed_inactivity) {
  285:                             if (-e $roledb) {
  286:                                 my $lastrolechg=(stat($roledb))[9];
  287:                                 next if (($now - $lastrolechg) > $allowed_inactivity);
  288:                             } else {
  289:                                 next;
  290:                             }
  291:                         }
  292:                     } elsif (-e $roledb) {
  293:                         my $lastrolechg=(stat($roledb))[9];
  294:                         next if (($now - $lastrolechg) > $allowed_inactivity);
  295:                     } else {
  296:                         next;
  297:                     }
  298:                 }
  299:                 if ($check_unexpired) {
  300:                     my $roledb = $dir.'/'.$item.'/roles.db';
  301:                     my $unexpired = 0;
  302:                     my $now = time;
  303:                     if (-e $roledb) {
  304:                         my $roleshash = &LONCAPA::tie_user_hash($dom,$item,'roles',&GDBM_READER()) or next;
  305:                         if (ref($roleshash)) {
  306:                             while (my ($key,$value) = each(%$roleshash)) {
  307:                                 next if ($key =~ /^rolesdef/);
  308:                                 my ($role,$roleend,$rolestart) = split(/\_/,$value);
  309:                                 next if ($role =~ /^gr\//);
  310:                                 if (!$roleend || $roleend > $now) {
  311:                                     $unexpired = 1;
  312:                                     last;
  313:                                 }
  314:                             }
  315:                             &LONCAPA::untie_user_hash($roleshash);
  316:                             next unless ($unexpired);
  317:                         } else {
  318:                             next;
  319:                         }
  320:                     } else {
  321:                         next;
  322:                     }
  323:                 }
  324:                 $$alldomusers{$item} = '';
  325:             }
  326:         }
  327:     }
  328: }
  329: 
  330: sub affiliations_check {
  331:     my ($userhash,$insthashref) = @_;
  332:     my (@inststatuses,$insttypechg);;
  333:     if (ref($insthashref) eq 'HASH') {
  334:         if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
  335:             @inststatuses = @{$insthashref->{'inststatus'}};
  336:         }
  337:     }
  338:     my @currstatuses = map { &unescape($_); } (split(/:/,$userhash->{'inststatus'}));
  339:     foreach my $status (@inststatuses) {
  340:         if (!grep/^\Q$status\E/,@currstatuses) {
  341:             $insttypechg = 1;
  342:         }
  343:     }
  344:     if (!$insttypechg) {
  345:         foreach my $status (@currstatuses) {
  346:             if (!grep/^\Q$status\E/,@inststatuses) {
  347:                 $insttypechg = 1;
  348:             }
  349:         }
  350:     }
  351:     return ($insttypechg,@inststatuses);
  352: }
  353: 

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