source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases.pm @ 41

Last change on this file since 41 was 41, checked in by nanardon, 15 years ago
  • start synchronisation work
  • Property svn:keywords set to Id Rev
File size: 4.9 KB
Line 
1package LATMOS::Accounts::Bases;
2
3use 5.010000;
4use strict;
5use warnings;
6use LATMOS::Accounts::Bases::Objects;
7
8our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
9
10=head1 NAME
11
12LATMOS::Accounts::Bases - Base class for account data bases
13
14=head1 SYNOPSIS
15
16  use LATMOS::Accounts::Bases;
17  my $base = LATMOS::Accounts::Bases->new('type', %options);
18  ...
19
20=head1 DESCRIPTION
21
22This module provide basic functions for various account base
23
24=head1 FUNTIONS
25
26=cut
27
28=head2 new($type, %options)
29
30Return, if success, a new data base account object, $type is
31account base type, %options to setup the base.
32
33=cut
34
35sub new {
36    my ($class, $type, %options) = @_;
37
38    my $pclass = ucfirst(lc($type));
39    eval "require LATMOS::Accounts::Bases::$pclass;";
40    if ($@) { return } # error message ?
41    my $base = "LATMOS::Accounts::Bases::$pclass"->new(%options);
42    $base->{_type} = lc($pclass);
43    $base
44}
45
46sub type {
47    $_[0]->{_type};
48}
49
50sub _load_obj_class {
51    my ($self, $otype) = @_;
52
53    # finding perl class:
54    my $pclass = ref $self;
55    $pclass .= '::' . ucfirst(lc($otype));
56    eval "require $pclass;";
57    if ($@) { return } # error message ?
58    return $pclass;
59}
60
61=head2 list_canonicals_fields($otype)
62
63Return the list of supported fields by the database for object type $otype.
64
65=cut
66
67sub list_canonicals_fields {
68    my ($self, $otype) = @_;
69    my $pclass = $self->_load_obj_class($otype) or return;
70    $pclass->_canonical_fields;
71}
72
73=head2 get_field_name($otype, $c_fields)
74
75Return the internal fields name for $otype object for
76canonical fields $c_fields
77
78=cut
79
80sub get_field_name {
81    my ($self, $otype, $c_fields) = @_;
82    my $pclass = $self->_load_obj_class($otype) or return;
83    $pclass->_get_field_name($c_fields);
84}
85
86=head2 list_supported_objects(@otype)
87
88Return a list of supported object
89
90@type is an additionnal list of objects to check
91
92=cut
93
94sub list_supported_objects {
95    my ($self, @otype) = @_;
96    return grep { $self->is_supported_object($_) }
97        (qw(user group), @otype);
98}
99
100=head2 is_supported_object($otype)
101
102Return true is object type $otype is supported
103
104=cut
105
106sub is_supported_object {
107    my ($self, $otype) = @_;
108    return $self->_load_obj_class($otype) ? 1 : 0;
109}
110
111=head2 list_objects($otype)
112
113Return the list of UID for object of $otype.
114
115=cut
116
117sub list_objects {
118    my ($self, $otype) = @_;
119    my $pclass = $self->_load_obj_class($otype) or return;
120    $pclass->list($self);
121}
122
123=head2 get_object($type, $id)
124
125Return an object of $type (typically user or group) having identifier
126$id.
127
128=cut
129
130sub get_object {
131    my ($self, $otype, $id) = @_;
132
133    return LATMOS::Accounts::Bases::Objects->_new($self, $otype, $id);
134}
135
136=head2 create_object($type, $id, %data)
137
138Create and return an object of type $type with unique id
139$id having %data.
140
141This method should be provided by the data base handler.
142
143=cut
144
145sub create_object {
146    my ($self, $otype, $id, %data) = @_;
147    my $pclass = $self->_load_obj_class($otype);
148    $pclass->create($id, %data) or return;
149    $self->get_object($otype, $id);
150}
151
152=head2 create_c_object($type, $id, %data)
153
154Create and return an object of type $type with unique id
155$id having %data using canonical fields
156
157=cut
158
159sub create_c_object {
160    my ($self, $otype, $id, %cdata) = @_;
161
162    my %data;
163    foreach my $cfield (keys %cdata) {
164        my $field = $self->base->get_field_name($self->type, $cfield) or next;
165        $data{$field} = $cdata{$cfield};
166    }
167    keys %data or return 1; # TODO: return an error ?
168    $self->create_object($otype, $id, %data);
169}
170
171=head2 load
172
173Make account base loading data into memory if need.
174Should always be called, if database fetch data on the fly
175(SQL, LDAP), the function just return True.
176
177=cut
178
179sub load { 1 }
180
181=head2 is_transactionnal
182
183Return True is the database support commit and rollback
184
185=cut
186
187sub is_transactionnal {
188    my ($self) = @_;
189    return($self->can('_rollback') && $self->can('_commit'));
190}
191
192=head2 commit
193
194Save change into the database if change are not done immediately.
195This should always be called as you don't know when change are applied.
196
197Return always true if database does not support any transaction.
198
199The driver should provides a _commit functions to save data.
200
201=cut
202
203sub commit {
204    my ($self) = @_;
205    return $self->can('_commit') ? $self->_commit : 1;
206}
207
208=head2 rollback
209
210If database support transaction, rollback changes. Return false
211if database does not support.
212
213If supported, driver should provides a _rollback functions
214
215=cut
216
217sub rollback {
218    my ($self) = @_;
219    return $self->can('_rollback') ? $self->_rollback : 0;
220}
221
2221;
223
224__END__
225
226=head1 SEE ALSO
227
228=head1 AUTHOR
229
230Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt>
231
232=head1 COPYRIGHT AND LICENSE
233
234Copyright (C) 2009 by Thauvin Olivier
235
236This library is free software; you can redistribute it and/or modify
237it under the same terms as Perl itself, either Perl version 5.10.0 or,
238at your option, any later version of Perl 5 you may have available.
239
240=cut
Note: See TracBrowser for help on using the repository browser.