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

(-)a/C4/Utils/DataTables/Members.pm (-24 / +2 lines)
Lines 23-52 sub search { Link Here
23
    # If branches are independent and user is not superlibrarian
23
    # If branches are independent and user is not superlibrarian
24
    # The search has to be only on the user branch
24
    # The search has to be only on the user branch
25
    my $userenv = C4::Context->userenv;
25
    my $userenv = C4::Context->userenv;
26
    my @restricted_branchcodes;
26
    my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
27
    if (C4::Context::only_my_library) {
27
    my @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
28
        push @restricted_branchcodes, $userenv->{branch};
29
    }
30
    else {
31
        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
32
        unless (
33
            $logged_in_user->can(
34
                { borrowers => 'view_borrower_infos_from_any_libraries' }
35
            )
36
          )
37
        {
38
            if ( my $library_groups = $logged_in_user->library->library_groups )
39
            {
40
                while ( my $library_group = $library_groups->next ) {
41
                    push @restricted_branchcodes,
42
                      $library_group->parent->children->get_column('branchcode');
43
                }
44
            }
45
            else {
46
                push @restricted_branchcodes, $userenv->{branch};
47
            }
48
        }
49
    }
50
28
51
    my ($sth, $query, $iTotalQuery, $iTotalRecords, $iTotalDisplayRecords);
29
    my ($sth, $query, $iTotalQuery, $iTotalRecords, $iTotalDisplayRecords);
52
    my $dbh = C4::Context->dbh;
30
    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 735-740 sub can_see_patron_infos { Link Here
735
    return $can;
736
    return $can;
736
}
737
}
737
738
739
=head3 libraries_where_can_see_patrons
740
741
my $libraries = $patron-libraries_where_can_see_patrons;
742
743
Return the list of branchcodes(!) of libraries the patron is allowed to see other patron's infos.
744
The branchcodes are arbitrarily returned sorted.
745
We are supposing here that the object is related to the logged in patron (use of C4::Context::only_my_library)
746
747
An empty array means no restriction, the patron can see patron's infos from any libraries.
748
749
=cut
750
751
sub libraries_where_can_see_patrons {
752
    my ( $self ) = @_;
753
    my $userenv = C4::Context->userenv;
754
755
    return () unless $userenv; # For tests, but userenv should be defined in tests...
756
757
    my @restricted_branchcodes;
758
    if (C4::Context::only_my_library) {
759
        push @restricted_branchcodes, $self->branchcode;
760
    }
761
    else {
762
        unless (
763
            $self->can(
764
                { borrowers => 'view_borrower_infos_from_any_libraries' }
765
            )
766
          )
767
        {
768
            my $library_groups = $self->library->library_groups;
769
            if ( $library_groups->count )
770
            {
771
                while ( my $library_group = $library_groups->next ) {
772
                    push @restricted_branchcodes, $library_group->parent->children->get_column('branchcode');
773
                }
774
            }
775
            else {
776
                push @restricted_branchcodes, $self->branchcode;
777
            }
778
        }
779
    }
780
    return sort(uniq(@restricted_branchcodes));
781
}
782
738
sub can {
783
sub can {
739
    my ( $self, $flagsrequired ) = @_;
784
    my ( $self, $flagsrequired ) = @_;
740
    return unless $self->userid;
785
    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