Obsolete
/
dnsmanagerv1
Archived
3
0
Fork 0

page d'admin, suppression utilisateur, quelques tests en plus

master
Philippe Pittoli 2014-01-22 17:01:34 +01:00
parent 33305b7901
commit 9c3765fdb2
11 changed files with 427 additions and 101 deletions

View File

@ -47,29 +47,34 @@ sub init {
sub auth { sub auth {
my ($self, $login, $passwd) = @_; my ($self, $login, $passwd) = @_;
return ${$self->um}->auth($login, $passwd); ${$self->um}->auth($login, $passwd);
} }
sub register_user { sub register_user {
my ($self, $login, $passwd) = @_; my ($self, $login, $passwd) = @_;
return ${$self->um}->register_user($login, $passwd); ${$self->um}->register_user($login, $passwd);
} }
# TODO # TODO
sub set_admin { sub set_admin {
my ($self, $login) = @_; my ($self, $login) = @_;
return ${$self->um}->set_admin($login); ${$self->um}->set_admin($login);
} }
sub update_passwd { sub update_passwd {
my ($self, $login, $new) = @_; my ($self, $login, $new) = @_;
my $user = ${$self->um}->get_user($login); my ($success, $user, $isadmin) = ${$self->um}->get_user($login);
return $user->passwd($new); $user->passwd($new);
} }
sub delete_user { sub delete_user {
my ($self, $login) = @_; my ($self, $login) = @_;
return ${$self->um}->delete_user($login); my ($success, @domains) = $self->get_domains($login);
if($success) {
$self->delete_domain($login, $_) foreach(@domains);
${$self->um}->delete_user($login);
}
} }
### domains ### domains
@ -77,7 +82,12 @@ sub delete_user {
# return yes or no # return yes or no
sub add_domain { sub add_domain {
my ($self, $login, $domain) = @_; my ($self, $login, $domain) = @_;
my $user = ${$self->um}->get_user($login); my ($success, $user, $isadmin) = ${$self->um}->get_user($login);
unless($success) {
return 0;
}
$user->add_domain($domain); $user->add_domain($domain);
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir); my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
@ -86,7 +96,7 @@ sub add_domain {
sub delete_domain { sub delete_domain {
my ($self, $login, $domain) = @_; my ($self, $login, $domain) = @_;
my $user = ${$self->um}->get_user($login); my ($success, $user, $isadmin) = ${$self->um}->get_user($login);
$user->delete_domain($domain); $user->delete_domain($domain);
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir); my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
$ze->del(); $ze->del();
@ -112,13 +122,13 @@ sub get_domain {
sub get_domains { sub get_domains {
my ($self, $login) = @_; my ($self, $login) = @_;
${$self->um}->get_domains($login);
my $user = ${$self->um}->get_user($login);
return $user->domains;
} }
sub activate_domain { sub get_all_domains {
my ($self, $domain) = @_; my ($self) = @_;
# % domain login
${$self->um}->get_all_domains;
} }
sub new_tmp { sub new_tmp {

View File

@ -16,53 +16,24 @@ has [qw/dbh/] => qw/is rw required 1/;
# ($success, $user, $admin) auth_user($login, $passwd) # ($success, $user, $admin) auth_user($login, $passwd)
sub auth { sub auth {
my ($self, $login, $passwd) = @_; my ($self, $login, $passwd) = @_;
my ($sth, $user, @domains); my ($sth, $success, $user, $isadmin);
$sth = $self->dbh->prepare('SELECT * FROM user WHERE login=? and passwd=?'); $sth = $self->dbh->prepare('SELECT * FROM user WHERE login=? and passwd=?');
unless ( $sth->execute($login, $passwd)) { unless ($sth->execute($login, $passwd)) {
$sth->finish(); $sth->finish();
return 0; return 0;
} }
if (my $ref = $sth->fetchrow_arrayref) { if (my $ref = $sth->fetchrow_arrayref) {
$sth = $self->dbh->prepare('SELECT domain FROM domain WHERE login=?'); # if this user exists and is auth
unless ( $sth->execute($login)) { ($success, $user, $isadmin) = $self->get_user($login);
$sth->finish(); }
return 0; else {
} $success = 0;
# get domains
#push @domains, @$_[0] while($sth->fetchrow_arrayref);
while(my $ref2 = $sth->fetchrow_arrayref) {
push @domains, @$ref2[0];
}
# si admin
if(@$ref[2]) {
# TODO : the admin module
$user = app::bdd::admin->new(login => @$ref[0]
, passwd => @$ref[1]
, dbh => $self->dbh
, domains => [@domains]);
$sth->finish();
return 1, $user, 1;
}
else {
$user = app::bdd::lambda->new(login => @$ref[0]
, passwd => @$ref[1]
, dbh => $self->dbh
, domains => [@domains]);
$sth->finish();
return 1, $user, 0;
}
} }
$sth->finish(); $sth->finish();
return 0; return ($success, $user, $isadmin);
} }
# ($success) register_user # ($success) register_user
@ -75,12 +46,14 @@ sub register_user {
return 0; return 0;
} }
# if an user already exists
if (my $ref = $sth->fetchrow_arrayref) { if (my $ref = $sth->fetchrow_arrayref) {
#say join (', ', @$ref); #say join (', ', @$ref);
$sth->finish(); $sth->finish();
return 0; return 0;
} }
# if not
$sth = $self->dbh->prepare('insert into user VALUES(?,?,?)'); $sth = $self->dbh->prepare('insert into user VALUES(?,?,?)');
unless ($sth->execute($login, $pass, 0)) { unless ($sth->execute($login, $pass, 0)) {
$sth->finish(); $sth->finish();
@ -96,6 +69,14 @@ sub delete_user {
my ($self, $login) = @_; my ($self, $login) = @_;
my $sth; my $sth;
# TODO non utile
$sth = $self->dbh->prepare('delete from domain where login=?');
unless ( $sth->execute($login) ) {
$sth->finish();
return 0;
}
$sth->finish();
$sth = $self->dbh->prepare('delete from user where login=?'); $sth = $self->dbh->prepare('delete from user where login=?');
unless ( $sth->execute($login) ) { unless ( $sth->execute($login) ) {
$sth->finish(); $sth->finish();
@ -103,12 +84,6 @@ sub delete_user {
} }
$sth->finish(); $sth->finish();
$sth = $self->dbh->prepare('delete from domain where login=?');
unless ( $sth->execute($login) ) {
$sth->finish();
return 0;
}
$sth->finish();
return 1; return 1;
} }
@ -130,9 +105,6 @@ sub get_user {
return 0; return 0;
} }
# get domains
#push @domains, @$_[0] while($sth->fetchrow_arrayref);
while(my $ref2 = $sth->fetchrow_arrayref) { while(my $ref2 = $sth->fetchrow_arrayref) {
push @domains, @$ref2[0]; push @domains, @$ref2[0];
} }
@ -151,11 +123,50 @@ sub get_user {
, dbh => $self->dbh , dbh => $self->dbh
, domains => [@domains]); , domains => [@domains]);
} }
$sth->finish(); $sth->finish();
return 1, $user; return (1, $user, @$ref[2]);
} }
$sth->finish(); $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];
}
$sth->finish();
%domains;
} }
1; 1;

