diff --git a/console/web-api/lib/Email/SpoofingDemo/Web.pm b/console/web-api/lib/Email/SpoofingDemo/Web.pm index f03a086..45abdd1 100644 --- a/console/web-api/lib/Email/SpoofingDemo/Web.pm +++ b/console/web-api/lib/Email/SpoofingDemo/Web.pm @@ -6,6 +6,37 @@ use REST::Client; our $VERSION = '0.1'; +sub call_api { + my ($method, $target, $url, $body_parameters) = @_; + + my $host = config->{'api'}{$target}; + die "Invalid target: $target" unless defined $host; + + my $client = REST::Client->new(); + $client->setHost($host); + $client->setTimeout(5); + $client->addHeader('Accept' => 'application/json'); + $client->addHeader('Content-Type' => 'application/json'); + + my $body_data; + $body_data = encode_json($body_parameters) if defined $body_parameters; + + $client->request($method, $url, $body_data); + + my $status = $client->responseCode(); + if ($status =~ /^2\d\d$/) { + my $response; + if ($client->responseContent() ne '') { + $response = decode_json($client->responseContent()); + } + return ($response, $status); + } + else { + warn "API request returned $status"; + return ($client->responseContent(), $status); + } +} + get '/' => sub { template 'index' => { 'title' => 'Accueil' }; }; @@ -59,6 +90,26 @@ post '/dns/zone-edit/:zone' => sub { } redirect "/dns/zone-edit/$zone?success=$success", 303; +get '/recipient/settings' => sub { + my ($system_status, $http_code) = call_api(GET => 'recipient', '/status'); + die if $http_code ne '200'; + + template 'recipient/settings' => { + title => 'Paramètres du système destinataire', + system_status => $system_status + }; +}; + +post '/recipient/settings' => sub { + my %api_params = map { + $_ => body_parameters->{"$_-status"} ? 'enabled' : 'disabled' + } qw(spf dkim dmarc); + + my (undef, $status) = call_api(PUT => 'recipient', '/status', \%api_params); + my $success = ($status eq 200) ? 'success' : 'failure'; + redirect "/recipient/settings?success=$success", 303; +}; + }; get '/recipient/webmail' => sub { diff --git a/console/web-api/views/layouts/_nav.tt b/console/web-api/views/layouts/_nav.tt index e41e4b6..ec11acf 100644 --- a/console/web-api/views/layouts/_nav.tt +++ b/console/web-api/views/layouts/_nav.tt @@ -30,6 +30,7 @@