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

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

add delUnknownSshKey attribute

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