1 | package LATMOS::Accounts::SynchAccess::base; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use vars qw($AUTOLOAD); |
---|
6 | use Carp; |
---|
7 | use LATMOS::Accounts::Log; |
---|
8 | |
---|
9 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | LATMOS::Accounts::SynchAccess::base - Fake base object for sync access |
---|
14 | |
---|
15 | =head1 DESCRIPTION |
---|
16 | |
---|
17 | This module fake a base object to send same actions tu multiple base. This is |
---|
18 | used for actions basic synchronisation process cannot handle. |
---|
19 | |
---|
20 | =head1 FUNCTIONS |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | sub AUTOLOAD { |
---|
25 | my ($self, @args) = @_; |
---|
26 | my ($constname, $sub) = $AUTOLOAD =~ m/(.*)::([^:]+)/; |
---|
27 | if ($sub =~ /^(wexported)/) { |
---|
28 | $self->_no_trap($sub, @args); |
---|
29 | } elsif ($sub =~ /^(check_password)$/) { |
---|
30 | $self->_run_first($sub, @args); |
---|
31 | } else { |
---|
32 | $self->_trap_false($sub, @args); |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | =head2 new( \@bases ) |
---|
37 | |
---|
38 | Create a new SynchAccess instance over given bases |
---|
39 | |
---|
40 | =cut |
---|
41 | |
---|
42 | sub new { |
---|
43 | my ($class, $bases) = @_; |
---|
44 | bless { |
---|
45 | bases => [ @{ $bases } ], |
---|
46 | }, $class; |
---|
47 | } |
---|
48 | |
---|
49 | =head2 bases |
---|
50 | |
---|
51 | Return lists of bases in ths instance. |
---|
52 | |
---|
53 | =cut |
---|
54 | |
---|
55 | sub bases { |
---|
56 | my ($self) = @_; |
---|
57 | @{ $self->{bases} || []} |
---|
58 | } |
---|
59 | |
---|
60 | sub _return_all { |
---|
61 | my ($self, $sub, @args) = @_; |
---|
62 | map { $_->$sub(@args) } $self->bases |
---|
63 | } |
---|
64 | |
---|
65 | sub _run_first { |
---|
66 | my ($self, $sub, @args) = @_; |
---|
67 | my ($fbase) = $self->bases; |
---|
68 | $fbase->$sub(@args); |
---|
69 | } |
---|
70 | |
---|
71 | sub _no_trap { |
---|
72 | my ($self, $sub, @args) = @_; |
---|
73 | foreach ($self->bases) { |
---|
74 | $_->$sub(@args); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | sub _trap_false { |
---|
79 | my ($self, $sub, @args) = @_; |
---|
80 | my $failure = 0; |
---|
81 | foreach ($self->bases) { |
---|
82 | $_->$sub(@args) or do { |
---|
83 | la_log(LA_ERR, "SyncA: Erreur call $sub() for %s", $_); |
---|
84 | $failure = 1; |
---|
85 | }; |
---|
86 | } |
---|
87 | $failure ? 0 : 1; |
---|
88 | } |
---|
89 | |
---|
90 | sub _trap_true { |
---|
91 | my ($self, $sub, @args) = @_; |
---|
92 | my $failure = 0; |
---|
93 | foreach ($self->bases) { |
---|
94 | $_->$sub(@args) and $failure = 1; |
---|
95 | } |
---|
96 | $failure ? 0 : 1; |
---|
97 | } |
---|
98 | |
---|
99 | =head2 rename_object |
---|
100 | |
---|
101 | =cut |
---|
102 | |
---|
103 | sub rename_object { |
---|
104 | my ($self, $otype, $uid, $newuid) = @_; |
---|
105 | foreach ($self->bases) { |
---|
106 | if ($_->is_supported_object($otype)) { |
---|
107 | $_->rename_object($otype, $uid, $newuid) or do { |
---|
108 | la_log(LA_ERR, |
---|
109 | "Cannot get object $otype/$uid in %s/%s", |
---|
110 | $_->type, |
---|
111 | $_->label |
---|
112 | ); |
---|
113 | return; |
---|
114 | }; |
---|
115 | } |
---|
116 | } |
---|
117 | return 1; |
---|
118 | } |
---|
119 | |
---|
120 | sub DESTROY {} |
---|
121 | |
---|
122 | 1; |
---|
123 | |
---|
124 | __END__ |
---|
125 | |
---|
126 | =head1 SEE ALSO |
---|
127 | |
---|
128 | L<LATMOS::Accounts>, L<LATMOS::Accounts::Bases> |
---|
129 | |
---|
130 | =head1 AUTHOR |
---|
131 | |
---|
132 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
133 | |
---|
134 | =head1 COPYRIGHT AND LICENSE |
---|
135 | |
---|
136 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
137 | |
---|
138 | This library is free software; you can redistribute it and/or modify |
---|
139 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
140 | at your option, any later version of Perl 5 you may have available. |
---|
141 | |
---|
142 | =cut |
---|