Obsolete
/
dnsmanagerv1
Archived
3
0
Fork 0

Moved function to delete an entry to app::app

master
Julien Simonet 2014-01-26 17:27:04 +01:00
parent 59cf727e28
commit e18f8a1a3a
2 changed files with 252 additions and 235 deletions

View File

@ -168,4 +168,71 @@ sub new_tmp {
$ze->new_tmp(); $ze->new_tmp();
} }
sub delete_entry {
my ($self, $login, $domain, $entryToDelete) = @_;
my $name = $entryToDelete->{'name'};
my $type = $entryToDelete->{'type'};
my $ttl = $entryToDelete->{'ttl'};
my $host = $entryToDelete->{'host'};
my $zone = $self->get_domain($login , $domain);
my $dump = $zone->dump;
my $record;
my $found = 0;
given( lc $type )
{
when ('a')
{
$record = $zone->a;
$found = 1;
}
when ('aaaa')
{
$record = $zone->aaaa;
$found = 1;
}
when ('cname')
{
$record = $zone->cname;
$found = 1;
}
when ('ns')
{
$record = $zone->ns;
$found = 1;
}
when ('mx')
{
$record = $zone->mx;
$found = 1;
}
when ('ptr')
{
$record = $zone->ptr;
$found = 1;
}
}
if( $found )
{
foreach my $i ( 0 .. scalar @{$record}-1 )
{
delete $record->[$i]
if( $record->[$i]->{'name'} eq $name &&
$record->[$i]->{'host'} eq $host &&
$record->[$i]->{'ttl'} == $ttl );
}
}
$self->update_domain( $login, $zone, $domain );
}
1; 1;

View File

