aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
authorEdmund von der Burg <evdb@mysociety.org>2011-03-28 14:21:56 +0100
committerEdmund von der Burg <evdb@mysociety.org>2011-03-28 14:21:56 +0100
commit9942c5c59ab28d0441ea5ba49a7bd426f17e45c3 (patch)
tree95449ac8b6e0f7069bdb7aa73c71ad1b1fbdf85d /perllib/FixMyStreet
parent632dd7b8e84faa4bcb19401678565751677729cc (diff)
Handle problem confirmation tokens in catalyst and get all tests working
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App.pm3
-rw-r--r--perllib/FixMyStreet/App/Controller/Tokens.pm114
-rw-r--r--perllib/FixMyStreet/App/View/Web.pm15
3 files changed, 132 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm
index d73e721bf..a35ba6c01 100644
--- a/perllib/FixMyStreet/App.pm
+++ b/perllib/FixMyStreet/App.pm
@@ -76,6 +76,9 @@ __PACKAGE__->config(
# Start the application
__PACKAGE__->setup();
+# set up DB handle for old code
+FixMyStreet->configure_mysociety_dbhandle;
+
# disable debug logging unless in debaug mode
__PACKAGE__->log->disable('debug') #
unless __PACKAGE__->debug;
diff --git a/perllib/FixMyStreet/App/Controller/Tokens.pm b/perllib/FixMyStreet/App/Controller/Tokens.pm
new file mode 100644
index 000000000..6feae9e9c
--- /dev/null
+++ b/perllib/FixMyStreet/App/Controller/Tokens.pm
@@ -0,0 +1,114 @@
+package FixMyStreet::App::Controller::Tokens;
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'Catalyst::Controller'; }
+
+use FixMyStreet::Alert;
+
+=head1 NAME
+
+FixMyStreet::App::Controller::Tokens - Handle auth tokens
+
+=head1 DESCRIPTION
+
+Act on the various tokens that can be submitted.
+
+=head1 METHODS
+
+=cut
+
+=head2 confirm_problem
+
+ /[Pp]/([0-9A-Za-z]{16,18}).*$
+
+Confirm a problem - url appears in emails sent to users after they create the
+problem but are not logged in.
+
+=cut
+
+sub confirm_problem : Path('/P') {
+ my ( $self, $c, $token_code ) = @_;
+
+ my $auth_token =
+ $c->forward( 'load_auth_token', [ $token_code, 'problem' ] );
+
+ # Load the problem
+ my $problem_id = $auth_token->data;
+ my $problem = $c->model('DB::Problem')->find( { id => $problem_id } )
+ || $c->detach('token_error');
+ $c->stash->{problem} = $problem;
+
+ # check that this email or domain are not the cause of abuse. If so hide it.
+ if ( $problem->is_from_abuser ) {
+ $problem->update(
+ { state => 'hidden', lastupdate => \'ms_current_timestamp()' } );
+ $c->stash->{template} = 'tokens/abuse.html';
+ return;
+ }
+
+ # We have a problem - confirm it if needed!
+ $problem->update(
+ {
+ state => 'confirmed',
+ confirmed => \'ms_current_timestamp()',
+ lastupdate => \'ms_current_timestamp()',
+ }
+ ) if $problem->state eq 'unconfirmed';
+
+ # Subscribe problem reporter to email updates
+ my $alert_id =
+ FixMyStreet::Alert::create( $problem->user->email, 'new_updates',
+ $problem->cobrand, $problem->cobrand_data, $problem_id );
+ FixMyStreet::Alert::confirm($alert_id);
+
+ # log the problem creation user in to the site
+ $c->authenticate( { email => $problem->user->email }, 'no_password' );
+
+ return 1;
+}
+
+=head2 load_auth_token
+
+ my $auth_token =
+ $c->forward( 'load_auth_token', [ $token_code, $token_scope ] );
+
+
+Load the token if possible. If token is not found, or not valid detach to a nice
+error message.
+
+=cut
+
+sub load_auth_token : Private {
+ my ( $self, $c, $token_code, $scope ) = @_;
+
+ # clean the token of bad chars (in case of email client issues)
+ $token_code ||= '';
+ $token_code =~ s{[^a-zA-Z0-9]+}{}g;
+
+ # try to load the token
+ my $token = $c->model('DB::Token')->find(
+ {
+ scope => $scope,
+ token => $token_code,
+ }
+ ) || $c->detach('token_error');
+
+ return $token;
+}
+
+=head2 token_error
+
+Display an error page saying that there is something wrong with the token.
+
+=cut
+
+sub token_error : Private {
+ my ( $self, $c ) = @_;
+ $c->stash->{template} = 'tokens/error.html';
+ $c->detach;
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/perllib/FixMyStreet/App/View/Web.pm b/perllib/FixMyStreet/App/View/Web.pm
index e5983ea23..75ca4dd81 100644
--- a/perllib/FixMyStreet/App/View/Web.pm
+++ b/perllib/FixMyStreet/App/View/Web.pm
@@ -66,5 +66,20 @@ sub tprintf {
return sprintf $format, @args;
}
+=head2 display_crossell_advert
+
+ [% display_crossell_advert( email, name ) %]
+
+Displays a crosssell advert if permitted by the cobrand.
+
+=cut
+
+sub display_crossell_advert {
+ my ( $self, $c, $email, $name ) = @_;
+
+ return unless $c->cobrand->allow_crosssell_adverts();
+ return CrossSell::display_advert( $c->req, $email, $name );
+}
+
1;