40
t/auth.pl Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use v5.14;
use autodie;
use Modern::Perl;
use DNS::ZoneParse;
use Config::Simple;
use Data::Dump qw( dump );
use lib '../';
use app::app;
sub initco {
my $cfg = new Config::Simple('./config.ini');
my $app = app->new( zdir => $cfg->param('zones_path'),
dbname => $cfg->param('dbname'),
dbhost => $cfg->param('host'),
dbport => $cfg->param('port'),
dbuser => $cfg->param('user'),
dbpass => $cfg->param('passwd'),
sgbd => $cfg->param('sgbd'),
dnsapp => $cfg->param('dnsapp') );
$app->init();
return $app;
}
if( @ARGV < 2) {
say "usage : ./auth.pl login mdp";
exit 1;
}
my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth($ARGV[0], $ARGV[1]);
say "auth $auth_ok";
say "isadmin $isadmin";

11
t/config.ini Normal file
View File

@ -0,0 +1,11 @@
dbname = dnsmanager
host = localhost
# other options : see DBI module
sgbd = mysql
# default port for mysql
port = 3306
user = bla
passwd = HardPass4bla
# possible options for dnsserver : bind rndc
dnsapp = rndc
zones_path = "/srv/named/"

52
t/get_all_domains.pl Executable file
View File

