Koha/svc/members/search
Jonathan Druart 9a23ba3166
Bug 24964: Do not filter patrons after they have been fetched
The svc/members/search script is called in different places.
In some places (Set owner for a fund, add users to a fund, or set a
manager to a suggestion), we need patrons to be filtered depending on
the permissions they have.
For instance you can only set a fund's owner with a patron that has
acquisition.order_manage.

Currently we have fetching X (default 20) patrons, then filter them
depending on their permission.
Says you have 3 patrons that have the correct permissions but are not in
the 20 first patrons, if you do not define a search term, the search
result will be empty.

This is not ideal and we should filter when requesting the DB.

Test plan:
- Have more than 20 patrons, remove them their permissions
- Create 3 more:
1 superlibrarian
1 with the full acq permission
1 with acquisition.order_manage
- Create a fund and set a owner
- Search for patrons, without specifying a search term (to get them all)
=> Without this patch the new patrons you created are not displayed
=> With this patch they are!

Same test plan apply to set a manager to a suggestion (freshly pushed,
see bug 23590), with suggestions and suggestions.suggestions_manage

Note: The code has been written that way to rely on
C4::Auth::haspermission, but the SQL query is quite trivial and the gain
is important.

Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
2020-03-27 08:35:03 +00:00

150 lines
4.9 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2013 BibLibre
#
# This file is part of Koha
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use CGI;
use C4::Auth qw( get_template_and_user haspermission get_user_subpermissions );
use C4::Output qw( output_with_http_headers );
use C4::Utils::DataTables qw( dt_get_params );
use C4::Utils::DataTables::Members qw( search );
use Koha::DateUtils qw( output_pref dt_from_string );
use Koha::Patrons;
my $input = new CGI;
exit unless $input->param('template_path');
my ($template, $user, $cookie) = get_template_and_user({
template_name => scalar $input->param('template_path'),
query => $input,
type => "intranet",
authnotrequired => 0,
flagsrequired => { borrowers => 'edit_borrowers' }
});
my $searchmember = $input->param('searchmember');
my $firstletter = $input->param('firstletter');
my $categorycode = $input->param('categorycode');
my $branchcode = $input->param('branchcode');
my $searchtype = $input->param('searchtype');
my $searchfieldstype = $input->param('searchfieldstype') || 'standard';
my $has_permission = $input->param('has_permission');
my $selection_type = $input->param('selection_type');
# variable information for DataTables (id)
my $sEcho = $input->param('sEcho');
my %dt_params = dt_get_params($input);
foreach (grep {$_ =~ /^mDataProp/} keys %dt_params) {
$dt_params{$_} =~ s/^dt_//;
}
my $results;
# If the user filled a term, maybe it's a cardnumber.
# This cannot be the case if a first letter is given.
if ( $searchmember
and not $firstletter
and $searchfieldstype
and $searchfieldstype eq 'standard' )
{
my $member = Koha::Patrons->find( { cardnumber => $searchmember } );
$results = {
iTotalRecords => 1,
iTotalDisplayRecords => 1,
patrons => [ $member->unblessed ],
} if $member;
}
if ($has_permission) {
my ( $permission, $subpermission ) = split /\./, $has_permission;
$has_permission = {permission => $permission, subpermission => $subpermission};
}
# Perform the patrons search
$results = C4::Utils::DataTables::Members::search(
{
searchmember => $searchmember,
firstletter => $firstletter,
categorycode => $categorycode,
branchcode => $branchcode,
searchtype => $searchtype,
searchfieldstype => $searchfieldstype,
dt_params => \%dt_params,
( $has_permission ? ( has_permission => $has_permission ) : () ),
}
) unless $results;
$template->param(
sEcho => $sEcho,
iTotalRecords => $results->{iTotalRecords},
iTotalDisplayRecords => $results->{iTotalDisplayRecords},
aaData => $results->{patrons},
selection_type => $selection_type,
);
output_with_http_headers $input, $cookie, $template->output, 'json';
__END__
=head1 NAME
search - a search script for finding patrons
=head1 SYNOPSIS
This script provides a service for template for patron search using DataTables
=head2 Performing a search
Call this script from a DataTables table my $searchmember = $input->param('searchmember');
All following params are optional:
searchmember => the search terms
firstletter => search patrons with surname begins with this pattern (currently only used for 1 letter)
categorycode and branchcode => search patrons belong to a given categorycode or a branchcode
searchtype: can be 'contain' or 'start_with'
searchfieldstype: Can be 'standard', 'email', 'borrowernumber', 'userid', 'phone' or 'address'
=cut
=back
=head1 LICENSE
Copyright 2013 BibLibre
This file is part of Koha.
Koha is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
Koha is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Koha is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Koha; if not, see <http://www.gnu.org/licenses>.