Newer
Older
Sonia Zorba
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?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() {
$connectionString = "mysql:host=" . $this->config['hostname'] . ";dbname=" . $this->config['dbname'];
return new PDO($connectionString, $this->config['username'], $this->config['password']);
}
public function insertTokenData($token, $data) {
global $log;
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("INSERT INTO token (token, data) VALUES(:token, :data)");
$params = array(
':token' => $token,
':data' => $data
);
if ($stmt->execute($params)) {
return $token;
} else {
$log->error($stmt->errorInfo()[2]);
throw new \Exception("SQL error while storing user token");
}
}
public function findTokenData($token) {
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("SELECT data FROM token WHERE token = :token AND CURRENT_TIMESTAMP < TIMESTAMPADD(MINUTE,1,creation_time)");
$stmt->bindParam(':token', $token);
$stmt->execute();
foreach ($stmt->fetchAll() as $row) {
return $row['data'];
}
return null;
}
public function deleteToken($token) {
$dbh = $this->getDBHandler();
$stmt = $dbh->prepare("DELETE FROM token WHERE token = :token");
$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->getType());
$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();
}
public function findUserById($userId) {
$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();
$user = new User();
$user->id = $userId;
foreach ($stmt->fetchAll() as $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'];
$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();
$stmt = $dbh->prepare("SELECT user_id FROM identity WHERE type = :type AND typed_id = :typed_id AND local_db_id = :local_db_id");
$stmt->bindParam(':type', $type);
$stmt->bindParam(':typed_id', $identifier);
$stmt->bindParam(':local_db_id', $dbIdentifier);
$stmt->execute();
$result = $stmt->fetchAll();
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);
}
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();
}
}