source: trunk/lib/Vote/DB/Voting.pm @ 193

Last change on this file since 193 was 182, checked in by nanardon, 15 years ago
  • use template for mail everywhere (#5)
  • Property svn:keywords set to Id Rev
File size: 1.9 KB
Line 
1package Vote::DB::Voting;
2
3# $Id$
4
5use strict;
6use warnings;
7use base 'Vote::DB::common';
8use Vote::DB::Poll;
9
10=head1 NAME
11
12Vote::Model::Vote - Catalyst Model
13
14=head1 DESCRIPTION
15
16Catalyst Model.
17
18=cut
19
20sub new {
21    my ($class, $dbstring, $key) = @_;
22   
23    bless {
24        key => $key,
25        dbstring => $dbstring,
26        db => Vote::DB::common::_newdb($dbstring),
27    }, $class;
28}
29
30sub votingkey { $_[0]->{key} }
31
32sub poll {
33    my ($self) = @_;
34    Vote::DB::Poll->new($self->{dbstring}, $self->info->{poll});
35}
36
37sub info {
38    my ($self) = @_;
39
40    my $sth = $self->db->prepare_cached(
41        q{
42        select *, voting.key as vkey from voting left join signing
43        on signing.key = voting.key
44        where voting.key = ?
45        }
46    );
47    $sth->execute($self->votingkey);
48
49    my $res = $sth->fetchrow_hashref;
50    $sth->finish;
51    $res
52}
53
54sub auth {
55    my ($self, $password) = @_;
56    my $userinfo = $self->info or return;
57
58    $userinfo->{passwd} or return;
59    if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) {
60        return 1;
61    } else {
62        return 0;
63    }
64}
65
66sub has_sign {
67    my ($self) = @_;
68
69    my $sth = $self->db->prepare_cached(
70        q{
71        select date from signing join voting
72        on voting.key = signing.key
73        where voting.key = ?
74        }
75    );
76
77    $sth->execute($self->votingkey);
78    my $res = $sth->fetchrow_hashref;
79    $sth->finish;
80    return $res->{date}
81}
82
83sub gen_passwd {
84    my ($self) = @_;
85    my $passwd = Vote::DB::common::random_string(8);
86    my $encpasswd = $self->gen_enc_passwd($passwd);
87    my $upd_voting = $self->db->prepare_cached(
88        q{update voting set passwd = ? where key = ?}
89    );
90
91    $upd_voting->execute($encpasswd, $self->votingkey);
92    $self->db->commit;
93    $passwd
94}
95
96=head1 AUTHOR
97
98Thauvin Olivier
99
100=head1 LICENSE
101
102This library is free software, you can redistribute it and/or modify
103it under the same terms as Perl itself or CeCILL.
104
105=cut
106
1071;
Note: See TracBrowser for help on using the repository browser.