source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm @ 71

Last change on this file since 71 was 71, checked in by nanardon, 15 years ago
  • make objects path configurable
  • Property svn:keywords set to Id Rev
File size: 5.4 KB
Line 
1package LATMOS::Accounts::Bases::Objects;
2
3use 5.010000;
4use strict;
5use warnings;
6
7our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
8
9=head1 NAME
10
11LATMOS::Accounts::Bases::Objects - Base class for account objects
12
13=head1 SYNOPSIS
14
15  use LATMOS::Accounts::Bases::Objects;
16  LATMOS::Accounts::Bases::Objects->new($base, $type, $id);
17
18=head1 DESCRIPTION
19
20=head1 FUNCTIONS
21
22=cut
23
24=head2 list($base)
25
26List object supported by this module existing in base $base
27
28Must be provide by object class
29
30    sub list {
31        my ($class, $base) = @_;
32    }
33
34=cut
35
36=head2 new($base, $id)
37
38Create a new object having $id as uid.
39
40=cut
41
42sub new {
43    my ($class, $base, $id, @args) = @_;
44    # So can be call as $class->SUPER::new()
45    bless {
46        _base => $base,
47        _type => ($class =~ m/[^:]*$/)[0],
48    }, $class;
49}
50
51# _new($base, $type, $id, ...)
52
53# Return a new object of type $type having unique identifier
54# $id, all remaining arguments are passed to the subclass.
55
56sub _new {
57    my ($class, $base, $otype, $id, @args) = @_;
58
59    # finding perl class:
60    my $pclass = $base->_load_obj_class($otype) or return;
61    my $newobj = "$pclass"->new($base, $id, @args) or return;
62    $newobj->{_base} = $base;
63    $newobj->{_type} = lc($otype);
64    return $newobj;
65}
66
67=head2 _create($class, $base, $id, %data)
68
69Must create a new object in database.
70
71Is called if underling base does not override create_object
72
73    sub _create(
74        my ($class, $base, $id, %data)
75    }
76
77=cut
78
79=head2 type
80
81Return the type of the object
82
83=cut
84
85sub type {
86    my ($self) = @_;
87    if (ref $self) {
88        return $self->{_type}
89    } else {
90        return lc(($self =~ /::([^:]+)$/)[0]);
91    }
92}
93
94=head2 base
95
96Return the base handle for this object.
97
98=cut
99
100sub base {
101    return $_[0]->{_base}
102}
103
104=head2 list_canonical_fields($for)
105
106Object shortcut to get the list of field supported by the object.
107
108=cut
109
110sub list_canonical_fields {
111    my ($self, $for) = @_;
112    $self->base->list_canonical_fields($self->type, $for);
113}
114
115=head2 get_field_name($field, $for)
116
117Object shortcut to get the field name supported by the object.
118
119=cut
120
121sub get_field_name {
122    my ($self, $field, $for) = @_;
123    $self->base->get_field_name($self->type, $field, $for);
124}
125
126=head2 _canonical_fields
127
128Must return the list of field supported by the object.
129
130Notice this query will always come from the upstream data base,
131this function is just a facility to store data in the module, but the
132underling database can reply themself.
133
134Is call if underling base doesn't override list_canonical_fields()
135
136See list_canonical_fields().
137
138    sub _canonical_fields {
139        my ($self) = @_;
140    }
141
142=cut
143
144sub _delayed_fields {
145    my ($self)= @_;
146    return ();
147}
148
149=head2 _get_fields_name($field, $for)
150
151Return the fields name for canonical field $field.
152$for, if set, is a string containing 'r' for read, 'w' for write,
153depending usage context.
154
155    sub _get_field_name {
156        my ($self, $field, $for) = @_;
157    }
158
159=cut
160
161=head2 get_field($field)
162
163Return the value for $field, must be provide by data base.
164
165    sub get_field {
166        my ($self, $field)
167    }
168
169=cut
170
171=head2 get_c_fields($cfield)
172
173Return the value for canonical field $cfield.
174
175Call driver specific get_field_name() and get_field()
176
177=cut
178
179sub get_c_field {
180    my ($self, $cfield) = @_;
181    my $field = $self->base->get_field_name($self->type, $cfield, 'r') or return;
182    $self->get_field($field);
183}
184
185=head2 set_fields(%data)
186
187Set values for this object. %data is a list or peer field => values.
188
189    sub set_fields {
190        my ($self, %data) = @_;
191    }
192
193=cut
194
195=head2 set_c_fields(%data)
196
197Set values for this object. %data is a list or peer
198canonical field => values. Fields names are translated.
199
200=cut
201
202sub set_c_fields {
203    my ($self, %cdata) = @_;
204    my %data;
205    foreach my $cfield (keys %cdata) {
206        my $field = $self->base->get_field_name($self->type, $cfield) or next;
207        $data{$field} = $cdata{$cfield};
208    }
209    keys %data or return 1; # TODO: return an error ?
210    $self->set_fields(%data);
211}
212
213=head2 set_password($password)
214
215Set the password into the database, $password is the clear version
216of the password.
217
218This function store it into userPassword canonical field if supported
219using crypt unix and md5 algorythm (crypt md5), the salt is 8 random
220caracters.
221
222The base driver should override it if another encryption is need.
223
224=cut
225
226sub set_password {
227    my ($self, $clear_pass) = @_;
228    if (my $field = $self->base->get_field_name($self->type, 'userPassword')) {
229        my @salt_char = (('a' .. 'z'), ('A' .. 'Z'), (0 .. 9), '/', '.');
230        my $salt = join('', map { $salt_char[rand(scalar(@salt_char))] } (1 .. 8));
231        $self->set_fields($field, crypt($clear_pass, $salt));
232    }
233}
234
2351;
236
237__END__
238
239=head1 CANICALS FIELDS
240
241=head2 User class
242
243=head2 Group class
244
245=head1 SEE ALSO
246
247Mention other useful documentation such as the documentation of
248related modules or operating system documentation (such as man pages
249in UNIX), or any relevant external documentation such as RFCs or
250standards.
251
252If you have a mailing list set up for your module, mention it here.
253
254If you have a web site set up for your module, mention it here.
255
256=head1 AUTHOR
257
258Thauvin Olivier, E<lt>olivier.thauvin.ipsl.fr@localdomainE<gt>
259
260=head1 COPYRIGHT AND LICENSE
261
262Copyright (C) 2009 by Thauvin Olivier
263
264This library is free software; you can redistribute it and/or modify
265it under the same terms as Perl itself, either Perl version 5.10.0 or,
266at your option, any later version of Perl 5 you may have available.
267
268=cut
Note: See TracBrowser for help on using the repository browser.