1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
package HaCi::Authentication::imap;
use strict;
use HaCi::Log qw/warnl debug/;
use HaCi::GUI::gettext qw/_gettext/;
use Net::IMAP::Simple;
require Exporter;
our @ISA = qw(Exporter);
our $conf; *conf = \$HaCi::Conf::conf;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
sub user {
my $self = shift;
my $user = shift;
if (defined $user) {
$self->{user} = $user;
} else {
return $self->{user};
}
}
sub pass {
my $self = shift;
my $pass = shift;
if (defined $pass) {
$self->{pass} = $pass;
} else {
return $self->{pass};
}
}
sub session {
my $self = shift;
my $sess = shift;
if (defined $sess) {
$self->{sess} = $sess;
} else {
return $self->{sess};
}
}
sub authenticate {
my $self = shift;
$self->{sess}->param('authenticated', 0);
my $host = $conf->{user}->{auth}->{authparams}->{imap}->{host} || 'localhost';
my $imap = undef;
unless ($imap = Net::IMAP::Simple->new($host)) {
$conf->{var}->{authenticationError} = "Authentication failed: Cannot connect to IMAP Server: " . $Net::IMAP::Simple::errstr;
return 0;
}
unless ($imap->login($self->user(), $self->pass())) {
&HaCi::Utils::debug("Authentication Failed: " . $imap->errstr . "\n");
$conf->{var}->{authenticationError} = _gettext("Authentication failed!");
} else {
$self->{sess}->param('authenticated', 1);
$self->{sess}->param('username', $self->user());
&HaCi::Utils::debug("Sucessfully logged in!");
}
return &isAutenticated($self);
}
sub isAutenticated {
my $self = shift;
my $username = $self->{sess}->param('username') || $self->user();
if ($username) {
my $userTable = $conf->{var}->{TABLES}->{user};
unless (defined $userTable) {
warn "Cannot authenticate! DB Error (user)\n";
return 0;
}
my $user = ($userTable->search(['ID'], {'%CS%' => 1, username => $username}))[0];
if (!defined $user || !exists $user->{ID}) {
&debug("Authentication failed: User '$username' not in Database!");
$conf->{var}->{authenticationError} = _gettext("Authentication failed!");
$self->{sess}->clear('authenticated');
$self->{sess}->param('authenticated', 0);
return 0;
}
}
return (defined $self->{sess}->param('authenticated')) ? $self->{sess}->param('authenticated') : 0;
}
1;
|