1 | package LATMOS::Accounts::Bases::Objects; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | LATMOS::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 | |
---|
26 | List object supported by this module existing in base $base |
---|
27 | |
---|
28 | Must be provide by object class |
---|
29 | |
---|
30 | sub list { |
---|
31 | my ($class, $base) = @_; |
---|
32 | } |
---|
33 | |
---|
34 | =cut |
---|
35 | |
---|
36 | =head2 new($base, $id) |
---|
37 | |
---|
38 | Create a new object having $id as uid. |
---|
39 | |
---|
40 | =cut |
---|
41 | |
---|
42 | sub 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 | |
---|
56 | sub _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 | |
---|
69 | Must create a new object in database. |
---|
70 | |
---|
71 | Is 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 | |
---|
81 | Return the type of the object |
---|
82 | |
---|
83 | =cut |
---|
84 | |
---|
85 | sub 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 | |
---|
96 | Return the base handle for this object. |
---|
97 | |
---|
98 | =cut |
---|
99 | |
---|
100 | sub base { |
---|
101 | return $_[0]->{_base} |
---|
102 | } |
---|
103 | |
---|
104 | =head2 list_canonical_fields($for) |
---|
105 | |
---|
106 | Object shortcut to get the list of field supported by the object. |
---|
107 | |
---|
108 | =cut |
---|
109 | |
---|
110 | sub 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 | |
---|
117 | Object shortcut to get the field name supported by the object. |
---|
118 | |
---|
119 | =cut |
---|
120 | |
---|
121 | sub 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 | |
---|
128 | Must return the list of field supported by the object. |
---|
129 | |
---|
130 | Notice this query will always come from the upstream data base, |
---|
131 | this function is just a facility to store data in the module, but the |
---|
132 | underling database can reply themself. |
---|
133 | |
---|
134 | Is call if underling base doesn't override list_canonical_fields() |
---|
135 | |
---|
136 | See list_canonical_fields(). |
---|
137 | |
---|
138 | sub _canonical_fields { |
---|
139 | my ($self) = @_; |
---|
140 | } |
---|
141 | |
---|
142 | =cut |
---|
143 | |
---|
144 | sub _delayed_fields { |
---|
145 | my ($self)= @_; |
---|
146 | return (); |
---|
147 | } |
---|
148 | |
---|
149 | =head2 _get_fields_name($field, $for) |
---|
150 | |
---|
151 | Return the fields name for canonical field $field. |
---|
152 | $for, if set, is a string containing 'r' for read, 'w' for write, |
---|
153 | depending usage context. |
---|
154 | |
---|
155 | sub _get_field_name { |
---|
156 | my ($self, $field, $for) = @_; |
---|
157 | } |
---|
158 | |
---|
159 | =cut |
---|
160 | |
---|
161 | =head2 get_field($field) |
---|
162 | |
---|
163 | Return 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 | |
---|
173 | Return the value for canonical field $cfield. |
---|
174 | |
---|
175 | Call driver specific get_field_name() and get_field() |
---|
176 | |
---|
177 | =cut |
---|
178 | |
---|
179 | sub 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 | |
---|
187 | Set 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 | |
---|
197 | Set values for this object. %data is a list or peer |
---|
198 | canonical field => values. Fields names are translated. |
---|
199 | |
---|
200 | =cut |
---|
201 | |
---|
202 | sub 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 | |
---|
215 | Set the password into the database, $password is the clear version |
---|
216 | of the password. |
---|
217 | |
---|
218 | This function store it into userPassword canonical field if supported |
---|
219 | using crypt unix and md5 algorythm (crypt md5), the salt is 8 random |
---|
220 | caracters. |
---|
221 | |
---|
222 | The base driver should override it if another encryption is need. |
---|
223 | |
---|
224 | =cut |
---|
225 | |
---|
226 | sub 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 | |
---|
235 | 1; |
---|
236 | |
---|
237 | __END__ |
---|
238 | |
---|
239 | =head1 CANICALS FIELDS |
---|
240 | |
---|
241 | =head2 User class |
---|
242 | |
---|
243 | =head2 Group class |
---|
244 | |
---|
245 | =head1 SEE ALSO |
---|
246 | |
---|
247 | Mention other useful documentation such as the documentation of |
---|
248 | related modules or operating system documentation (such as man pages |
---|
249 | in UNIX), or any relevant external documentation such as RFCs or |
---|
250 | standards. |
---|
251 | |
---|
252 | If you have a mailing list set up for your module, mention it here. |
---|
253 | |
---|
254 | If you have a web site set up for your module, mention it here. |
---|
255 | |
---|
256 | =head1 AUTHOR |
---|
257 | |
---|
258 | Thauvin Olivier, E<lt>olivier.thauvin.ipsl.fr@localdomainE<gt> |
---|
259 | |
---|
260 | =head1 COPYRIGHT AND LICENSE |
---|
261 | |
---|
262 | Copyright (C) 2009 by Thauvin Olivier |
---|
263 | |
---|
264 | This library is free software; you can redistribute it and/or modify |
---|
265 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
266 | at your option, any later version of Perl 5 you may have available. |
---|
267 | |
---|
268 | =cut |
---|