From 64aa32db21ffa26ac1f69ca9aa6481b80fc34f8c Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Wed, 24 May 2023 09:29:30 +0200
Subject: [PATCH] Bug 33815: Do not explode if logged in user modify their own
 userid

If the logged in librarian modifies their own userid they will get the
following error when submitting the form:
Can't call method "password_expired" on an undefined value at /kohadevbox/koha/C4/Auth.pm line 1780

We could handle this situation and flag the session as expired. Better
would be to deal with this specific user case and update the cookie (?)

Signed-off-by: Owen Leonard <oleonard@myacpl.org>
---
 C4/Auth.pm | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index 8399bcdcc8..07afd24018 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -1774,6 +1774,15 @@ sub check_cookie_auth {
         } elsif ( $userid ) {
             $session->param( 'lasttime', time() );
             my $patron = Koha::Patrons->find({ userid => $userid });
+
+            # If the user modify their own userid
+            # Better than 500 but we could do better
+            unless ( $patron ) {
+                $session->delete();
+                $session->flush;
+                return ("expired", undef);
+            }
+
             $patron = Koha::Patrons->find({ cardnumber => $userid })
               unless $patron;
             return ("password_expired", undef ) if $patron->password_expired;
-- 
2.30.2