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

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