source: LATMOS-Accounts/lib/LATMOS/Accounts.pm @ 178

Last change on this file since 178 was 178, checked in by nanardon, 15 years ago
  • fix cryptmd5 salt
  • w/o options sync_access use defaults values
  • Property svn:keywords set to Id Rev
File size: 4.7 KB
Line 
1package LATMOS::Accounts;
2
3use 5.010000;
4use strict;
5use warnings;
6use base qw(Config::IniFiles);
7use LATMOS::Accounts::Bases;
8use LATMOS::Accounts::Synchro;
9use LATMOS::Accounts::SynchAccess;
10
11our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
12
13sub new {
14    my ($class, $config) = @_;
15
16    $config ||= '/etc/latmos-account.ini';
17
18    my $self = Config::IniFiles->new(
19        -file => $config,
20        '-default' => '_default_',
21    );
22
23    bless($self, $class)
24}
25
26sub base {
27    my ($self, $section) = @_;
28    # this method perform a cache
29    $self->{_bases}{$section} and return $self->{_bases}{$section};
30    $self->load_base($section) ? $self->{_bases}{$section} : undef;
31}
32
33sub default_base {
34    my ($self) = @_;
35    my $default = $self->default_base_name or return;
36    $self->base($default);
37}
38
39# load or a if need base
40sub load_base {
41    my ($self, $section) = @_;
42    return ($self->{_bases}{$section} ||= $self->_load_base($section))
43        ? 1
44        : 0;
45}
46
47# do the bad work
48sub _load_base {
49    my ($self, $section) = @_;
50    my $type = $self->val($section, 'type') or return;
51    my %params = (
52        map { $_ => ($self->val($section, $_)) } $self->Parameters($section),
53        defattr => { map { $_ => ($self->val('_defattr_', $_)) } $self->Parameters('_defattr_') },
54    );
55    my $base = LATMOS::Accounts::Bases->new($type, %params, label => $section);
56    $base->load or return;
57    $base;
58}
59
60sub default_base_name {
61    my ($self) = @_;
62    $self->val('_default_', 'base', ($self->list_bases)[0]);
63}
64
65sub default_synchro_name {
66    my ($self) = @_;
67    $self->val('_default_', 'sync');
68}
69
70sub list_bases {
71    my ($self) = @_;
72    grep {
73        !m/^_.*_$/ &&
74        !m/^sync:/
75    } $self->Sections
76}
77
78sub list_synchro {
79    my ($self) = @_;
80    grep { $_ } map { /^sync:(.*)$/; $1 } $self->Sections
81}
82
83sub load_all_base {
84    my ($self) = @_;
85    foreach ($self->list_bases) {
86        $self->load_base($_) or do {
87            warn "Cannot load base $_\n";
88            return 0;
89        };
90    }
91    1;
92}
93
94sub default_synchro {
95    my ($self, %options) = @_;
96    my $syncname = $self->default_synchro_name or return;
97    $self->create_synchro($syncname, %options);
98}
99
100sub sync_from_name {
101    my ($self, $syncname) = @_;
102    return if (!$syncname);
103    $self->val("sync:$syncname", 'from', $self->default_base_name);
104}
105
106sub create_synchro {
107    my ($self, $name, %options) = @_;
108
109    # taking options from config
110    if ($name) {
111        foreach my $param ($self->Parameters("sync:$name")) {
112            if (!defined($options{$param})) {
113                my @args = $self->val("sync:$name", $param);
114                $options{$param} = ($args[1] || $param eq 'to')
115                    ? [ @args ]
116                    : $args[0];
117            }
118        }
119    }
120
121    my $labfrom = $options{from} ? $self->base($options{from}) : $self->default_base;
122
123    my @labto = map { $self->base($_) } @{ $options{to} || []} or return;
124
125    my $sync = LATMOS::Accounts::Synchro->new(
126        $labfrom, [ @labto ],
127        state_file => $self->val('_default_', 'state_file'),
128        %options,
129        name => $name,
130    );
131}
132
133sub sync_access {
134    my ($self, $name, %options) = @_;
135
136    my @obases;
137    if ($name) {
138        @obases =
139        (map { $self->base($_) } ($self->sync_from_name($name), $self->val("sync:$name", 'to')));
140    } elsif(@{ $options{bases} || []}) {
141        @obases = map { $self->base($_) } @{ $options{bases} || []};
142    } elsif (my $sname = $self->default_synchro_name) {
143        @obases = (map { $self->base($_) }
144            ($self->sync_from_name($sname), $self->val("sync:$sname", 'to'))
145        );
146    }
147
148    LATMOS::Accounts::SynchAccess->new([ @obases ]);
149}
150
1511;
152
153__END__
154# Below is stub documentation for your module. You'd better edit it!
155
156=head1 NAME
157
158LATMOS::Accounts - Perl extension for blah blah blah
159
160=head1 SYNOPSIS
161
162  use LATMOS::Accounts;
163  blah blah blah
164
165=head1 DESCRIPTION
166
167Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the
168author of the extension was negligent enough to leave the stub
169unedited.
170
171Blah blah blah.
172
173=head2 EXPORT
174
175None by default.
176
177
178
179=head1 SEE ALSO
180
181Mention other useful documentation such as the documentation of
182related modules or operating system documentation (such as man pages
183in UNIX), or any relevant external documentation such as RFCs or
184standards.
185
186If you have a mailing list set up for your module, mention it here.
187
188If you have a web site set up for your module, mention it here.
189
190=head1 AUTHOR
191
192Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt>
193
194=head1 COPYRIGHT AND LICENSE
195
196Copyright (C) 2009 by Thauvin Olivier
197
198This library is free software; you can redistribute it and/or modify
199it under the same terms as Perl itself, either Perl version 5.10.0 or,
200at your option, any later version of Perl 5 you may have available.
201
202
203=cut
Note: See TracBrowser for help on using the repository browser.