From 5972196ee5ac7deb2291814bd1c278ce69e4b32a Mon Sep 17 00:00:00 2001 From: Olivier Thauvin Date: Tue, 19 Oct 2010 22:50:58 +0000 Subject: - ask confirm by mail on host information update to avoid spam --- lib/MGA/Mirrors/Controller/Mirrors.pm | 14 ++++++-- lib/MGA/Mirrors/Controller/Validate.pm | 54 +++++++++++++++++++++++++++++++ lib/MGA/Mirrors/DB.pm | 6 ++-- lib/MGA/Mirrors/View/Mail.pm | 59 ++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 lib/MGA/Mirrors/Controller/Validate.pm create mode 100644 lib/MGA/Mirrors/View/Mail.pm (limited to 'lib/MGA/Mirrors') 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 + +=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; -- cgit v1.2.1