1 | package LATMOS::Accounts::Web::Controller::NetHosts; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'LATMOS::Accounts::Web::Controller'; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | LATMOS::Accounts::Web::Controller::Users - Catalyst Controller |
---|
10 | |
---|
11 | =head1 DESCRIPTION |
---|
12 | |
---|
13 | Catalyst Controller. |
---|
14 | |
---|
15 | =head1 METHODS |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | |
---|
20 | =head2 index |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | sub index : Private { |
---|
25 | my ( $self, $c ) = @_; |
---|
26 | |
---|
27 | $c->stash->{ofilter} = $c->model('AttrFilter', 'nethost'); |
---|
28 | my $start = $c->req->param('start'); |
---|
29 | $c->stash->{uparams} = { %{ $c->req->params || {} } }; |
---|
30 | my %initials; |
---|
31 | my @userlist; |
---|
32 | foreach (map { $_->id } @{ $c->stash->{objectslist} }) { |
---|
33 | $initials{substr($_, 0, 1)} = 1; |
---|
34 | if ($start) { |
---|
35 | if (index($_, $start) == 0) { |
---|
36 | push(@userlist, $_); |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | if (@{ $c->stash->{objectslist} } < 20) { |
---|
41 | @userlist = map { $_->id } @{ $c->stash->{objectslist} }; |
---|
42 | } else { |
---|
43 | $c->stash->{initials} = [ sort keys %initials ]; |
---|
44 | } |
---|
45 | $c->stash->{page}{title} = 'Listes des hÎtes réseau (' . scalar(@{ |
---|
46 | $c->stash->{objectslist} }) . ')'; |
---|
47 | $c->stash->{nethostlist} = \@userlist; |
---|
48 | } |
---|
49 | |
---|
50 | sub default : LocalPath { |
---|
51 | my ( $self, $c, undef, $nethostname ) = @_; |
---|
52 | |
---|
53 | my $base = $c->model('Accounts')->db; |
---|
54 | $c->stash->{page}{title} = "HÎte réseau :: $nethostname"; |
---|
55 | $c->stash->{nethost} = $base->get_object('nethost', $nethostname) or do { |
---|
56 | $c->forward('/no_object'); |
---|
57 | return; |
---|
58 | }; |
---|
59 | $c->stash->{nethostname} = $nethostname; |
---|
60 | $c->stash->{form} = $c->model('AttrForms', 'nethost', $c->stash->{nethost}); |
---|
61 | if ($c->req->param('addip')) { |
---|
62 | for (1) { |
---|
63 | my $zone = $base->get_object('netzone', $c->req->param('zone')) |
---|
64 | or last; |
---|
65 | my $ip = $c->req->param('newip') || 'any'; |
---|
66 | if ($ip =~ /^(any|first)$/) { |
---|
67 | my @freeips = $zone->get_attributes('freeIP') or last; |
---|
68 | my $idx = $ip eq 'first' ? 0 : rand(scalar(@freeips)); |
---|
69 | $ip = $freeips[$idx] or last; |
---|
70 | } |
---|
71 | my @currentips = grep { $_ } $c->stash->{nethost}->get_attributes('ip'); |
---|
72 | $c->stash->{nethost}->set_c_fields(ip => [ @currentips, $ip ]) |
---|
73 | or do { |
---|
74 | $base->rollback; |
---|
75 | last; |
---|
76 | }; |
---|
77 | $base->commit; |
---|
78 | last; |
---|
79 | } |
---|
80 | } elsif ($c->req->param('rename') && $c->req->param('to')) { |
---|
81 | my $to = $c->req->param('to'); |
---|
82 | if ($base->rename_nethost($nethostname, $to, |
---|
83 | addcname => $c->req->param('addcname') || undef, |
---|
84 | )) { |
---|
85 | $base->commit; |
---|
86 | $c->res->redirect($to); |
---|
87 | } else { |
---|
88 | $base->rollback; |
---|
89 | } |
---|
90 | } elsif ($c->req->param('delete')) { |
---|
91 | if ($base->delete_object('nethost', $nethostname)) { |
---|
92 | $base->commit; |
---|
93 | $c->res->redirect('/nethosts'); |
---|
94 | } else { |
---|
95 | $base->rollback; |
---|
96 | } |
---|
97 | } else { |
---|
98 | $c->stash->{form}->set_attrs; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | =head1 AUTHOR |
---|
103 | |
---|
104 | Thauvin Olivier |
---|
105 | |
---|
106 | =head1 LICENSE |
---|
107 | |
---|
108 | This library is free software, you can redistribute it and/or modify |
---|
109 | it under the same terms as Perl itself. |
---|
110 | |
---|
111 | =cut |
---|
112 | |
---|
113 | 1; |
---|