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 |
---|
44 | } |
---|
45 | |
---|
46 | sub type { |
---|
47 | $_[0]->{_type}; |
---|
48 | } |
---|
49 | |
---|
50 | sub _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 | |
---|
63 | Return the list of supported fields by the database for object type $otype. |
---|
64 | |
---|
65 | =cut |
---|
66 | |
---|
67 | sub 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 | |
---|
75 | Return the internal fields name for $otype object for |
---|
76 | canonical fields $c_fields |
---|
77 | |
---|
78 | =cut |
---|
79 | |
---|
80 | sub 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 | |
---|
88 | Return a list of supported object |
---|
89 | |
---|
90 | @type is an additionnal list of objects to check |
---|
91 | |
---|
92 | =cut |
---|
93 | |
---|
94 | sub 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 | |
---|
102 | Return true is object type $otype is supported |
---|
103 | |
---|
104 | =cut |
---|
105 | |
---|
106 | sub is_supported_object { |
---|
107 | my ($self, $otype) = @_; |
---|
108 | return $self->_load_obj_class($otype) ? 1 : 0; |
---|
109 | } |
---|
110 | |
---|
111 | =head2 list_objects($otype) |
---|
112 | |
---|
113 | Return the list of UID for object of $otype. |
---|
114 | |
---|
115 | =cut |
---|
116 | |
---|
117 | sub 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 | |
---|
125 | Return an object of $type (typically user or group) having identifier |
---|
126 | $id. |
---|
127 | |
---|
128 | =cut |
---|
129 | |
---|
130 | sub 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 | |
---|
138 | Create and return an object of type $type with unique id |
---|
139 | $id having %data. |
---|
140 | |
---|
141 | This method should be provided by the data base handler. |
---|
142 | |
---|
143 | =cut |
---|
144 | |
---|
145 | sub 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 | |
---|
154 | Create and return an object of type $type with unique id |
---|
155 | $id having %data using canonical fields |
---|
156 | |
---|
157 | =cut |
---|
158 | |
---|
159 | sub 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 | |
---|
173 | Make account base loading data into memory if need. |
---|
174 | Should always be called, if database fetch data on the fly |
---|
175 | (SQL, LDAP), the function just return True. |
---|
176 | |
---|
177 | =cut |
---|
178 | |
---|
179 | sub load { 1 } |
---|
180 | |
---|
181 | =head2 is_transactionnal |
---|
182 | |
---|
183 | Return True is the database support commit and rollback |
---|
184 | |
---|
185 | =cut |
---|
186 | |
---|
187 | sub is_transactionnal { |
---|
188 | my ($self) = @_; |
---|
189 | return($self->can('_rollback') && $self->can('_commit')); |
---|
190 | } |
---|
191 | |
---|
192 | =head2 commit |
---|
193 | |
---|
194 | Save change into the database if change are not done immediately. |
---|
195 | This should always be called as you don't know when change are applied. |
---|
196 | |
---|
197 | Return always true if database does not support any transaction. |
---|
198 | |
---|
199 | The driver should provides a _commit functions to save data. |
---|
200 | |
---|
201 | =cut |
---|
202 | |
---|
203 | sub commit { |
---|
204 | my ($self) = @_; |
---|
205 | return $self->can('_commit') ? $self->_commit : 1; |
---|
206 | } |
---|
207 | |
---|
208 | =head2 rollback |
---|
209 | |
---|
210 | If database support transaction, rollback changes. Return false |
---|
211 | if database does not support. |
---|
212 | |
---|
213 | If supported, driver should provides a _rollback functions |
---|
214 | |
---|
215 | =cut |
---|
216 | |
---|
217 | sub rollback { |
---|
218 | my ($self) = @_; |
---|
219 | return $self->can('_rollback') ? $self->_rollback : 0; |
---|
220 | } |
---|
221 | |
---|
222 | 1; |
---|
223 | |
---|
224 | __END__ |
---|
225 | |
---|
226 | =head1 SEE ALSO |
---|
227 | |
---|
228 | =head1 AUTHOR |
---|
229 | |
---|
230 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt> |
---|
231 | |
---|
232 | =head1 COPYRIGHT AND LICENSE |
---|
233 | |
---|
234 | Copyright (C) 2009 by Thauvin Olivier |
---|
235 | |
---|
236 | This library is free software; you can redistribute it and/or modify |
---|
237 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
238 | at your option, any later version of Perl 5 you may have available. |
---|
239 | |
---|
240 | =cut |
---|