Skip to content

Commit

Permalink
[UK] Add ability to allow domain access to heatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Jan 6, 2022
1 parent e64a0a1 commit ff13e17
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 36 deletions.
9 changes: 6 additions & 3 deletions perllib/FixMyStreet/App/Controller/Dashboard.pm
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ sub check_page_allowed : Private {
if ($c->get_param('body')) {
$body = $c->model('DB::Body')->find({ id => $c->get_param('body') });
} else {
$body = $cobrand_body;
return $cobrand_body;
}
} elsif ($c->user->from_body && (!$cobrand_body || $cobrand_body->id == $c->user->from_body->id)) {
$body = $c->user->from_body;
} else {
$c->detach( '/page_error_404_not_found' )
} elsif ($c->action eq 'dashboard/heatmap' && $c->cobrand->feature('heatmap_dashboard_body')) {
# Heatmap might be able to be seen by more people
$body = $c->cobrand->call_hook('dashboard_body');
$body = undef unless $body && $cobrand_body && $body->id == $cobrand_body->id;
}
$c->detach('/page_error_404_not_found') unless $body;
return $body;
}

Expand Down
36 changes: 3 additions & 33 deletions perllib/FixMyStreet/Cobrand/FixMyStreet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ sub council_dashboard_hook {

$c->detach('/reports/summary') if $c->user->is_superuser;

my $body = $c->user->from_body || _user_to_body($c);
my $body = $self->dashboard_body;
if ($body) {
# Matching URL and user's email body
$c->detach('/reports/summary') if $body->id eq $c->stash->{body}->id;
Expand All @@ -231,36 +231,6 @@ sub council_dashboard_hook {
$c->res->redirect('/about/council-dashboard');
}

sub _user_to_body {
my $c = shift;
my $email = lc $c->user->email;
return _email_to_body($c, $email);
}

sub _email_to_body {
my ($c, $email) = @_;
my ($domain) = $email =~ m{ @ (.*) \z }x;

my @data = eval { FixMyStreet->path_to('../data/fixmystreet-councils.csv')->slurp };
my $body;
foreach (@data) {
chomp;
my ($d, $b) = split /\|/;
if ($d eq $domain || $d eq $email) {
$body = $b;
last;
}
}
# If we didn't find a lookup entry, default to the first part of the domain
unless ($body) {
$domain =~ s/\.gov\.uk$//;
$body = ucfirst $domain;
}

$body = $c->forward('/reports/body_find', [ $body ]);
return $body;
}

sub about_hook {
my $self = shift;
my $c = $self->{c};
Expand All @@ -269,7 +239,7 @@ sub about_hook {
$c->stash->{form_name} = $c->get_param('name') || '';
$c->stash->{email} = $c->get_param('username') || '';
if ($c->user_exists) {
my $body = $c->user->from_body || _user_to_body($c);
my $body = $self->dashboard_body;
if ($body) {
$c->stash->{body} = $body;
$c->stash->{wards} = [ { name => 'summary' } ];
Expand All @@ -279,7 +249,7 @@ sub about_hook {
if (my $email = $c->get_param('username')) {
$email = lc $email;
$email =~ s/\s+//g;
my $body = _email_to_body($c, $email);
my $body = $self->_email_to_body($email);
if ($body) {
# Send confirmation email (hopefully)
$c->stash->{template} = 'auth/general.html';
Expand Down
41 changes: 41 additions & 0 deletions perllib/FixMyStreet/Cobrand/UK.pm
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,45 @@ sub _fetch_url {
$ua->get($url)->content;
}

# UK council dashboard summary/heatmap access

sub dashboard_body {
my $self = shift;
my $c = $self->{c};
my $body = $c->user->from_body || $self->_user_to_body;
return $body;
}

sub _user_to_body {
my $self = shift;
my $c = $self->{c};
my $email = lc $c->user->email;
return $self->_email_to_body($email);
}

sub _email_to_body {
my ($self, $email) = @_;
my $c = $self->{c};
my ($domain) = $email =~ m{ @ (.*) \z }x;

my @data = eval { FixMyStreet->path_to('../data/fixmystreet-councils.csv')->slurp };
my $body;
foreach (@data) {
chomp;
my ($d, $b) = split /\|/;
if ($d eq $domain || $d eq $email) {
$body = $b;
last;
}
}
# If we didn't find a lookup entry, default to the first part of the domain
unless ($body) {
$domain =~ s/\.gov\.uk$//;
$body = ucfirst $domain;
}

$body = $c->forward('/reports/body_find', [ $body ]);
return $body;
}

1;
22 changes: 22 additions & 0 deletions t/cobrand/fixmystreet.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ my $resolver = Test::MockModule->new('Email::Valid');
$resolver->mock('address', sub { $_[1] });

my $body = $mech->create_body_ok( 2514, 'Birmingham' );
$mech->create_body_ok( 2482, 'Bromley' );

my $contact = $mech->create_contact_ok(
body_id => $body->id,
Expand Down Expand Up @@ -175,6 +176,27 @@ FixMyStreet::override_config {
};
};

subtest 'check heatmap page for cllr' => sub {
my $user = $mech->create_user_ok( 'cllr@bromley.gov.uk', name => 'Cllr Bromley' );
my $config = {
ALLOWED_COBRANDS => 'bromley',
MAPIT_URL => 'http://mapit.uk/',
COBRAND_FEATURES => { heatmap => { bromley => 1 } },
};
FixMyStreet::override_config $config, sub {
$mech->log_out_ok;
$mech->get('/dashboard/heatmap');
is $mech->res->previous->code, 302;
$mech->log_in_ok($user->email);
$mech->get('/dashboard/heatmap');
is $mech->res->code, 404;
};
$config->{COBRAND_FEATURES}{heatmap_dashboard_body}{bromley} = 1;
FixMyStreet::override_config $config, sub {
$mech->get_ok('/dashboard/heatmap');
};
};

FixMyStreet::override_config {
ALLOWED_COBRANDS => 'fixmystreet',
}, sub {
Expand Down

0 comments on commit ff13e17

Please sign in to comment.