1 | package LATMOS::Accounts::Bases; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | use LATMOS::Accounts::Bases::Objects; |
---|
7 | |
---|
8 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | LATMOS::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 | |
---|
22 | This module provide basic functions for various account base |
---|
23 | |
---|
24 | =head1 FUNTIONS |
---|
25 | |
---|
26 | =cut |
---|
27 | |
---|
28 | =head2 new($type, %options) |
---|
29 | |
---|
30 | Return, if success, a new data base account object, $type is |
---|
31 | account base type, %options to setup the base. |
---|
32 | |
---|
33 | =cut |
---|
34 | |
---|
35 | sub 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 | |
---|
47 | sub label { |
---|
48 | $_[0]->{_label}; |
---|
49 | } |
---|
50 | |
---|
51 | sub type { |
---|
52 | $_[0]->{_type}; |
---|
53 | } |
---|
54 | |
---|
55 | sub _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 | |
---|
68 | Return the list of supported fields by the database for object type $otype. |
---|
69 | |
---|
70 | Optionnal $for specify the goal for which the list is requested, only supported |
---|
71 | fields will be returns |
---|
72 | |
---|
73 | =cut |
---|
74 | |
---|
75 | sub 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 | sub delayed_fields { |
---|
83 | my ($self, $otype, $for) = @_; |
---|
84 | $for ||= 'rw'; |
---|
85 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
86 | $pclass->_delayed_fields($self, $for); |
---|
87 | } |
---|
88 | |
---|
89 | =head2 get_field_name($otype, $c_fields, $for) |
---|
90 | |
---|
91 | Return the internal fields name for $otype object for |
---|
92 | canonical fields $c_fields |
---|
93 | |
---|
94 | =cut |
---|
95 | |
---|
96 | sub get_field_name { |
---|
97 | my ($self, $otype, $c_fields, $for) = @_; |
---|
98 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
99 | $pclass->_get_field_name($c_fields, $self, $for); |
---|
100 | } |
---|
101 | |
---|
102 | =head2 list_supported_objects(@otype) |
---|
103 | |
---|
104 | Return a list of supported object |
---|
105 | |
---|
106 | @type is an additionnal list of objects to check |
---|
107 | |
---|
108 | =cut |
---|
109 | |
---|
110 | sub list_supported_objects { |
---|
111 | my ($self, @otype) = @_; |
---|
112 | return grep { $self->is_supported_object($_) } |
---|
113 | (qw(group user), @otype); |
---|
114 | } |
---|
115 | |
---|
116 | =head2 is_supported_object($otype) |
---|
117 | |
---|
118 | Return true is object type $otype is supported |
---|
119 | |
---|
120 | =cut |
---|
121 | |
---|
122 | sub is_supported_object { |
---|
123 | my ($self, $otype) = @_; |
---|
124 | return $self->_load_obj_class($otype) ? 1 : 0; |
---|
125 | } |
---|
126 | |
---|
127 | =head2 list_objects($otype) |
---|
128 | |
---|
129 | Return the list of UID for object of $otype. |
---|
130 | |
---|
131 | =cut |
---|
132 | |
---|
133 | sub list_objects { |
---|
134 | my ($self, $otype) = @_; |
---|
135 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
136 | $pclass->list($self); |
---|
137 | } |
---|
138 | |
---|
139 | =head2 get_object($type, $id) |
---|
140 | |
---|
141 | Return an object of $type (typically user or group) having identifier |
---|
142 | $id. |
---|
143 | |
---|
144 | =cut |
---|
145 | |
---|
146 | sub get_object { |
---|
147 | my ($self, $otype, $id) = @_; |
---|
148 | |
---|
149 | return LATMOS::Accounts::Bases::Objects->_new($self, $otype, $id); |
---|
150 | } |
---|
151 | |
---|
152 | =head2 create_object($type, $id, %data) |
---|
153 | |
---|
154 | Create and return an object of type $type with unique id |
---|
155 | $id having %data. |
---|
156 | |
---|
157 | This method should be provided by the data base handler. |
---|
158 | |
---|
159 | =cut |
---|
160 | |
---|
161 | sub create_object { |
---|
162 | my ($self, $otype, $id, %data) = @_; |
---|
163 | my $pclass = $self->_load_obj_class($otype); |
---|
164 | $pclass->_create($self, $id, %data) or return; |
---|
165 | $self->get_object($otype, $id); |
---|
166 | } |
---|
167 | |
---|
168 | =head2 create_c_object($type, $id, %data) |
---|
169 | |
---|
170 | Create and return an object of type $type with unique id |
---|
171 | $id having %data using canonical fields |
---|
172 | |
---|
173 | =cut |
---|
174 | |
---|
175 | sub create_c_object { |
---|
176 | my ($self, $otype, $id, %cdata) = @_; |
---|
177 | |
---|
178 | my %data; |
---|
179 | foreach my $cfield (keys %cdata) { |
---|
180 | my $field = $self->get_field_name($otype, $cfield, 'write') or next; |
---|
181 | $data{$field} = $cdata{$cfield}; |
---|
182 | } |
---|
183 | keys %data or return 0; # TODO: return an error ? |
---|
184 | $self->create_object($otype, $id, %data); |
---|
185 | } |
---|
186 | |
---|
187 | =head2 load |
---|
188 | |
---|
189 | Make account base loading data into memory if need. |
---|
190 | Should always be called, if database fetch data on the fly |
---|
191 | (SQL, LDAP), the function just return True. |
---|
192 | |
---|
193 | =cut |
---|
194 | |
---|
195 | sub load { 1 } |
---|
196 | |
---|
197 | =head2 is_transactionnal |
---|
198 | |
---|
199 | Return True is the database support commit and rollback |
---|
200 | |
---|
201 | =cut |
---|
202 | |
---|
203 | sub is_transactionnal { |
---|
204 | my ($self) = @_; |
---|
205 | return($self->can('_rollback') && $self->can('_commit')); |
---|
206 | } |
---|
207 | |
---|
208 | =head2 commit |
---|
209 | |
---|
210 | Save change into the database if change are not done immediately. |
---|
211 | This should always be called as you don't know when change are applied. |
---|
212 | |
---|
213 | Return always true if database does not support any transaction. |
---|
214 | |
---|
215 | The driver should provides a _commit functions to save data. |
---|
216 | |
---|
217 | =cut |
---|
218 | |
---|
219 | sub commit { |
---|
220 | my ($self) = @_; |
---|
221 | return $self->can('_commit') ? $self->_commit : 1; |
---|
222 | } |
---|
223 | |
---|
224 | =head2 rollback |
---|
225 | |
---|
226 | If database support transaction, rollback changes. Return false |
---|
227 | if database does not support. |
---|
228 | |
---|
229 | If supported, driver should provides a _rollback functions |
---|
230 | |
---|
231 | =cut |
---|
232 | |
---|
233 | sub rollback { |
---|
234 | my ($self) = @_; |
---|
235 | return $self->can('_rollback') ? $self->_rollback : 0; |
---|
236 | } |
---|
237 | |
---|
238 | =head2 current_rev |
---|
239 | |
---|
240 | Return the current revision of the database |
---|
241 | |
---|
242 | Must be provide by base driver if incremental synchro is supported |
---|
243 | |
---|
244 | =cut |
---|
245 | |
---|
246 | sub current_rev { return } |
---|
247 | |
---|
248 | =head2 list_objects_from_rev($otype, $rev) |
---|
249 | |
---|
250 | Return the list of UID for object of $otype. |
---|
251 | |
---|
252 | =cut |
---|
253 | |
---|
254 | sub list_objects_from_rev { |
---|
255 | my ($self, $otype, $rev) = @_; |
---|
256 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
257 | if (defined($rev) && $pclass->can('list_from_rev')) { |
---|
258 | return $pclass->list_from_rev($self, $rev); |
---|
259 | } else { |
---|
260 | # no support, return all objects... |
---|
261 | return $self->list_objects($otype); |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | 1; |
---|
266 | |
---|
267 | __END__ |
---|
268 | |
---|
269 | =head1 SEE ALSO |
---|
270 | |
---|
271 | =head1 AUTHOR |
---|
272 | |
---|
273 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt> |
---|
274 | |
---|
275 | =head1 COPYRIGHT AND LICENSE |
---|
276 | |
---|
277 | Copyright (C) 2009 by Thauvin Olivier |
---|
278 | |
---|
279 | This library is free software; you can redistribute it and/or modify |
---|
280 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
281 | at your option, any later version of Perl 5 you may have available. |
---|
282 | |
---|
283 | =cut |
---|