@ -363,305 +363,255 @@ prefix '/domain' => sub {
else { else {
# Load :domain and search for corresponding data # Load :domain and search for corresponding data
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') );
my $zone = $app->get_domain(session('login') , param('domain')); $app->delete_entry( session('login'),
my $dump = $zone->dump; param('domain'),
my $record;
my $found = false;
given( lc param('type') )
{
when ('a')
{ {
$record = $zone->a; type => param('type'),
$found = true; name => param('name'),
} host => param('host'),
when ('aaaa') ttl => param('ttl')
{ });
$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 ) redirect '/domain/details/'. param('domain');
{
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'); any ['get', 'post'] => '/admin' => sub {
}
};
any ['get', 'post'] => '/admin' => sub { unless( session('login') )
{
redirect '/';
}
else
{
my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
session('password') );
unless( session('login') ) unless ( $auth_ok && $isadmin ) {
{ redirect '/ ';
redirect '/'; }
} else {
else
{
my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
session('password') );
unless ( $auth_ok && $isadmin ) { my %alldomains = $app->get_all_domains;
redirect '/ '; my %allusers = $app->get_all_users;
} my ($success, @domains) = $app->get_domains( session('login') );
else {
my %alldomains = $app->get_all_domains; template administration => {
my %allusers = $app->get_all_users; login => session('login')
my ($success, @domains) = $app->get_domains( session('login') ); , admin => session('admin')
, errmsg => get_errmsg
, domains => [ @domains ]
, alldomains => { %alldomains }
, allusers => { %allusers } };
}
}
};
template administration => {
login => session('login')
, admin => session('admin')
, errmsg => get_errmsg
, domains => [ @domains ]
, alldomains => { %alldomains }
, allusers => { %allusers } };
}
}
}; };
prefix '/user' => sub { prefix '/user' => sub {
get '/logout' => sub { get '/logout' => sub {
session->destroy; session->destroy;
redirect '/'; redirect '/';
}; };
post '/add/' => sub { post '/add/' => sub {
if ( param('login') && param('password') ) if ( param('login') && param('password') )
{ {
my $app = initco(); my $app = initco();
my ($success) = $app->register_user(param('login') my ($success) = $app->register_user(param('login')
, param('password')); , param('password'));
if($success) { if($success) {
session login => param('login'); session login => param('login');
session password => param('password'); session password => param('password');
redirect '/home'; redirect '/home';
} }
else { else {
session errmsg => q/Ce pseudo est déjà pris./; session errmsg => q/Ce pseudo est déjà pris./;
redirect '/user/subscribe'; redirect '/user/subscribe';
} }
} }
else { else {
session errmsg => q/login ou password non renseignés/; session errmsg => q/login ou password non renseignés/;
redirect '/user/subscribe'; redirect '/user/subscribe';
} }
}; };
get '/subscribe' => sub { get '/subscribe' => sub {
if( defined session('login') ) if( defined session('login') )
{ {
redirect '/home'; redirect '/home';
} }
else { else {
template subscribe => { template subscribe => {
errmsg => get_errmsg errmsg => get_errmsg
}; };
} }
}; };
get '/unsetadmin/:user' => sub { get '/unsetadmin/:user' => sub {
unless( defined param('user') ) unless( defined param('user') )
{ {
# TODO ajouter une erreur à afficher # TODO ajouter une erreur à afficher
session errmsg => "L'administrateur n'est pas défini." ; session errmsg => "L'administrateur n'est pas défini." ;
redirect request->referer; redirect request->referer;
} }
elsif(! defined session('login') ) elsif(! defined session('login') )
{ {
session errmsg => "Vous n'êtes pas connecté." ; session errmsg => "Vous n'êtes pas connecté." ;
redirect '/'; redirect '/';
} }
else { else {
my $app = initco(); my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
session('password') ); session('password') );
if ( $auth_ok && $isadmin ) { if ( $auth_ok && $isadmin ) {
$app->set_admin(param('user'), 0); $app->set_admin(param('user'), 0);
} }
else { else {
session errmsg => q/Vous n'êtes pas administrateur./; session errmsg => q/Vous n'êtes pas administrateur./;
} }
if( request->referer =~ "/admin" ) { if( request->referer =~ "/admin" ) {
redirect request->referer; redirect request->referer;
} }
else { else {
redirect '/'; redirect '/';
} }
} }
}; };
get '/setadmin/:user' => sub { get '/setadmin/:user' => sub {
unless( defined param('user') ) unless( defined param('user') )
{ {
# TODO ajouter une erreur à afficher # TODO ajouter une erreur à afficher
session errmsg => "L'utilisateur n'est pas défini." ; session errmsg => "L'utilisateur n'est pas défini." ;
redirect request->referer; redirect request->referer;
} }
elsif(! defined session('login') ) elsif(! defined session('login') )
{ {
session errmsg => "Vous n'êtes pas connecté." ; session errmsg => "Vous n'êtes pas connecté." ;
redirect '/'; redirect '/';
} }
else { else {
my $app = initco(); my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
session('password') ); session('password') );
if ( $auth_ok && $isadmin ) { if ( $auth_ok && $isadmin ) {
$app->set_admin(param('user'), 1); $app->set_admin(param('user'), 1);
} }
if( request->referer =~ "/admin" ) { if( request->referer =~ "/admin" ) {
redirect request->referer; redirect request->referer;
} }
else { else {
redirect '/'; redirect '/';
} }
} }
}; };
get '/del/:user' => sub { get '/del/:user' => sub {
if(defined param 'user') { if(defined param 'user') {
my $app = initco(); my $app = initco();
my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), my ($auth_ok, $user, $isadmin) = $app->auth(session('login'),
session('password') ); session('password') );
if ( $auth_ok && $isadmin || session('login') eq param('user')) { if ( $auth_ok && $isadmin || session('login') eq param('user')) {
unless ( $app->delete_user(param('user'))) { unless ( $app->delete_user(param('user'))) {
session errmsg => "L'utilisateur " session errmsg => "L'utilisateur "
. param 'user' . param 'user'
. " n'a pas pu être supprimé."; . " n'a pas pu être supprimé.";
} }
} }
} }
else { else {
session errmsg => q{Le nom d'utilisateur n'est pas renseigné.}; session errmsg => q{Le nom d'utilisateur n'est pas renseigné.};
} }
if( defined request->referer) { if( defined request->referer) {
redirect request->referer; redirect request->referer;
} }
else { else {
redirect '/'; redirect '/';
} }
}; };
post '/login' => sub { post '/login' => sub {
# Check if user is already logged # Check if user is already logged
unless ( session('login') ) unless ( session('login') )
{ {
# Check user login and password # Check user login and password
if ( param('login') && param('password') ) if ( param('login') && param('password') )
{ {
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 )
{ {
session login => param('login'); session login => param('login');
# TODO : change password storage… # TODO : change password storage…
session password => param('password'); session password => param('password');
session user => freeze( $user ); session user => freeze( $user );
session admin => $isadmin; session admin => $isadmin;
if( $isadmin ) { if( $isadmin ) {
redirect '/admin'; redirect '/admin';
return; return;
} }
} }
else else
{ {
session errmsg => q<Impossible de se connecter (login ou mot de passe incorrect).>; session errmsg => q<Impossible de se connecter (login ou mot de passe incorrect).>;
redirect '/'; redirect '/';
} }
} }
} }
redirect '/home'; redirect '/home';
};
};
}; };