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 | return "LATMOS::Accounts::Bases::$pclass"->new(%options); |
---|
42 | } |
---|
43 | |
---|
44 | sub _load_obj_class { |
---|
45 | my ($self, $otype) = @_; |
---|
46 | |
---|
47 | # finding perl class: |
---|
48 | my $pclass = ref $self; |
---|
49 | $pclass .= '::' . ucfirst(lc($otype)); |
---|
50 | eval "require $pclass;"; |
---|
51 | if ($@) { return } # error message ? |
---|
52 | return $pclass; |
---|
53 | } |
---|
54 | |
---|
55 | =head2 list_canonicals_fields($otype) |
---|
56 | |
---|
57 | Return the list of supported fields by the database for object type $otype. |
---|
58 | |
---|
59 | =cut |
---|
60 | |
---|
61 | sub list_canonicals_fields { |
---|
62 | my ($self, $otype) = @_; |
---|
63 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
64 | $pclass->_canonical_fields; |
---|
65 | } |
---|
66 | |
---|
67 | =head2 get_field_name($otype, $c_fields) |
---|
68 | |
---|
69 | Return the internal fields name for $otype object for |
---|
70 | canonical fields $c_fields |
---|
71 | |
---|
72 | =cut |
---|
73 | |
---|
74 | sub get_field_name { |
---|
75 | my ($self, $otype, $c_fields) = @_; |
---|
76 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
77 | $pclass->_get_field_name($c_fields); |
---|
78 | } |
---|
79 | |
---|
80 | =head2 list_objects($otype) |
---|
81 | |
---|
82 | Return the list of UID for object of $otype. |
---|
83 | |
---|
84 | =cut |
---|
85 | |
---|
86 | sub list_objects { |
---|
87 | my ($self, $otype) = @_; |
---|
88 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
89 | $pclass->list($self); |
---|
90 | } |
---|
91 | |
---|
92 | =head2 get_object($type, $id) |
---|
93 | |
---|
94 | Return an object of $type (typically user or group) having identifier |
---|
95 | $id. |
---|
96 | |
---|
97 | =cut |
---|
98 | |
---|
99 | sub get_object { |
---|
100 | my ($self, $otype, $id) = @_; |
---|
101 | |
---|
102 | return LATMOS::Accounts::Bases::Objects->_new($self, $otype, $id); |
---|
103 | } |
---|
104 | |
---|
105 | =head2 create_object($type, $id, %data) |
---|
106 | |
---|
107 | Create and return an object of type $type with unique id |
---|
108 | $id having %data. |
---|
109 | |
---|
110 | This method should be provided by the data base handler. |
---|
111 | |
---|
112 | =cut |
---|
113 | |
---|
114 | sub create_object { |
---|
115 | my ($self, $otype, $id, %data) = @_; |
---|
116 | my $pclass = $self->_load_obj_class($otype); |
---|
117 | $pclass->create($id, %data) or return; |
---|
118 | $self->get_object($otype, $id); |
---|
119 | } |
---|
120 | |
---|
121 | =head2 create_c_object($type, $id, %data) |
---|
122 | |
---|
123 | Create and return an object of type $type with unique id |
---|
124 | $id having %data using canonical fields |
---|
125 | |
---|
126 | =cut |
---|
127 | |
---|
128 | sub create_c_object { |
---|
129 | my ($self, $otype, $id, %cdata) = @_; |
---|
130 | |
---|
131 | my %data; |
---|
132 | foreach my $cfield (keys %cdata) { |
---|
133 | my $field = $self->base->get_field_name($self->type, $cfield) or next; |
---|
134 | $data{$field} = $cdata{$cfield}; |
---|
135 | } |
---|
136 | keys %data or return 1; # TODO: return an error ? |
---|
137 | $self->create_object($otype, $id, %data); |
---|
138 | } |
---|
139 | |
---|
140 | =head2 load |
---|
141 | |
---|
142 | Make account base loading data into memory if need. |
---|
143 | Should always be called, if database fetch data on the fly |
---|
144 | (SQL, LDAP), the function just return True. |
---|
145 | |
---|
146 | =cut |
---|
147 | |
---|
148 | sub load { 1 } |
---|
149 | |
---|
150 | =head2 is_transactionnal |
---|
151 | |
---|
152 | Return True is the database support commit and rollback |
---|
153 | |
---|
154 | =cut |
---|
155 | |
---|
156 | sub is_transactionnal { |
---|
157 | my ($self) = @_; |
---|
158 | return($self->can('_rollback') && $self->can('_commit')); |
---|
159 | } |
---|
160 | |
---|
161 | =head2 commit |
---|
162 | |
---|
163 | Save change into the database if change are not done immediately. |
---|
164 | This should always be called as you don't know when change are applied. |
---|
165 | |
---|
166 | Return always true if database does not support any transaction. |
---|
167 | |
---|
168 | The driver should provides a _commit functions to save data. |
---|
169 | |
---|
170 | =cut |
---|
171 | |
---|
172 | sub commit { |
---|
173 | my ($self) = @_; |
---|
174 | return $self->can('_commit') ? $self->_commit : 1; |
---|
175 | } |
---|
176 | |
---|
177 | =head2 rollback |
---|
178 | |
---|
179 | If database support transaction, rollback changes. Return false |
---|
180 | if database does not support. |
---|
181 | |
---|
182 | If supported, driver should provides a _rollback functions |
---|
183 | |
---|
184 | =cut |
---|
185 | |
---|
186 | sub rollback { |
---|
187 | my ($self) = @_; |
---|
188 | return $self->can('_rollback') ? $self->_rollback : 0; |
---|
189 | } |
---|
190 | |
---|
191 | 1; |
---|
192 | |
---|
193 | __END__ |
---|
194 | |
---|
195 | =head1 SEE ALSO |
---|
196 | |
---|
197 | =head1 AUTHOR |
---|
198 | |
---|
199 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt> |
---|
200 | |
---|
201 | =head1 COPYRIGHT AND LICENSE |
---|
202 | |
---|
203 | Copyright (C) 2009 by Thauvin Olivier |
---|
204 | |
---|
205 | This library is free software; you can redistribute it and/or modify |
---|
206 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
207 | at your option, any later version of Perl 5 you may have available. |
---|
208 | |
---|
209 | =cut |
---|