3
0

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

This commit is contained in:
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 {
my ($self, $login, $passwd) = @_;
return ${$self->um}->auth($login, $passwd);
${$self->um}->auth($login, $passwd);
}
sub register_user {
my ($self, $login, $passwd) = @_;
return ${$self->um}->register_user($login, $passwd);
${$self->um}->register_user($login, $passwd);
}
# TODO
sub set_admin {
my ($self, $login) = @_;
return ${$self->um}->set_admin($login);
${$self->um}->set_admin($login);
}
sub update_passwd {
my ($self, $login, $new) = @_;
my $user = ${$self->um}->get_user($login);
return $user->passwd($new);
my ($success, $user, $isadmin) = ${$self->um}->get_user($login);
$user->passwd($new);
}
sub delete_user {
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
@ -77,7 +82,12 @@ sub delete_user {
# return yes or no
sub add_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);
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
@ -86,7 +96,7 @@ sub add_domain {
sub delete_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);
my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir);
$ze->del();
@ -112,13 +122,13 @@ sub get_domain {
sub get_domains {
my ($self, $login) = @_;
my $user = ${$self->um}->get_user($login);
return $user->domains;
${$self->um}->get_domains($login);
}
sub activate_domain {
my ($self, $domain) = @_;
sub get_all_domains {
my ($self) = @_;
# % domain login
${$self->um}->get_all_domains;
}
sub new_tmp {

View File

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

View File

@ -41,12 +41,20 @@ get '/' => sub {
if( session('login') )
{
my $app = initco();
$app->get_domains( session('login') );
template index => {
logged => true
, login => session('login')
, admin => session('admin')
, domains => $app->get_domains(session('login')) };
my ($success, @domains) = $app->get_domains( session('login') );
if( $success ) {
template index => {
logged => true
, login => session('login')
, admin => session('admin')
, domains => [ @domains ] };
}
else {
session->destroy;
template 'index';
}
}
else
{
@ -64,38 +72,36 @@ get '/home' => sub {
else
{
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');
session 'creationSuccess' => '';
my $cf = session('creationFailure');
session 'creationFailure' => '';
my $dn = session('domainName');
session 'creationSuccess' => '';
session 'creationFailure' => '';
session 'domainName' => '';
template home =>
{ 'login' => session('login'),
'domains' => $app->get_domains(session('login')),
'zones_domains' => \%domains,
'zone_properties' => \%zone_properties,
'admin' => session('admin'),
'creationSuccess' => $cs,
'creationFailure' => $cf,
'domainName' => $dn };
template home => {
login => session('login')
, admin => session('admin')
, domains => [@domains]
, zones_domains => \%domains
, zone_properties => \%zone_properties
, creationSuccess => $cs
, creationFailure => $cf
, domainName => $dn };
}
else
{
template home =>
{ 'login' => session('login'),
'domains' => $app->get_domains(session('login')),
'zones_domains' => \%domains,
'zone_properties' => \%zone_properties,
'admin' => session('admin') };
else {
session->destroy;
redirect '/ ';
}
}
};
@ -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]+$/ )
{
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');
# $domain =~ s/\.{2,}/\./g;
# say "domain after sed : $domain";
# create domain
my $app = initco();
# Add tld
# create domain
my $app = initco();
# Add tld
# create domain
$app->add_domain( session('login'), $domain );
$creationSuccess = true;
@ -194,7 +200,8 @@ prefix '/domain' => sub {
# TODO tests des droits
my $app = initco();
$app->delete_domain(session('login'), param('domain'));
redirect '/home';
redirect request->referer;
};
@ -202,15 +209,32 @@ prefix '/domain' => sub {
};
any ['get', 'post'] => '/admin' => sub {
unless( session('login') )
{
redirect '/';
}
else
{
template administration => {
login => session('login')
, admin => session('admin') };
my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
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 '/';
};
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 {
# Check if user is already logged
@ -233,6 +272,7 @@ prefix '/user' => sub {
my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
param('password') );
if( $auth_ok )
{
@ -242,10 +282,16 @@ prefix '/user' => sub {
session user => freeze( $user );
session admin => $isadmin;
if( $isadmin ) {
redirect '/admin';
return;
}
}
else
{
# User login and/or password are incorrect
redirect '/';
}
}
}

View File

@ -2,16 +2,57 @@
Welcome <% login %> !
<a href="/user/logout">Déconnexion</a>
<br />
<a href="/home">Ma page</a>
<br />
<a href='/admin'>Page d'administration</a>
<br />
<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 id="page">
<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>

View File

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