2014-01-20 01:08:03 +01:00
|
|
|
use Modern::Perl;
|
|
|
|
use autodie;
|
|
|
|
use v5.14;
|
|
|
|
use DBI;
|
|
|
|
|
|
|
|
use lib '../';
|
|
|
|
use app::bdd::lambda;
|
|
|
|
use app::bdd::admin;
|
|
|
|
use app::zone::interface;
|
|
|
|
|
|
|
|
package app::bdd::management;
|
|
|
|
use Moose;
|
|
|
|
|
|
|
|
has [qw/dbh/] => qw/is rw required 1/;
|
|
|
|
|
|
|
|
# ($success, $user, $admin) auth_user($login, $passwd)
|
|
|
|
sub auth {
|
|
|
|
my ($self, $login, $passwd) = @_;
|
2014-01-22 17:01:34 +01:00
|
|
|
my ($sth, $success, $user, $isadmin);
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
|
|
$sth = $self->dbh->prepare('SELECT * FROM user WHERE login=? and passwd=?');
|
2014-01-22 17:01:34 +01:00
|
|
|
unless ($sth->execute($login, $passwd)) {
|
2014-01-20 01:08:03 +01:00
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (my $ref = $sth->fetchrow_arrayref) {
|
2014-01-22 17:01:34 +01:00
|
|
|
# if this user exists and is auth
|
|
|
|
($success, $user, $isadmin) = $self->get_user($login);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$success = 0;
|
2014-01-20 01:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$sth->finish();
|
2014-01-22 17:01:34 +01:00
|
|
|
return ($success, $user, $isadmin);
|
2014-01-20 01:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# ($success) register_user
|
|
|
|
sub register_user {
|
|
|
|
my ($self, $login, $pass) = @_;
|
|
|
|
|
|
|
|
my $sth = $self->dbh->prepare('select * from user where login=?');
|
|
|
|
unless ( $sth->execute($login) ) {
|
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
# if an user already exists
|
2014-01-20 01:08:03 +01:00
|
|
|
if (my $ref = $sth->fetchrow_arrayref) {
|
|
|
|
#say join (', ', @$ref);
|
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
# if not
|
2014-01-20 01:08:03 +01:00
|
|
|
$sth = $self->dbh->prepare('insert into user VALUES(?,?,?)');
|
|
|
|
unless ($sth->execute($login, $pass, 0)) {
|
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$sth->finish();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# ($success) delete_user
|
|
|
|
sub delete_user {
|
|
|
|
my ($self, $login) = @_;
|
|
|
|
my $sth;
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
# TODO non utile
|
|
|
|
$sth = $self->dbh->prepare('delete from domain where login=?');
|
2014-01-20 01:08:03 +01:00
|
|
|
unless ( $sth->execute($login) ) {
|
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$sth->finish();
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
$sth = $self->dbh->prepare('delete from user where login=?');
|
2014-01-20 01:08:03 +01:00
|
|
|
unless ( $sth->execute($login) ) {
|
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$sth->finish();
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_user {
|
|
|
|
my ($self, $login) = @_;
|
|
|
|
my ($sth, $user, @domains);
|
|
|
|
|
|
|
|
$sth = $self->dbh->prepare('SELECT * FROM user WHERE login=?');
|
|
|
|
unless ( $sth->execute($login)) {
|
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (my $ref = $sth->fetchrow_arrayref) {
|
|
|
|
$sth = $self->dbh->prepare('SELECT domain FROM domain WHERE login=?');
|
|
|
|
unless ( $sth->execute($login)) {
|
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(my $ref2 = $sth->fetchrow_arrayref) {
|
|
|
|
push @domains, @$ref2[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
# si admin
|
|
|
|
if(@$ref[2]) {
|
|
|
|
$user = app::bdd::admin->new(login => @$ref[0]
|
|
|
|
, passwd => @$ref[1]
|
|
|
|
, dbh => $self->dbh
|
|
|
|
, domains => [@domains]);
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$user = app::bdd::lambda->new(login => @$ref[0]
|
|
|
|
, passwd => @$ref[1]
|
|
|
|
, dbh => $self->dbh
|
|
|
|
, domains => [@domains]);
|
|
|
|
}
|
2014-01-22 17:01:34 +01:00
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
$sth->finish();
|
2014-01-22 17:01:34 +01:00
|
|
|
return (1, $user, @$ref[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sth->finish();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_domains {
|
|
|
|
my ($self, $login) = @_;
|
|
|
|
my ($sth, @domains);
|
|
|
|
|
|
|
|
$sth = $self->dbh->prepare('SELECT domain FROM domain where login=?');
|
|
|
|
unless ($sth->execute($login)) {
|
|
|
|
$sth->finish();
|
|
|
|
return (0, @domains);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(my $ref = $sth->fetchrow_arrayref) {
|
|
|
|
push @domains, @$ref[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$sth->finish();
|
|
|
|
|
|
|
|
return (1, @domains);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_all_domains {
|
|
|
|
my ($self) = @_;
|
|
|
|
my ($sth, $user, %domains);
|
|
|
|
|
|
|
|
$sth = $self->dbh->prepare('SELECT domain, login FROM domain');
|
|
|
|
unless ( $sth->execute()) {
|
|
|
|
$sth->finish();
|
|
|
|
undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( my $ref = $sth->fetchrow_arrayref) {
|
|
|
|
$domains{@$ref[0]} = @$ref[1];
|
2014-01-20 01:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$sth->finish();
|
2014-01-22 17:01:34 +01:00
|
|
|
%domains;
|
2014-01-20 01:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|