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

Last change on this file since 2057 was 2057, checked in by nanardon, 7 years ago

Add support for netzone radius

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