diff options
author | Matthew Somerville <matthew@mysociety.org> | 2020-06-17 15:56:04 +0100 |
---|---|---|
committer | M Somerville <matthew-github@dracos.co.uk> | 2020-11-11 10:29:20 +0000 |
commit | d7aca19faade6fb8f4aa47213d0f38b14cb9854a (patch) | |
tree | 41d370d7e69ad994f7f1f6be8d63cf1e692f60df /perllib | |
parent | e8127dd563cb823a369104e505795e8897d2c1ad (diff) |
[Bromley] Restrict waste lookups per day.
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bromley.pm | 14 | ||||
-rw-r--r-- | perllib/Memcached.pm | 11 |
2 files changed, 23 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index c9e85a484..263549e71 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -11,6 +11,7 @@ use Sort::Key::Natural qw(natkeysort_inplace); use Try::Tiny; use FixMyStreet::DateRange; use FixMyStreet::WorkingDays; +use Memcached; sub council_area_id { return 2482; } sub council_area { return 'Bromley'; } @@ -435,8 +436,17 @@ sub look_up_property { my $self = shift; my $uprn = shift; - my $echo = $self->feature('echo'); - $echo = Integrations::Echo->new(%$echo); + my $cfg = $self->feature('echo'); + my $echo = Integrations::Echo->new(%$cfg); + + if ($cfg->{max_per_day}) { + my $today = DateTime->today->set_time_zone(FixMyStreet->local_time_zone)->ymd; + my $ip = $self->{c}->req->address; + my $key = FixMyStreet->test_mode ? "bromley-test" : "bromley-$ip-$today"; + my $count = Memcached::increment($key, 86400) || 0; + $self->{c}->detach('/page_error_403_access_denied', []) if $count > $cfg->{max_per_day}; + } + my $result = $echo->GetPointAddress($uprn); return { id => $result->{Id}, diff --git a/perllib/Memcached.pm b/perllib/Memcached.pm index d03897e5a..099440f82 100644 --- a/perllib/Memcached.pm +++ b/perllib/Memcached.pm @@ -33,4 +33,15 @@ sub delete { instance->delete(@_); } +sub increment { + my $key = shift; + my $timeout = shift; + my $count = instance->incr($key); + if (!defined $count) { + instance->add($key, 0, $timeout); + $count = instance->incr($key); + }; + return $count; +} + 1; |