-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbutil.cpp
408 lines (287 loc) · 10.1 KB
/
dbutil.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "dbutil.h"
#include <QtSql>
#include <QStringList>
#include <QCryptographicHash>
#include <QPixmap>
QString psqlSessionRole()
{
QString sr = QSqlDatabase::database("default").userName();
qDebug() << "psqlSessionRole(): " << sr;
return sr;
}
QString psqlCurrentRole(){
QSqlQuery query(QSqlDatabase::database("default"));
query.exec(QString("SELECT current_user"));
QString result = QString("desconocido");
if(query.first()){
result = query.value(0).toString();
}
query.finish();
qDebug() << "psqlCurrentRole(): " << result;
return result;
}
/**
* @brief psqlSessionIsAdmin
* @return true si la sesion es "admin", false en caso contrario
*/
bool psqlSessionIsAdmin(){
bool sessionIsAdmin = (0==QString::compare(psqlCurrentRole(),"admin"));
qDebug() << "psqlSessionIsAdmin(): " << sessionIsAdmin;
return sessionIsAdmin;
}
bool psqlAuthenticate(const QString &user, const QString &pw)
{
// Evitar inyeccion SQL
if(!psqlRoleExists(user))
return false;
QSqlQuery query(QSqlDatabase::database("default"));
bool result = false;
QString encryptedPassword = psqlEncryptPassword(user,pw);
query.exec(QString("SELECT check_password(%1, %2)")
.arg(psqlQuoteQueryString(user))
.arg(psqlQuoteQueryString(encryptedPassword))
);
if(query.first()){
bool ok= false;
int cnt = query.value(0).toInt(&ok);
result = ok && cnt == 1;
}
query.finish();
qDebug() << "psqlAuthenticate(): " << result;
return result;
}
QString psqlEncryptPassword(const QString &role, const QString &pw){
QCryptographicHash *qch = new QCryptographicHash(QCryptographicHash::Md5);
qch->addData(QString("%1%2").arg(pw).arg(role).toUtf8());
QString hash = qch->result().toHex().toLower();
delete qch;
return QString("%1%2").arg("md5").arg(hash);
}
QString psqlQuoteQueryString(const QString &str){
QString s = str;
s.replace(QString("\\"),QString("\\\\"));
s.replace(QString("'"),QString("\\'"));
s.prepend("'");
s.append("'");
return s ;
}
bool psqlSetPassword(const QString &role, const QString &pw){
QString r = role.trimmed();
QString p = pw.trimmed();
if ((r.isEmpty())||(p.isEmpty()))
return false;
if(!psqlRoleExists(r))
return false;
QSqlDatabase::database("default").transaction();
QSqlQuery query(QSqlDatabase::database("default"));
QString encryptedPassword = psqlEncryptPassword(r,p);
query.exec(QString("ALTER ROLE %1 WITH ENCRYPTED PASSWORD '%2'")
.arg(r)
.arg(encryptedPassword)
);
bool ok = QSqlDatabase::database("default").commit();
query.finish();
return ok;
}
QString psqlSessionInfo()
{
QSqlQuery query(QSqlDatabase::database("default"));
QString info;
query.exec(QString("SELECT session_user,"
" inet_client_addr(),"
" inet_client_port(),"
" inet_server_addr(),"
" inet_server_port(),"
" version();"));
if (query.first()){
info = QString("%1@%2:%3 conectado a %4:%5")
.arg(query.value(0).toString())
.arg(query.value(1).toString())
.arg(query.value(2).toString())
.arg(query.value(3).toString())
.arg(query.value(4).toString());
}
query.finish();
return info;
}
QString psqlVersion()
{
QSqlQuery query(QSqlDatabase::database("default"));
QString version = "Desconocido";
query.exec(QString("SELECT version();"));
if (query.first()){
version = QString("%1").arg(query.value(0).toString());
}
query.finish();
return version;
}
QStringList psqlRoles(){
QSqlQuery query(QSqlDatabase::database("default"));
QStringList roles;
query.exec(QString("SELECT rolname FROM pg_catalog.pg_roles"));
while (query.next()){
roles << QString("%1").arg(query.value(0).toString());
}
query.finish();
return roles;
}
QString psqlTest(){
QSqlQuery query(QSqlDatabase::database("default"));
query.exec(QString("SELECT * FROM nonexistent"));
return QString("Database text: %1\nDriver text: %2").arg(query.lastError().databaseText()).arg(query.lastError().driverText());
}
bool psqlAddRole(const QString &rolename, const QString &fullname){
QRegExp reRole(QString("^(\\w+)\\d*$"));
if(!reRole.exactMatch(rolename))
return false;
QSqlDatabase::database("default").transaction();
QSqlQuery query(QSqlDatabase::database("default"));
query.prepare(QString("INSERT INTO usuario(role,fullname) VALUES(?,?)"));
query.bindValue(0,rolename);
query.bindValue(1,fullname);
query.exec();
query.exec(QString("CREATE ROLE %1 LOGIN INHERIT").arg(rolename));
bool ok = QSqlDatabase::database("default").commit();
query.finish();
return ok;
}
bool psqlDisableRole(const QString &role){
if(!psqlRoleExists(role))
return false;
QSqlDatabase::database("default").transaction();
QSqlQuery query(QSqlDatabase::database("default"));
query.prepare(QString("UPDATE usuario SET active=false WHERE role=?"));
query.bindValue(0,role);
query.exec();
query.exec(QString("ALTER ROLE %1 WITH NOLOGIN").arg(role));
bool ok = QSqlDatabase::database("default").commit();
query.finish();
return ok;
}
bool psqlEnableRole(const QString &role){
if(!psqlRoleExists(role))
return false;
QSqlDatabase::database("default").transaction();
QSqlQuery query(QSqlDatabase::database("default"));
query.prepare(QString("UPDATE usuario SET active=true WHERE role=?"));
query.bindValue(0,role);
query.exec();
query.exec(QString("ALTER ROLE %1 WITH LOGIN").arg(role));
bool ok = QSqlDatabase::database("default").commit();
query.finish();
return ok;
}
bool psqlDelRole(const QString &role){
if(!psqlRoleExists(role))
return false;
QSqlDatabase::database("default").transaction();
QSqlQuery query(QSqlDatabase::database("default"));
query.prepare(QString("DELETE FROM usuario WHERE role=?"));
query.bindValue(0,role);
query.exec();
query.exec(QString("DROP ROLE %1").arg(role));
bool ok = QSqlDatabase::database("default").commit();
query.finish();
return ok;
}
bool psqlRoleExists(const QString &role){
QStringList users = psqlRoles();
return users.contains(role);
}
bool psqlGrantRole(const QString &grantRole, const QString &role){
if(!psqlRoleExists(grantRole) || !psqlRoleExists(role))
return false;
QSqlQuery query(QSqlDatabase::database("default"));
query.exec(QString("GRANT %1 TO %2;").arg(grantRole).arg(role));
bool ok = query.isActive();
query.finish();
qDebug() << "psqlGrantRole(" << grantRole << ", " << role << "): " << ok;
/// qDebug() << QString("Database text: %1\nDriver text: %2").arg(query.lastError().databaseText()).arg(query.lastError().driverText());
return ok;
}
bool psqlRevokeRole(const QString &revokeRole, const QString &role){
if(!psqlRoleExists(revokeRole) || !psqlRoleExists(role))
return false;
QSqlQuery query(QSqlDatabase::database("default"));
query.exec(QString("REVOKE %1 FROM %2;").arg(revokeRole).arg(role));
bool ok = query.isActive();
query.finish();
qDebug() << "psqlRevokeRole(" << revokeRole << ", " << role << "): " << ok;
/// qDebug() << QString("Database text: %1\nDriver text: %2").arg(query.lastError().databaseText()).arg(query.lastError().driverText());
return ok;
}
/*
* Eleva/modifica el rol de la sesion actual
*/
bool psqlSetRole(const QString &role){
if(!psqlRoleExists(role))
return false;
QSqlQuery query(QSqlDatabase::database("default"));
query.exec(QString("SET ROLE %1;").arg(role));
bool ok = query.isActive();
query.finish();
qDebug() << "psqlSetRole(): " << ok;
return ok;
}
/*
* Restaura rol de la sesion
*/
bool psqlResetRole(){
QSqlQuery query(QSqlDatabase::database("default"));
query.exec(QString("RESET ROLE;"));
bool ok = query.isActive();
query.finish();
qDebug() << "psqlResetRole(): " << ok;
return ok;
}
int savePixmap(const QPixmap &pmap){
if(!pmap.isNull()){
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
pmap.save(&buffer,"PNG");
QString base64 = bytes.toBase64().data();
QString sha1Hash = QCryptographicHash::hash(bytes, QCryptographicHash::Sha1).toHex();
QSqlQuery query(QSqlDatabase::database("default"));
query.prepare("SELECT id FROM pixmap WHERE hash=?");
query.bindValue(0,sha1Hash);
query.exec();
if(query.first()){
return query.value(0).toInt();
}
query.finish();
query.exec(QString("INSERT INTO pixmap(id,tstamp,userid,base64data,hash) "
"VALUES (DEFAULT,DEFAULT,DEFAULT,'%1','%2') RETURNING id").arg(base64).arg(sha1Hash));
if(query.first()){
return query.value(0).toInt();
}
}
return -1;
}
QPixmap retrievePixmap(int id){
QSqlQuery query(QSqlDatabase::database("default"));
query.exec(QString("SELECT base64data FROM pixmap WHERE id=%1").arg(id));
if(query.first()){
QByteArray base64 = query.value(0).toByteArray();
QByteArray bytes = QByteArray::fromBase64(base64);
QPixmap pixmap;
pixmap.loadFromData(bytes,"PNG");
return pixmap;
}
return QPixmap();
}
/*
bool testInsert(){
QSqlTableModel model(0, QSqlDatabase::database("default"));
model.setTable("prueba");
int colId = model.fieldIndex("id");
model.removeColumn(colId);
QSqlRecord rec= model.record();
// rec.setGenerated("id",false);
rec.setValue("valor", QVariant("algo"));
rec.setValue("tstamp", QDateTime::currentDateTime().toString(Qt::ISODate));
bool ok = model.insertRecord(-1,rec);
qDebug() << model.lastError().databaseText() << "\n" << model.query().lastQuery();
return ok;
}
*/