diff --git a/classes/datalayer/UserDAO.php b/classes/datalayer/UserDAO.php index 9b8a0a6ddd4c5c896adb2c0b2e0cb43332b7b441..f1342de14d399f8052b2f081eee5da0c87d447bb 100644 --- a/classes/datalayer/UserDAO.php +++ b/classes/datalayer/UserDAO.php @@ -61,6 +61,13 @@ interface UserDAO { */ function searchUser($searchText); + /** + * Retrieve a list of all users having given identifiers. + * @param array $identifiers + * @return array + */ + function getUsers(array $identifiers): array; + /** * Perform a join request. * @param type $userId1 the user that will receive all identities diff --git a/classes/datalayer/mysql/MySQLUserDAO.php b/classes/datalayer/mysql/MySQLUserDAO.php index 6ec181240ae2e6706ee934f7bf647ca9eabf48bf..9a4b211793e63a971b803452e76d5f7aff521204 100644 --- a/classes/datalayer/mysql/MySQLUserDAO.php +++ b/classes/datalayer/mysql/MySQLUserDAO.php @@ -174,6 +174,47 @@ class MySQLUserDAO extends BaseMySQLDAO implements UserDAO { $stmt->bindParam(':surname', $searchParam); $stmt->bindParam(':namesurname', $searchParam); + return $this->getUsersListFromStatement($stmt); + } + + public function getUsers(array $identifiers): array { + + if (count($identifiers) === 0) { + return []; + } + + $dbh = $this->getDBHandler(); + + $query = "SELECT `user_id`, (u.`primary_identity` = i.`id`) AS `primary`," + . " i.`id`, `type`, `typed_id`, `email`, `name`, `surname`, `institution`, `eppn`" + . " FROM identity i" + . " JOIN `user` u on u.id = i.user_id" + . " WHERE i.user_id IN ("; + + $first = true; + foreach ($identifiers as $id) { + if (!$first) { + $query .= ','; + } + $query .= ':id_' . $id; + if ($first) { + $first = !$first; + } + } + + $query .= ')'; + + $stmt = $dbh->prepare($query); + + foreach ($identifiers as $id) { + $stmt->bindParam(':id_' . $id, $id); + } + + return $this->getUsersListFromStatement($stmt); + } + + private function getUsersListFromStatement(\PDOStatement $stmt): array { + $stmt->execute(); $userMap = array(); diff --git a/include/rest-web-service.php b/include/rest-web-service.php index a09c66ef8cd3d28cfb8a16deb73e7b9166dae89d..89e738b9b59f8b4449417db619551df95b68bae0 100644 --- a/include/rest-web-service.php +++ b/include/rest-web-service.php @@ -65,8 +65,17 @@ Flight::route('GET ' . $WS_PREFIX . '/user', function() { $locator->getOAuth2RequestHandler()->validateToken(); $searchText = Flight::request()->query['search']; + if ($searchText !== null) { + $users = $locator->getUserDAO()->searchUser($searchText); + } else { + $identifiers = Flight::request()->query['identifiers']; + if ($identifiers === null) { + throw new \RAP\BadRequestException("Missing identifiers parameters"); + } + $identifiers = explode(',', $identifiers); + $users = $locator->getUserDAO()->getUsers($identifiers); + } - $users = $locator->getUserDAO()->searchUser($searchText); Flight::json($users); }); @@ -115,9 +124,9 @@ Flight::route('POST ' . $WS_PREFIX . '/user', function() { Flight::route('POST ' . $WS_PREFIX . '/join', function() { global $locator; - + $locator->getOAuth2RequestHandler()->validateToken(); - + $postData = Flight::request()->data; $userId1 = $postData['user1'];