From 044a9bcd005b85639cb01af8598a3eb1f7227516 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Tue, 14 Jul 2015 15:01:04 +0000 Subject: [PATCH] Bug 14539 - Introduction to castToObject(), aka. make a Koha::Object from various input types Finds a Borrower-object from various different types of inputs. Currently Koha is like an archeological dig site, we have different layers of dealing with various business objects. We started with DBI and numerous ways of passing an HASH around. There is no telling if it will be a List of column => values, or a reference to HASH, or a HASH or just any of the business object's (eg. Borrower's) unique identifiers (userid, cardnumber, borrowernumber). Then DBIx came to the rescue and now we are need to learn DBI and DBIx and SQL to do DB operatons in Koha. Migration to DBIx is on the way. Finally we have Koha::Object and subclasses, which include and use the DBIx, but those are not directly compatible, since Koha::Object is not a subclass of DBIx::Class making life occasionally miserable. Now we need to know 3 methods of DB accession. I am really frustrated with all of those different layers of history, and making things work nicely across all different programming patterns, I have had great success in using a casting system, where we take any value and try to make a Koha::Object-subclass out of it. So we try to cast a Scalar or a reference of Koha::Object-implementation or DBIx::ResultSet or HASH, to the desired Koha::Object-implementation. This is a nice validation/entry function in any subroutine dealing with business objects, making sure that we always have the "correct" implementation of the same business object. --- Koha/AuthUtils.pm | 39 +++++++++++++++++++++++++++++++ Koha/Borrowers.pm | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 1 deletion(-) diff --git a/Koha/AuthUtils.pm b/Koha/AuthUtils.pm index b748c1b..5fa0afb 100644 --- a/Koha/AuthUtils.pm +++ b/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__ diff --git a/Koha/Borrowers.pm b/Koha/Borrowers.pm index 9fb01f0..a581962 100644 --- a/Koha/Borrowers.pm +++ b/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 -- 1.9.1