aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Thauvin <nanardon@mageia.org>2010-10-19 22:50:58 +0000
committerOlivier Thauvin <nanardon@mageia.org>2010-10-19 22:50:58 +0000
commit5972196ee5ac7deb2291814bd1c278ce69e4b32a (patch)
tree0e48bb471d3b7ee99b21e6ad823f8dc347b2a9e8
parent9fa18a22880f52192667c9df42d37bd75d11f1f4 (diff)
downloadmgamirrors-5972196ee5ac7deb2291814bd1c278ce69e4b32a.tar
mgamirrors-5972196ee5ac7deb2291814bd1c278ce69e4b32a.tar.gz
mgamirrors-5972196ee5ac7deb2291814bd1c278ce69e4b32a.tar.bz2
mgamirrors-5972196ee5ac7deb2291814bd1c278ce69e4b32a.tar.xz
mgamirrors-5972196ee5ac7deb2291814bd1c278ce69e4b32a.zip
- ask confirm by mail on host information update to avoid spam
-rw-r--r--lib/MGA/Mirrors/Controller/Mirrors.pm14
-rw-r--r--lib/MGA/Mirrors/Controller/Validate.pm54
-rw-r--r--lib/MGA/Mirrors/DB.pm6
-rw-r--r--lib/MGA/Mirrors/View/Mail.pm59
-rw-r--r--root/html/includes/host_information.tt59
-rw-r--r--root/html/pages/mirrors/mirror.tt3
-rw-r--r--root/html/pages/new/new_host.tt2
-rw-r--r--root/html/pages/validate/error.tt1
-rw-r--r--root/html/pages/validate/validate.tt1
-rw-r--r--root/mail/host_up_request.tt11
-rw-r--r--t/controller_Validate.t9
11 files changed, 197 insertions, 22 deletions
diff --git a/lib/MGA/Mirrors/Controller/Mirrors.pm b/lib/MGA/Mirrors/Controller/Mirrors.pm
index 73fa5ae..aa22848 100644
--- a/lib/MGA/Mirrors/Controller/Mirrors.pm
+++ b/lib/MGA/Mirrors/Controller/Mirrors.pm
@@ -31,12 +31,12 @@ sub mirror :Path :Args(1) {
my ( $self, $c, $host ) = @_;
$c->stash->{hostname} = $host;
- if ($c->req->param('hostinfo')) {
+ if ($c->req->param('hostinfo') && $c->req->param('mail')) {
my $hinfo = $c->model('Mirrors')->find_mirrors({
hostname => $host,
})->[0];
if (! $hinfo->{readonly}) {
- $c->model('Mirrors')->add_or_update_host($host,
+ my $reqid = $c->model('Mirrors')->add_host_change_request($host,
bandwidth => $c->req->param('bandwidth'),
city => $c->req->param('city'),
country => $c->req->param('country'),
@@ -44,6 +44,16 @@ sub mirror :Path :Args(1) {
latitude => $c->req->param('latitude'),
longitude => $c->req->param('longitude'),
);
+ $c->forward(
+ q'MGA::Mirrors::View::Mail', 'render',
+ [ 'host_up_request.tt', {
+ To => $c->req->param('mail'),
+ Subject => 'Update Mageia mirror request',
+ mail => {
+ reqid => $reqid,
+ }
+ } ]
+ );
}
}
diff --git a/lib/MGA/Mirrors/Controller/Validate.pm b/lib/MGA/Mirrors/Controller/Validate.pm
new file mode 100644
index 0000000..61d18f5
--- /dev/null
+++ b/lib/MGA/Mirrors/Controller/Validate.pm
@@ -0,0 +1,54 @@
+package MGA::Mirrors::Controller::Validate;
+use Moose;
+use namespace::autoclean;
+
+BEGIN {extends 'Catalyst::Controller'; }
+
+=head1 NAME
+
+MGA::Mirrors::Controller::Validate - Catalyst Controller
+
+=head1 DESCRIPTION
+
+Catalyst Controller.
+
+=head1 METHODS
+
+=cut
+
+
+=head2 index
+
+=cut
+
+sub index :Path :Args(0) {
+ my ( $self, $c ) = @_;
+
+ $c->response->body('Matched MGA::Mirrors::Controller::Validate in Validate.');
+}
+
+
+sub validate :Path :Args(1) {
+ my ( $self, $c, $reqid ) = @_;
+
+ if (my $hostname = $c->model('Mirrors')->apply_change_request($reqid)) {
+ $c->stash->{hostname} = $hostname;
+ } else {
+ $c->stash->{template} = 'validate/error.tt';
+ }
+}
+
+=head1 AUTHOR
+
+Olivier Thauvin
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/lib/MGA/Mirrors/DB.pm b/lib/MGA/Mirrors/DB.pm
index 16070dc..a7655b8 100644
--- a/lib/MGA/Mirrors/DB.pm
+++ b/lib/MGA/Mirrors/DB.pm
@@ -468,8 +468,10 @@ sub apply_change_request {
delete $res->{reqid};
$self->add_or_update_host($hostname, %{$res});
+ my $del = $self->db->prepare(q{delete from hosts_ch_req where reqid = ?});
+ $del->execute($reqid);
$self->db->commit;
- 1
+ $hostname
}
sub add_or_update_host {
@@ -478,7 +480,7 @@ sub add_or_update_host {
my (@fields, @vals);
while (my ($field, $val) = each(%info)) {
push(@fields, $field);
- push(@vals, $val || undef);
+ push(@vals, (defined($val) && $val ne '' ? $val : undef));
}
if (keys %info) {
my $upd = $self->db->prepare(sprintf(q{
diff --git a/lib/MGA/Mirrors/View/Mail.pm b/lib/MGA/Mirrors/View/Mail.pm
new file mode 100644
index 0000000..0c694be
--- /dev/null
+++ b/lib/MGA/Mirrors/View/Mail.pm
@@ -0,0 +1,59 @@
+package MGA::Mirrors::View::Mail;
+
+use strict;
+use base 'Catalyst::View::TT';
+use Mail::Mailer;
+use MGA::Mirrors;
+
+__PACKAGE__->config(
+ TEMPLATE_EXTENSION => '.tt',
+ INCLUDE_PATH => MGA::Mirrors->path_to( 'root', 'mail' ),
+);
+
+=head1 NAME
+
+MGA::Mirrors::View::TT - TT View for MGA::Mirrors
+
+=head1 DESCRIPTION
+
+TT View for MGA::Mirrors.
+
+=cut
+
+sub render {
+ my ($self, $c, $template, $args) = @_;
+
+ $ENV{MAILADDRESS} = $args->{From};
+ my $mailer = new Mail::Mailer 'smtp', Server => (MGA::Mirrors->config->{smtp} || 'localhost');
+ eval {
+ $mailer->open({
+ (map { $_ => $args->{$_} } grep { $_ !~ /^(mail)$/ } keys %{ $args || {}}),
+ 'Content-Type' => 'text/plain; charset=UTF-8; format=flowed',
+ 'Content-Transfer-Encoding' => '8bit',
+ 'X-MGA-Mirrors-version' => $MGA::Mirrors::DB::VERSION,
+ });
+ print $mailer
+ Catalyst::View::TT::render($self, $c, $template, { %{ $args->{mail} || {} }, c => $c });
+ $mailer->close;
+ };
+ if ($@) {
+ $c->stash->{mail_error} = $@;
+ }
+}
+
+=head1 SEE ALSO
+
+L<MGA::Mirrors>
+
+=head1 AUTHOR
+
+Thauvin Olivier
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself or CeCILL.
+
+=cut
+
+1;
diff --git a/root/html/includes/host_information.tt b/root/html/includes/host_information.tt
index 9093f98..b7b02f3 100644
--- a/root/html/includes/host_information.tt
+++ b/root/html/includes/host_information.tt
@@ -1,13 +1,17 @@
<!-- $Id$ -->
<div style="float: left">
-[% IF NOT host.readonly %]
+[% IF c.req.param('modify') AND NOT host.readonly %]
+ [% edit = 1 %]
+[% END %]
+
+[% IF edit %]
<form action="[% action || c.uri_for() %]" method="POST">
<input type="hidden" name="hostinfo" value="1">
[% END %]
<table border=0>
<tr><th>Country</th>
<td>
-[% IF host.readonly %]
+[% IF NOT edit %]
[% c.model('Mirrors').country_info(host.country).name | html %]
[% ELSE %]
<select name="country">
@@ -22,38 +26,38 @@
<tr><th>City</th>
<td>
-[% IF host.readonly %]
-[% host.city | html %]
-[% ELSE %]
+[% IF edit %]
<input type="text" name="city" value="[% host.city | html %]">
+[% ELSE %]
+[% host.city | html %]
[% END %]
</td>
</tr>
<tr><th>Latitude</th>
<td>
-[% IF host.readonly %]
-[% host.latitude | html %]
-[% ELSE %]
+[% IF edit %]
<input type="text" id="latitude" name="latitude" value="[% host.latitude | html %]">
+[% ELSE %]
+[% host.latitude | html %]
[% END %]
</td>
</tr>
<tr><th>Longitude</th>
<td>
-[% IF host.readonly %]
-[% host.longitude | html %]
-[% ELSE %]
+[% IF edit %]
<input type="text" id="longitude" name="longitude" value="[% host.longitude | html %]">
+[% ELSE %]
+[% host.longitude | html %]
[% END %]
</td>
</tr>
-[% IF NOT host.readonly %]
+[% IF edit %]
<tr><td colspan=2>Click on the map to update latitude and longitude</td></tr>
[% END %]
<tr><th>Synchronized from</th>
<td>
-[% IF host.readonly %]
+[% IF NOT edit %]
[% host.syncfrom | html %]
[% ELSE %]
[% FOREACH mirror = c.model('Mirrors').find_mirrors %]
@@ -74,7 +78,7 @@
<tr><th>Approximated bandwidth</th>
<td>
-[% IF host.readonly %]
+[% IF NOT edit %]
[% c.model('Mirrors').bandwidth_name(host.bandwidth) | html %]
[% ELSE %]
<select name="bandwidth">
@@ -86,11 +90,27 @@
[% END %]
</td>
</tr>
+[% IF edit AND needmail %]
+<tr>
+<td colspan=2>
+Please enter your e-mail address:<br>
+<input type="text" name="mail"><br>
+a mail will be be sent with the link to confirm<br>
+your update request.<br>
+</td>
+</tr>
+[% END %]
</table>
-[% IF NOT host.readonly %]
+[% IF edit %]
<input type="submit">
<input type="reset">
</form>
+[% ELSE %]
+[% IF NOT host.readonly %]
+<form action="[% action || c.uri_for() %]" method="POST">
+<input type="submit" name="modify" value="update information">
+</form>
+[% END %]
[% END %]
</div>
@@ -111,7 +131,7 @@
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
-[% IF NOT host.readonly %]
+[% IF edit %]
google.maps.event.addListener(map, 'click', function(event) {
recordposition(event.latLng);
});
@@ -120,6 +140,13 @@
document.getElementById("latitude").value = location.lat();
document.getElementById("longitude").value = location.lng();
}
+[% ELSE %]
+ var myLatlng = new google.maps.LatLng([% host.latitude %],[% host.longitude %]);
+ var marker = new google.maps.Marker({
+ position: myLatlng,
+ map: map,
+ title:"[% host.hostname %]"
+ });
[% END %]
}
diff --git a/root/html/pages/mirrors/mirror.tt b/root/html/pages/mirrors/mirror.tt
index dad43c2..b27cfac 100644
--- a/root/html/pages/mirrors/mirror.tt
+++ b/root/html/pages/mirrors/mirror.tt
@@ -3,7 +3,8 @@
<h2>Host information</h2>
[% INCLUDE 'host_information.tt'
- action = c.uri_for(hostname)
+ action = c.uri_for(hostname),
+ needmail = 1
%]
<h2>URLs to the distribution</h2>
diff --git a/root/html/pages/new/new_host.tt b/root/html/pages/new/new_host.tt
index d73600c..ecf51b8 100644
--- a/root/html/pages/new/new_host.tt
+++ b/root/html/pages/new/new_host.tt
@@ -11,4 +11,4 @@
<input type="hidden" name="url" value="[% uri | html %]">
<input type="hidden" name="hostinfo" value="1">
-[% INCLUDE 'host_information.tt' %]
+[% INCLUDE 'host_information.tt' edit = 1 %]
diff --git a/root/html/pages/validate/error.tt b/root/html/pages/validate/error.tt
new file mode 100644
index 0000000..a5b88c4
--- /dev/null
+++ b/root/html/pages/validate/error.tt
@@ -0,0 +1 @@
+<p>No request found</p>
diff --git a/root/html/pages/validate/validate.tt b/root/html/pages/validate/validate.tt
new file mode 100644
index 0000000..d57e10e
--- /dev/null
+++ b/root/html/pages/validate/validate.tt
@@ -0,0 +1 @@
+<p>[% hostname | html %] formation has been updated</p>
diff --git a/root/mail/host_up_request.tt b/root/mail/host_up_request.tt
new file mode 100644
index 0000000..56b4e91
--- /dev/null
+++ b/root/mail/host_up_request.tt
@@ -0,0 +1,11 @@
+Hi,
+
+You (hopefully) asked to update information about Mageia mirror.
+
+To confirm the requst please visit:
+
+[% c.uri_for('/validate', reqid) %]
+
+If this is an unwanted action, please ignore this mail.
+
+Regards.
diff --git a/t/controller_Validate.t b/t/controller_Validate.t
new file mode 100644
index 0000000..eb9b7df
--- /dev/null
+++ b/t/controller_Validate.t
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN { use_ok 'Catalyst::Test', 'MGA::Mirrors' }
+BEGIN { use_ok 'MGA::Mirrors::Controller::Validate' }
+
+ok( request('/validate')->is_success, 'Request should succeed' );
+done_testing();