aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/New.pm46
-rw-r--r--perllib/FixMyStreet/App/Controller/Report/Update.pm39
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm8
-rw-r--r--perllib/FixMyStreet/TestMech.pm1
4 files changed, 83 insertions, 11 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm
index bbd27c666..8eabf64c1 100644
--- a/perllib/FixMyStreet/App/Controller/Report/New.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/New.pm
@@ -718,16 +718,31 @@ sub process_user : Private {
}
}
- # The user is already signed in
- if ( $c->user_exists ) {
+ # The user is already signed in. Extra bare block for 'last'.
+ if ( $c->user_exists ) { {
my $user = $c->user->obj;
+
+ if ($c->stash->{contributing_as_another_user} = $user->contributing_as('another_user', $c, $c->stash->{bodies})) {
+ # Act as if not logged in (and it will be auto-confirmed later on)
+ $report->user(undef);
+ last;
+ }
+
$user->name( Utils::trim_text( $params{name} ) ) if $params{name};
$user->phone( Utils::trim_text( $params{phone} ) );
$user->title( $user_title ) if $user_title;
$report->user( $user );
- $report->name( $user->name );
+
+ if ($c->stash->{contributing_as_body} = $user->contributing_as('body', $c, $c->stash->{bodies})) {
+ $report->name($user->from_body->name);
+ $user->name($user->from_body->name) unless $user->name;
+ $c->stash->{no_reporter_alert} = 1;
+ } else {
+ $report->name($user->name);
+ }
+
return 1;
- }
+ } }
# cleanup the email address
my $email = $params{email} ? lc $params{email} : '';
@@ -796,7 +811,11 @@ sub process_report : Private {
$report->send_questionnaire( $c->cobrand->send_questionnaires() );
# set some simple bool values (note they get inverted)
- $report->anonymous( $params{may_show_name} ? 0 : 1 );
+ if ($c->stash->{contributing_as_body}) {
+ $report->anonymous(0);
+ } else {
+ $report->anonymous( $params{may_show_name} ? 0 : 1 );
+ }
# clean up text before setting
$report->title( Utils::cleanup_text( $params{title} ) );
@@ -1075,7 +1094,10 @@ sub save_user_and_report : Private {
$report->user->insert();
}
$report->confirm();
-
+ } elsif ( $c->forward('created_as_someone_else', [ $c->stash->{bodies} ]) ) {
+ # If created on behalf of someone else, we automatically confirm it,
+ # but we don't want to update the user account
+ $report->confirm();
} elsif ( !$report->user->in_storage ) {
# User does not exist.
$c->forward('tokenize_user', [ $report ]);
@@ -1111,6 +1133,11 @@ sub save_user_and_report : Private {
return 1;
}
+sub created_as_someone_else : Private {
+ my ($self, $c, $bodies) = @_;
+ return $c->stash->{contributing_as_another_user} || $c->stash->{contributing_as_body};
+}
+
=head2 generate_map
Add the html needed to for the map to the stash.
@@ -1166,6 +1193,11 @@ sub redirect_or_confirm_creation : Private {
if ( $report->confirmed ) {
# Subscribe problem reporter to email updates
$c->forward( 'create_reporter_alert' );
+ if ($c->stash->{contributing_as_another_user}) {
+ $c->send_email( 'other-reported.txt', {
+ to => [ [ $report->user->email, $report->name ] ],
+ } );
+ }
$c->log->info($report->user->id . ' was logged in, showing confirmation page for ' . $report->id);
$c->stash->{created_report} = 'loggedin';
$c->stash->{template} = 'tokens/confirm_problem.html';
@@ -1184,6 +1216,8 @@ sub redirect_or_confirm_creation : Private {
sub create_reporter_alert : Private {
my ( $self, $c ) = @_;
+ return if $c->stash->{no_reporter_alert};
+
my $problem = $c->stash->{report};
my $alert = $c->model('DB::Alert')->find_or_create( {
user => $problem->user,
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm
index d03797f56..705e6ee99 100644
--- a/perllib/FixMyStreet/App/Controller/Report/Update.pm
+++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm
@@ -109,8 +109,15 @@ sub process_user : Private {
my $update = $c->stash->{update};
- if ( $c->user_exists ) {
+ # Extra block to use 'last'
+ if ( $c->user_exists ) { {
my $user = $c->user->obj;
+
+ if ($c->stash->{contributing_as_another_user} = $user->contributing_as('another_user', $c, $update->problem->bodies_str)) {
+ # Act as if not logged in (and it will be auto-confirmed later on)
+ last;
+ }
+
my $name = $c->get_param('name');
$user->name( Utils::trim_text( $name ) ) if $name;
my $title = $c->get_param('fms_extra_title');
@@ -119,8 +126,14 @@ sub process_user : Private {
$user->title( Utils::trim_text( $title ) );
}
$update->user( $user );
+
+ # Just in case, make sure the user will have a name
+ if ($c->stash->{contributing_as_body}) {
+ $user->name($user->from_body->name) unless $user->name;
+ }
+
return 1;
- }
+ } }
# Extract all the params to a hash to make them easier to work with
my %params = map { $_ => $c->get_param($_) }
@@ -254,16 +267,23 @@ sub process_update : Private {
Utils::cleanup_text( $params{update}, { allow_multiline => 1 } );
my $name = Utils::trim_text( $params{name} );
- my $anonymous = $c->get_param('may_show_name') ? 0 : 1;
$params{reopen} = 0 unless $c->user && $c->user->id == $c->stash->{problem}->user->id;
my $update = $c->stash->{update};
$update->text($params{update});
- $update->name($name);
+
$update->mark_fixed($params{fixed} ? 1 : 0);
$update->mark_open($params{reopen} ? 1 : 0);
- $update->anonymous($anonymous);
+
+ $c->stash->{contributing_as_body} = $c->user_exists && $c->user->contributing_as('body', $c, $update->problem->bodies_str);
+ if ($c->stash->{contributing_as_body}) {
+ $update->name($c->user->from_body->name);
+ $update->anonymous(0);
+ } else {
+ $update->name($name);
+ $update->anonymous($c->get_param('may_show_name') ? 0 : 1);
+ }
if ( $params{state} ) {
$params{state} = 'fixed - council'
@@ -431,6 +451,10 @@ sub save_update : Private {
$update->user->insert();
}
$update->confirm();
+ } elsif ( $c->forward('/report/new/created_as_someone_else', [ $update->problem->bodies_str ]) ) {
+ # If created on behalf of someone else, we automatically confirm it,
+ # but we don't want to update the user account
+ $update->confirm();
} elsif ( !$update->user->in_storage ) {
# User does not exist.
$c->forward('tokenize_user', [ $update ]);
@@ -474,6 +498,11 @@ sub redirect_or_confirm_creation : Private {
if ( $update->confirmed ) {
$c->forward( 'update_problem' );
$c->forward( 'signup_for_alerts' );
+ if ($c->stash->{contributing_as_another_user}) {
+ $c->send_email( 'other-updated.txt', {
+ to => [ [ $update->user->email, $update->name ] ],
+ } );
+ }
$c->stash->{template} = 'tokens/confirm_update.html';
return 1;
}
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index 65dd1dab1..7d1785c4b 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -243,6 +243,14 @@ sub has_permission_to {
return $permission ? 1 : undef;
}
+sub contributing_as {
+ my ($self, $other, $c, $bodies) = @_;
+ $bodies = join(',', keys %$bodies) if ref $bodies eq 'HASH';
+ $c->log->error("Bad data $bodies passed to contributing_as") if ref $bodies;
+ my $form_as = $c->get_param('form_as') || '';
+ return 1 if $form_as eq $other && $self->has_permission_to("contribute_as_$other", $bodies);
+}
+
sub adopt {
my ($self, $other) = @_;
diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm
index 5f4a6ceed..45f32f46a 100644
--- a/perllib/FixMyStreet/TestMech.pm
+++ b/perllib/FixMyStreet/TestMech.pm
@@ -165,6 +165,7 @@ sub delete_user {
}
$_->delete for $user->comments;
$_->delete for $user->admin_logs;
+ $_->delete for $user->user_body_permissions;
$user->delete;
return 1;