From b94fca0292701843d7a0a5c2d730d81eed56ec84 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Fri, 26 Jun 2015 18:43:05 +0300 Subject: [PATCH] Bug 7174 - Authentication rewriting - Looking into generating a superuser and encapsulating it nicely. --- C4/Auth.pm | 1 + Koha/AuthUtils.pm | 71 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 14 deletions(-) diff --git a/C4/Auth.pm b/C4/Auth.pm index e069550..0b6d7f8 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -1821,6 +1821,7 @@ sub checkpw_internal { } } + #@DEPRECATED see Bug 7174. I think the demo-user should be represented with permissions instead of a hard-coded non-borrower anomaly. if ( $userid && $userid eq 'demo' && "$password" eq 'demo' && C4::Context->config('demo') ) diff --git a/Koha/AuthUtils.pm b/Koha/AuthUtils.pm index 5342c04..d1d178e 100644 --- a/Koha/AuthUtils.pm +++ b/Koha/AuthUtils.pm @@ -30,6 +30,7 @@ use base 'Exporter'; our $VERSION = '1.01'; our @EXPORT_OK = qw(hash_password); +our @usernameAliasColumns = ('userid', 'cardnumber'); #Possible columns to treat as the username when authenticating. Must be UNIQUE in DB. =head1 NAME @@ -145,28 +146,21 @@ sub generate_salt { Checks if the given username and password match anybody in the Koha DB @PARAM1 String, user identifier, either the koha.borrowers.userid, or koha.borrowers.cardnumber @PARAM2 String, clear text password from the authenticating user -@RETURN String 'superuser', if user is the Koha DB user (superuser account) - undef, if the user is but a man. +@RETURN Koha::Borrower, if login succeeded. + Sets Koha::Borrower->isSuperuser() if the user is a superuser. + Sets Koha::Borrower->isDemouser() if the user is a demouser. @THROWS Koha::Exception::LoginFailed, if no matching password was found for all username aliases in Koha. =cut -TODO:: Should return the Koha::Borrower if login succeeded. + sub checkKohaPassword { my ($userid, $password) = @_; + my $borrower; #Find the borrower to return - if ( $userid && $userid eq C4::Context->config('user') ) { - if ( $password && $password eq C4::Context->config('pass') ) { - # Koha superuser account - # C4::Context->set_userenv(0,0,C4::Context->config('user'),C4::Context->config('user'),C4::Context->config('user'),"",1); - return 'superuser'; - } - else { - Koha::Exception::LoginFailed->throw(error => "Password authentication failed"); - } - } + $borrower = _checkKohaSuperuser($userid, $password); + return $borrower if $borrower; my $usernameFound = 0; #Report to the user if userid/barcode was found, even if the login failed. #Check for each username alias if we can confirm a login with that. - my @usernameAliasColumns = ('userid', 'cardnumber'); for my $unameAlias (@usernameAliasColumns) { my $borrower = Koha::Borrowers->find({$unameAlias => $userid}); if ( $borrower ) { @@ -240,6 +234,55 @@ sub checkLDAPPassword { return 0; } +=head _checkKohaSuperuser + + my $borrower = Koha::AuthUtils::_checkKohaSuperuser($userid, $password); + +Check if the userid and password match the ones in the $KOHA_CONF +@PARAM1 String, user identifier, either the koha.borrowers.userid, or koha.borrowers.cardnumber +@PARAM2 String, clear text password from the authenticating user +@RETURNS Koha::Borrower branded as superuser with ->isSuperuser() + or undef if user logging in is not a superuser. +@THROWS Koha::Exception::LoginFailed if user identifier matches, but password doesn't +=cut +TODO: add Koha::Borrower->isSuperuser(1); +sub _checkKohaSuperuser { + my ($userid, $password) = @_; + + if ( $userid && $userid eq C4::Context->config('user') ) { + if ( $password && $password eq C4::Context->config('pass') ) { + return _createTemporarySuperuser(); + } + else { + Koha::Exception::LoginFailed->throw(error => "Password authentication failed"); + } + } +} + +=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, stamped as superuser. +=cut + +sub _createTemporarySuperuser { + my $borrower = Koha::Borrower->new(); + + my $superuserName = C4::Context->config('user'); + $borrower->isSuperuser(1); + $borrower->update({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__ -- 1.7.9.5