From e18f8a1a3a91cea967d47f12f7bc7d34e82f40f0 Mon Sep 17 00:00:00 2001 From: Julien Simonet Date: Sun, 26 Jan 2014 17:27:04 +0100 Subject: [PATCH] Moved function to delete an entry to app::app --- app/app.pm | 67 +++++++ www/lib/DNSManager.pm | 420 +++++++++++++++++++----------------------- 2 files changed, 252 insertions(+), 235 deletions(-) diff --git a/app/app.pm b/app/app.pm index a048268..cfe7167 100644 --- a/app/app.pm +++ b/app/app.pm @@ -168,4 +168,71 @@ sub 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; diff --git a/www/lib/DNSManager.pm b/www/lib/DNSManager.pm index da21a93..be655f0 100644 --- a/www/lib/DNSManager.pm +++ b/www/lib/DNSManager.pm @@ -363,305 +363,255 @@ prefix '/domain' => sub { else { # Load :domain and search for corresponding data my $app = initco(); - my ($auth_ok, $user, $isadmin) = $app->auth(param('login'), - param('password') ); + # 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') + $app->delete_entry( session('login'), + param('domain'), { - $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; - } - } + type => param('type'), + name => param('name'), + host => param('host'), + ttl => param('ttl') + }); - 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'); } + }; - 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') ) - { - redirect '/'; - } - else - { - my $app = initco(); - my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), - session('password') ); + unless ( $auth_ok && $isadmin ) { + redirect '/ '; + } + else { - unless ( $auth_ok && $isadmin ) { - redirect '/ '; - } - else { + my %alldomains = $app->get_all_domains; + my %allusers = $app->get_all_users; + my ($success, @domains) = $app->get_domains( session('login') ); - my %alldomains = $app->get_all_domains; - my %allusers = $app->get_all_users; - my ($success, @domains) = $app->get_domains( session('login') ); + template administration => { + login => 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 { - get '/logout' => sub { - session->destroy; - redirect '/'; - }; + get '/logout' => sub { + session->destroy; + redirect '/'; + }; - post '/add/' => sub { + post '/add/' => sub { - if ( param('login') && param('password') ) - { + if ( param('login') && param('password') ) + { - my $app = initco(); - my ($success) = $app->register_user(param('login') - , param('password')); + my $app = initco(); + 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'; - } + 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'; + } - } - else { - session errmsg => q/login ou password non renseignés/; - redirect '/user/subscribe'; - } + } + else { + session errmsg => q/login ou password non renseignés/; + redirect '/user/subscribe'; + } - }; + }; - get '/subscribe' => sub { + get '/subscribe' => sub { - if( defined session('login') ) - { - redirect '/home'; - } - else { + if( defined session('login') ) + { + redirect '/home'; + } + else { - template subscribe => { - errmsg => get_errmsg - }; - } + template subscribe => { + errmsg => get_errmsg + }; + } - }; + }; - get '/unsetadmin/:user' => sub { + get '/unsetadmin/:user' => sub { - unless( defined param('user') ) - { + unless( defined param('user') ) + { - # TODO ajouter une erreur à afficher - session errmsg => "L'administrateur n'est pas défini." ; - redirect request->referer; + # TODO ajouter une erreur à afficher + session errmsg => "L'administrateur n'est pas défini." ; + redirect request->referer; - } - elsif(! defined session('login') ) - { + } + elsif(! defined session('login') ) + { - session errmsg => "Vous n'êtes pas connecté." ; - redirect '/'; + session errmsg => "Vous n'êtes pas connecté." ; + redirect '/'; - } - else { + } + else { - my $app = initco(); + my $app = initco(); - my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), - session('password') ); + my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), + session('password') ); - if ( $auth_ok && $isadmin ) { - $app->set_admin(param('user'), 0); - } - else { - session errmsg => q/Vous n'êtes pas administrateur./; - } + if ( $auth_ok && $isadmin ) { + $app->set_admin(param('user'), 0); + } + else { + session errmsg => q/Vous n'êtes pas administrateur./; + } - if( request->referer =~ "/admin" ) { - redirect request->referer; - } - else { - redirect '/'; - } + if( request->referer =~ "/admin" ) { + redirect request->referer; + } + else { + redirect '/'; + } - } + } - }; + }; - get '/setadmin/:user' => sub { + get '/setadmin/:user' => sub { - unless( defined param('user') ) - { + unless( defined param('user') ) + { - # TODO ajouter une erreur à afficher - session errmsg => "L'utilisateur n'est pas défini." ; - redirect request->referer; + # TODO ajouter une erreur à afficher + session errmsg => "L'utilisateur n'est pas défini." ; + redirect request->referer; - } - elsif(! defined session('login') ) - { + } + elsif(! defined session('login') ) + { - session errmsg => "Vous n'êtes pas connecté." ; - redirect '/'; + session errmsg => "Vous n'êtes pas connecté." ; + redirect '/'; - } - else { + } + else { - my $app = initco(); + my $app = initco(); - my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), - session('password') ); + my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), + session('password') ); - if ( $auth_ok && $isadmin ) { - $app->set_admin(param('user'), 1); - } + if ( $auth_ok && $isadmin ) { + $app->set_admin(param('user'), 1); + } - if( request->referer =~ "/admin" ) { - redirect request->referer; - } - else { - redirect '/'; - } + if( request->referer =~ "/admin" ) { + redirect request->referer; + } + else { + 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'), - session('password') ); + my ($auth_ok, $user, $isadmin) = $app->auth(session('login'), + session('password') ); - 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é.}; - } + 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é.}; + } - if( defined request->referer) { - redirect request->referer; - } - else { - redirect '/'; - } + if( defined request->referer) { + redirect request->referer; + } + else { + redirect '/'; + } - }; + }; - post '/login' => sub { + post '/login' => sub { - # Check if user is already logged - unless ( session('login') ) - { - # Check user login and password - if ( param('login') && param('password') ) - { + # Check if user is already logged + unless ( session('login') ) + { + # Check user login and password + if ( param('login') && param('password') ) + { - my $app = initco(); - my ($auth_ok, $user, $isadmin) = $app->auth(param('login'), - param('password') ); + my $app = initco(); + my ($auth_ok, $user, $isadmin) = $app->auth(param('login'), + param('password') ); - if( $auth_ok ) - { + if( $auth_ok ) + { - session login => param('login'); - # TODO : change password storage… - session password => param('password'); - session user => freeze( $user ); - session admin => $isadmin; + session login => param('login'); + # TODO : change password storage… + session password => param('password'); + session user => freeze( $user ); + session admin => $isadmin; - if( $isadmin ) { - redirect '/admin'; - return; - } + if( $isadmin ) { + redirect '/admin'; + return; + } - } - else - { + } + else + { - session errmsg => q; - redirect '/'; + session errmsg => q; + redirect '/'; - } - } - } + } + } + } - redirect '/home'; - - }; + redirect '/home'; + }; };