1 | package Vote::DB::Voting; |
---|
2 | |
---|
3 | # $Id$ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use Mail::Mailer; |
---|
8 | use base 'Vote::DB::common'; |
---|
9 | use Vote::DB::Poll; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | Vote::Model::Vote - Catalyst Model |
---|
14 | |
---|
15 | =head1 DESCRIPTION |
---|
16 | |
---|
17 | Catalyst Model. |
---|
18 | |
---|
19 | =cut |
---|
20 | |
---|
21 | sub new { |
---|
22 | my ($class, $dbstring, $key) = @_; |
---|
23 | |
---|
24 | bless { |
---|
25 | key => $key, |
---|
26 | dbstring => $dbstring, |
---|
27 | db => Vote::DB::common::_newdb($dbstring), |
---|
28 | }, $class; |
---|
29 | } |
---|
30 | |
---|
31 | sub votingkey { $_[0]->{key} } |
---|
32 | |
---|
33 | sub poll { |
---|
34 | my ($self) = @_; |
---|
35 | Vote::DB::Poll->new($self->{dbstring}, $self->info->{poll}); |
---|
36 | } |
---|
37 | |
---|
38 | sub info { |
---|
39 | my ($self) = @_; |
---|
40 | |
---|
41 | my $sth = $self->db->prepare_cached( |
---|
42 | q{ |
---|
43 | select *, voting.key as vkey from voting left join signing |
---|
44 | on signing.key = voting.key |
---|
45 | where voting.key = ? |
---|
46 | } |
---|
47 | ); |
---|
48 | $sth->execute($self->votingkey); |
---|
49 | |
---|
50 | my $res = $sth->fetchrow_hashref; |
---|
51 | $sth->finish; |
---|
52 | $res |
---|
53 | } |
---|
54 | |
---|
55 | sub auth { |
---|
56 | my ($self, $password) = @_; |
---|
57 | my $userinfo = $self->info or return; |
---|
58 | |
---|
59 | $userinfo->{passwd} or return; |
---|
60 | if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) { |
---|
61 | return 1; |
---|
62 | } else { |
---|
63 | return 0; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | sub has_sign { |
---|
68 | my ($self) = @_; |
---|
69 | |
---|
70 | my $sth = $self->db->prepare_cached( |
---|
71 | q{ |
---|
72 | select date from signing join voting |
---|
73 | on voting.key = signing.key |
---|
74 | where voting.key = ? |
---|
75 | } |
---|
76 | ); |
---|
77 | |
---|
78 | $sth->execute($self->votingkey); |
---|
79 | my $res = $sth->fetchrow_hashref; |
---|
80 | $sth->finish; |
---|
81 | return $res->{date} |
---|
82 | } |
---|
83 | |
---|
84 | sub mail_voting_passwd { |
---|
85 | my ($self, $mailinfo) = @_; |
---|
86 | |
---|
87 | my $vinfo = $self->info or return; |
---|
88 | my $voteinfo = $self->poll->info; |
---|
89 | $voteinfo->{description} ||= ""; |
---|
90 | |
---|
91 | my $passwd = Vote::DB::common::random_string(8); |
---|
92 | my $encpasswd = $self->gen_enc_passwd($passwd); |
---|
93 | |
---|
94 | my $upd_voting = $self->db->prepare_cached( |
---|
95 | q{update voting set passwd = ? where key = ?} |
---|
96 | ); |
---|
97 | |
---|
98 | $upd_voting->execute($encpasswd, $self->votingkey); |
---|
99 | |
---|
100 | my $date = $voteinfo->{dstart} && $voteinfo->{dend} |
---|
101 | ? sprintf("\n" . 'Vous pourrez voter entre le %s %s et le %s %s' . "\n", |
---|
102 | $voteinfo->{dstart}, $voteinfo->{hstart}, $voteinfo->{dend}, $voteinfo->{hend}) |
---|
103 | : ''; |
---|
104 | |
---|
105 | # TODO complete this properly: |
---|
106 | my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost'); |
---|
107 | $ENV{MAILADDRESS} = $voteinfo->{owner}; |
---|
108 | $mailer->open({ |
---|
109 | From => $voteinfo->{owner}, |
---|
110 | To => $vinfo->{mail}, |
---|
111 | Subject => 'Invitation a voter: ' . $voteinfo->{label}, |
---|
112 | 'X-Epoll-poll' => $self->poll->voteid, |
---|
113 | Vote::DB::common::mail_header(), |
---|
114 | }); |
---|
115 | print $mailer <<EOF; |
---|
116 | Vous êtes convié à participer a ce vote: |
---|
117 | |
---|
118 | -------- |
---|
119 | $voteinfo->{label} |
---|
120 | -------- |
---|
121 | $voteinfo->{description} |
---|
122 | -------- |
---|
123 | |
---|
124 | Ã l'adresse: |
---|
125 | |
---|
126 | $mailinfo->{voteurl} |
---|
127 | $date |
---|
128 | |
---|
129 | -- |
---|
130 | Votre identifiant est: $vinfo->{mail} |
---|
131 | Votre mot de passe est: $passwd |
---|
132 | |
---|
133 | Conservez précieusement ces identifiants, il ne vous seront pas retransmits. |
---|
134 | |
---|
135 | Cordialement. |
---|
136 | EOF |
---|
137 | $mailer->close or warn "couldn't send whole message: $!\n"; |
---|
138 | |
---|
139 | $self->db->commit; |
---|
140 | } |
---|
141 | |
---|
142 | =head1 AUTHOR |
---|
143 | |
---|
144 | Thauvin Olivier |
---|
145 | |
---|
146 | =head1 LICENSE |
---|
147 | |
---|
148 | This library is free software, you can redistribute it and/or modify |
---|
149 | it under the same terms as Perl itself or CeCILL. |
---|
150 | |
---|
151 | =cut |
---|
152 | |
---|
153 | 1; |
---|