View | Details | Raw Unified | Return to bug 18403
Collapse All | Expand All

(-)a/C4/Utils/DataTables/Members.pm (-24 / +2 lines)
Lines 22-51 sub search { Link Here
22
    # If branches are independent and user is not superlibrarian
22
    # If branches are independent and user is not superlibrarian
23
    # The search has to be only on the user branch
23
    # The search has to be only on the user branch
24
    my $userenv = C4::Context->userenv;
24
    my $userenv = C4::Context->userenv;
25
    my @restricted_branchcodes;
25
    my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
26
    if (C4::Context::only_my_library) {
26
    my @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
27
        push @restricted_branchcodes, $userenv->{branch};
28
    }
29
    else {
30
        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
31
        unless (
32
            $logged_in_user->can(
33
                { borrowers => 'view_borrower_infos_from_any_libraries' }
34
            )
35
          )
36
        {
37
            if ( my $library_groups = $logged_in_user->library->library_groups )
38
            {
39
                while ( my $library_group = $library_groups->next ) {
40
                    push @restricted_branchcodes,
41
                      $library_group->parent->children->get_column('branchcode');
42
                }
43
            }
44
            else {
45
                push @restricted_branchcodes, $userenv->{branch};
46
            }
47
        }
48
    }
49
27
50
    my ($sth, $query, $iTotalQuery, $iTotalRecords, $iTotalDisplayRecords);
28
    my ($sth, $query, $iTotalQuery, $iTotalRecords, $iTotalDisplayRecords);
51
    my $dbh = C4::Context->dbh;
29
    my $dbh = C4::Context->dbh;
(-)a/Koha/Libraries.pm (-22 / +7 lines)
Lines 47-78 sub search_filtered { Link Here
47
47
48
    my @branchcodes;
48
    my @branchcodes;
49
    if ( my $userenv = C4::Context->userenv ) {
49
    if ( my $userenv = C4::Context->userenv ) {
50
        if ( C4::Context::only_my_library ) {
50
        my $only_from_group = $params->{only_from_group};
51
            push @branchcodes, $userenv->{branch};
51
        if ( $only_from_group ) {
52
        }
53
        else {
54
            my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
52
            my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
55
            unless (
53
            my @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
56
                $logged_in_user->can(
54
            $params->{branchcode} = { -in => \@branchcodes } if @branchcodes;
57
                    { borrowers => 'view_borrower_infos_from_any_libraries' }
55
        } else {
58
                )
56
            if ( C4::Context::only_my_library ) {
59
              )
57
                $params->{branchcode} = C4::Context->userenv->{branch};
60
            {
61
                if ( my $library_groups = $logged_in_user->library->library_groups )
62
                {
63
                    while ( my $library_group = $library_groups->next ) {
64
                        push @branchcodes,
65
                          $library_group->parent->children->get_column('branchcode');
66
                    }
67
                }
68
                else {
69
                    push @branchcodes, $userenv->{branch};
70
                }
71
            }
58
            }
72
        }
59
        }
73
    }
60
    }
74
75
    $params->{branchcode} = { -in => \@branchcodes } if @branchcodes;
76
    delete $params->{only_from_group};
61
    delete $params->{only_from_group};
77
    return $self->SUPER::search( $params, $attributes );
62
    return $self->SUPER::search( $params, $attributes );
78
}
63
}
(-)a/Koha/Patron.pm (+45 lines)
Lines 21-26 package Koha::Patron; Link Here
21
use Modern::Perl;
21
use Modern::Perl;
22
22
23
use Carp;
23
use Carp;
24
use List::MoreUtils qw( uniq );
24
25
25
use C4::Context;
26
use C4::Context;
26
use C4::Log;
27
use C4::Log;
Lines 610-615 sub can_see_patron_infos { Link Here
610
    return $can;
611
    return $can;
611
}
612
}
612
613
614
=head3 libraries_where_can_see_patrons
615
616
my $libraries = $patron-libraries_where_can_see_patrons;
617
618
Return the list of branchcodes(!) of libraries the patron is allowed to see other patron's infos.
619
The branchcodes are arbitrarily returned sorted.
620
We are supposing here that the object is related to the logged in patron (use of C4::Context::only_my_library)
621
622
An empty array means no restriction, the patron can see patron's infos from any libraries.
623
624
=cut
625
626
sub libraries_where_can_see_patrons {
627
    my ( $self ) = @_;
628
    my $userenv = C4::Context->userenv;
629
630
    return () unless $userenv; # For tests, but userenv should be defined in tests...
631
632
    my @restricted_branchcodes;
633
    if (C4::Context::only_my_library) {
634
        push @restricted_branchcodes, $self->branchcode;
635
    }
636
    else {
637
        unless (
638
            $self->can(
639
                { borrowers => 'view_borrower_infos_from_any_libraries' }
640
            )
641
          )
642
        {
643
            my $library_groups = $self->library->library_groups;
644
            if ( $library_groups->count )
645
            {
646
                while ( my $library_group = $library_groups->next ) {
647
                    push @restricted_branchcodes, $library_group->parent->children->get_column('branchcode');
648
                }
649
            }
650
            else {
651
                push @restricted_branchcodes, $self->branchcode;
652
            }
653
        }
654
    }
655
    return sort(uniq(@restricted_branchcodes));
656
}
657
613
sub can {
658
sub can {
614
    my ( $self, $flagsrequired ) = @_;
659
    my ( $self, $flagsrequired ) = @_;
615
    return unless $self->userid;
660
    return unless $self->userid;
(-)a/Koha/Patrons.pm (-18 / +3 lines)
Lines 54-76 sub search_limited { Link Here
54
54
55
    my $userenv = C4::Context->userenv;
55
    my $userenv = C4::Context->userenv;
56
    my @restricted_branchcodes;
56
    my @restricted_branchcodes;
57
    my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
57
    if ( $userenv ) {
58
    if ( $logged_in_user and not
58
        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
59
        $logged_in_user->can(
59
        @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
60
            { borrowers => 'view_borrower_infos_from_any_libraries' }
61
        )
62
      )
63
    {
64
        if ( my $library_groups = $logged_in_user->library->library_groups )
65
        {
66
            while ( my $library_group = $library_groups->next ) {
67
                push @restricted_branchcodes,
68
                  $library_group->parent->children->get_column('branchcode');
69
            }
70
        }
71
        else {
72
            push @restricted_branchcodes, $userenv->{branch};
73
        }
74
    }
60
    }
75
    $params->{'me.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
61
    $params->{'me.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
76
    return $self->search( $params, $attributes );
62
    return $self->search( $params, $attributes );
77
- 

Return to bug 18403