source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Employment.pm @ 2378

Last change on this file since 2378 was 2378, checked in by nanardon, 4 years ago

make user activation from employment working (enable unxported account)

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1package LATMOS::Accounts::Task::Employment;
2
3use strict;
4use warnings;
5use base qw(LATMOS::Accounts::Task);
6use LATMOS::Accounts;
7use LATMOS::Accounts::Log;
8use LATMOS::Accounts::Utils;
9use LATMOS::Accounts::Bases::Sql::Employment;
10use DateTime;
11use DateTime::TimeZone;
12
13=head1 NAME
14
15LATMOS::Accounts::Task::Stats - Store value to perform some statistics
16
17=head1 DESCRIPTION
18
19=cut
20
21sub order { 0 }
22
23sub init {
24    my ($self) = @_;
25    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1);
26    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
27    $labase && $labase->load or die "Cannot load base";
28
29    $self->{_la} = $LA;
30    $self->{_base} = $labase;
31
32    1;
33}
34
35# Run it every 10 minutes, not more often
36sub runDelay { 10 * 60 }
37
38sub run {
39    my ($self) = @_;
40
41    $self->{_base}->temp_switch_unexported( sub {
42        # Export user is employement is incoming
43        foreach my $user ($self->{_base}->search_objects('user',
44                'exported=0',
45                'arrival>now()'
46            )) {
47                my $ouser = $self->{_base}->get_object('user', $user);
48                if ($ouser->_set_c_fields( 'exported', 1 )) {
49                    $self->{_base}->commit;
50                } else {
51                    $self->{_base}->rollback;
52                }
53        }
54    }, 1 );
55
56    my %users = map { $_ => undef } $self->{_base}->search_objects('user', 'exported=true');
57    my %old = $self->{_base}->attributes_summary_by_object('user', 'appliedEmployement');
58    foreach (keys %old) {
59        exists($users{$_}) or next;
60        $users{$_} = $old{$_}->[0] || '';
61    }
62
63    my $now = DateTime->now()->iso8601 . 'Z';
64
65    my $listempl = $self->{_base}->db->prepare_cached(q{
66        SELECT * FROM employment where firstday <= ?::timestamp and (lastday is null or lastday >= ?::timestamp - '1 days'::interval)
67            and exported = true
68            order by lastday, firstday
69    });
70    $listempl->execute($now, $now);
71    my %new  = ();
72    while (my $res = $listempl->fetchrow_hashref) {
73        my $employment = $res->{name};
74        my $user = $res->{user};
75        exists($users{$user}) or next;
76        # avoid undef:
77        $users{$user} ||= '';
78        if ($employment ne $users{$user}) {
79            my $ouser = $self->{_base}->get_object('user', $user) or next;
80
81            $self->{_base}->log(LA_NOTICE, "Updating user $user to match employment `%s'", $employment || '');
82            $ouser->applyCurrentEmployment;
83            $self->{_base}->commit;
84        }
85        delete($users{$user});
86    }
87
88    # Change has old employment => nothing
89    # if user is still active, expired account remains untouched
90    foreach my $user ($self->{_base}->search_objects('user', 'exported=true')) {
91        if ($users{$user}) {
92            my $ouser = $self->{_base}->get_object('user', $user);
93            if ($ouser->resetEmployment) {
94                $self->{_base}->commit;
95            } else {
96                $self->{_base}->rollback;
97            }
98        }
99    }
100
101    return 1;
102}
103
1041;
105
106__END__
107
108=head1 SEE ALSO
109
110L<LATMOS::Accounts>, L<LATMOS::Accounts::Task>
111
112=head1 AUTHOR
113
114Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
115
116=head1 COPYRIGHT AND LICENSE
117
118Copyright (C) 2012 CNRS SA/CETP/LATMOS
119
120This library is free software; you can redistribute it and/or modify
121it under the same terms as Perl itself, either Perl version 5.10.0 or,
122at your option, any later version of Perl 5 you may have available.
123
124=cut
125
Note: See TracBrowser for help on using the repository browser.