From 5e215672045db73f25127283dac3ee25fc2cd65f Mon Sep 17 00:00:00 2001
From: Erik Thuning <boooink@gmail.com>
Date: Wed, 22 Sep 2021 15:14:35 +0200
Subject: [PATCH] Implemented picking users based on email on the checkout page

---
 html/fragments.html      | 14 ++++++++--
 include/CheckoutPage.php | 56 ++++++++++++++++++++++++++++++++++------
 include/Ldap.php         | 19 +++++++-------
 3 files changed, 70 insertions(+), 19 deletions(-)

diff --git a/html/fragments.html b/html/fragments.html
index b09a101..409dc08 100644
--- a/html/fragments.html
+++ b/html/fragments.html
@@ -534,12 +534,22 @@
              list="user_suggest"
              autocomplete="off"
              placeholder="Användarnamn"
-             value="¤user¤"
-             required />
+             value="¤user¤" />
       <button type="submit" >
         Välj
       </button>
     </div>
+    <div>
+      <label for="email">
+        E-post:
+      </label>
+      <input type="text"
+             name="email"
+             id="email"
+             autocomplete="off"
+             placeholder="E-post"
+             value="¤email¤" />
+    </div>
     <div>
       <label for="displayname">
         Namn:
diff --git a/include/CheckoutPage.php b/include/CheckoutPage.php
index 1da4759..e779b1c 100644
--- a/include/CheckoutPage.php
+++ b/include/CheckoutPage.php
@@ -1,30 +1,68 @@
 <?php
 class CheckoutPage extends Page {
     private $userstr = '';
+    private $emailstr = '';
     private $user = null;
 
     public function __construct() {
         parent::__construct();
         if(isset($_GET['user'])) {
             $this->userstr = trim(strtolower($_GET['user']));
+        }
+        if(isset($_GET['email'])) {
+            $this->emailstr = trim(strtolower($_GET['email']));
+        }
+        try {
+            $this->user = $this->user_init($this->userstr,
+                                           $this->emailstr);
+        } catch(Exception $e) {
+            $this->error = $e->getMessage();
+        }
+    }
+
+    protected function user_init($name, $email) {
+        $nameuser = null;
+        $emailuser = null;
+        if($name) {
             try {
-                $this->user = new User($this->userstr, 'name');
+                $nameuser = new User($this->userstr, 'name');
             } catch(Exception $ue) {
+                # The user wasn't found locally
                 try {
-                    $ldap = new Ldap();
-                    $ldap->get_user($this->userstr);
-                    $this->user = User::create_user($this->userstr);
+                    $this->ldap->get_user($this->userstr);
+                    $nameuser = User::create_user($this->userstr);
                 } catch(Exception $le) {
-                    $this->error = "Användarnamnet '";
-                    $this->error .= $this->userstr;
-                    $this->error .= "' kunde inte hittas.";
+                    $err = "Användarnamnet '$name' kunde inte hittas.";
+                    throw new Exception($err);
                 }
             }
         }
+        if($email) {
+            try {
+                # Lookup email directly in ldap since we don't store it
+                $emailuser = new User($this->ldap->search_email($email),
+                                      'name');
+            } catch(Exception $ue) {
+                $err = "E-postadressen '$emailuser' kunde inte hittas.";
+                throw new Exception($err);
+            }
+        }
+        if($nameuser && $emailuser) {
+            if($nameuser != $emailuser) {
+                $err = "Användarnamn och e-post matchar olika användare.";
+                throw new Exception($err);
+            }
+            return $nameuser;
+        }
+        if($nameuser) {
+            return $nameuser;
+        }
+        return $emailuser;
     }
 
     protected function render_body() {
         $username = '';
+        $email = '';
         $displayname = '';
         $notes = '';
         $loan_table = '';
@@ -33,6 +71,7 @@ class CheckoutPage extends Page {
         $disabled = 'disabled';
         if($this->user !== null) {
             $username = $this->user->get_name();
+            $email = $this->user->get_email($this->ldap);
             $displayname = $this->user->get_displayname($this->ldap);
             $notes = $this->user->get_notes();
             $enddate = format_date(default_loan_end(time()));
@@ -45,7 +84,8 @@ class CheckoutPage extends Page {
             $subhead = replace(array('title' => 'Lånade artiklar'),
                                $this->fragments['subtitle']);
         }
-        print(replace(array('user' => $this->userstr,
+        print(replace(array('user' => $username,
+                            'email' => $email,
                             'displayname' => $displayname,
                             'notes' => $notes,
                             'end' => $enddate,
diff --git a/include/Ldap.php b/include/Ldap.php
index 91c7c84..00e17b4 100644
--- a/include/Ldap.php
+++ b/include/Ldap.php
@@ -17,7 +17,8 @@ class Ldap {
     public function get_user($uid) {
         $data = $this->search("uid=$uid", 'cn', 'uid');
         if($data['count'] !== 1) {
-            throw new Exception("LDAP search for '$uid' did not return exactly one result");
+            $err = "LDAP search for '$uid' did not return exactly one result";
+            throw new Exception($err);
         }
         return $data[0]['cn'][0];
     }
@@ -25,20 +26,20 @@ class Ldap {
     public function get_user_email($uid) {
         $data = $this->search("uid=$uid", 'mail', 'uid');
         if($data['count'] !== 1) {
-            throw new Exception("LDAP search for '$uid' did not return exactly one result");
+            $err = "LDAP search for '$uid' did not return exactly one result";
+            throw new Exception($err);
         }
         return $data[0]['mail'][0];
     }
 
-    public function search_user($uid) {
-        $data = $this->search("uid=$uid", 'cn', 'uid');
+    public function search_email($email) {
+        $data = $this->search("mail=$email", 'mail', 'uid');
         $out = array();
-        foreach($data as $result) {
-            if(isset($result['uid'])) {
-                $out[$result['uid'][0]] = $result['cn'][0];
-            }
+        if($data['count'] !== 1) {
+            $err = "LDAP search for '$email' did not return exactly one result.";
+            throw new Exception($err);
         }
-        return $out;
+        return $data[0]['uid'][0];
     }
 }
 ?>