@ -0,0 +1,52 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use v5.14;
use autodie;
use Modern::Perl;
use DNS::ZoneParse;
use Config::Simple;
use Data::Dump qw( dump );
use lib '../';
use app::app;
sub initco {
my $cfg = new Config::Simple('./config.ini');
my $app = app->new( zdir => $cfg->param('zones_path'),
dbname => $cfg->param('dbname'),
dbhost => $cfg->param('host'),
dbport => $cfg->param('port'),
dbuser => $cfg->param('user'),
dbpass => $cfg->param('passwd'),
sgbd => $cfg->param('sgbd'),
dnsapp => $cfg->param('dnsapp') );
$app->init();
return $app;
}
if( @ARGV != 0 ) {
say "usage : ./get_all_domains.pl";
exit 1;
}
my $app = initco();
my %domains = $app->get_all_domains();
dump(%domains);
#if( $domains ) {
# if( scalar(@$domains) != 0) {
# say join ", ", @{$domains};
# }
# else {
# say "tableau vide";
# }
#}
#else {
# say "domains undef";
#}

59
t/get_domains.pl Executable file
View File

@ -0,0 +1,59 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use v5.14;
use autodie;
use Modern::Perl;
use DNS::ZoneParse;
use Config::Simple;
use Data::Dump qw( dump );
use lib '../';
use app::app;
sub initco {
my $cfg = new Config::Simple('./config.ini');
my $app = app->new( zdir => $cfg->param('zones_path'),
dbname => $cfg->param('dbname'),
dbhost => $cfg->param('host'),
dbport => $cfg->param('port'),
dbuser => $cfg->param('user'),
dbpass => $cfg->param('passwd'),
sgbd => $cfg->param('sgbd'),
dnsapp => $cfg->param('dnsapp') );
$app->init();
return $app;
}
if( @ARGV < 2) {
say "usage : ./auth.pl login mdp";
exit 1;
}
my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth($ARGV[0], $ARGV[1]);
if($auth_ok) {
say "auth $auth_ok";
say "isadmin $isadmin";
}
my ($success, $domains) = $app->get_domains( $ARGV[0] );
say "success $success";
dump($domains);
#if( $domains ) {
# if( scalar(@$domains) != 0) {
# say join ", ", @{$domains};
# }
# else {
# say "tableau vide";
# }
#}
#else {
# say "domains undef";
#}

55
t/update_domains.pl Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/perl -w
use strict;
use warnings;
use v5.14;
use autodie;
use Modern::Perl;
use DNS::ZoneParse;
use Config::Simple;
use Data::Dump qw( dump );
use lib '../';
use app::app;
sub initco {
my $cfg = new Config::Simple('./config.ini');
my $app = app->new( zdir => $cfg->param('zones_path'),
dbname => $cfg->param('dbname'),
dbhost => $cfg->param('host'),
dbport => $cfg->param('port'),
dbuser => $cfg->param('user'),
dbpass => $cfg->param('passwd'),
sgbd => $cfg->param('sgbd'),
dnsapp => $cfg->param('dnsapp') );
$app->init();
return $app;
}
if( @ARGV < 2) {
say "usage : ./auth.pl login domain";
exit 1;
}
my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth($ARGV[0], $ARGV[1]);
if($auth_ok) {
say "auth $auth_ok";
say "isadmin $isadmin";
}
else {
say "erreur connexion";
exit(0);
}
# TODO
my $zone = $app->get_domain($ARGV[0], $ARGV[1]);
say "origin : " . $zone->origin;
my $zcontent = $zone->output() . "\nwww IN A 10.0.0.2";
$app->update_domain_raw( $ARGV[0], $zcontent , $ARGV[1] );

