source: trunk/lib/Epoll/DB/Voting.pm @ 317

Last change on this file since 317 was 317, checked in by nanardon, 14 years ago
  • use extern_uid existance to figure if the voters need an external authentication
  • Property svn:keywords set to Id Rev
File size: 2.5 KB
Line 
1package Epoll::DB::Voting;
2
3# $Id$
4
5use strict;
6use warnings;
7use base 'Epoll::DB::common';
8use Epoll::DB::Poll;
9
10=head1 NAME
11
12Epoll::DB::Voting - 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 => Epoll::DB::common::_newdb($dbstring),
27    }, $class;
28}
29
30sub votingkey { $_[0]->{key} }
31
32sub poll {
33    my ($self) = @_;
34    Epoll::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        left join extern_auth on voting.extern_auth = extern_auth.id
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
55sub auth {
56    my ($self, $password) = @_;
57    my $userinfo = $self->info or return;
58
59        warn "ee";
60    if ($userinfo->{extern_uid}) {
61        warn "rr";
62        my $handle = $self->stored_import_handle($userinfo->{extern_auth})
63            or return;
64        return $handle->authenticate($userinfo->{mail}, $userinfo->{extern_uid}, $password);
65    } else {
66        $userinfo->{passwd} or return;
67        if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) {
68            return 1;
69        } else {
70            return 0;
71        }
72    }
73}
74
75sub has_sign {
76    my ($self) = @_;
77
78    my $sth = $self->db->prepare_cached(
79        q{
80        select date from signing join voting
81        on voting.key = signing.key
82        where voting.key = ?
83        }
84    );
85
86    $sth->execute($self->votingkey);
87    my $res = $sth->fetchrow_hashref;
88    $sth->finish;
89    return $res->{date}
90}
91
92sub gen_passwd {
93    my ($self) = @_;
94    warn "gotcha";
95    my $info = $self->info;
96    my $passwd;
97    my $upd_voting = $self->db->prepare_cached(
98        q{update voting set passwd = ? where key = ?}
99    );
100    if ($info->{extern_auth}) {
101        $upd_voting->execute('EXTERN_AUTH', $self->votingkey);
102        $passwd = $info->{desc} || $info->{auth_type};
103    } else {
104        $passwd = Epoll::DB::common::random_string(8);
105        my $encpasswd = $self->gen_enc_passwd($passwd);
106
107        $upd_voting->execute($encpasswd, $self->votingkey);
108    }
109    $self->commit;
110    $passwd
111}
112
113=head1 AUTHOR
114
115Thauvin Olivier
116
117=head1 LICENSE
118
119This library is free software, you can redistribute it and/or modify
120it under the same terms as Perl itself or CeCILL.
121
122=cut
123
1241;
Note: See TracBrowser for help on using the repository browser.