1 | package LATMOS::Accounts::Task::Setattribute; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base qw(LATMOS::Accounts::Task); |
---|
6 | use LATMOS::Accounts; |
---|
7 | use LATMOS::Accounts::Log; |
---|
8 | use LATMOS::Accounts::Utils; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | LATMOS::Accounts::Task::Setattribute - Task to disable expired users |
---|
13 | |
---|
14 | =head1 DESCRIPTION |
---|
15 | |
---|
16 | Set attribute to specific value according filter |
---|
17 | |
---|
18 | =head1 CONFIGURATION |
---|
19 | |
---|
20 | =over 4 |
---|
21 | |
---|
22 | =item otype |
---|
23 | |
---|
24 | The type of object to modify |
---|
25 | |
---|
26 | =item filter |
---|
27 | |
---|
28 | The filter matching object to modify (can be multi-valued) |
---|
29 | |
---|
30 | =item attribute |
---|
31 | |
---|
32 | The attribute to set |
---|
33 | |
---|
34 | =item values |
---|
35 | |
---|
36 | The values the attribute must set to. |
---|
37 | Can be a queryformat expression. |
---|
38 | |
---|
39 | =item noeval |
---|
40 | |
---|
41 | If set, values are taken litteral w/o queryformat evaluation |
---|
42 | |
---|
43 | =back |
---|
44 | |
---|
45 | =cut |
---|
46 | |
---|
47 | sub order { 0 } |
---|
48 | |
---|
49 | sub init { |
---|
50 | my ($self) = @_; |
---|
51 | my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
52 | my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base; |
---|
53 | $labase && $labase->load or die "Cannot load base"; |
---|
54 | |
---|
55 | my @errors = (); |
---|
56 | |
---|
57 | $self->{otype} = $self->config('otype') or do { |
---|
58 | push(@errors, "Cannot find config `otype' value"); |
---|
59 | }; |
---|
60 | @{ $self->{filter} } = $self->config('filter') or do { |
---|
61 | push(@errors, "Cannot find config `filter' value"); |
---|
62 | }; |
---|
63 | # Getting attributes to set |
---|
64 | $self->{attribute} = $self->config('attribute') or do { |
---|
65 | push(@errors, "Cannot find config `attribute' value"); |
---|
66 | }; |
---|
67 | @{ $self->{values} } = grep { $_ } $self->config('values'); |
---|
68 | |
---|
69 | if (@errors) { |
---|
70 | la_log(LA_ERR, $_) foreach (@errors); |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | $self->{_la} = $LA; |
---|
75 | $self->{_base} = $labase; |
---|
76 | |
---|
77 | 1; |
---|
78 | } |
---|
79 | |
---|
80 | sub run { |
---|
81 | my ($self) = @_; |
---|
82 | |
---|
83 | my @objects = $self->{_base}->search_objects( $self->{otype}, @{ $self->{filter} }); |
---|
84 | foreach my $obj (@objects) { |
---|
85 | |
---|
86 | my $o = $self->{_base}->get_object($self->{otype}, $obj) or next; |
---|
87 | |
---|
88 | my $value = undef; |
---|
89 | |
---|
90 | my @values = $self->config('noeval') |
---|
91 | ? @{ $self->{values} } |
---|
92 | : map { $o->queryformat($_) } @{ $self->{values} }; |
---|
93 | |
---|
94 | if (scalar(@values) > 1) { |
---|
95 | $value = \@values; |
---|
96 | } elsif (scalar(@values) == 1) { |
---|
97 | $value = $values[0]; |
---|
98 | } |
---|
99 | if ($o->_set_c_fields( $self->{attribute} => $value )) { |
---|
100 | la_log(LA_DEBUG, "Attribute %s set to '%s' for Object %s", $self->{attribute}, join(', ', @values), $o->id); |
---|
101 | $self->{_base}->commit; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | 1; |
---|
106 | } |
---|
107 | |
---|
108 | 1; |
---|
109 | |
---|
110 | __END__ |
---|
111 | |
---|
112 | =head1 SEE ALSO |
---|
113 | |
---|
114 | L<LATMOS::Accounts>, L<LATMOS::Accounts::Task> |
---|
115 | |
---|
116 | =head1 AUTHOR |
---|
117 | |
---|
118 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
119 | |
---|
120 | =head1 COPYRIGHT AND LICENSE |
---|
121 | |
---|
122 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
123 | |
---|
124 | This library is free software; you can redistribute it and/or modify |
---|
125 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
126 | at your option, any later version of Perl 5 you may have available. |
---|
127 | |
---|
128 | =cut |
---|
129 | |
---|