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

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

Add sshKey managment for service accounts

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