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-24 22:37:02 +01:00
|
|
|
|
sub get_errmsg {
|
|
|
|
|
my $err = session 'errmsg';
|
|
|
|
|
session errmsg => '';
|
|
|
|
|
$err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub get_route {
|
|
|
|
|
my $route = '/';
|
|
|
|
|
$route = request->referer if (defined request->referer);
|
|
|
|
|
$route;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
|
get '/' => sub {
|
|
|
|
|
if( session('login') )
|
|
|
|
|
{
|
2014-01-24 22:37:02 +01:00
|
|
|
|
|
2014-01-20 22:58:30 +01:00
|
|
|
|
my $app = initco();
|
2014-01-22 17:01:34 +01:00
|
|
|
|
my ($success, @domains) = $app->get_domains( session('login') );
|
|
|
|
|
|
|
|
|
|
if( $success ) {
|
|
|
|
|
|
|
|
|
|
template index => {
|
2014-01-23 00:25:33 +01:00
|
|
|
|
login => session('login')
|
2014-01-22 17:01:34 +01:00
|
|
|
|
, admin => session('admin')
|
2014-01-24 22:37:02 +01:00
|
|
|
|
, errmsg => get_errmsg
|
2014-01-22 17:01:34 +01:00
|
|
|
|
, domains => [ @domains ] };
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
session->destroy;
|
|
|
|
|
template 'index';
|
|
|
|
|
}
|
2014-01-24 22:37:02 +01:00
|
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-24 22:37:02 +01:00
|
|
|
|
|
|
|
|
|
template 'index' => {
|
|
|
|
|
errmsg => get_errmsg
|
|
|
|
|
};
|
2014-01-20 01:08:03 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
|
my ($success, @domains) = $app->get_domains( session('login') );
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
|
if( $success ) {
|
|
|
|
|
|
|
|
|
|
my (%zone_properties, %domains);
|
2014-01-21 00:16:26 +01:00
|
|
|
|
my $cs = session('creationSuccess');
|
|
|
|
|
my $dn = session('domainName');
|
2014-01-22 17:01:34 +01:00
|
|
|
|
|
2014-01-22 23:04:54 +01:00
|
|
|
|
session creationSuccess => '';
|
|
|
|
|
session domainName => '';
|
2014-01-22 17:01:34 +01:00
|
|
|
|
|
|
|
|
|
template home => {
|
2014-01-24 22:37:02 +01:00
|
|
|
|
login => session('login')
|
|
|
|
|
, admin => session('admin')
|
|
|
|
|
, domains => [@domains]
|
|
|
|
|
, zones_domains => \%domains
|
|
|
|
|
, zone_properties => \%zone_properties
|
|
|
|
|
, creationSuccess => $cs
|
|
|
|
|
, errmsg => get_errmsg
|
|
|
|
|
, domainName => $dn };
|
2014-01-22 17:01:34 +01:00
|
|
|
|
|
2014-01-21 00:16:26 +01:00
|
|
|
|
}
|
2014-01-22 17:01:34 +01:00
|
|
|
|
else {
|
|
|
|
|
session->destroy;
|
|
|
|
|
redirect '/ ';
|
2014-01-21 00:16:26 +01:00
|
|
|
|
}
|
2014-01-22 17:01:34 +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-24 22:42:34 +01:00
|
|
|
|
any ['post', 'get'] => '/updateraw/: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
|
|
|
|
|
{
|
2014-01-24 22:37:02 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
my $app = initco();
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
|
|
|
|
|
param('password') );
|
2014-01-20 22:58:30 +01:00
|
|
|
|
|
2014-01-25 14:08:01 +01:00
|
|
|
|
my $success = $app->update_domain_raw(session('login')
|
2014-01-21 20:06:10 +01:00
|
|
|
|
, 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-25 14:08:01 +01:00
|
|
|
|
}
|
2014-01-24 22:42:34 +01:00
|
|
|
|
|
2014-01-25 14:08:01 +01:00
|
|
|
|
};
|
2014-01-24 22:42:34 +01:00
|
|
|
|
|
|
|
|
|
any ['post', 'get'] => '/update/:domain' => sub {
|
|
|
|
|
unless( session('login') && param('domain') )
|
|
|
|
|
{
|
|
|
|
|
redirect '/';
|
2014-01-21 20:06:10 +01:00
|
|
|
|
}
|
2014-01-24 22:42:34 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-25 14:08:01 +01:00
|
|
|
|
my $type = param('type');
|
|
|
|
|
my $name = param('name');
|
|
|
|
|
my $value = param('value');
|
|
|
|
|
my $ttl = param('ttl');
|
|
|
|
|
my $priority = param('priority');
|
2014-01-21 01:33:17 +01:00
|
|
|
|
|
2014-01-25 14:08:01 +01:00
|
|
|
|
my $app = initco();
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
|
|
|
|
|
param('password') );
|
|
|
|
|
my $zone = $app->get_domain( session('login') , param('domain') );
|
|
|
|
|
given( $type )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
when ('A') {
|
|
|
|
|
my $a = $zone->a();
|
|
|
|
|
push( @$a, {name => $name
|
|
|
|
|
, class => "IN"
|
|
|
|
|
, host => $value
|
|
|
|
|
, ttl => $ttl
|
|
|
|
|
, ORIGIN => $zone->origin} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when ('AAAA') {
|
|
|
|
|
my $aaaa = $zone->aaaa;
|
|
|
|
|
push(@$aaaa, {name => $name
|
|
|
|
|
, class => "IN"
|
|
|
|
|
, host => $value
|
|
|
|
|
, ttl => $ttl
|
|
|
|
|
, ORIGIN => $zone->origin} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when ('CNAME') {
|
|
|
|
|
my $cname = $zone->cname;
|
|
|
|
|
push(@$cname,
|
|
|
|
|
{name => $name
|
|
|
|
|
, class => "IN"
|
|
|
|
|
, host => $value
|
|
|
|
|
, ttl => $ttl
|
|
|
|
|
, ORIGIN => $zone->origin} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when ('MX') {
|
|
|
|
|
my $mx = $zone->mx;
|
|
|
|
|
push(@$mx, { name => $name
|
|
|
|
|
, class => "IN"
|
|
|
|
|
, host => $value
|
|
|
|
|
, priority => $priority
|
|
|
|
|
, ttl => $ttl
|
|
|
|
|
, ORIGIN => $zone->origin} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when ('PTR') {
|
|
|
|
|
my $ptr = $zone->ptr;
|
|
|
|
|
push(@$ptr, {name => $name
|
|
|
|
|
, class => "IN"
|
|
|
|
|
, host => $value
|
|
|
|
|
, ttl => $ttl
|
|
|
|
|
, ORIGIN => $zone->origin} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when ('NS') {
|
|
|
|
|
my $ns = $zone->ns;
|
|
|
|
|
push(@$ns, {name => $name
|
|
|
|
|
, class => "IN"
|
|
|
|
|
, host => $value
|
|
|
|
|
, ttl => $ttl
|
|
|
|
|
, ORIGIN => $zone->origin} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$zone->new_serial();
|
|
|
|
|
my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini');
|
|
|
|
|
my $ed = app::zone::edit->new(zdir=>$cfg->param('zones_path')
|
|
|
|
|
, zname => param('domain'));
|
|
|
|
|
|
|
|
|
|
$ed->update($zone);
|
|
|
|
|
redirect '/domain/details/'.param('domain');
|
2014-01-24 22:42:34 +01:00
|
|
|
|
|
2014-01-25 14:08:01 +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-24 20:18:49 +01:00
|
|
|
|
if( param( 'expert' ) )
|
|
|
|
|
{
|
|
|
|
|
template details => {
|
|
|
|
|
login => session('login')
|
|
|
|
|
, domain => param('domain')
|
|
|
|
|
, domain_zone => $zone->output()
|
|
|
|
|
, expert => true };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
# say dump( $zone->cname());
|
|
|
|
|
template details => {
|
|
|
|
|
login => session('login')
|
|
|
|
|
, domain => param('domain')
|
|
|
|
|
, domain_zone => $zone->output()
|
|
|
|
|
, a => $zone->a()
|
|
|
|
|
, aaaa => $zone->aaaa()
|
|
|
|
|
, cname => $zone->cname()
|
|
|
|
|
, ptr => $zone->ptr()
|
|
|
|
|
, mx => $zone->mx()
|
|
|
|
|
, ns => $zone->ns() };
|
|
|
|
|
}
|
2014-01-21 20:06:10 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
post '/add/' => sub {
|
|
|
|
|
|
|
|
|
|
# check if user is logged & if domain parameter is set
|
|
|
|
|
unless( session('login') && param('domain'))
|
|
|
|
|
{
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
my $creationSuccess = '';
|
|
|
|
|
|
2014-01-21 00:16:26 +01:00
|
|
|
|
if( param('domain') =~ /^[a-zA-Z0-9]+[a-zA-Z0-9-]+[a-zA-Z0-9]+$|^[a-zA-Z0-9]+$/ )
|
|
|
|
|
{
|
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
|
my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini');
|
2014-01-21 00:16:26 +01:00
|
|
|
|
my $domain = param('domain').$cfg->param('tld');
|
2014-01-22 17:01:34 +01:00
|
|
|
|
my $app = initco();
|
2014-01-24 22:37:02 +01:00
|
|
|
|
my ($success) = $app->add_domain( session('login'), $domain );
|
|
|
|
|
|
|
|
|
|
if ($success) {
|
|
|
|
|
$creationSuccess = q{Le nom de domaine a bien été réservé ! };
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
session errmsg => q{Le nom de domaine est déjà pris.};
|
|
|
|
|
}
|
2014-01-21 00:16:26 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-24 22:37:02 +01:00
|
|
|
|
session errmsg =>
|
|
|
|
|
q{Le nom de domaine entré contient des caractères invalides};
|
2014-01-21 00:16:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
session creationSuccess => $creationSuccess;
|
|
|
|
|
session domainName => param('domain');
|
2014-01-21 20:06:10 +01:00
|
|
|
|
redirect '/home';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
get '/del/:domain' => sub {
|
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
unless( defined param('domain') ) {
|
|
|
|
|
session errmsg => q<Domaine non renseigné.>;
|
|
|
|
|
redirect get_route;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
my $app = initco();
|
2014-01-22 17:01:34 +01:00
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
# TODO tests des droits
|
|
|
|
|
if( session('login') ) {
|
2014-01-23 02:46:16 +01:00
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
if($app->delete_domain(session('login'), param('domain'))) {
|
2014-01-23 02:46:16 +01:00
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
if( request->referer =~ "/domain/details" ) {
|
|
|
|
|
redirect '/home';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
redirect request->referer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
session errmsg => "Impossible de supprimer le domaine "
|
2014-01-25 02:33:38 +01:00
|
|
|
|
. param('domain')
|
|
|
|
|
. '.' ;
|
2014-01-24 22:37:02 +01:00
|
|
|
|
redirect request->referer;
|
|
|
|
|
|
|
|
|
|
}
|
2014-01-23 02:46:16 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-21 20:06:10 +01:00
|
|
|
|
|
|
|
|
|
};
|
2014-01-21 01:33:17 +01:00
|
|
|
|
|
2014-01-25 23:35:26 +01:00
|
|
|
|
get '/del/:domain/:name/:type/:host/:ttl' => sub {
|
|
|
|
|
|
|
|
|
|
unless( session( 'user' ) and defined param('domain') ) {
|
|
|
|
|
session errmsg => q<Domaine non renseigné.>;
|
|
|
|
|
redirect get_route;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
# Load :domain and search for corresponding data
|
|
|
|
|
my $app = initco();
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
|
|
|
|
|
param('password') );
|
|
|
|
|
|
|
|
|
|
my $zone = $app->get_domain(session('login') , param('domain'));
|
|
|
|
|
my $dump = $zone->dump;
|
|
|
|
|
|
|
|
|
|
my $record;
|
|
|
|
|
my $found = false;
|
|
|
|
|
|
|
|
|
|
given( lc param('type') )
|
|
|
|
|
{
|
|
|
|
|
when ('a')
|
|
|
|
|
{
|
|
|
|
|
$record = $zone->a;
|
|
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
when ('aaaa')
|
|
|
|
|
{
|
|
|
|
|
$record = $zone->aaaa;
|
|
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
when ('cname')
|
|
|
|
|
{
|
|
|
|
|
$record = $zone->cname;
|
|
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
when ('ns')
|
|
|
|
|
{
|
|
|
|
|
$record = $zone->ns;
|
|
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
when ('mx')
|
|
|
|
|
{
|
|
|
|
|
$record = $zone->mx;
|
|
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
when ('ptr')
|
|
|
|
|
{
|
|
|
|
|
$record = $zone->ptr;
|
|
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( $found )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
foreach my $i ( 0 .. scalar @{$record}-1 )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if( $record->[$i]->{'name'} eq param('name') &&
|
|
|
|
|
$record->[$i]->{'host'} eq param('host') &&
|
|
|
|
|
$record->[$i]->{'ttl'} == param('ttl') )
|
|
|
|
|
{
|
|
|
|
|
delete $record->[$i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$app->update_domain( session('login'), $zone, param('domain') );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redirect '/domain/details/'. param('domain');
|
|
|
|
|
}
|
2014-01-20 01:08:03 +01:00
|
|
|
|
};
|
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
any ['get', 'post'] => '/admin' => sub {
|
2014-01-22 17:01:34 +01:00
|
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
|
unless( session('login') )
|
|
|
|
|
{
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-22 17:01:34 +01:00
|
|
|
|
my $app = initco();
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
|
|
|
|
|
session('password') );
|
|
|
|
|
|
|
|
|
|
unless ( $auth_ok && $isadmin ) {
|
|
|
|
|
redirect '/ ';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
my %alldomains = $app->get_all_domains;
|
2014-01-23 21:30:52 +01:00
|
|
|
|
my %allusers = $app->get_all_users;
|
2014-01-22 17:01:34 +01:00
|
|
|
|
my ($success, @domains) = $app->get_domains( session('login') );
|
|
|
|
|
|
|
|
|
|
template administration => {
|
|
|
|
|
login => session('login')
|
|
|
|
|
, admin => session('admin')
|
2014-01-24 22:37:02 +01:00
|
|
|
|
, errmsg => get_errmsg
|
2014-01-22 17:01:34 +01:00
|
|
|
|
, domains => [ @domains ]
|
2014-01-23 21:30:52 +01:00
|
|
|
|
, alldomains => { %alldomains }
|
|
|
|
|
, allusers => { %allusers } };
|
2014-01-22 17:01:34 +01:00
|
|
|
|
}
|
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-22 23:04:54 +01:00
|
|
|
|
post '/add/' => sub {
|
|
|
|
|
|
|
|
|
|
if ( param('login') && param('password') )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
my $app = initco();
|
2014-01-24 22:37:02 +01:00
|
|
|
|
my ($success) = $app->register_user(param('login')
|
|
|
|
|
, param('password'));
|
|
|
|
|
|
|
|
|
|
if($success) {
|
|
|
|
|
session login => param('login');
|
|
|
|
|
session password => param('password');
|
|
|
|
|
redirect '/home';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
session errmsg => q/Ce pseudo est déjà pris./;
|
|
|
|
|
redirect '/user/subscribe';
|
|
|
|
|
}
|
2014-01-22 23:04:54 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
session errmsg => q/login ou password non renseignés/;
|
|
|
|
|
redirect '/user/subscribe';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
get '/subscribe' => sub {
|
|
|
|
|
|
|
|
|
|
if( defined session('login') )
|
|
|
|
|
{
|
|
|
|
|
redirect '/home';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
template subscribe => {
|
2014-01-24 22:37:02 +01:00
|
|
|
|
errmsg => get_errmsg
|
2014-01-22 23:04:54 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-23 21:30:52 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
get '/unsetadmin/:user' => sub {
|
|
|
|
|
|
|
|
|
|
unless( defined param('user') )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
# TODO ajouter une erreur à afficher
|
2014-01-24 22:37:02 +01:00
|
|
|
|
session errmsg => "L'administrateur n'est pas défini." ;
|
2014-01-23 21:30:52 +01:00
|
|
|
|
redirect request->referer;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
elsif(! defined session('login') )
|
|
|
|
|
{
|
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
session errmsg => "Vous n'êtes pas connecté." ;
|
2014-01-23 21:30:52 +01:00
|
|
|
|
redirect '/';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
my $app = initco();
|
|
|
|
|
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
|
|
|
|
|
session('password') );
|
|
|
|
|
|
|
|
|
|
if ( $auth_ok && $isadmin ) {
|
|
|
|
|
$app->set_admin(param('user'), 0);
|
|
|
|
|
}
|
2014-01-24 22:37:02 +01:00
|
|
|
|
else {
|
|
|
|
|
session errmsg => q/Vous n'êtes pas administrateur./;
|
|
|
|
|
}
|
2014-01-23 21:30:52 +01:00
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
if( request->referer =~ "/admin" ) {
|
|
|
|
|
redirect request->referer;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
2014-01-23 21:30:52 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
get '/setadmin/:user' => sub {
|
|
|
|
|
|
|
|
|
|
unless( defined param('user') )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
# TODO ajouter une erreur à afficher
|
2014-01-24 22:37:02 +01:00
|
|
|
|
session errmsg => "L'utilisateur n'est pas défini." ;
|
2014-01-23 21:30:52 +01:00
|
|
|
|
redirect request->referer;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
elsif(! defined session('login') )
|
|
|
|
|
{
|
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
session errmsg => "Vous n'êtes pas connecté." ;
|
2014-01-23 21:30:52 +01:00
|
|
|
|
redirect '/';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
my $app = initco();
|
|
|
|
|
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
|
|
|
|
|
session('password') );
|
|
|
|
|
|
|
|
|
|
if ( $auth_ok && $isadmin ) {
|
|
|
|
|
$app->set_admin(param('user'), 1);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
if( request->referer =~ "/admin" ) {
|
|
|
|
|
redirect request->referer;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
2014-01-23 21:30:52 +01:00
|
|
|
|
|
|
|
|
|
}
|
2014-01-22 23:04:54 +01:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
|
get '/del/:user' => sub {
|
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
if(defined param 'user') {
|
2014-01-22 17:01:34 +01:00
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
my $app = initco();
|
|
|
|
|
|
|
|
|
|
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
|
|
|
|
|
session('password') );
|
2014-01-22 17:01:34 +01:00
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
if ( $auth_ok && $isadmin || session('login') eq param('user')) {
|
|
|
|
|
unless ( $app->delete_user(param('user'))) {
|
|
|
|
|
session errmsg => "L'utilisateur "
|
|
|
|
|
. param 'user'
|
|
|
|
|
. " n'a pas pu être supprimé.";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
session errmsg => q{Le nom d'utilisateur n'est pas renseigné.};
|
2014-01-22 17:01:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-24 22:37:02 +01:00
|
|
|
|
if( defined request->referer) {
|
|
|
|
|
redirect request->referer;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
redirect '/';
|
|
|
|
|
}
|
2014-01-22 17:01:34 +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') );
|
2014-01-22 17:01:34 +01:00
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
if( $auth_ok )
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
session login => param('login');
|
|
|
|
|
# TODO : change password storage…
|
|
|
|
|
session password => param('password');
|
|
|
|
|
session user => freeze( $user );
|
|
|
|
|
session admin => $isadmin;
|
|
|
|
|
|
2014-01-22 17:01:34 +01:00
|
|
|
|
if( $isadmin ) {
|
|
|
|
|
redirect '/admin';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-21 20:06:10 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-24 22:37:02 +01:00
|
|
|
|
|
|
|
|
|
session errmsg => q<Impossible de se connecter (login ou mot de passe incorrect).>;
|
2014-01-22 17:01:34 +01:00
|
|
|
|
redirect '/';
|
2014-01-24 22:37:02 +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
|
|
|
|
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
|
|
|
|
};
|