2020-05-08 12:43:46 +02:00
|
|
|
#include "msql_acces.h"
|
|
|
|
#include <iostream>
|
2020-05-26 21:58:33 +02:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-06-03 19:00:32 +02:00
|
|
|
std::string msql_acces::user="root";
|
|
|
|
std::string msql_acces::pass="1234";
|
|
|
|
msql_acces* msql_acces::instance=nullptr;
|
|
|
|
|
|
|
|
msql_acces* msql_acces::get_instance(){
|
|
|
|
if(msql_acces::instance==nullptr){
|
|
|
|
msql_acces::instance=new msql_acces();
|
|
|
|
}
|
|
|
|
return msql_acces::instance;
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:43:46 +02:00
|
|
|
msql_acces::msql_acces()
|
|
|
|
{
|
|
|
|
driver = get_driver_instance();
|
2020-06-03 19:00:32 +02:00
|
|
|
con = driver->connect("tcp://127.0.0.1:3306", msql_acces::user, msql_acces::pass);
|
2020-05-08 12:43:46 +02:00
|
|
|
con->setSchema("bin_database");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string msql_acces::get_passwd(std::string username){
|
|
|
|
sql::PreparedStatement *pstmt = con->prepareStatement("select passwd from users where username=?");
|
|
|
|
pstmt->setString(1,username);
|
|
|
|
sql::ResultSet *res = pstmt->executeQuery();
|
|
|
|
std::string ret;
|
|
|
|
while(res->next())
|
|
|
|
ret = res->getString("passwd");
|
|
|
|
delete res;
|
2020-05-31 19:24:35 +02:00
|
|
|
delete pstmt;
|
2020-05-08 12:43:46 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2020-05-13 00:58:32 +02:00
|
|
|
|
2020-05-26 18:48:31 +02:00
|
|
|
bool msql_acces::get_admin(std::string username){
|
|
|
|
sql::PreparedStatement *pstmt = con->prepareStatement("select admin from users where username=?");
|
|
|
|
pstmt->setString(1,username);
|
|
|
|
sql::ResultSet *res = pstmt->executeQuery();
|
|
|
|
bool ret=false;
|
|
|
|
while(res->next())
|
|
|
|
ret = res->getBoolean("admin");
|
|
|
|
delete res;
|
2020-05-31 19:24:35 +02:00
|
|
|
delete pstmt;
|
2020-05-26 18:48:31 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-13 00:58:32 +02:00
|
|
|
std::list<std::string> msql_acces::get_pinfo(){
|
2020-05-19 20:36:56 +02:00
|
|
|
sql::PreparedStatement *pstmt =
|
|
|
|
con->prepareStatement("select packages.name, packages.created, packages.uses, users.username "
|
|
|
|
"from packages "
|
|
|
|
"join users on users.id=packages.user ");
|
2020-05-13 00:58:32 +02:00
|
|
|
sql::ResultSet *res = pstmt->executeQuery();
|
|
|
|
std::list<std::string> ret;
|
|
|
|
std::string aux;
|
|
|
|
while(res->next()){
|
|
|
|
aux="";
|
2020-05-19 20:36:56 +02:00
|
|
|
aux += res->getString(1);
|
|
|
|
aux += ":"+res->getString(2);
|
|
|
|
if(res->getBoolean(3)){
|
|
|
|
aux+=":t";
|
2020-05-13 00:58:32 +02:00
|
|
|
}else{
|
2020-05-19 20:36:56 +02:00
|
|
|
aux+=":f";
|
2020-05-13 00:58:32 +02:00
|
|
|
}
|
2020-05-19 20:36:56 +02:00
|
|
|
aux += ":"+res->getString(4);
|
|
|
|
ret.push_back(aux);
|
2020-05-13 00:58:32 +02:00
|
|
|
}
|
|
|
|
delete res;
|
2020-05-31 19:24:35 +02:00
|
|
|
delete pstmt;
|
2020-05-13 00:58:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2020-05-15 21:39:23 +02:00
|
|
|
|
2020-05-20 00:10:05 +02:00
|
|
|
std::list<std::string> msql_acces::get_uinfo(){
|
|
|
|
sql::PreparedStatement *pstmt =
|
|
|
|
con->prepareStatement("select username,admin from users");
|
|
|
|
sql::ResultSet *res = pstmt->executeQuery();
|
|
|
|
std::list<std::string> ret;
|
|
|
|
std::string aux;
|
|
|
|
while(res->next()){
|
|
|
|
aux="";
|
|
|
|
aux += res->getString(1);
|
|
|
|
//aux += ":"+res->getString(2);
|
|
|
|
if(res->getBoolean(2)){
|
|
|
|
aux+=":t";
|
|
|
|
}else{
|
|
|
|
aux+=":f";
|
|
|
|
}
|
|
|
|
ret.push_back(aux);
|
|
|
|
}
|
|
|
|
delete res;
|
2020-05-31 19:24:35 +02:00
|
|
|
delete pstmt;
|
2020-05-20 00:10:05 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-15 21:39:23 +02:00
|
|
|
void msql_acces::write_install(std::string package, std::string user){
|
|
|
|
sql::PreparedStatement *pstmt =
|
|
|
|
con->prepareStatement("insert into packages(name,user) values(?,(select id from users where username=?))");
|
|
|
|
pstmt->setString(1,package);
|
|
|
|
pstmt->setString(2,user);
|
|
|
|
pstmt->executeUpdate();
|
2020-05-31 19:24:35 +02:00
|
|
|
delete pstmt;
|
2020-05-15 21:39:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void msql_acces::write_remove(std::string package){
|
|
|
|
sql::PreparedStatement *pstmt =
|
|
|
|
con->prepareStatement("delete from packages where name=?");
|
|
|
|
pstmt->setString(1,package);
|
|
|
|
pstmt->executeUpdate();
|
2020-05-31 19:24:35 +02:00
|
|
|
delete pstmt;
|
2020-05-15 21:39:23 +02:00
|
|
|
}
|
2020-05-16 20:52:50 +02:00
|
|
|
|
|
|
|
bool msql_acces::get_package_exists(std::string package){
|
|
|
|
sql::PreparedStatement *pstmt = con->prepareStatement("select count(id) from packages where name=?");
|
|
|
|
pstmt->setString(1,package);
|
|
|
|
sql::ResultSet *res = pstmt->executeQuery();
|
|
|
|
int ret=0;
|
2020-05-31 19:24:35 +02:00
|
|
|
while(res->next()){
|
2020-05-16 20:52:50 +02:00
|
|
|
ret = res->getInt(1);
|
2020-05-31 19:24:35 +02:00
|
|
|
}
|
|
|
|
delete pstmt;
|
2020-05-16 20:52:50 +02:00
|
|
|
return ret>0;
|
|
|
|
}
|
2020-05-21 20:57:18 +02:00
|
|
|
|
|
|
|
void msql_acces::create_user(std::string user, std::string pass, bool admin){
|
|
|
|
sql::PreparedStatement *pstmt = con->prepareStatement("insert into users(username, passwd, admin) values(?, ?, ?)");
|
|
|
|
pstmt->setString(1,user);
|
2020-05-31 19:24:35 +02:00
|
|
|
pstmt->setString(2,std::string(data_acces::get_hash(&pass[0])));
|
2020-05-21 20:57:18 +02:00
|
|
|
pstmt->setBoolean(3,admin);
|
2020-05-26 21:58:33 +02:00
|
|
|
pstmt->executeQuery();
|
2020-05-31 19:24:35 +02:00
|
|
|
delete pstmt;
|
2020-05-21 20:57:18 +02:00
|
|
|
}
|
2020-05-26 01:37:34 +02:00
|
|
|
|
|
|
|
void msql_acces::remove_user(std::string user){
|
|
|
|
sql::PreparedStatement *pstmt =
|
|
|
|
con->prepareStatement("delete from users where username=?");
|
|
|
|
pstmt->setString(1,user);
|
|
|
|
pstmt->executeUpdate();
|
2020-05-31 19:24:35 +02:00
|
|
|
delete pstmt;
|
2020-05-26 01:37:34 +02:00
|
|
|
}
|