2014-01-20 01:08:03 +01:00
|
|
|
|
package DNSManager;
|
|
|
|
|
|
|
|
|
|
use Dancer ':syntax';
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
use v5.14;
|
|
|
|
|
use Modern::Perl;
|
|
|
|
|
use Data::Dump qw( dump );
|
|
|
|
|
use Data::Structure::Util qw ( unbless );
|
|
|
|
|
use File::Basename;
|
|
|
|
|
use Config::Simple;
|
|
|
|
|
use Storable qw( freeze thaw );
|
|
|
|
|
$Storable::Deparse = true;
|
|
|
|
|
$Storable::Eval=true;
|
|
|
|
|
|
|
|
|
|
# Include other libs relative to current path
|
2014-01-20 22:58:30 +01:00
|
|
|
|
use Find::Lib '../../'; # TODO remove it when it won't be usefull anymore
|
2014-01-20 01:08:03 +01:00
|
|
|
|
use app::app;
|
|
|
|
|
|
|
|
|
|
our $VERSION = '0.1';
|
|
|
|
|
|
2014-01-20 22:58:30 +01:00
|
|
|
|
# eventually change place
|
|
|
|
|
sub initco {
|
|
|
|
|
|
|
|
|
|
my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
|
get '/' => sub {
|
|
|
|
|
if( session('login') )
|
|
|
|
|
{
|
2014-01-20 22:58:30 +01:00
|
|
|
|
my $app = initco();
|
2014-01-20 01:08:03 +01:00
|
|
|
|
$app->get_domains( session('login') );
|
2014-01-20 22:58:30 +01:00
|
|
|
|
template index => {
|
|
|
|
|
logged => true
|
|
|
|
|
, login => session('login')
|
|
|
|
|
, admin => session('admin')
|
|
|
|
|
, domains => $app->get_domains(session('login')) };
|
2014-01-20 01:08:03 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
template 'index';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
get '/home' => sub {
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
|
|
|
|
unless( session('login') )
|
|
|
|
|
{
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-20 22:58:30 +01:00
|
|
|
|
my $app = initco();
|
2014-01-20 01:08:03 +01:00
|
|
|
|
my %domains = ();
|
|
|
|
|
my %zone_properties = ();
|
|
|
|
|
|
|
|
|
|
my @d = @{$app->get_domains( session('login') )};
|
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
template home => {
|
2014-01-20 22:58:30 +01:00
|
|
|
|
login => session('login')
|
|
|
|
|
, domains => $app->get_domains(session('login'))
|
|
|
|
|
, zones_domains => \%domains
|
|
|
|
|
, zone_properties => \%zone_properties
|
|
|
|
|
, admin => session('admin') };
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
2014-01-20 22:58:30 +01:00
|
|
|
|
}
|
2014-01-20 01:08:03 +01:00
|
|
|
|
};
|
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
prefix '/domain' => sub {
|
2014-01-21 01:33:17 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
any ['post', 'get'] => '/update/:domain' => sub {
|
2014-01-20 22:58:30 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
# check if user is logged & if domain parameter is set
|
|
|
|
|
unless( session('login') && param('domain'))
|
|
|
|
|
{
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
my $app = initco();
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
|
|
|
|
|
param('password') );
|
2014-01-20 22:58:30 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
$app->update_domain_raw(session('login')
|
|
|
|
|
, param('zoneupdated')
|
|
|
|
|
, param('domain'));
|
2014-01-20 22:58:30 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
redirect '/domain/details/' . param('domain');
|
|
|
|
|
}
|
2014-01-21 01:33:17 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
};
|
2014-01-20 22:58:30 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
get '/details/:domain' => sub {
|
|
|
|
|
|
|
|
|
|
# check if user is logged & if domain parameter is set
|
|
|
|
|
unless( session('login') && param('domain'))
|
|
|
|
|
{
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
my $app = initco();
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
|
|
|
|
|
param('password') );
|
2014-01-21 01:33:17 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
my $zone = $app->get_domain(session('login') , param('domain'));
|
2014-01-21 01:33:17 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
template details => {
|
|
|
|
|
login => session('login')
|
|
|
|
|
, domain => param('domain')
|
|
|
|
|
, domain_zone => $zone->output() };
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
post '/add/' => sub {
|
|
|
|
|
|
|
|
|
|
# check if user is logged & if domain parameter is set
|
|
|
|
|
unless( session('login') && param('domain'))
|
|
|
|
|
{
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
2014-01-20 19:45:48 +01:00
|
|
|
|
# Add tld
|
|
|
|
|
my $domain = param('domain').$cfg->param('tld');
|
|
|
|
|
$domain =~ s/\.{2,}/\./g;
|
|
|
|
|
# create domain
|
2014-01-21 20:06:10 +01:00
|
|
|
|
my $app = initco();
|
|
|
|
|
$app->add_domain( session('login'), param('domain') );
|
|
|
|
|
redirect '/home';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
get '/del/:domain' => sub {
|
|
|
|
|
|
|
|
|
|
# TODO tests des droits
|
|
|
|
|
my $app = initco();
|
|
|
|
|
$app->delete_domain(session('login'), param('domain'));
|
|
|
|
|
redirect '/home';
|
|
|
|
|
|
|
|
|
|
};
|
2014-01-21 01:33:17 +01:00
|
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
any ['get', 'post'] => '/admin' => sub {
|
2014-01-20 01:08:03 +01:00
|
|
|
|
unless( session('login') )
|
|
|
|
|
{
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-20 22:58:30 +01:00
|
|
|
|
template administration => {
|
|
|
|
|
login => session('login')
|
|
|
|
|
, admin => session('admin') };
|
2014-01-20 01:08:03 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
prefix '/user' => sub {
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
get '/logout' => sub {
|
|
|
|
|
session->destroy;
|
2014-01-20 22:58:30 +01:00
|
|
|
|
redirect '/';
|
2014-01-21 20:06:10 +01:00
|
|
|
|
};
|
2014-01-20 22:58:30 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
post '/login' => sub {
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
# Check if user is already logged
|
|
|
|
|
unless ( session('login') )
|
2014-01-20 01:08:03 +01:00
|
|
|
|
{
|
2014-01-21 20:06:10 +01:00
|
|
|
|
# Check user login and password
|
|
|
|
|
if ( param('login') && param('password') )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
my $app = initco();
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
|
|
|
|
|
param('password') );
|
|
|
|
|
if( $auth_ok )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
session login => param('login');
|
|
|
|
|
# TODO : change password storage…
|
|
|
|
|
session password => param('password');
|
|
|
|
|
session user => freeze( $user );
|
|
|
|
|
session admin => $isadmin;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
# User login and/or password are incorrect
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-20 01:08:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
redirect '/home';
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
};
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
};
|