1 | package Epoll::Controller::Admin; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Controller'; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | Epoll::Controller::Admin - 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 begin : Private { |
---|
25 | my ( $self, $c, @args ) = @_; |
---|
26 | $c->model('Vote')->rollback; |
---|
27 | } |
---|
28 | |
---|
29 | sub index : Private { |
---|
30 | my ( $self, $c ) = @_; |
---|
31 | |
---|
32 | $c->res->redirect($c->uri_for('/')); |
---|
33 | } |
---|
34 | |
---|
35 | sub 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 |
---|
77 | free_choice elected_count elected_choice procedure) |
---|
78 | ) and $poll->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 $poll->commit; |
---|
84 | } elsif ($c->req->param('notcrypted')) { |
---|
85 | $poll->param(public_key => undef, private_key => undef) |
---|
86 | and $poll->commit; |
---|
87 | } |
---|
88 | }; |
---|
89 | |
---|
90 | /^(BEFORE|RUNNING)$/ and do { |
---|
91 | if (my ($upload) = $c->req->upload('votinglist')) { |
---|
92 | $poll->voting_from_file( |
---|
93 | $upload->fh, |
---|
94 | $c->req->param('delete'), |
---|
95 | ) and $poll->commit; |
---|
96 | } elsif($c->req->param('delvoting')) { |
---|
97 | $poll->delete_voting($c->req->param('delvoting')) |
---|
98 | and $poll->commit; |
---|
99 | } elsif ($c->req->param('mail')) { |
---|
100 | $poll->addupd_voting($c->req->param('mail'), $c->req->param('id')) |
---|
101 | and $poll->commit; |
---|
102 | } elsif($c->req->param('mailpasswd')) { |
---|
103 | # TODO |
---|
104 | foreach my $vkey ($poll->list_voting_no_passwd) { |
---|
105 | my $voting = $poll->voting($vkey); |
---|
106 | my $pass = $voting->gen_passwd; |
---|
107 | $c->forward( |
---|
108 | q'Epoll::View::Mail', 'render', |
---|
109 | [ 'voting_passwd.tt', { |
---|
110 | From => $poll->info->{owner}, |
---|
111 | To => $voting->info->{mail}, |
---|
112 | Subject => "Invitation à voter", |
---|
113 | mail => { |
---|
114 | voteid => $id, |
---|
115 | mail => $voting->info->{mail}, |
---|
116 | passwd => $pass, |
---|
117 | } |
---|
118 | } ] |
---|
119 | ); |
---|
120 | } |
---|
121 | } |
---|
122 | }; |
---|
123 | |
---|
124 | /^AFTER$/ and do { |
---|
125 | if ($c->req->param('mapfrom')) { |
---|
126 | $poll->map_value( |
---|
127 | $c->req->param('mapfrom'), |
---|
128 | $c->req->param('mapto') || undef, |
---|
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 | |
---|
158 | sub 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 | |
---|
175 | sub 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 | |
---|
190 | sub voting: LocalRegex('^(\w+)/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 | |
---|
205 | sub ballot: LocalRegex('^(\w+)/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 | |
---|
220 | sub date: LocalRegex('^(\w+)/date$') { |
---|
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 | |
---|
230 | $c->forward('auth') or return; |
---|
231 | $c->forward('modify_poll'); |
---|
232 | $c->stash->{page}{title} = $poll->info('label') . ': Administration, Date'; |
---|
233 | } |
---|
234 | |
---|
235 | sub privatekey : LocalRegex('^(\w+)/privatekey$') { |
---|
236 | my ($self, $c) = @_; |
---|
237 | ($c->stash->{voteid}) = @{ $c->req->snippets || [] }; |
---|
238 | my $poll = $c->model('Vote')->poll($c->stash->{voteid}); |
---|
239 | |
---|
240 | $poll->info or do { |
---|
241 | $c->res->redirect($c->uri_for('/')); |
---|
242 | return; |
---|
243 | }; |
---|
244 | $c->response->body($poll->info('private_key') || ''); |
---|
245 | } |
---|
246 | |
---|
247 | sub end : Private { |
---|
248 | my ($self, $c) = @_; |
---|
249 | if ($c->res->body) { return } |
---|
250 | elsif ($c->stash->{latex}) { $c->forward(qw/Epoll::View::Latex/) } |
---|
251 | else { $c->forward(qw/Epoll::View::TT/) } |
---|
252 | } |
---|
253 | |
---|
254 | =head1 AUTHOR |
---|
255 | |
---|
256 | Thauvin Olivier |
---|
257 | |
---|
258 | =head1 LICENSE |
---|
259 | |
---|
260 | This library is free software, you can redistribute it and/or modify |
---|
261 | it under the same terms as Perl itself or CeCILL. |
---|
262 | |
---|
263 | =cut |
---|
264 | |
---|
265 | 1; |
---|