[16] | 1 | package Vote::Controller::Admin; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use base 'Catalyst::Controller'; |
---|
| 6 | |
---|
| 7 | =head1 NAME |
---|
| 8 | |
---|
| 9 | Vote::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 | |
---|
[53] | 24 | sub begin : Private { |
---|
[167] | 25 | my ( $self, $c, @args ) = @_; |
---|
[53] | 26 | $c->model('Vote')->db->rollback; |
---|
| 27 | } |
---|
| 28 | |
---|
[16] | 29 | sub index : Private { |
---|
| 30 | my ( $self, $c ) = @_; |
---|
| 31 | |
---|
[49] | 32 | $c->res->redirect($c->uri_for('/')); |
---|
[16] | 33 | } |
---|
| 34 | |
---|
[167] | 35 | sub modify_poll : Private { |
---|
| 36 | my ( $self, $c) = @_; |
---|
| 37 | my $id = $c->stash->{voteid}; |
---|
| 38 | |
---|
[29] | 39 | my $vote = $c->model('Vote'); |
---|
[175] | 40 | my $poll = $c->model('Vote')->poll($id); |
---|
[75] | 41 | for ($vote->vote_status($id) || '') { |
---|
| 42 | /^BEFORE$/ and do { |
---|
| 43 | if ($c->req->param('addch')) { |
---|
[29] | 44 | $vote->vote_add_choice($id, $c->req->param('addch')) |
---|
| 45 | and $vote->db->commit; |
---|
| 46 | } elsif ($c->req->param('delch')) { |
---|
| 47 | $vote->delete_choice($c->req->param('delch')) |
---|
| 48 | and $vote->db->commit; |
---|
[169] | 49 | } elsif ($c->req->param('pollparam')) { |
---|
[61] | 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 | } |
---|
[29] | 60 | $vote->vote_param( |
---|
| 61 | $id, |
---|
| 62 | map { $_ => ($c->req->param($_) || undef) } |
---|
[169] | 63 | grep { exists($c->req->params->{$_}) } |
---|
[29] | 64 | qw(label description start end choice_count free_choice) |
---|
| 65 | ) and $vote->db->commit; |
---|
[175] | 66 | } elsif ($c->req->param('encrypted')) { |
---|
| 67 | $poll->gen_poll_keys() |
---|
| 68 | and $vote->db->commit; |
---|
| 69 | } elsif ($c->req->param('notcrypted')) { |
---|
| 70 | $poll->param(public_key => undef, private_key => undef) |
---|
| 71 | and $vote->db->commit; |
---|
[75] | 72 | } |
---|
| 73 | }; |
---|
| 74 | |
---|
| 75 | /^(BEFORE|RUNNING)$/ and do { |
---|
| 76 | if (my ($upload) = $c->req->upload('votinglist')) { |
---|
| 77 | $vote->voting_from_file( |
---|
| 78 | $id, |
---|
| 79 | $upload->fh, |
---|
| 80 | $c->req->param('delete'), |
---|
| 81 | ) and $vote->db->commit; |
---|
[29] | 82 | } elsif($c->req->param('delvoting')) { |
---|
| 83 | $vote->delete_voting($c->req->param('delvoting')) |
---|
| 84 | and $vote->db->commit; |
---|
| 85 | } elsif ($c->req->param('mail')) { |
---|
[42] | 86 | $vote->addupd_voting($id, $c->req->param('mail'), $c->req->param('id')) |
---|
[29] | 87 | and $vote->db->commit; |
---|
| 88 | } elsif($c->req->param('mailpasswd')) { |
---|
[74] | 89 | $vote->mail_passwd_ifnul($id, { |
---|
| 90 | voteurl => $c->uri_for('/ballot', $id), |
---|
| 91 | }); |
---|
[29] | 92 | } |
---|
[75] | 93 | }; |
---|
| 94 | |
---|
| 95 | /^AFTER$/ and do { |
---|
[43] | 96 | if ($c->req->param('mapfrom') && $c->req->param('mapto')) { |
---|
| 97 | $vote->vote_map_value( |
---|
| 98 | $id, |
---|
| 99 | $c->req->param('mapfrom'), |
---|
| 100 | $c->req->param('mapto'), |
---|
| 101 | ); |
---|
| 102 | } |
---|
[29] | 103 | foreach my $bid ($vote->list_vote_ballot_needvalid($id)) { |
---|
| 104 | if (!$c->req->param($bid)) { |
---|
| 105 | next; |
---|
| 106 | } elsif($c->req->param($bid) eq 'invalid') { |
---|
| 107 | $vote->mark_ballot_invalid($bid, 1); |
---|
| 108 | $vote->db->commit; |
---|
| 109 | } elsif($c->req->param($bid) eq 'valid') { |
---|
| 110 | $vote->mark_ballot_invalid($bid, 0); |
---|
| 111 | $vote->db->commit; |
---|
| 112 | } |
---|
| 113 | } |
---|
[75] | 114 | }; |
---|
[16] | 115 | } |
---|
[167] | 116 | } |
---|
[22] | 117 | |
---|
[167] | 118 | sub auth : Private { |
---|
| 119 | my ($self, $c) = @_; |
---|
| 120 | my $vote = $c->model('Vote'); |
---|
| 121 | my $password = $c->session->{vpassword} || $c->req->param('vpassword'); |
---|
| 122 | |
---|
| 123 | if (!$c->model('Vote')->auth_poll($c->stash->{voteid}, $password)) { |
---|
| 124 | $c->stash->{page}{title} = $vote->vote_info( |
---|
| 125 | $c->stash->{voteid} |
---|
| 126 | )->{label} . ': Login d\'administration'; |
---|
| 127 | $c->session->{vpassword} = undef; |
---|
| 128 | $c->stash->{template} = 'admin/login.tt'; |
---|
| 129 | return; |
---|
| 130 | } |
---|
| 131 | $c->session->{vpassword} = $password; |
---|
| 132 | return 1; |
---|
[16] | 133 | } |
---|
| 134 | |
---|
[167] | 135 | sub default : Private { |
---|
| 136 | my ( $self, $c, undef, $id ) = @_; |
---|
| 137 | $c->stash->{voteid} = $id; |
---|
| 138 | my $vote = $c->model('Vote'); |
---|
| 139 | |
---|
| 140 | $vote->vote_info($id) or do { |
---|
| 141 | $c->res->redirect($c->uri_for('/')); |
---|
| 142 | return; |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | $c->forward('auth') or return; |
---|
| 146 | $c->forward('modify_poll'); |
---|
| 147 | $c->stash->{page}{title} = $c->model('Vote')->vote_info($id)->{label} . ': Administration'; |
---|
[169] | 148 | } |
---|
[167] | 149 | |
---|
[169] | 150 | sub voting: LocalRegex('^(\d+)/voting$') { |
---|
| 151 | my ($self, $c, $id, @sub) = @_; |
---|
| 152 | ($c->stash->{voteid}) = @{ $c->req->snippets || [] }; |
---|
| 153 | my $vote = $c->model('Vote'); |
---|
[167] | 154 | |
---|
[169] | 155 | $vote->vote_info($id) or do { |
---|
| 156 | $c->res->redirect($c->uri_for('/')); |
---|
| 157 | return; |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | $c->forward('auth') or return; |
---|
| 161 | $c->forward('modify_poll'); |
---|
| 162 | $c->stash->{page}{title} = $c->model('Vote')->vote_info( |
---|
| 163 | $c->stash->{voteid} |
---|
| 164 | )->{label} . ': Administration, liste des electeurs'; |
---|
[167] | 165 | } |
---|
| 166 | |
---|
[169] | 167 | sub ballot: LocalRegex('^(\d+)/ballot$') { |
---|
[167] | 168 | my ($self, $c, $id, @sub) = @_; |
---|
| 169 | ($c->stash->{voteid}) = @{ $c->req->snippets || [] }; |
---|
| 170 | my $vote = $c->model('Vote'); |
---|
| 171 | |
---|
| 172 | $vote->vote_info($id) or do { |
---|
| 173 | $c->res->redirect($c->uri_for('/')); |
---|
| 174 | return; |
---|
| 175 | }; |
---|
| 176 | |
---|
| 177 | $c->forward('auth') or return; |
---|
| 178 | $c->forward('modify_poll'); |
---|
[169] | 179 | $c->stash->{page}{title} = $c->model('Vote')->vote_info( |
---|
| 180 | $c->stash->{voteid} |
---|
| 181 | )->{label} . ': Administration, bulletin'; |
---|
[167] | 182 | } |
---|
| 183 | |
---|
[175] | 184 | sub privatekey : LocalRegex('^(\d+)/privatekey$') { |
---|
| 185 | my ($self, $c, $id, @sub) = @_; |
---|
| 186 | ($c->stash->{voteid}) = @{ $c->req->snippets || [] }; |
---|
| 187 | my $vote = $c->model('Vote'); |
---|
| 188 | |
---|
| 189 | $vote->vote_info($id) or do { |
---|
| 190 | $c->res->redirect($c->uri_for('/')); |
---|
| 191 | return; |
---|
| 192 | }; |
---|
| 193 | $c->response->body($vote->vote_info($c->stash->{voteid})->{private_key} || ''); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | sub dec: LocalRegex('^(\d+)/dec$') { |
---|
| 197 | my ($self, $c, $id, @sub) = @_; |
---|
| 198 | ($c->stash->{voteid}) = @{ $c->req->snippets || [] }; |
---|
| 199 | my $vote = $c->model('Vote'); |
---|
| 200 | |
---|
| 201 | $vote->vote_info($id) or do { |
---|
| 202 | $c->res->redirect($c->uri_for('/')); |
---|
| 203 | return; |
---|
| 204 | }; |
---|
| 205 | |
---|
| 206 | $c->forward('auth') or return; |
---|
| 207 | $c->stash->{page}{title} = $c->model('Vote')->vote_info( |
---|
| 208 | $c->stash->{voteid} |
---|
| 209 | )->{label} . ': Administration, bulletin'; |
---|
| 210 | $c->model('Vote')->poll($c->stash->{voteid})->decrypted_ballots; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | sub end : Private { |
---|
| 214 | my ($self, $c) = @_; |
---|
| 215 | if ($c->res->body) { return } |
---|
| 216 | else { $c->forward(qw/Vote::View::TT/) } |
---|
| 217 | } |
---|
| 218 | |
---|
[16] | 219 | =head1 AUTHOR |
---|
| 220 | |
---|
| 221 | Thauvin Olivier |
---|
| 222 | |
---|
| 223 | =head1 LICENSE |
---|
| 224 | |
---|
| 225 | This library is free software, you can redistribute it and/or modify |
---|
[128] | 226 | it under the same terms as Perl itself or CeCILL. |
---|
[16] | 227 | |
---|
| 228 | =cut |
---|
| 229 | |
---|
| 230 | 1; |
---|