source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Model/AttrForms.pm @ 2268

Last change on this file since 2268 was 2268, checked in by nanardon, 5 years ago

Add lastlogin attribute

File size: 9.9 KB
Line 
1package LATMOS::Accounts::Web::Model::AttrForms;
2
3use strict;
4use warnings;
5use base 'Catalyst::Model';
6use LATMOS::Accounts::Log;
7use HTML::Prototype;
8use utf8;
9
10=head1 NAME
11
12LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
13
14=head1 DESCRIPTION
15
16Catalyst Model.
17
18=cut
19
20my $forms = {
21    usersys => {
22        name => 'SystÚme',
23        attrs => [ qw(
24            sn givenName
25            description
26            comment
27            managerContact
28            uid uidNumber gidNumber
29            gecos homeDirectory loginShell
30            mail
31            expire
32            endcircuit
33            lastlogin
34            locked
35            sshPublicKey
36            sshPublicKeyFilter
37            delUnknownSshKey
38            createRequestId
39            create
40            createdby
41            date
42            modifiedby
43        ) ],
44    },
45    add => {
46        name => 'Others',
47        attrs => [ qw(
48            sn givenName
49            nationality nativeCountry
50            initials nickname
51            snNative
52            givenNameNative
53            wWWHomePage
54            halReference
55            firstAidTrainingValidity
56        ) ],
57    },
58    user => {
59        name => 'Statut RH',
60        attrs => [ qw(
61            sn givenName
62            nationality nativeCountry
63            description
64            company
65            employer
66            department
67            managerContact
68            manager
69            contratType
70            expire
71            endcircuit
72            create
73            createdby
74            date
75            modifiedby
76        ) ],
77    },
78    useremployment => {
79        name => 'Suivi RH',
80        attrs => [ qw(
81            sn givenName
82            nationality nativeCountry
83            expire
84        ) ]
85    },
86    useraddress => {
87        name => 'Adresse',
88        attrs => [ qw(
89            initials
90            mail
91        ) ],
92    },
93    usermy => {
94        name => 'My',
95        attrs => [ qw(
96            snNative
97            givenNameNative
98            wWWHomePage
99            halReference
100        ) ],
101    },
102    site => {
103        name => 'Site',
104        attrs => [ qw(
105            description
106            siteNick
107            streetAddress
108            postOfficeBox
109            postalCode
110            l
111            st
112            co
113            facsimileTelephoneNumber
114            create
115            date
116        ) ],
117    },
118    address => {
119        name => 'Adresse',
120        attrs => [ qw(
121            isMainAddress
122            contactOnly
123            expire
124            telephoneNumber
125            streetAddress
126            postOfficeBox
127            postalCode
128            l
129            st
130            physicalDeliveryOfficeName
131            site
132            co
133            unexported
134            description
135            facsimileTelephoneNumber
136        ) ],
137    },
138    employment => {
139        name => 'Nouvelle période',
140        attrs => [ qw(
141            firstday
142            lastday
143            endcircuit
144            lengthText
145            description
146            company
147            employer
148            contratType
149            assigned
150            department
151            managerContact
152            requestId
153        ) ],
154    },
155    group => {
156        name => 'SystÚme',
157        attrs => [ qw(
158            gidNumber
159            description label
160            comment
161            managedBy
162            managedAlsoBy
163            autoMemberFilter
164            autoFromSutype
165            autoInclude
166            autoExclude
167            sutype
168            services
169            create
170            createdby
171            date
172            modifiedby
173        ) ],
174    },
175    nethost => {
176        name => 'Ordinateur',
177        attrs => [ qw(
178            name
179            description
180            hostType
181            comment
182            endOfWarranty
183            serialNumber
184            inventoryNumber
185            expire
186            netLocked
187            related
188            encryptKey
189            site
190            physicalDeliveryOfficeName
191            owner
192            user
193            ip
194            macaddr
195            puppetClass
196            noInheritPuppet
197            noDynamic
198            services
199            cname
200            otherName
201            reverse
202            create
203            createdby
204            date
205            modifiedby
206            unexported
207            sshfp
208        ) ],
209    },
210    netzone => {
211        name => 'Zone réseau',
212        attrs => [ qw(
213            name
214            description
215            type
216            net
217            netExclude
218            puppetClass
219            outputD
220            templateD
221            site
222            allow_dyn
223            dynFrom
224            domain
225            ipCount
226            freeIPCount
227            create
228            createdby
229            date
230            modifiedby
231            dnsRevision
232            lastUpdate
233            unexported
234            hostParams
235        ) ],
236    },
237    aliases => {
238        name => 'Alias mail',
239        attrs => [ qw(
240            expire
241            description
242            comment
243            autoMemberFilter
244            autoFromSutype
245            autoInclude
246            autoExclude
247            autoAddEmail
248            create
249            createdby
250            date
251            modifiedby
252            unexported
253            forward
254            finalpoint
255            parents
256        )],
257    },
258    services => {
259        name => 'Services',
260        attrs => [ qw(
261            type
262            description
263            comment
264            start
265            end
266            manager
267            dependOn
268            create
269            createdby
270            date
271            modifiedby
272            unexported
273        )],
274    },
275};
276
277sub escape {
278    my ($self, $text) = @_;
279    $text ||= '';
280    for ($text) {
281        s/&/&/g;
282        s/</&lt;/g;
283        s/>/&gt;/g;
284        s/"/&quot;/g;
285    }
286    $text;
287}
288
289sub new {
290    my ($class) = @_;
291    bless({}, $class);
292}
293
294# call either OBJ or type + base
295
296sub ACCEPT_CONTEXT {
297    my ($self, $c, $form, $object, $base) = @_;
298    my $new = {};
299    $new->{c} = $c;
300    $new->{form} = $form;
301    $new->{object} = $object if (ref $object);
302    $new->{base} = $base || (ref $object ? $object->base : $c->model('Accounts')->db);
303    $new->{otype} = ref $object ? $object->type : $object;
304
305
306    if ($form) {
307        foreach (@{ $forms->{$form}->{attrs} }) {
308            $new->{_attr}{$_} = $c->model('AttrFormsA', $_, (ref $object
309                    ? ($object)
310                    : ($base, $new->{otype})))
311                or die "Cannot get attribute $_";
312        }
313    }
314
315    bless($new, __PACKAGE__);
316}
317
318sub base {
319    my ( $self ) = @_;
320    $self->{base}
321}
322
323sub otype {
324    my ($self) = @_;
325    $self->{otype};
326}
327
328sub object {
329    my ($self) = @_;
330    $self->{object};
331}
332
333sub label {
334    my ($self) = @_;
335    $forms->{$self->{form}}->{name} || ''
336}
337
338sub attributes {
339    my ($self, $for) = @_;
340    grep { $_ }
341    grep { $self->base->attribute($self->otype, $_) }
342    @{ $forms->{$self->{form}}->{attrs} };
343}
344
345sub _uri_part {
346    my ($self, $ref) = @_;
347    my $uri_part = {
348        user => 'users',
349        group => 'groups',
350        nethost => 'nethosts',
351        netzone => 'netzones',
352        site => 'sites',
353        aliasess => 'aliases',
354    }->{$ref};
355}
356
357sub attr_label {
358    my ($self, $attr, $label, $hint) = @_;
359
360    if ($label) {
361        return $label;
362    } else {
363        my $oattr = $self->base->attribute($self->otype, $attr)
364            or return $attr;
365        my $label = $oattr->label;
366        return $label;
367    }
368}
369
370sub attr_hint {
371    my ($self, $attr) = @_;
372
373    $self->{_attr}{$attr}->attr_hint;
374}
375
376sub attr_field {
377    my ($self, $attr, $type) = @_;
378
379    $self->{_attr}{$attr}->attr_field($type);
380}
381
382sub field {
383    my ($self, $attr, $type) = @_;
384
385    if (!$self->{_attr}{$attr}) {
386        $self->{_attr}{$attr} = $self->{c}->model('AttrFormsA', $attr, ($self->object
387                ? ($self->object)
388                : ($self->base, $self->otype)))
389            or die "Cannot get attribute $_";
390    }
391
392    $self->{_attr}{$attr};
393}
394
395
396sub write_attributes {
397    my ($self) = @_;
398    my @attrs;
399    foreach (values %{ $self->{_attr} }) {
400        $_->readonly and next;
401        push(@attrs, $_);
402    }
403    @attrs;
404}
405
406sub set_attrs {
407    my ($self, $attrs, $id) = @_;
408    $self->{c}->req->param($self->{form}) || $attrs or return;
409    my %fields;
410    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
411        my $attr = ref $_ ? $_ : $self->{_attr}{$_};
412        $attr ||= $self->{c}->model('AttrFormsA', $_, (
413                ref $self->object
414                    ? ($self->object)
415                    : ($self->base, $self->{otype})
416                )
417        );
418
419        if ($attr->{multiple}) {
420            $fields{$attr->name} = [ grep { $_ } $self->{c}->req->param($attr->htmlname) ];
421        } else {
422            $fields{$attr->name} = $self->{c}->req->param($attr->htmlname) || undef;
423        }
424    }
425    if ($self->object) {
426        $self->object->set_c_fields(%fields) or do {
427            $self->{c}->stash->{page}{error} =
428                LATMOS::Accounts::Log::lastmessage(LA_ERR);
429            $self->object->base->rollback;
430            return;
431        };
432    } else {
433        $id ||= join('', map {('a'..'z')[rand(26)]}(0..8));
434        $self->base->create_c_object($self->otype, $id, %fields) or do {
435            $self->{c}->stash->{page}{error} =
436                LATMOS::Accounts::Log::lastmessage(LA_ERR);
437            $self->base->rollback;
438            return;
439        };
440    }
441    $self->base->commit;
442    $self->reset_param;
443    return $id || $self->object->id;
444   
445}
446
447sub reset_param {
448    my ($self) = @_;
449    foreach (values %{ $self->{_attr} }) {
450        delete($self->{c}->req->params->{$_->htmlname});
451    }
452}
453
454sub submit {
455    my ($self, $hidden) = @_;
456    return sprintf(
457        '<input type="%s" name="%s" value="Enregistrer">',
458        ($hidden ? 'hidden' : 'submit'),
459        $self->escape($self->{form}),
460    );
461}
462
463
464=head1 AUTHOR
465
466Thauvin Olivier
467
468=head1 LICENSE
469
470This library is free software, you can redistribute it and/or modify
471it under the same terms as Perl itself.
472
473=cut
474
4751;
Note: See TracBrowser for help on using the repository browser.