View File

@ -15,4 +15,4 @@ passwd = HardPass4bla
# possible options for dnsserver : bind rndc # possible options for dnsserver : bind rndc
dnsapp = rndc dnsapp = rndc
zones_path = "/srv/named/" zones_path = "/etc/bind/named/"

View File

@ -41,12 +41,20 @@ get '/' => sub {
if( session('login') ) if( session('login') )
{ {
my $app = initco(); my $app = initco();
$app->get_domains( session('login') ); my ($success, @domains) = $app->get_domains( session('login') );
template index => {
logged => true if( $success ) {
, login => session('login')
, admin => session('admin') template index => {
, domains => $app->get_domains(session('login')) }; logged => true
, login => session('login')
, admin => session('admin')
, domains => [ @domains ] };
}
else {
session->destroy;
template 'index';
}
} }
else else
{ {
@ -64,38 +72,36 @@ get '/home' => sub {
else else
{ {
my $app = initco(); my $app = initco();
my %domains = ();
my %zone_properties = ();
# my @d = @{$app->get_domains( session('login') )}; my ($success, @domains) = $app->get_domains( session('login') );
if( session('creationSuccess') || session('creationFailure') ) if( $success ) {
{
my (%zone_properties, %domains);
my $cs = session('creationSuccess'); my $cs = session('creationSuccess');
session 'creationSuccess' => '';
my $cf = session('creationFailure'); my $cf = session('creationFailure');
session 'creationFailure' => '';
my $dn = session('domainName'); my $dn = session('domainName');
session 'creationSuccess' => '';
session 'creationFailure' => '';
session 'domainName' => ''; session 'domainName' => '';
template home =>
{ 'login' => session('login'), template home => {
'domains' => $app->get_domains(session('login')), login => session('login')
'zones_domains' => \%domains, , admin => session('admin')
'zone_properties' => \%zone_properties, , domains => [@domains]
'admin' => session('admin'), , zones_domains => \%domains
'creationSuccess' => $cs, , zone_properties => \%zone_properties
'creationFailure' => $cf, , creationSuccess => $cs
'domainName' => $dn }; , creationFailure => $cf
, domainName => $dn };
} }
else else {
{ session->destroy;
template home => redirect '/ ';
{ 'login' => session('login'),
'domains' => $app->get_domains(session('login')),
'zones_domains' => \%domains,
'zone_properties' => \%zone_properties,
'admin' => session('admin') };
} }
} }
}; };
@ -162,14 +168,14 @@ prefix '/domain' => sub {
if( param('domain') =~ /^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$|^[a-zA-Z0-9]+$/ ) if( param('domain') =~ /^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$|^[a-zA-Z0-9]+$/ )
{ {
my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini'); my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini');
my $domain = param('domain').$cfg->param('tld'); my $domain = param('domain').$cfg->param('tld');
# $domain =~ s/\.{2,}/\./g; # $domain =~ s/\.{2,}/\./g;
# say "domain after sed : $domain"; # say "domain after sed : $domain";
# create domain # create domain
my $app = initco(); my $app = initco();
# Add tld # Add tld
# create domain # create domain
$app->add_domain( session('login'), $domain ); $app->add_domain( session('login'), $domain );
$creationSuccess = true; $creationSuccess = true;
@ -194,7 +200,8 @@ prefix '/domain' => sub {
# TODO tests des droits # TODO tests des droits
my $app = initco(); my $app = initco();
$app->delete_domain(session('login'), param('domain')); $app->delete_domain(session('login'), param('domain'));
redirect '/home';
redirect request->referer;
}; };
@ -202,15 +209,32 @@ prefix '/domain' => sub {
}; };
any ['get', 'post'] => '/admin' => sub { any ['get', 'post'] => '/admin' => sub {
unless( session('login') ) unless( session('login') )
{ {
redirect '/'; redirect '/';
} }
else else
{ {
template administration => { my $app = initco();
login => session('login') my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
, admin => session('admin') }; session('password') );
unless ( $auth_ok && $isadmin ) {
redirect '/ ';
return;
}
else {
my %alldomains = $app->get_all_domains;
my ($success, @domains) = $app->get_domains( session('login') );
template administration => {
login => session('login')
, admin => session('admin')
, domains => [ @domains ]
, alldomains => { %alldomains } };
}
} }
}; };
@ -221,6 +245,21 @@ prefix '/user' => sub {
redirect '/'; redirect '/';
}; };
get '/del/:user' => sub {
my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
session('password') );
if ( $auth_ok && $isadmin || session('login') eq param('user')) {
$app->delete_user(param('user'));
}
redirect request->referer;
};
post '/login' => sub { post '/login' => sub {
# Check if user is already logged # Check if user is already logged
@ -233,6 +272,7 @@ prefix '/user' => sub {
my $app = initco(); my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(param('login'), my ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
param('password') ); param('password') );
if( $auth_ok ) if( $auth_ok )
{ {
@ -242,10 +282,16 @@ prefix '/user' => sub {
session user => freeze( $user ); session user => freeze( $user );
session admin => $isadmin; session admin => $isadmin;
if( $isadmin ) {
redirect '/admin';
return;
}
} }
else else
{ {
# User login and/or password are incorrect # User login and/or password are incorrect
redirect '/';
} }
} }
} }

