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