Annotation of loncom/enrollment/Autoupdate.pl, revision 1.24
1.1 raeburn 1: #!/usr/bin/perl
2: #
3: # Automated Userinfo update script
1.24 ! raeburn 4: # $Id: Autoupdate.pl,v 1.23 2016/01/27 22:23:18 raeburn Exp $
1.1 raeburn 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;
1.24 ! raeburn 31: use GDBM_File;
1.1 raeburn 32: use Apache::lonnet;
33: use Apache::loncommon;
1.4 raeburn 34: use Apache::lonlocal;
1.11 raeburn 35: use Apache::lonuserutils();
1.5 raeburn 36: use LONCAPA::Configuration;
1.2 raeburn 37: use LONCAPA;
1.1 raeburn 38:
1.5 raeburn 39: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
1.19 raeburn 40: exit if (ref($perlvarref) ne 'HASH');
41: my $logfile = $perlvarref->{'lonDaemons'}.'/logs/autoupdate.log';
42: exit if ($perlvarref->{'lonRole'} ne 'library');
1.10 raeburn 43: my $hostid = $perlvarref->{'lonHostID'};
1.19 raeburn 44:
1.6 raeburn 45: my @info = ('inststatus','lockedname','internalname','lastname',
1.14 raeburn 46: 'firstname','middlename','generation','id','permanentemail');
1.6 raeburn 47: # Initialize language handler
48: &Apache::lonlocal::get_language_handle();
1.1 raeburn 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;
1.24 ! raeburn 57: my $allowed_inactivity;
! 58: my $check_unexpired;
1.1 raeburn 59: if (ref($domconfig{'autoupdate'}) eq 'HASH') {
60: $settings = $domconfig{'autoupdate'};
1.2 raeburn 61: if ($settings->{'run'} eq '1') {
1.1 raeburn 62: $run_update = 1;
63: }
1.24 ! raeburn 64: if ($settings->{'lastactive'} =~/^\d+$/) {
! 65: $allowed_inactivity = 86400 * $settings->{'lastactive'};
! 66: }
! 67: if ($settings->{'unexpired'}) {
! 68: $check_unexpired = 1;
! 69: }
1.1 raeburn 70: }
71: next if (!$run_update);
1.7 albertel 72: open(my $fh,">>$logfile");
1.17 raeburn 73: print $fh ("********************\n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages start for domain: [_1]',$dom).' --'."\n");
1.9 raeburn 74: # get courseIDs for domain
1.10 raeburn 75: my %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.');
1.2 raeburn 76: # get user information
1.20 raeburn 77: my (%users,%instusers,%instids);
78: my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
1.24 ! raeburn 79: &descend_tree($dom,$dir,0,\%users,\%courses,$allowed_inactivity,$check_unexpired);
1.22 raeburn 80: my $resp = &localenroll::allusers_info($dom,\%instusers,\%instids,\%users);
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: next;
87: }
1.20 raeburn 88: my (%unamechg,%possnames);
1.1 raeburn 89: my @types = ('active','future');
90: my @roles = ('st');
91: my @cdoms = ($dom);
1.18 raeburn 92: foreach my $uname (sort(keys(%users))) {
1.1 raeburn 93: my %userhash = &Apache::lonnet::userenvironment($dom,$uname,@info);
1.2 raeburn 94: my (@inststatuses);
95: if (!$userhash{'internalname'}) {
96: if (defined($instusers{$uname})) {
97: (my $insttypechg,@inststatuses) =
98: &affiliations_check(\%userhash,$instusers{$uname});
99: if ($insttypechg) {
1.13 raeburn 100: my $inststatusstr = join(':',map { &escape($_) } (@inststatuses));
1.2 raeburn 101: my %statushash = ( inststatus => $inststatusstr );
102: my $statusres = &Apache::lonnet::put('environment',\%statushash,$dom,$uname);
103: }
104: }
105: }
1.1 raeburn 106: if (!$userhash{'lockedname'} && !$userhash{'internalname'}) {
1.2 raeburn 107: if (defined($instusers{$uname})) {
108: my (@fields,%changes,$changed);
109: if (@inststatuses > 0) {
110: foreach my $type (@inststatuses) {
111: if (ref($settings->{fields}{$type}) eq 'ARRAY') {
112: foreach my $field (@{$settings->{fields}{$type}}) {
113: if (!grep(/^\Q$field\E$/,@fields)) {
114: push(@fields,$field);
115: }
116: }
117: }
118: }
119: } else {
120: if (ref($settings->{fields}{'default'}) eq 'ARRAY') {
121: @fields = @{$settings->{fields}{'default'}};
1.1 raeburn 122: }
123: }
124: foreach my $field (@fields) {
1.2 raeburn 125: if ($userhash{$field} ne $instusers{$uname}{$field}) {
1.1 raeburn 126: $changed = 1;
1.9 raeburn 127: if ($settings->{'classlists'} == 1) {
1.1 raeburn 128: if ($field eq 'id') {
129: $changes{'id'} = 1;
1.15 raeburn 130: } elsif ($field eq 'lastname' || $field eq 'firstname' || $field eq 'middlename' || $field eq 'generation') {
1.1 raeburn 131: $changes{'fullname'} = 1;
132: }
133: }
1.9 raeburn 134: }
1.1 raeburn 135: }
136: # Make the change
137: if ($changed) {
138: my %userupdate;
1.14 raeburn 139: foreach my $item ('lastname','firstname','middlename','generation','id',
140: 'permanentemail') {
141: $userupdate{$item} = $userhash{$item};
142: }
1.1 raeburn 143: foreach my $field (@fields) {
1.2 raeburn 144: $userupdate{$field} = $instusers{$uname}{$field};
1.1 raeburn 145: }
1.16 raeburn 146: 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);
1.2 raeburn 147: if ($modresult eq 'ok') {
1.9 raeburn 148: 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";
149: if ($settings->{'classlists'} == 1) {
1.1 raeburn 150: if ($changes{'id'} || $changes{'fullname'}) {
151: my %roleshash =
152: &Apache::lonnet::get_my_roles($uname,
1.4 raeburn 153: $dom,'userroles',\@types,\@roles,\@cdoms);
1.10 raeburn 154: foreach my $item (keys(%roleshash)) {
1.1 raeburn 155: my ($cnum,$cdom,$role) = split(/:/,$item);
156: my ($start,$end) = split(/:/,$roleshash{$item});
1.9 raeburn 157: if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.11 raeburn 158: my $result =
159: &Apache::lonuserutils::update_classlist($cdom,$cnum,$dom,$uname,\%userupdate);
1.9 raeburn 160: if ($result eq 'ok') {
161: print $fh "Classlist change: $uname:$dom - class -> $cnum:$cdom\n";
162: } else {
163: print $fh "Error - $result -during classlist update for $uname:$dom in $cnum:$cdom\n";
164: }
1.1 raeburn 165: }
166: }
167: }
168: }
1.9 raeburn 169: } else {
170: print $fh "User change detected for $uname:$dom. Error during update: $modresult\n";
1.1 raeburn 171: }
172: }
1.2 raeburn 173: } else {
174: # check if the username has changed
175: if (defined($instids{$userhash{'id'}})) {
176: if (ref($instids{$userhash{'id'}}) eq 'ARRAY') {
177: foreach my $name (@{$instids{$userhash{'id'}}}) {
178: if (!exists($users{$name})) {
179: push(@{$possnames{$uname}},$name);
180: }
181: }
182: } else {
183: if (!exists($users{$instids{$userhash{'id'}}})) {
184: $unamechg{$uname} = $instids{$userhash{'id'}};
1.7 albertel 185: print $fh (&mt('Username change to [_1] detected for [_2] in domain [_3].',$unamechg{$uname},$uname,$dom)."\n");
1.2 raeburn 186: }
187: }
188: }
1.1 raeburn 189: }
190: }
191: }
1.4 raeburn 192: if (keys(%possnames) > 0) {
193: foreach my $uname (keys(%possnames)) {
194: my $altnames = join(' or ',@{$possnames{$uname}});
1.7 albertel 195: print $fh (&mt('Possible username change to [_1] detected for [_2] in domain [_3].',$altnames,$uname,$dom)."\n");
1.4 raeburn 196: }
197: }
1.9 raeburn 198: print $fh ("-- \n".&Apache::lonlocal::locallocaltime(time).' '.&mt('Autoupdate messages end')."\n*******************\n\n");
1.6 raeburn 199: close($fh);
1.1 raeburn 200: }
201:
202: sub descend_tree {
1.24 ! raeburn 203: my ($dom,$dir,$depth,$alldomusers,$coursesref,$allowed_inactivity,$check_unexpired) = @_;
1.1 raeburn 204: if (-d $dir) {
205: opendir(DIR,$dir);
206: my @contents = grep(!/^\./,readdir(DIR));
207: closedir(DIR);
208: $depth ++;
209: foreach my $item (@contents) {
1.23 raeburn 210: if (($depth < 4) && (length($item) == 1)) {
1.24 ! raeburn 211: &descend_tree($dom,$dir.'/'.$item,$depth,$alldomusers,$coursesref,
! 212: $allowed_inactivity,$check_unexpired);
1.19 raeburn 213: } elsif (-e $dir.'/'.$item.'/passwd') {
1.20 raeburn 214: if (ref($coursesref) eq 'HASH') {
1.21 raeburn 215: next if (exists($coursesref->{$dom.'_'.$item}));
1.20 raeburn 216: }
1.24 ! raeburn 217: if ($allowed_inactivity) {
! 218: my $now = time;
! 219: my $aclog = $dir.'/'.$item.'/activity.log';
! 220: my $roledb = $dir.'/'.$item.'/roles.db';
! 221: if (-e $aclog) {
! 222: my $lastac=(stat($aclog))[9];
! 223: if (($now - $lastac) > $allowed_inactivity) {
! 224: if (-e $roledb) {
! 225: my $lastrolechg=(stat($roledb))[9];
! 226: next if (($now - $lastrolechg) > $allowed_inactivity);
! 227: } else {
! 228: next;
! 229: }
! 230: }
! 231: } elsif (-e $roledb) {
! 232: my $lastrolechg=(stat($roledb))[9];
! 233: next if (($now - $lastrolechg) > $allowed_inactivity);
! 234: } else {
! 235: next;
! 236: }
! 237: }
! 238: if ($check_unexpired) {
! 239: my $roledb = $dir.'/'.$item.'/roles.db';
! 240: my $unexpired = 0;
! 241: my $now = time;
! 242: if (-e $roledb) {
! 243: my $roleshash = &LONCAPA::tie_user_hash($dom,$item,'roles',&GDBM_READER()) or next;
! 244: if (ref($roleshash)) {
! 245: while (my ($key,$value) = each(%$roleshash)) {
! 246: next if ($key =~ /^rolesdef/);
! 247: my ($role,$roleend,$rolestart) = split(/\_/,$value);
! 248: next if ($role =~ /^gr\//);
! 249: if (!$roleend || $roleend > $now) {
! 250: $unexpired = 1;
! 251: last;
! 252: }
! 253: }
! 254: &LONCAPA::untie_user_hash($roleshash);
! 255: next unless ($unexpired);
! 256: } else {
! 257: next;
! 258: }
! 259: } else {
! 260: next;
! 261: }
! 262: }
1.9 raeburn 263: $$alldomusers{$item} = '';
1.1 raeburn 264: }
265: }
266: }
267: }
268:
1.2 raeburn 269: sub affiliations_check {
270: my ($userhash,$insthashref) = @_;
271: my (@inststatuses,$insttypechg);;
272: if (ref($insthashref) eq 'HASH') {
1.8 raeburn 273: if (ref($insthashref->{'inststatus'}) eq 'ARRAY') {
274: @inststatuses = @{$insthashref->{'inststatus'}};
1.2 raeburn 275: }
276: }
1.12 raeburn 277: my @currstatuses = map { &unescape($_); } (split(/:/,$userhash->{'inststatus'}));
1.2 raeburn 278: foreach my $status (@inststatuses) {
279: if (!grep/^\Q$status\E/,@currstatuses) {
280: $insttypechg = 1;
281: }
282: }
283: if (!$insttypechg) {
284: foreach my $status (@currstatuses) {
285: if (!grep/^\Q$status\E/,@inststatuses) {
286: $insttypechg = 1;
287: }
288: }
289: }
290: return ($insttypechg,@inststatuses);
291: }
292:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>