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

Last change on this file since 57 was 57, checked in by nanardon, 15 years ago
  • SQL has now a table to store any want fields
  • make difference between r/w fields
  • Property svn:keywords set to Id Rev
File size: 5.7 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->{_label} = $options{label};
44    $base
45}
46
47sub label {
48    $_[0]->{_label};
49}
50
51sub type {
52    $_[0]->{_type};
53}
54
55sub _load_obj_class {
56    my ($self, $otype) = @_;
57
58    # finding perl class:
59    my $pclass = ref $self;
60    $pclass .= '::' . ucfirst(lc($otype));
61    eval "require $pclass;";
62    if ($@) { return } # error message ?
63    return $pclass;
64}
65
66=head2 list_canonicals_fields($otype, $for)
67
68Return the list of supported fields by the database for object type $otype.
69
70Optionnal $for specify the goal for which the list is requested, only supported
71fields will be returns
72
73=cut
74
75sub list_canonicals_fields {
76    my ($self, $otype, $for) = @_;
77    $for ||= 'rw';
78    my $pclass = $self->_load_obj_class($otype) or return;
79    $pclass->_canonical_fields($self, $for);
80}
81
82=head2 get_field_name($otype, $c_fields, $for)
83
84Return the internal fields name for $otype object for
85canonical fields $c_fields
86
87=cut
88
89sub get_field_name {
90    my ($self, $otype, $c_fields, $for) = @_;
91    my $pclass = $self->_load_obj_class($otype) or return;
92    $pclass->_get_field_name($c_fields, $self, $for);
93}
94
95=head2 list_supported_objects(@otype)
96
97Return a list of supported object
98
99@type is an additionnal list of objects to check
100
101=cut
102
103sub list_supported_objects {
104    my ($self, @otype) = @_;
105    return grep { $self->is_supported_object($_) }
106        (qw(group user), @otype);
107}
108
109=head2 is_supported_object($otype)
110
111Return true is object type $otype is supported
112
113=cut
114
115sub is_supported_object {
116    my ($self, $otype) = @_;
117    return $self->_load_obj_class($otype) ? 1 : 0;
118}
119
120=head2 list_objects($otype)
121
122Return the list of UID for object of $otype.
123
124=cut
125
126sub list_objects {
127    my ($self, $otype) = @_;
128    my $pclass = $self->_load_obj_class($otype) or return;
129    $pclass->list($self);
130}
131
132=head2 get_object($type, $id)
133
134Return an object of $type (typically user or group) having identifier
135$id.
136
137=cut
138
139sub get_object {
140    my ($self, $otype, $id) = @_;
141
142    return LATMOS::Accounts::Bases::Objects->_new($self, $otype, $id);
143}
144
145=head2 create_object($type, $id, %data)
146
147Create and return an object of type $type with unique id
148$id having %data.
149
150This method should be provided by the data base handler.
151
152=cut
153
154sub create_object {
155    my ($self, $otype, $id, %data) = @_;
156    my $pclass = $self->_load_obj_class($otype);
157    $pclass->_create($self, $id, %data) or return;
158    $self->get_object($otype, $id);
159}
160
161=head2 create_c_object($type, $id, %data)
162
163Create and return an object of type $type with unique id
164$id having %data using canonical fields
165
166=cut
167
168sub create_c_object {
169    my ($self, $otype, $id, %cdata) = @_;
170
171    my %data;
172    foreach my $cfield (keys %cdata) {
173        my $field = $self->get_field_name($otype, $cfield, 'write') or next;
174        $data{$field} = $cdata{$cfield};
175    }
176    keys %data or return 0; # TODO: return an error ?
177    $self->create_object($otype, $id, %data);
178}
179
180=head2 load
181
182Make account base loading data into memory if need.
183Should always be called, if database fetch data on the fly
184(SQL, LDAP), the function just return True.
185
186=cut
187
188sub load { 1 }
189
190=head2 is_transactionnal
191
192Return True is the database support commit and rollback
193
194=cut
195
196sub is_transactionnal {
197    my ($self) = @_;
198    return($self->can('_rollback') && $self->can('_commit'));
199}
200
201=head2 commit
202
203Save change into the database if change are not done immediately.
204This should always be called as you don't know when change are applied.
205
206Return always true if database does not support any transaction.
207
208The driver should provides a _commit functions to save data.
209
210=cut
211
212sub commit {
213    my ($self) = @_;
214    return $self->can('_commit') ? $self->_commit : 1;
215}
216
217=head2 rollback
218
219If database support transaction, rollback changes. Return false
220if database does not support.
221
222If supported, driver should provides a _rollback functions
223
224=cut
225
226sub rollback {
227    my ($self) = @_;
228    return $self->can('_rollback') ? $self->_rollback : 0;
229}
230
231=head2 current_rev
232
233Return the current revision of the database
234
235Must be provide by base driver if incremental synchro is supported
236
237=cut
238
239sub current_rev { return }
240
241=head2 list_objects_from_rev($otype, $rev)
242
243Return the list of UID for object of $otype.
244
245=cut
246
247sub list_objects_from_rev {
248    my ($self, $otype, $rev) = @_;
249    my $pclass = $self->_load_obj_class($otype) or return;
250    if (defined($rev) && $pclass->can('list_from_rev')) {
251        return $pclass->list_from_rev($self, $rev);
252    } else {
253        # no support, return all objects...
254        return $self->list_objects($otype);
255    }
256}
257
2581;
259
260__END__
261
262=head1 SEE ALSO
263
264=head1 AUTHOR
265
266Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt>
267
268=head1 COPYRIGHT AND LICENSE
269
270Copyright (C) 2009 by Thauvin Olivier
271
272This library is free software; you can redistribute it and/or modify
273it under the same terms as Perl itself, either Perl version 5.10.0 or,
274at your option, any later version of Perl 5 you may have available.
275
276=cut
Note: See TracBrowser for help on using the repository browser.