View File

@ -2,16 +2,57 @@
Welcome <% login %> ! Welcome <% login %> !
<a href="/user/logout">Déconnexion</a> <a href="/user/logout">Déconnexion</a>
<br />
<a href="/home">Ma page</a>
<br /> <br />
<a href='/admin'>Page d'administration</a> <a href='/admin'>Page d'administration</a>
<br /> <br />
<a href='/'>Accueil</a> <a href='/'>Accueil</a>
<br />
<% IF domains %>
Liens rapides :<br />
<% FOREACH domain IN domains %>
<a href="/domain/details/<% domain %>"><% domain %></a><br />
<% END %>
<% END %>
</div> </div>
<div id="page"> <div id="page">
<h1>Bienvenue sur la page d'administration</h1> <h1>Bienvenue sur la page d'administration</h1>
<div id="waiting_dn">
<h3>Les sites en attente</h3>
<table>
<tr>
<th>domaine</th>
<th>utilisateur</th>
<th>supprimer domaine</th>
<th>supprimer utilisateur</th>
</tr>
<% FOREACH pair IN alldomains.pairs %>
<tr>
<td><% pair.key %></td>
<td><% pair.value %></td>
<td>
<a href="/domain/del/<% pair.key %>">sup. domaine</a>
</td>
<td>
<a href="/user/del/<% pair.value %>">sup. utilisateur</a>
</td>
</tr>
<%END%>
</table>
</div>
</div> </div>

View File

@ -7,7 +7,8 @@
<a href="/user/logout">Déconnexion</a> <a href="/user/logout">Déconnexion</a>
<br /> <br />
<a href='/home'>Votre page de configuration</a><br /> <a href='/home'>Ma page</a><br />
<% IF admin == 1 %> <% IF admin == 1 %>
<a href='/admin'>Page d'administration</a> <a href='/admin'>Page d'administration</a>
<br /> <br />
@ -16,7 +17,7 @@
<% IF domains %> <% IF domains %>
Liens rapides :<br /> Liens rapides :<br />
<% FOREACH domain IN domains %> <% FOREACH domain IN domains %>
<a href="details/<% domain %>"><% domain %></a><br /> <a href="/domain/details/<% domain %>"><% domain %></a><br />
<% END %> <% END %>
<% END %> <% END %>