Commit c10ecd68 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Implemented user search in Join Request modal

parent 9762c6ef
Loading
Loading
Loading
Loading
+67 −0
Original line number Original line Diff line number Diff line
<?php

/* ----------------------------------------------------------------------------
 *               INAF - National Institute for Astrophysics
 *               IRA  - Radioastronomical Institute - Bologna
 *               OATS - Astronomical Observatory - Trieste
 * ----------------------------------------------------------------------------
 *
 * Copyright (C) 2016 Istituto Nazionale di Astrofisica
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License Version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

namespace RAP;

class CallbackHandler {

    /**
     * returns null if the callback URL is not listed in configuration file.
     */
    public static function getCallbackTitle($callbackURL) {

        if ($callbackURL === null) {
            return "Account Management";
        }

        global $CALLBACKS;

        foreach ($CALLBACKS as $callback) {
            if ($callback['url'] === $callbackURL) {
                return $callback['title'];
            }
        }

        throw new \Exception("Unauthorized callback URL");
    }

    public static function manageLoginRedirect($user) {

        global $BASE_PATH, $session;

        if (isset($session->callback) && $session->callback !== null) {
            // External login using token
            $token = Util::createNewToken();
            DAO::get()->insertTokenData($token, $user->id);
            header('Location: ' . $session->callback . '?token=' . $token);
        } else {
            // Login in session
            $session->user = $user;
            $session->save();
            // Return to index
            header('Location: ' . $BASE_PATH);
        }
    }

}
+13 −8
Original line number Original line Diff line number Diff line
@@ -28,11 +28,11 @@ abstract class DAO {


    public abstract function getDBHandler();
    public abstract function getDBHandler();


    public abstract function insertTokenData($token, $data);
    public abstract function createLoginToken($token, $userId);


    public abstract function findTokenData($token);
    public abstract function findLoginToken($token);


    public abstract function deleteToken($token);
    public abstract function deleteLoginToken($token);


    /**
    /**
     * Return the new identity ID.
     * Return the new identity ID.
@@ -54,8 +54,12 @@ abstract class DAO {
     */
     */
    public abstract function findUserByIdentity($type, $identifier, $dbIdentifier);
    public abstract function findUserByIdentity($type, $identifier, $dbIdentifier);


    public abstract function searchUser($searchText);

    public abstract function addEmailToUser($email, $userId);
    public abstract function addEmailToUser($email, $userId);


    public abstract function createJoinRequest($token, $applicantUserId, $targetUserId);

    public $config;
    public $config;


    public function __construct($config) {
    public function __construct($config) {
@@ -63,12 +67,13 @@ abstract class DAO {
    }
    }


    public static function get() {
    public static function get() {
        $config = parse_ini_file(ROOT . '/config.ini', true);
        global $DATABASE;
        switch ($config['dbtype']) {

        switch ($DATABASE['dbtype']) {
            case 'MySQL':
            case 'MySQL':
                return new MySQLDAO($config);
                return new MySQLDAO($DATABASE);
            default:
            default:
                throw new \Exception($config['dbtype'] . ' not supported yet');
                throw new \Exception($DATABASE['dbtype'] . ' not supported yet');
        }
        }
    }
    }


+1 −5
Original line number Original line Diff line number Diff line
@@ -43,7 +43,7 @@ class Identity {
    /**
    /**
     * One of the types specified above. Mandatory field.
     * One of the types specified above. Mandatory field.
     */
     */
    private $type;
    public $type;


    /**
    /**
     * Data related to specific account type (shibboleth persistent id, facebook id, etc, ...). Mandatory field.
     * Data related to specific account type (shibboleth persistent id, facebook id, etc, ...). Mandatory field.
@@ -101,8 +101,4 @@ class Identity {
        $this->type = $userType;
        $this->type = $userType;
    }
    }


    public function getType() {
        return $this->type;
    }

}
}
+107 −21
Original line number Original line Diff line number Diff line
@@ -33,16 +33,16 @@ class MySQLDAO extends DAO {
        return new PDO($connectionString, $this->config['username'], $this->config['password']);
        return new PDO($connectionString, $this->config['username'], $this->config['password']);
    }
    }


    public function insertTokenData($token, $data) {
    public function createLoginToken($token, $userId) {


        global $log;
        global $log;


        $dbh = $this->getDBHandler();
        $dbh = $this->getDBHandler();
        $stmt = $dbh->prepare("INSERT INTO token (token, data) VALUES(:token, :data)");
        $stmt = $dbh->prepare("INSERT INTO login_token (token, data) VALUES(:token, :data)");


        $params = array(
        $params = array(
            ':token' => $token,
            ':token' => $token,
            ':data' => $data
            ':data' => $userId
        );
        );


        if ($stmt->execute($params)) {
        if ($stmt->execute($params)) {
@@ -53,11 +53,11 @@ class MySQLDAO extends DAO {
        }
        }
    }
    }


    public function findTokenData($token) {
    public function findLoginToken($token) {


        $dbh = $this->getDBHandler();
        $dbh = $this->getDBHandler();


        $stmt = $dbh->prepare("SELECT data FROM token WHERE token = :token AND CURRENT_TIMESTAMP < TIMESTAMPADD(MINUTE,1,creation_time)");
        $stmt = $dbh->prepare("SELECT data FROM login_token WHERE token = :token AND CURRENT_TIMESTAMP < TIMESTAMPADD(MINUTE,1,creation_time)");
        $stmt->bindParam(':token', $token);
        $stmt->bindParam(':token', $token);


        $stmt->execute();
        $stmt->execute();
@@ -69,11 +69,11 @@ class MySQLDAO extends DAO {
        return null;
        return null;
    }
    }


    public function deleteToken($token) {
    public function deleteLoginToken($token) {


        $dbh = $this->getDBHandler();
        $dbh = $this->getDBHandler();


        $stmt = $dbh->prepare("DELETE FROM token WHERE token = :token");
        $stmt = $dbh->prepare("DELETE FROM login_token WHERE token = :token");
        $stmt->bindParam(':token', $token);
        $stmt->bindParam(':token', $token);
        $stmt->execute();
        $stmt->execute();
    }
    }
@@ -86,7 +86,7 @@ class MySQLDAO extends DAO {
                . " VALUES(:user_id, :type, :email, :name, :surname, :institution, :username, :local_db_id, :typed_id, :eppn)");
                . " VALUES(:user_id, :type, :email, :name, :surname, :institution, :username, :local_db_id, :typed_id, :eppn)");


        $stmt->bindParam(':user_id', $userId);
        $stmt->bindParam(':user_id', $userId);
        $stmt->bindParam(':type', $identity->getType());
        $stmt->bindParam(':type', $identity->type);
        $stmt->bindParam(':email', $identity->email);
        $stmt->bindParam(':email', $identity->email);
        $stmt->bindParam(':name', $identity->name);
        $stmt->bindParam(':name', $identity->name);
        $stmt->bindParam(':surname', $identity->surname);
        $stmt->bindParam(':surname', $identity->surname);
@@ -111,6 +111,23 @@ class MySQLDAO extends DAO {
        return $dbh->lastInsertId();
        return $dbh->lastInsertId();
    }
    }


    private function getIdentityByRow($row) {

        $identity = new Identity($row['type']);

        $identity->id = $row['id'];
        $identity->typedId = $row['typed_id'];
        $identity->email = $row['email'];
        $identity->localDBId = $row['local_db_id'];
        $identity->name = $row['name'];
        $identity->surname = $row['surname'];
        $identity->institution = $row['institution'];
        $identity->username = $row['username'];
        $identity->eppn = $row['eppn'];

        return $identity;
    }

    public function findUserById($userId) {
    public function findUserById($userId) {


        $dbh = $this->getDBHandler();
        $dbh = $this->getDBHandler();
@@ -121,20 +138,16 @@ class MySQLDAO extends DAO {
        $stmt->bindParam(':user_id', $userId);
        $stmt->bindParam(':user_id', $userId);
        $stmt->execute();
        $stmt->execute();


        $result = $stmt->fetchAll();
        if (count($result) === 0) {
            return null;
        }

        $user = new User();
        $user = new User();
        $user->id = $userId;
        $user->id = $userId;


        foreach ($stmt->fetchAll() as $row) {
        foreach ($result as $row) {
            $identity = new Identity($row['type']);
            $identity = $this->getIdentityByRow($row);
            $identity->id = $row['id'];
            $identity->typedId = $row['typed_id'];
            $identity->email = $row['email'];
            $identity->localDBId = $row['local_db_id'];
            $identity->name = $row['name'];
            $identity->surname = $row['surname'];
            $identity->institution = $row['institution'];
            $identity->username = $row['username'];
            $identity->eppn = $row['eppn'];
            $user->addIdentity($identity);
            $user->addIdentity($identity);
        }
        }


@@ -152,15 +165,25 @@ class MySQLDAO extends DAO {


        $dbh = $this->getDBHandler();
        $dbh = $this->getDBHandler();


        $stmt = $dbh->prepare("SELECT user_id FROM identity WHERE type = :type AND typed_id = :typed_id AND local_db_id = :local_db_id");
        $query = "SELECT user_id FROM identity WHERE type = :type AND typed_id = :typed_id";
        if (isset($dbIdentifier) && $dbIdentifier !== null) {
            $query .= " AND local_db_id = :local_db_id";
        }

        $stmt = $dbh->prepare($query);
        $stmt->bindParam(':type', $type);
        $stmt->bindParam(':type', $type);
        $stmt->bindParam(':typed_id', $identifier);
        $stmt->bindParam(':typed_id', $identifier);
        if (isset($dbIdentifier) && $dbIdentifier !== null) {
            $stmt->bindParam(':local_db_id', $dbIdentifier);
            $stmt->bindParam(':local_db_id', $dbIdentifier);
        }


        $stmt->execute();
        $stmt->execute();


        $result = $stmt->fetchAll();
        $result = $stmt->fetchAll();


        global $log;
        $log->debug("count = " . count($result));

        if (count($result) === 0) {
        if (count($result) === 0) {
            return null;
            return null;
        }
        }
@@ -172,6 +195,55 @@ class MySQLDAO extends DAO {
        return $this->findUserById($userId);
        return $this->findUserById($userId);
    }
    }


    public function searchUser($searchText) {

        $dbh = $this->getDBHandler();

        // TODO: Add additional email search...

        $query = "SELECT `user_id`, `id`, `type`, `typed_id`, `email`, `local_db_id`, `name`, `surname`, `institution`, `username`, `eppn`"
                . " FROM identity WHERE `email` LIKE :email OR `name` LIKE :name OR `surname` LIKE :surname";

        $stmt = $dbh->prepare($query);

        $searchParam = $searchText . '%';
        $stmt->bindParam(':email', $searchParam);
        $stmt->bindParam(':name', $searchParam);
        $stmt->bindParam(':surname', $searchParam);

        $stmt->execute();

        $userMap = array();

        //global $log;
        //$log->debug('In searchUser');

        foreach ($stmt->fetchAll() as $row) {

            //$log->debug($row['user_id']);

            $identity = $this->getIdentityByRow($row);
            //$log->debug(json_encode($identity));

            $userId = $row['user_id'];
            if (array_key_exists($userId, $userMap)) {
                $user = $userMap[$userId];
            } else {
                $user = new User();
                $user->id = $userId;
                $userMap[$userId] = $user;
            }

            array_push($user->identities, $identity);
        }

        $users = [];
        foreach ($userMap as $userId => $user) {
            array_push($users, $user);
        }
        return $users;
    }

    public function addEmailToUser($email, $userId) {
    public function addEmailToUser($email, $userId) {


        $dbh = $this->getDBHandler();
        $dbh = $this->getDBHandler();
@@ -183,4 +255,18 @@ class MySQLDAO extends DAO {
        $stmt->execute();
        $stmt->execute();
    }
    }


    public function createJoinRequest($token, $applicantUserId, $targetUserId) {

        $dbh = $this->getDBHandler();

        $stmt = $dbh->prepare("INSERT INTO `join_request`(`token`, `applicant_user_id`, `target_user_id`)"
                . " VALUES(:token, :applicant_user_id, :target_user_id)");

        $stmt->bindParam(':token', $token);
        $stmt->bindParam(':applicant_user_id', $applicantUserId);
        $stmt->bindParam(':target_user_id', $targetUserId);

        $stmt->execute();
    }

}
}
+33 −1
Original line number Original line Diff line number Diff line
@@ -26,8 +26,10 @@ namespace RAP;


class SessionData {
class SessionData {


    public $callback;
    private $callbackURL;
    private $callbackTitle;
    public $user;
    public $user;
    public $userSearchResults;


    public function save() {
    public function save() {
        $_SESSION['SessionData'] = $this;
        $_SESSION['SessionData'] = $this;
@@ -41,4 +43,34 @@ class SessionData {
        }
        }
        return $_SESSION['SessionData'];
        return $_SESSION['SessionData'];
    }
    }

    public function setCallbackURL($callbackURL) {
        $this->callbackURL = $callbackURL;
        $this->callbackTitle = CallbackHandler::getCallbackTitle($callbackURL);
        $this->save();
    }

    public function getCallbackURL() {
        return $this->callbackURL;
    }

    public function getCallbackTitle() {
        return $this->callbackTitle;
    }

    public function searchUser($searchText) {
        $users = DAO::get()->searchUser($searchText);

        $this->userSearchResults = [];
        foreach ($users as $user) {
            // this search shouldn't contains the user itself
            if ($user->id !== $this->user->id) {
                $searchResult = UserSearchResult::buildFromUser($user);
                array_push($this->userSearchResults, $searchResult);
            }
        }

        $this->save();
    }

}
}
Loading