Newer
Older
Sonia Zorba
committed
<?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;
use PDO;
class MySQLDAO extends DAO {
public function getDBHandler() {
global $DATABASE;
$connectionString = "mysql:host=" . $DATABASE['hostname'] . ";dbname=" . $DATABASE['dbname'];
$dbh = new PDO($connectionString, $DATABASE['username'], $DATABASE['password']);
// For transaction errors (see https://stackoverflow.com/a/9659366/771431)
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
Sonia Zorba
committed
}
public function createLoginToken($token, $userId) {
Sonia Zorba
committed
global $log;
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("INSERT INTO login_token (token, data) VALUES(:token, :data)");
Sonia Zorba
committed
$params = array(
':token' => $token,
Sonia Zorba
committed
);
if ($stmt->execute($params)) {
return $token;
} else {
$log->error($stmt->errorInfo()[2]);
throw new \Exception("SQL error while storing user token");
}
}
public function findLoginToken($token) {
Sonia Zorba
committed
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("SELECT data FROM login_token WHERE token = :token AND CURRENT_TIMESTAMP < TIMESTAMPADD(MINUTE,1,creation_time)");
Sonia Zorba
committed
$stmt->bindParam(':token', $token);
$stmt->execute();
foreach ($stmt->fetchAll() as $row) {
return $row['data'];
}
return null;
}
public function deleteLoginToken($token) {
Sonia Zorba
committed
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("DELETE FROM login_token WHERE token = :token");
Sonia Zorba
committed
$stmt->bindParam(':token', $token);
$stmt->execute();
}
public function insertIdentity(Identity $identity, $userId) {
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("INSERT INTO identity(`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(':type', $identity->type);
Sonia Zorba
committed
$stmt->bindParam(':email', $identity->email);
$stmt->bindParam(':name', $identity->name);
$stmt->bindParam(':surname', $identity->surname);
$stmt->bindParam(':institution', $identity->institution);
$stmt->bindParam(':username', $identity->username);
$stmt->bindParam(':local_db_id', $identity->localDBId);
$stmt->bindParam(':typed_id', $identity->typedId);
$stmt->bindParam(':eppn', $identity->eppn);
$stmt->execute();
return $dbh->lastInsertId();
}
public function createUser() {
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("INSERT INTO user() VALUES()");
$stmt->execute();
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;
}
Sonia Zorba
committed
public function findUserById($userId) {
if (!filter_var($userId, FILTER_VALIDATE_INT)) {
return null;
}
Sonia Zorba
committed
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("SELECT `id`, `type`, `typed_id`, `email`, `local_db_id`, `name`, `surname`, `institution`, `username`, `eppn`"
. " FROM identity WHERE user_id = :user_id");
$stmt->bindParam(':user_id', $userId);
$stmt->execute();
$result = $stmt->fetchAll();
if (count($result) === 0) {
return null;
}
Sonia Zorba
committed
$user = new User();
$user->id = $userId;
foreach ($result as $row) {
$identity = $this->getIdentityByRow($row);
Sonia Zorba
committed
$user->addIdentity($identity);
}
$stmtMail = $dbh->prepare("SELECT `email` FROM `additional_email` WHERE `user_id` = :user_id");
$stmtMail->bindParam(':user_id', $userId);
$stmtMail->execute();
foreach ($stmtMail->fetchAll() as $row) {
$user->addAdditionalEmail($row['email']);
}
return $user;
}
public function findUserByIdentity($type, $identifier, $dbIdentifier) {
$dbh = $this->getDBHandler();
$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);
Sonia Zorba
committed
$stmt->bindParam(':type', $type);
$stmt->bindParam(':typed_id', $identifier);
if (isset($dbIdentifier) && $dbIdentifier !== null) {
$stmt->bindParam(':local_db_id', $dbIdentifier);
}
Sonia Zorba
committed
$stmt->execute();
$result = $stmt->fetchAll();
global $log;
$log->debug("count = " . count($result));
Sonia Zorba
committed
if (count($result) === 0) {
return null;
}
if (count($result) > 1) {
throw new Exception("Found multiple users associated to the same identity!");
}
$userId = $result[0]['user_id'];
return $this->findUserById($userId);
}
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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;
}
Sonia Zorba
committed
public function addEmailToUser($email, $userId) {
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("INSERT INTO `additional_email`(`user_id`, `email`) VALUES(:user_id, :email)");
$stmt->bindParam(':user_id', $userId);
$stmt->bindParam(':email', $email);
$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();
}
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
public function findJoinRequest($token) {
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("SELECT `applicant_user_id`, `target_user_id` FROM `join_request` WHERE `token` = :token");
$stmt->bindParam(':token', $token);
$stmt->execute();
$result = $stmt->fetchAll();
switch (count($result)) {
case 0:
return null;
case 1:
$row = $result[0];
return [$row['applicant_user_id'], $row['target_user_id']];
default:
throw new Exception("Found multiple join request with the same token");
}
}
public function deleteUser($userId) {
$dbh = $this->getDBHandler();
$stmt3 = $dbh->prepare("DELETE FROM TABLE `user` WHERE `id` = :id2");
$stmt3->bindParam(':id2', $userId);
$stmt3->execute();
}
public function joinUsers($userId1, $userId2) {
$dbh = $this->getDBHandler();
try {
$dbh->beginTransaction();
// Moving identities from user2 to user1
$stmt1 = $dbh->prepare("UPDATE `identity` SET `user_id` = :id1 WHERE `user_id` = :id2");
$stmt1->bindParam(':id1', $userId1);
$stmt1->bindParam(':id2', $userId2);
$stmt1->execute();
// Moving additional email addresses from user2 to user1
$stmt2 = $dbh->prepare("UPDATE `additional_email` SET `user_id` = :id1 WHERE `user_id` = :id2");
$stmt2->bindParam(':id1', $userId1);
$stmt2->bindParam(':id2', $userId2);
$stmt2->execute();
// Deleting user2 join requests
$stmt3 = $dbh->prepare("DELETE FROM `join_request` WHERE `target_user_id` = :id2");
$stmt3->bindParam(':id2', $userId2);
$stmt3->execute();
// Deleting user2
$stmt4 = $dbh->prepare("DELETE FROM `user` WHERE `id` = :id2");
$stmt4->bindParam(':id2', $userId2);
$stmt4->execute();
$dbh->commit();
} catch (Exception $ex) {
$dbh->rollBack();
throw $ex;
}
}
public function deleteJoinRequest($token) {
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("DELETE FROM `join_request` WHERE `token` = :token");
$stmt->bindParam(':token', $token);
$stmt->execute();
}