diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2016-12-16 18:22:17 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2016-12-16 18:22:17 +0000 |
commit | 1a1b9970ff51acbe86bf74874a033a8efa41e467 (patch) | |
tree | 60e56879c0704130f0f84a4ef05eb3331430c9f8 /perllib/FixMyStreet/DB/Result/User.pm | |
parent | 44ab167797df1f8497ef67c2b9467def796f08d7 (diff) | |
parent | e7ed4a1168a1661e75d86025c284a3517d2d9340 (diff) |
Merge branch 'issues/forcouncils/21-print-reports'
Diffstat (limited to 'perllib/FixMyStreet/DB/Result/User.pm')
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index f4e5144f8..72acb6940 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -248,6 +248,15 @@ sub split_name { return { first => $first || '', last => $last || '' }; } +has body_permissions => ( + is => 'ro', + lazy => 1, + default => sub { + my $self = shift; + return [ $self->user_body_permissions->all ]; + }, +); + sub permissions { my ($self, $c, $body_id) = @_; @@ -258,9 +267,7 @@ sub permissions { return unless $self->belongs_to_body($body_id); - my @permissions = $self->user_body_permissions->search({ - body_id => $self->from_body->id, - })->all; + my @permissions = grep { $_->body_id == $self->from_body->id } @{$self->body_permissions}; return { map { $_->permission_type => 1 } @permissions }; } @@ -269,12 +276,13 @@ sub has_permission_to { return 1 if $self->is_superuser; return 0 if !$body_ids || (ref $body_ids && !@$body_ids); + $body_ids = [ $body_ids ] unless ref $body_ids; + my %body_ids = map { $_ => 1 } @$body_ids; - my $permission = $self->user_body_permissions->find({ - permission_type => $permission_type, - body_id => $body_ids, - }); - return $permission ? 1 : 0; + foreach (@{$self->body_permissions}) { + return 1 if $_->permission_type eq $permission_type && $body_ids{$_->body_id}; + } + return 0; } =head2 has_body_permission_to |