source: trunk/lib/Vote/Controller/Admin.pm @ 206

Last change on this file since 206 was 206, checked in by nanardon, 15 years ago
  • kill some old function usage
File size: 7.6 KB
Line 
1package Vote::Controller::Admin;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6
7=head1 NAME
8
9Vote::Controller::Admin - Catalyst Controller
10
11=head1 DESCRIPTION
12
13Catalyst Controller.
14
15=head1 METHODS
16
17=cut
18
19
20=head2 index
21
22=cut
23
24sub begin : Private {
25    my ( $self, $c, @args ) = @_;
26    $c->model('Vote')->rollback;
27}
28
29sub index : Private {
30    my ( $self, $c ) = @_;
31
32    $c->res->redirect($c->uri_for('/'));
33}
34
35sub modify_poll : Private {
36    my ( $self, $c) = @_;
37    my $id = $c->stash->{voteid};
38
39    my $poll = $c->model('Vote')->poll($id);
40    for ($poll->status || '') {
41    /^BEFORE$/ and do {
42        if ($c->req->param('addch')) {
43            $poll->add_choice($c->req->param('addch'))
44                and $poll->commit;
45        } elsif ($c->req->param('delch')) {
46            $poll->delete_choice($c->req->param('delch'))
47                and $poll->commit;
48        } elsif ($c->req->param('pollparam')) {
49            if ($c->req->param('dstart')) {
50                $c->req->param('start',
51                    $c->req->param('dstart') . ' ' . ($c->req->param('hstart') || '')
52                );
53            }
54            if ($c->req->param('dend')) {
55                $c->req->param('end',
56                    $c->req->param('dend') . ' ' . ($c->req->param('hend') || '')
57                );
58            }
59            if ($c->req->param('end') && $c->req->param('start')) {
60                if ($poll->check_date_max($c->req->param('start'))) {
61                    if (! $poll->check_date_max($c->req->param('end'), $c->req->param('start'))) {
62                        $c->stash->{dateerror} = "Le vote se termine avant de commencer";
63                        next;
64                    }
65                } else {
66                    $c->stash->{dateerror} = "Le debut du vote doit être dans le futur";
67                    next;
68                }
69            } elsif ($c->req->param('end') || $c->req->param('start')) {
70                $c->stash->{dateerror} = "Vous devez définir un debut et une fin";
71                next;
72            }
73            $poll->param(
74                map { $_ => ($c->req->param($_) || undef) }
75                grep { exists($c->req->params->{$_}) }
76                qw(label description start end choice_count free_choice)
77            ) and $poll->commit;
78        } elsif ($c->req->param('encrypted')) {
79            my $passphrase = $c->req->param('passphrase') ||
80                $c->session->{'vpass' . $c->stash->{voteid}};
81            $poll->gen_poll_keys($passphrase)
82                and $poll->commit;
83        } elsif ($c->req->param('notcrypted')) {
84            $poll->param(public_key => undef, private_key => undef)
85                and $poll->commit;
86        }
87    };
88
89    /^(BEFORE|RUNNING)$/ and do {
90        if (my ($upload) = $c->req->upload('votinglist')) {
91            $poll->voting_from_file(
92                $upload->fh,
93                $c->req->param('delete'),
94            ) and $poll->commit;
95        } elsif($c->req->param('delvoting')) {
96            $poll->delete_voting($c->req->param('delvoting'))
97                and $poll->commit;
98        } elsif ($c->req->param('mail')) {
99            $poll->addupd_voting($c->req->param('mail'), $c->req->param('id'))
100                and $poll->commit;
101        } elsif($c->req->param('mailpasswd')) {
102            # TODO
103            foreach my $vkey ($poll->list_voting_no_passwd) {
104                my $voting = $poll->voting($vkey);
105                my $pass = $voting->gen_passwd;
106                $c->forward(
107                    q'Vote::View::Mail', 'render',
108                    [ 'voting_passwd.tt', {
109                        From => $poll->info->{owner},
110                        To => $voting->info->{mail},
111                        Subject => "Invitation à voter",
112                        mail => {
113                            voteid => $id,
114                            mail => $voting->info->{mail},
115                            passwd => $pass,
116                        }
117                    } ]
118                );
119            }
120        }
121    };
122
123    /^AFTER$/ and do {
124        if ($c->req->param('mapfrom') && $c->req->param('mapto')) {
125            $poll->map_value(
126                $id,
127                $c->req->param('mapfrom'),
128                $c->req->param('mapto'),
129            );
130        }
131        foreach my $bid ($poll->list_ballot_needvalid) {
132            if (!$c->req->param($bid)) {
133                next;
134            } elsif($c->req->param($bid) eq 'invalid') {
135                $poll->ballot($bid)->mark_invalid(1);
136                $poll->commit;
137            } elsif($c->req->param($bid) eq 'valid') {
138                $poll->ballot($bid)->mark_invalid(0);
139                $poll->commit;
140            }
141        }
142        if ($c->req->param('decryptballot')) {
143            my $passphrase = $c->req->param('passphrase') ||
144                $c->session->{'vpass' . $c->stash->{voteid}};
145            if ($c->model('Vote')->
146                poll($c->stash->{voteid})->
147                private_key($passphrase)) {
148                $c->model('Vote')->poll($c->stash->{voteid})->decrypted_ballots(
149                    $passphrase
150                );
151            } else {
152            }
153        }   
154    };
155    }
156}
157
158sub auth : Private {
159    my ($self, $c) = @_;
160    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
161    my $password = $c->session->{'vpass' . $c->stash->{voteid}} ||
162        $c->req->param('vpass' . $c->stash->{voteid});
163
164    if (!$poll->auth_poll($password)) {
165        $c->stash->{page}{title} = $poll->info('label') .
166            ': Login d\'administration';
167        $c->session->{'vpass' . $c->stash->{voteid}} = undef;
168        $c->stash->{template} = 'admin/login.tt';
169        return;
170    }
171    $c->session->{'vpass' . $c->stash->{voteid}} = $password;
172    return 1;
173}
174
175sub default : Private {
176    my ( $self, $c, undef, $id ) = @_;
177    $c->stash->{voteid} = $id;
178    my $poll = $c->model('Vote')->poll($id);
179
180    $poll->info or do {
181        $c->res->redirect($c->uri_for('/'));
182        return;
183    };
184
185    $c->forward('auth') or return;
186    $c->forward('modify_poll');
187    $c->stash->{page}{title} = $poll->info('label') . ': Administration';
188}
189
190sub voting: LocalRegex('^(\d+)/voting$') {
191    my ($self, $c) = @_;
192    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
193    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
194
195    $poll->info or do {
196        $c->res->redirect($c->uri_for('/'));
197        return;
198    };
199
200    $c->forward('auth') or return;
201    $c->forward('modify_poll');
202    $c->stash->{page}{title} = $poll->info('label') . ': Administration, liste des electeurs';
203}
204
205sub ballot: LocalRegex('^(\d+)/ballot$') {
206    my ($self, $c) = @_;
207    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
208    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
209
210    $poll->info or do {
211        $c->res->redirect($c->uri_for('/'));
212        return;
213    };
214
215    $c->forward('auth') or return;
216    $c->forward('modify_poll');
217    $c->stash->{page}{title} = $poll->info('label') . ': Administration, bulletin';
218}
219
220sub privatekey : LocalRegex('^(\d+)/privatekey$') {
221    my ($self, $c) = @_;
222    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
223    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
224
225    $poll->info or do {
226        $c->res->redirect($c->uri_for('/'));
227        return;
228    };
229    $c->response->body($poll->info('private_key') || '');
230}
231
232sub end : Private {
233    my ($self, $c) = @_;
234    if ($c->res->body) { return }
235    elsif ($c->stash->{latex}) { $c->forward(qw/Vote::View::Latex/) }
236    else { $c->forward(qw/Vote::View::TT/) }
237}
238
239=head1 AUTHOR
240
241Thauvin Olivier
242
243=head1 LICENSE
244
245This library is free software, you can redistribute it and/or modify
246it under the same terms as Perl itself or CeCILL.
247
248=cut
249
2501;
Note: See TracBrowser for help on using the repository browser.