@@ -, +, @@ Koha::Object from various input types --- Koha/AuthUtils.pm | 39 +++++++++++++++++++++++++++++++ Koha/Borrowers.pm | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 1 deletion(-) --- a/Koha/AuthUtils.pm +++ a/Koha/AuthUtils.pm @@ -22,6 +22,8 @@ use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64); use Encode qw( encode is_utf8 ); use Fcntl qw/O_RDONLY/; # O_RDONLY is used in generate_salt +use Koha::Borrower; + use base 'Exporter'; our $VERSION = '1.01'; @@ -133,6 +135,43 @@ sub generate_salt { close SOURCE; return $string; } + +=head checkKohaSuperuserFromUserid +See checkKohaSuperuser(), with only the "user identifier"-@PARAM. +@THROWS nothing. +=cut + +sub checkKohaSuperuserFromUserid { + my ($userid) = @_; + + if ( $userid && $userid eq C4::Context->config('user') ) { + return _createTemporarySuperuser(); + } +} + +=head _createTemporarySuperuser + +Create a temporary superuser which should be instantiated only to the environment +and then discarded. So do not ->store() it! +@RETURN Koha::Borrower +=cut + +sub _createTemporarySuperuser { + my $borrower = Koha::Borrower->new(); + + my $superuserName = C4::Context->config('user'); + $borrower->set({borrowernumber => 0, + userid => $superuserName, + cardnumber => $superuserName, + firstname => $superuserName, + surname => $superuserName, + branchcode => 'NO_LIBRARY_SET', + flags => 1, + email => C4::Context->preference('KohaAdminEmailAddress') + }); + return $borrower; +} + 1; __END__ --- a/Koha/Borrowers.pm +++ a/Koha/Borrowers.pm @@ -18,11 +18,12 @@ package Koha::Borrowers; # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use Modern::Perl; +use Scalar::Util qw(blessed); use Carp; use Koha::Database; - +use Koha::AuthUtils; use Koha::Borrower; use base qw(Koha::Objects); @@ -49,6 +50,73 @@ sub object_class { return 'Koha::Borrower'; } +=head castToBorrower + + my $borrower = Koha::Borrowers::castToBorrower('cardnumber'); + my $borrower = Koha::Borrowers::castToBorrower($Koha::Borrower); + my $borrower = Koha::Borrowers::castToBorrower('userid'); + my $borrower = Koha::Borrowers::castToBorrower('borrowernumber'); + my $borrower = Koha::Borrowers::castToBorrower({borrowernumber => 123, + }); + my $borrower = Koha::Borrowers::castToBorrower({firstname => 'Olli-Antti', + surname => 'Kivi', + address => 'Koskikatu 25', + }); + +Because there are gazillion million ways in Koha to invoke a Borrower, this is a +omnibus for easily creating a Borrower-object from all the arcane invocations present +in many parts of Koha. +Just throw the crazy and unpredictable return values from myriad subroutines returning +some kind of an borrowerish value to this casting function to get a brand new Koha::Borrower. +@PARAM1 Scalar, or HASHRef. +@RETURNS Koha::Borrower, possibly already in DB or a completely new one if nothing was + inferred from the DB. +@THROWS Koha::Exception::BadParameter, if no idea what to do with the input. +@THROWS Koha::Exception::UnknownObject, if we cannot find a Borrower with the given input. +=cut + +sub castToBorrower { + my ($input) = @_; + + unless ($input) { + Koha::Exception::BadParameter->throw(error => __PACKAGE__."::castToBorrower():> No parameter given!"); + } + if (blessed($input) && $input->isa('Koha::Borrower')) { + return $input; + } + if (blessed($input) && $input->isa('Koha::Schema::Result::Borrower')) { + return Koha::Borrower->_new_from_dbic($input); + } + + my ($borrowernumber, $cardnumber, $userid, $borrower); + #Extract unique keys and try to get the borrower from them. + if (ref($input) eq 'HASH') { + $borrowernumber = $input->{borrowernumber}; + $cardnumber = $input->{cardnumber}; + $userid = $input->{userid}; + } + elsif (not(ref($input))) { #We have a scalar + $borrowernumber = $input; + $cardnumber = $input; + $userid = $input; + } + if ($borrowernumber || $cardnumber || $userid) { + $borrower = Koha::Borrowers->search({'-or' => [{borrowernumber => $borrowernumber}, + {cardnumber => $cardnumber}, + {userid => $userid}, + ] + })->next(); + unless ($borrower) { + $borrower = Koha::AuthUtils::checkKohaSuperuserFromUserid($userid); + unless ($borrower) { + Koha::Exception::UnknownObject->throw(error => "Koha::Borrower::castToBorrower->new():> Cannot find an existing Borrower from borrowernumber|cardnumber|userid '$borrowernumber|$cardnumber|$userid'."); + } + } + } + + return $borrower; +} + =head1 AUTHOR Kyle M Hall --