aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin.pm39
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm6
2 files changed, 42 insertions, 3 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm
index 17425ad77..63414b555 100644
--- a/perllib/FixMyStreet/App/Controller/Admin.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin.pm
@@ -1134,6 +1134,7 @@ sub user_edit : Path('user_edit') : Args(1) {
}
$c->forward('fetch_all_bodies');
+ $c->forward('fetch_body_areas', [ $user->from_body ]) if $user->from_body;
if ( $c->get_param('submit') ) {
$c->forward('/auth/check_csrf_token');
@@ -1166,9 +1167,18 @@ sub user_edit : Path('user_edit') : Args(1) {
$user->from_body( undef );
}
+ # Has the user's from_body changed since we fetched areas (if we ever did)?
+ # If so, we need to re-fetch areas so the UI is up to date.
+ if ( $user->from_body && $user->from_body->id ne $c->stash->{fetched_areas_body_id} ) {
+ $c->forward('fetch_body_areas', [ $user->from_body ]);
+ }
+
if (!$user->from_body) {
- # Non-staff users aren't allowed any permissions
+ # Non-staff users aren't allowed any permissions or to be in an area
$user->user_body_permissions->delete_all;
+ $user->area_id(undef);
+ delete $c->stash->{areas};
+ delete $c->stash->{fetched_areas_body_id};
} elsif ($c->stash->{available_permissions}) {
my @all_permissions = map { keys %$_ } values %{ $c->stash->{available_permissions} };
my @user_permissions = grep { $c->get_param("permissions[$_]") ? 1 : undef } @all_permissions;
@@ -1184,6 +1194,12 @@ sub user_edit : Path('user_edit') : Args(1) {
}
}
+ if ( $user->from_body && $c->user->has_permission_to('user_assign_areas', $user->from_body->id) ) {
+ my %valid_areas = map { $_->{id} => 1 } @{ $c->stash->{areas} };
+ my $new_area = $c->get_param('area_id');
+ $user->area_id( $valid_areas{$new_area} ? $new_area : undef );
+ }
+
unless ($user->email) {
$c->stash->{field_errors}->{email} = _('Please enter a valid email');
return;
@@ -1614,6 +1630,27 @@ sub fetch_all_bodies : Private {
return 1;
}
+sub fetch_body_areas : Private {
+ my ($self, $c, $body ) = @_;
+
+ my $body_area = $body->body_areas->first;
+
+ unless ( $body_area ) {
+ # Body doesn't have any areas defined.
+ delete $c->stash->{areas};
+ delete $c->stash->{fetched_areas_body_id};
+ return;
+ }
+
+ my $areas = mySociety::MaPit::call('area/children', [ $body_area->area_id ],
+ type => $c->cobrand->area_types_children,
+ );
+
+ $c->stash->{areas} = [ sort { strcoll($a->{name}, $b->{name}) } values %$areas ];
+ # Keep track of the areas we've fetched to prevent a duplicate fetch later on
+ $c->stash->{fetched_areas_body_id} = $body->id;
+}
+
sub trim {
my $self = shift;
my $e = shift;
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index cc8e050da..697cfedf6 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -38,6 +38,8 @@ __PACKAGE__->add_columns(
{ data_type => "bigint", is_nullable => 1 },
"is_superuser",
{ data_type => "boolean", default_value => \"false", is_nullable => 0 },
+ "area_id",
+ { data_type => "integer", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->add_unique_constraint("users_email_key", ["email"]);
@@ -98,8 +100,8 @@ __PACKAGE__->has_many(
);
-# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-07-20 15:00:41
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:+pEOZ8GM14D4gqkp+fr+ZA
+# Created by DBIx::Class::Schema::Loader v0.07035 @ 2016-08-03 13:52:28
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:SX8BS91mWHoOm2oWdNth1w
use Moo;
use mySociety::EmailUtil;