aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Admin
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Admin')
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin/Bodies.pm43
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin/Reports.pm107
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin/Templates.pm7
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin/Triage.pm23
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin/Users.pm80
5 files changed, 129 insertions, 131 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm b/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm
index 6ae068cd9..31a717a9c 100644
--- a/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm
@@ -151,8 +151,7 @@ sub category : Chained('body') : PathPart('') {
my $history = $c->model('DB::ContactsHistory')->search(
{
- body_id => $c->stash->{body_id},
- category => $c->stash->{contact}->category
+ contact_id => $c->stash->{contact}->id,
},
{
order_by => ['contacts_history_id']
@@ -241,17 +240,27 @@ sub update_contact : Private {
if ($current_contact && $contact->id && $contact->id != $current_contact->id) {
$errors{category} = _('You cannot rename a category to an existing category');
} elsif ($current_contact && !$contact->id) {
- # Changed name
$contact = $current_contact;
- $c->model('DB::Problem')->to_body($c->stash->{body_id})->search({ category => $current_category })->update({ category => $category });
- $contact->category($category);
+ # Set the flag here so we can run the editable test on it
+ $contact->set_extra_metadata(open311_protect => $c->get_param('open311_protect'));
+ if (!$contact->category_uneditable) {
+ # Changed name
+ $c->model('DB::Problem')->to_body($c->stash->{body_id})->search({ category => $current_category })->update({ category => $category });
+ $contact->category($category);
+ }
}
my $email = $c->get_param('email');
- $email =~ s/\s+//g;
my $send_method = $c->get_param('send_method') || $contact->body->send_method || "";
+ if ($send_method eq 'Open311') {
+ $email =~ s/^\s+|\s+$//g;
+ } else {
+ $email =~ s/\s+//g;
+ }
my $email_unchanged = $contact->email && $email && $contact->email eq $email;
- unless ( $send_method eq 'Open311' || $email_unchanged ) {
+ my $cobrand = $contact->body->get_cobrand_handler;
+ my $cobrand_valid = $cobrand && $cobrand->call_hook(validate_contact_email => $email);
+ unless ( $send_method eq 'Open311' || $email_unchanged || $cobrand_valid ) {
$errors{email} = _('Please enter a valid email') unless is_valid_email_list($email) || $email eq 'REFUSED';
}
@@ -267,15 +276,19 @@ sub update_contact : Private {
$contact->send_method( $c->get_param('send_method') );
# Set flags in extra to the appropriate values
- if ( $c->get_param('photo_required') ) {
- $contact->set_extra_metadata_if_undefined( photo_required => 1 );
- } else {
- $contact->unset_extra_metadata( 'photo_required' );
+ foreach (qw(photo_required open311_protect updates_disallowed reopening_disallowed assigned_users_only anonymous_allowed)) {
+ if ( $c->get_param($_) ) {
+ $contact->set_extra_metadata( $_ => 1 );
+ } else {
+ $contact->unset_extra_metadata($_);
+ }
}
- if ( $c->get_param('open311_protect') ) {
- $contact->set_extra_metadata( open311_protect => 1 );
- } else {
- $contact->unset_extra_metadata( 'open311_protect' );
+ if ( $c->user->is_superuser ) {
+ if ( $c->get_param('hardcoded') ) {
+ $contact->set_extra_metadata( hardcoded => 1 );
+ } else {
+ $contact->unset_extra_metadata('hardcoded');
+ }
}
if ( my @group = $c->get_param_list('group') ) {
@group = grep { $_ } @group;
diff --git a/perllib/FixMyStreet/App/Controller/Admin/Reports.pm b/perllib/FixMyStreet/App/Controller/Admin/Reports.pm
index 7300fe676..20801e0cf 100644
--- a/perllib/FixMyStreet/App/Controller/Admin/Reports.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin/Reports.pm
@@ -4,6 +4,7 @@ use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }
+use utf8;
use List::MoreUtils 'uniq';
use FixMyStreet::SMS;
use Utils;
@@ -50,6 +51,8 @@ sub index : Path {
return if $c->cobrand->call_hook(report_search_query => $query, $p_page, $u_page, $order);
+ my $problems = $c->cobrand->problems;
+
if (my $search = $c->get_param('search')) {
$search = $self->trim($search);
@@ -62,9 +65,6 @@ sub index : Path {
$c->stash->{searched} = $search;
- my $search_n = 0;
- $search_n = int($search) if $search =~ /^\d+$/;
-
my $like_search = "%$search%";
my $parsed = FixMyStreet::SMS->parse_username($search);
@@ -92,24 +92,18 @@ sub index : Path {
'me.external_id' => { like => "%$1%" }
];
} else {
- $query->{'-or'} = [
- 'me.id' => $search_n,
- 'user.email' => { ilike => $like_search },
- 'user.phone' => { ilike => $like_search },
- 'me.external_id' => { ilike => $like_search },
- 'me.name' => { ilike => $like_search },
- 'me.title' => { ilike => $like_search },
- detail => { ilike => $like_search },
- bodies_str => { like => $like_search },
- cobrand_data => { like => $like_search },
- ];
+ $problems = $problems->search_text($search);
+ # The below is added so that PostgreSQL does not try and use other indexes
+ # besides the full text search. It should have no impact on results shown.
+ $order = [ $order, { -desc => "me.id" }, { -desc => "me.created" } ];
}
- my $problems = $c->cobrand->problems->search(
+ $problems = $problems->search(
$query,
{
join => 'user',
'+columns' => 'user.email',
+ prefetch => 'contact',
rows => 50,
order_by => $order,
}
@@ -118,6 +112,8 @@ sub index : Path {
$c->stash->{problems} = [ $problems->all ];
$c->stash->{problems_pager} = $problems->pager;
+ my $updates = $c->cobrand->updates;
+ $order = { -desc => 'me.id' };
if ($valid_email) {
$query = [
'user.email' => { ilike => $like_search },
@@ -132,30 +128,25 @@ sub index : Path {
'me.problem_id' => int($1),
];
} elsif ($search =~ /^area:(\d+)$/) {
- $query = [];
+ $query = 0;
} else {
- $query = [
- 'me.id' => $search_n,
- 'problem.id' => $search_n,
- 'user.email' => { ilike => $like_search },
- 'user.phone' => { ilike => $like_search },
- 'me.name' => { ilike => $like_search },
- text => { ilike => $like_search },
- 'me.cobrand_data' => { ilike => $like_search },
- ];
+ $updates = $updates->search_text($search);
+ $order = [ $order, { -desc => "me.created" } ];
+ $query = 1;
}
- if (@$query) {
- my $updates = $c->cobrand->updates->search(
- {
- -or => $query,
- },
+ $query = { -or => $query } if ref $query;
+
+ if ($query) {
+ $query = undef unless ref $query;
+ $updates = $updates->search(
+ $query,
{
'+columns' => ['user.email'],
join => 'user',
prefetch => [qw/problem/],
rows => 50,
- order_by => { -desc => 'me.id' }
+ order_by => $order,
}
)->page( $u_page );
$c->stash->{updates} = [ $updates->all ];
@@ -164,9 +155,15 @@ sub index : Path {
} else {
- my $problems = $c->cobrand->problems->search(
+ $problems = $problems->search(
$query,
- { order_by => $order, rows => 50 }
+ {
+ '+columns' => ['user.email'],
+ join => 'user',
+ prefetch => 'contact',
+ order_by => $order,
+ rows => 50
+ }
)->page( $p_page );
$c->stash->{problems} = [ $problems->all ];
$c->stash->{problems_pager} = $problems->pager;
@@ -227,6 +224,26 @@ sub edit : Path('/admin/report_edit') : Args(1) {
push @fields, { name => 'Duplicates', val => join( ',', @{ $problem->get_extra_metadata('duplicates') } ) };
delete $extra->{duplicates};
}
+
+ if ( $extra->{contributed_by} ) {
+ my $u = $c->cobrand->users->find({id => $extra->{contributed_by}});
+ if ( $u ) {
+ my $uri = $c->uri_for_action('admin/users/index', { search => $u->email } );
+ push @fields, {
+ name => _('Created By'),
+ val => FixMyStreet::Template::SafeString->new( "<a href=\"$uri\">@{[$u->name]} (@{[$u->email]})</a>" )
+ };
+ if ( $u->from_body ) {
+ push @fields, { name => _('Created Body'), val => $u->from_body->name };
+ } elsif ( $u->is_superuser ) {
+ push @fields, { name => _('Created Body'), val => _('Superuser') };
+ }
+ } else {
+ push @fields, { name => 'contributed_by', val => $extra->{contributed_by} };
+ }
+ delete $extra->{contributed_by};
+ }
+
for my $key ( keys %$extra ) {
push @fields, { name => $key, val => $extra->{$key} };
}
@@ -341,24 +358,10 @@ sub edit : Path('/admin/report_edit') : Args(1) {
if ( $problem->state ne $old_state ) {
$c->forward( '/admin/log_edit', [ $id, 'problem', 'state_change' ] );
- my $name = $c->user->moderating_user_name;
- my $extra = { is_superuser => 1 };
- if ($c->user->from_body) {
- delete $extra->{is_superuser};
- $extra->{is_body_user} = $c->user->from_body->id;
- }
- my $timestamp = \'current_timestamp';
$problem->add_to_comments( {
text => $c->stash->{update_text} || '',
- created => $timestamp,
- confirmed => $timestamp,
- user_id => $c->user->id,
- name => $name,
- mark_fixed => 0,
- anonymous => 0,
- state => 'confirmed',
+ user => $c->user->obj,
problem_state => $problem->state,
- extra => $extra
} );
}
$c->forward( '/admin/log_edit', [ $id, 'problem', 'edit' ] );
@@ -417,13 +420,7 @@ sub edit_category : Private {
} else {
$problem->add_to_comments({
text => $update_text,
- created => \'current_timestamp',
- confirmed => \'current_timestamp',
- user_id => $c->user->id,
- name => $c->user->from_body ? $c->user->from_body->name : $c->user->name,
- state => 'confirmed',
- mark_fixed => 0,
- anonymous => 0,
+ user => $c->user->obj,
});
}
$c->forward( '/admin/log_edit', [ $problem->id, 'problem', 'category_change' ] );
diff --git a/perllib/FixMyStreet/App/Controller/Admin/Templates.pm b/perllib/FixMyStreet/App/Controller/Admin/Templates.pm
index efff1b488..9fb401e2b 100644
--- a/perllib/FixMyStreet/App/Controller/Admin/Templates.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin/Templates.pm
@@ -69,7 +69,7 @@ sub edit : Path : Args(2) {
category => $_->category_display,
active => $active_contacts{$_->id},
email => $_->email,
- group => $_->get_extra_metadata('group') // '',
+ group => $_->groups,
} } @live_contacts;
$c->stash->{contacts} = \@all_contacts;
$c->forward('/report/stash_category_groups', [ \@all_contacts, 1 ]) if $c->cobrand->enable_category_groups;
@@ -100,7 +100,10 @@ sub edit : Path : Args(2) {
$template->text( $c->get_param('text') );
$template->state( $c->get_param('state') );
- $template->external_status_code( $c->get_param('external_status_code') );
+
+ my $ext_code = $c->cobrand->call_hook('admin_templates_external_status_code_hook');
+ $ext_code ||= $c->get_param('external_status_code');
+ $template->external_status_code($ext_code);
if ( $template->state && $template->external_status_code ) {
$c->stash->{errors} ||= {};
diff --git a/perllib/FixMyStreet/App/Controller/Admin/Triage.pm b/perllib/FixMyStreet/App/Controller/Admin/Triage.pm
index 428c35073..c5bb6628d 100644
--- a/perllib/FixMyStreet/App/Controller/Admin/Triage.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin/Triage.pm
@@ -90,10 +90,8 @@ sub setup_categories : Private {
delete $c->stash->{categories_hash};
my %category_groups = ();
for my $category (@{$c->stash->{end_options}}) {
- my $group = $category->{group} // $category->get_extra_metadata('group') // [''];
- # this could be an array ref or a string
- my @groups = ref $group eq 'ARRAY' ? @$group : ($group);
- push( @{$category_groups{$_}}, $category ) for @groups;
+ my $groups = $category->groups;
+ push( @{$category_groups{$_}}, $category ) for @$groups;
}
my @category_groups = ();
for my $group ( grep { $_ ne _('Other') } sort keys %category_groups ) {
@@ -119,27 +117,14 @@ sub update : Private {
$c->stash->{problem}->update( { state => 'confirmed' } );
$c->forward( '/admin/log_edit', [ $problem->id, 'problem', 'triage' ] );
- my $name = $c->user->moderating_user_name;
- my $extra = { is_superuser => 1 };
- if ($c->user->from_body) {
- delete $extra->{is_superuser};
- $extra->{is_body_user} = $c->user->from_body->id;
- }
-
+ my $extra;
$extra->{triage_report} = 1;
$extra->{holding_category} = $current_category;
$extra->{new_category} = $new_category;
- my $timestamp = \'current_timestamp';
my $comment = $problem->add_to_comments( {
text => "Report triaged from $current_category to $new_category",
- created => $timestamp,
- confirmed => $timestamp,
- user_id => $c->user->id,
- name => $name,
- mark_fixed => 0,
- anonymous => 0,
- state => 'confirmed',
+ user => $c->user->obj,
problem_state => $problem->state,
extra => $extra,
whensent => \'current_timestamp',
diff --git a/perllib/FixMyStreet/App/Controller/Admin/Users.pm b/perllib/FixMyStreet/App/Controller/Admin/Users.pm
index 046e19126..7ebfb9bbd 100644
--- a/perllib/FixMyStreet/App/Controller/Admin/Users.pm
+++ b/perllib/FixMyStreet/App/Controller/Admin/Users.pm
@@ -29,17 +29,29 @@ sub index :Path : Args(0) {
if ($c->req->method eq 'POST') {
my @uids = $c->get_param_list('uid');
- my @role_ids = $c->get_param_list('roles');
my $user_rs = FixMyStreet::DB->resultset("User")->search({ id => \@uids });
- foreach my $user ($user_rs->all) {
- $user->admin_user_body_permissions->delete;
- $user->user_roles->search({
- role_id => { -not_in => \@role_ids },
- })->delete;
- foreach my $role (@role_ids) {
- $user->user_roles->find_or_create({
- role_id => $role,
+ if ( $c->get_param('remove-staff') ) {
+ foreach my $user ($user_rs->all) {
+ $user->update({
+ from_body => undef,
+ email_verified => 0,
+ phone_verified => 0,
});
+ $user->user_roles->delete;
+ $user->admin_user_body_permissions->delete;
+ }
+ } else {
+ my @role_ids = $c->get_param_list('roles');
+ foreach my $user ($user_rs->all) {
+ $user->admin_user_body_permissions->delete;
+ $user->user_roles->search({
+ role_id => { -not_in => \@role_ids },
+ })->delete;
+ foreach my $role (@role_ids) {
+ $user->user_roles->find_or_create({
+ role_id => $role,
+ });
+ }
}
}
$c->stash->{status_message} = _('Updated!');
@@ -47,28 +59,13 @@ sub index :Path : Args(0) {
my $search = $c->get_param('search');
my $role = $c->get_param('role');
+ my $users = $c->cobrand->users;
if ($search || $role) {
- my $users = $c->cobrand->users;
- my $isearch;
if ($search) {
$search = $self->trim($search);
$search =~ s/^<(.*)>$/$1/; # In case email wrapped in <...>
$c->stash->{searched} = $search;
-
- $isearch = '%' . $search . '%';
- my $search_n = 0;
- $search_n = int($search) if $search =~ /^\d+$/;
-
- $users = $users->search(
- {
- -or => [
- email => { ilike => $isearch },
- phone => { ilike => $isearch },
- name => { ilike => $isearch },
- from_body => $search_n,
- ]
- }
- );
+ $users = $users->search_text($search);
}
if ($role) {
$c->stash->{role_selected} = $role;
@@ -78,25 +75,23 @@ sub index :Path : Args(0) {
join => 'user_roles',
});
}
-
- my @users = $users->all;
- $c->stash->{users} = [ @users ];
- if ($search) {
- $c->forward('/admin/add_flags', [ { email => { ilike => $isearch } } ]);
- }
-
} else {
$c->forward('/auth/get_csrf_token');
$c->forward('/admin/fetch_all_bodies');
$c->cobrand->call_hook('admin_user_edit_extra_data');
# Admin users by default
- my $users = $c->cobrand->users->search(
- { from_body => { '!=', undef } },
- { order_by => 'name' }
- );
- my @users = $users->all;
- $c->stash->{users} = \@users;
+ $users = $users->search({ from_body => { '!=', undef } });
+ }
+
+ $users = $users->search(undef, {
+ prefetch => 'from_body',
+ order_by => [ \"me.name = ''", 'me.name' ],
+ });
+ my @users = $users->all;
+ $c->stash->{users} = \@users;
+ if ($search) {
+ $c->forward('/admin/add_flags', [ { email => { ilike => "%$search%" } } ]);
}
my $rs;
@@ -373,6 +368,11 @@ sub edit : Chained('user') : PathPart('') : Args(0) {
my @live_contact_ids = map { $_->id } @live_contacts;
my @new_contact_ids = grep { $c->get_param("contacts[$_]") } @live_contact_ids;
$user->set_extra_metadata('categories', \@new_contact_ids);
+ if ($c->get_param('assigned_categories_only')) {
+ $user->set_extra_metadata(assigned_categories_only => 1);
+ } else {
+ $user->unset_extra_metadata('assigned_categories_only');
+ }
} else {
$user->unset_extra_metadata('categories');
}
@@ -396,7 +396,7 @@ sub edit : Chained('user') : PathPart('') : Args(0) {
id => $_->id,
category => $_->category,
active => $active_contacts{$_->id},
- group => $_->get_extra_metadata('group') // '',
+ group => $_->groups,
} } @live_contacts;
$c->stash->{contacts} = \@all_contacts;
$c->forward('/report/stash_category_groups', [ \@all_contacts, 1 ]) if $c->cobrand->enable_category_groups;