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

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