From e9d62827e21d174231ef4e83229c33eb246452eb Mon Sep 17 00:00:00 2001
From: Sonia Zorba <zorba@oats.inaf.it>
Date: Tue, 8 Aug 2017 10:43:39 +0200
Subject: [PATCH] Added workaround for IA2 users

---
 classes/Identity.php         | 15 +++++++++++++++
 classes/MySQLDAO.php         |  5 ++++-
 classes/UserSearchResult.php |  2 +-
 include/user-data.php        |  2 +-
 4 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/classes/Identity.php b/classes/Identity.php
index fc82a1d..500a1aa 100644
--- a/classes/Identity.php
+++ b/classes/Identity.php
@@ -98,4 +98,19 @@ class Identity {
         $this->primary = false;
     }
 
+    private static function endsWith($haystack, $needle) {
+        return substr($haystack, -strlen($needle)) === $needle;
+    }
+
+    /**
+     * Workaround for IA2 users
+     */
+    public function getUIType() {
+        if ($this->eppn !== null && $this->endsWith($this->eppn, '@ia2.inaf.it')) {
+            return 'IA2';
+        } else {
+            return $this->type;
+        }
+    }
+
 }
diff --git a/classes/MySQLDAO.php b/classes/MySQLDAO.php
index 02dea0d..d93d82d 100644
--- a/classes/MySQLDAO.php
+++ b/classes/MySQLDAO.php
@@ -208,13 +208,16 @@ class MySQLDAO implements DAO {
                 . " i.`id`, `type`, `typed_id`, `email`, `name`, `surname`, `institution`, `eppn`"
                 . " FROM identity i"
                 . " JOIN `user` u on u.id = i.user_id"
-                . " WHERE `email` LIKE :email OR `name` LIKE :name OR `surname` LIKE :surname"
+                . " WHERE `email` LIKE :email OR `email` LIKE :emailPart"
+                . " OR `name` LIKE :name OR `surname` LIKE :surname"
                 . " OR CONCAT(`name`,' ',`surname`) LIKE :namesurname";
 
         $stmt = $dbh->prepare($query);
 
         $searchParam = $searchText . '%';
+        $emailPartSearchParam = '%.' . $searchText . '%';
         $stmt->bindParam(':email', $searchParam);
+        $stmt->bindParam(':emailPart', $emailPartSearchParam);
         $stmt->bindParam(':name', $searchParam);
         $stmt->bindParam(':surname', $searchParam);
         $stmt->bindParam(':namesurname', $searchParam);
diff --git a/classes/UserSearchResult.php b/classes/UserSearchResult.php
index 25de1ad..685abc6 100644
--- a/classes/UserSearchResult.php
+++ b/classes/UserSearchResult.php
@@ -37,7 +37,7 @@ class UserSearchResult {
         $email = null;
         $identityTypes = [];
         foreach ($user->identities as $identity) {
-            array_push($identityTypes, $identity->type);
+            array_push($identityTypes, $identity->getUIType());
             if ($nameAndSurname === null && $identity->name !== null && $identity->surname !== null) {
                 $nameAndSurname = $identity->name . ' ' . $identity->surname;
             }
diff --git a/include/user-data.php b/include/user-data.php
index e2e7b4a..37c05e1 100644
--- a/include/user-data.php
+++ b/include/user-data.php
@@ -16,7 +16,7 @@
             <?php } ?>
             Type
         </dt>
-        <dd><?php echo $identity->type; ?></dd>
+        <dd><?php echo $identity->getUIType(); ?></dd>
         <dt>E-mail</dt>
         <dd><?php echo $identity->email; ?></dd>
         <?php if ($identity->eppn !== null) { ?>
-- 
GitLab