From 005be268955486d4da7a1a810c040396e02f3098 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 27 Mar 2019 13:31:28 -0400 Subject: [PATCH] Bug 20256: Add new methods for checking item editing permissions Signed-off-by: Bob Bennhoff - CLiC --- Koha/Patron.pm | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index a973a207d8..14f81821f0 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1358,6 +1358,108 @@ sub can_see_patrons_from { ); } +=head3 libraries_where_can_see_patrons + +my $libraries = $patron-libraries_where_can_see_patrons; + +Return the list of branchcodes(!) of libraries the patron is allowed to see other patron's infos. +The branchcodes are arbitrarily returned sorted. +We are supposing here that the object is related to the logged in patron (use of C4::Context::only_my_library) + +An empty array means no restriction, the patron can see patron's infos from any libraries. + +=cut + +sub libraries_where_can_see_patrons { + my ($self) = @_; + + return $self->libraries_where_can_see_things( + { + permission => 'borrowers', + subpermission => 'view_borrower_infos_from_any_libraries', + group_feature => 'ft_hide_patron_info', + } + ); +} + +=head3 can_edit_item + +my $can_edit = $patron->can_edit_item( $item ); + +Return true if the patron (usually the logged in user) can edit the given item + +The parameter can be a Koha::Item, an item hashref, or a branchcode. + +=cut + +sub can_edit_item { + my ( $self, $item ) = @_; + + my $userenv = C4::Context->userenv(); + + my $ref = ref($item); + + my $branchcode = + $ref eq 'Koha::Item' ? $item->homebranch + : $ref eq 'HASH' ? $item->{homebranch} + : $ref eq q{} ? $item + : undef; + + return unless $branchcode; + + return 1 if C4::Context->IsSuperLibrarian(); + + if ( C4::Context->preference('IndependentBranches') ) { + return $userenv->{branch} eq $branchcode; + } + + return $self->can_edit_items_from($branchcode); +} + +=head3 can_edit_items_from + +my $can_edit = $patron->can_edit_items_from( $branchcode ); + +Return true if this user can edit items from the give home branchcode + +=cut + +sub can_edit_items_from { + my ( $self, $branchcode ) = @_; + + return $self->can_see_things_from( + { + branchcode => $branchcode, + permission => 'editcatalogue', + subpermission => 'edit_any_item', + } + ); +} + +=head3 libraries_where_can_edit_items + +my $libraries = $patron->libraries_where_can_edit_items; + +Return the list of branchcodes(!) of libraries the patron is allowed to items for. +The branchcodes are arbitrarily returned sorted. +We are supposing here that the object is related to the logged in patron (use of C4::Context::only_my_library) + +An empty array means no restriction, the user can edit any item. + +=cut + +sub libraries_where_can_edit_items { + my ($self) = @_; + + return $self->libraries_where_can_see_things( + { + permission => 'editcatalogue', + subpermission => 'edit_any_item', + group_feature => 'ft_limit_item_editing', + } + ); +} + =head3 can_see_things_from my $can_see = $thing->can_see_things_from( $branchcode ); -- 2.24.3 (Apple Git-128)