2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 * User manipulation methods
25 * $Date: 2010/01/19 11:07:25 $
29 #include "firebird_store.h"
32 #include "stg/user_conf.h"
33 #include "stg/user_stat.h"
34 #include "stg/user_traff.h"
35 #include "stg/user_ips.h"
36 #include "stg/const.h"
37 #include "stg/common.h"
39 //-----------------------------------------------------------------------------
40 int FIREBIRD_STORE::GetUsersList(std::vector<std::string> * usersList) const
42 STG_LOCKER lock(&mutex);
44 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
45 IBPP::Statement st = IBPP::StatementFactory(db, tr);
50 st->Execute("select name from tb_users");
55 usersList->push_back(name);
60 catch (IBPP::Exception & ex)
63 strError = "IBPP exception";
64 printfd(__FILE__, ex.what());
70 //-----------------------------------------------------------------------------
71 int FIREBIRD_STORE::AddUser(const std::string & name) const
73 STG_LOCKER lock(&mutex);
75 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
76 IBPP::Statement st = IBPP::StatementFactory(db, tr);
81 st->Prepare("execute procedure sp_add_user(?, ?)");
88 catch (IBPP::Exception & ex)
91 strError = "IBPP exception";
92 printfd(__FILE__, ex.what());
98 //-----------------------------------------------------------------------------
99 int FIREBIRD_STORE::DelUser(const std::string & login) const
101 STG_LOCKER lock(&mutex);
103 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
104 IBPP::Statement st = IBPP::StatementFactory(db, tr);
109 st->Prepare("execute procedure sp_delete_user(?)");
115 catch (IBPP::Exception & ex)
118 strError = "IBPP exception";
119 printfd(__FILE__, ex.what());
125 //-----------------------------------------------------------------------------
126 int FIREBIRD_STORE::SaveUserStat(const STG::UserStat & stat,
127 const std::string & login) const
129 STG_LOCKER lock(&mutex);
131 return SaveStat(stat, login);
133 //-----------------------------------------------------------------------------
134 int FIREBIRD_STORE::SaveStat(const STG::UserStat & stat,
135 const std::string & login,
139 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
140 IBPP::Statement st = IBPP::StatementFactory(db, tr);
145 st->Prepare("select pk_user from tb_users where name = ?");
150 strError = "User \"" + login + "\" not found in database";
151 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
155 int32_t uid = Get<int32_t>(st, 1);
157 st->Prepare("select first 1 pk_stat from tb_stats where fk_user = ? order by stats_date desc");
163 strError = "No stat info for user \"" + login + "\"";
164 printfd(__FILE__, "No stat info for user '%s'\n", login.c_str());
171 IBPP::Timestamp actTime;
172 time_t2ts(stat.lastActivityTime, &actTime);
173 IBPP::Timestamp addTime;
174 time_t2ts(stat.lastCashAddTime, &addTime);
177 ym2date(year, month, &dt);
181 st->Prepare("update tb_stats set \
184 last_activity_time = ?, \
186 last_cash_add_time = ?, \
191 st->Set(1, stat.cash);
192 st->Set(2, stat.freeMb);
194 st->Set(4, stat.lastCashAdd);
196 st->Set(6, (int32_t)stat.passiveTime);
203 for(int i = 0; i < DIR_NUM; i++)
205 st->Prepare("update tb_stats_traffic set \
208 where fk_stat = ? and dir_num = ?");
209 st->Set(1, (int64_t)stat.monthUp[i]);
210 st->Set(2, (int64_t)stat.monthDown[i]);
220 catch (IBPP::Exception & ex)
223 strError = "IBPP exception";
224 printfd(__FILE__, ex.what());
230 //-----------------------------------------------------------------------------
231 int FIREBIRD_STORE::SaveUserConf(const STG::UserConf & conf,
232 const std::string & login) const
234 STG_LOCKER lock(&mutex);
236 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
237 IBPP::Statement st = IBPP::StatementFactory(db, tr);
242 st->Prepare("select pk_user from tb_users where name = ?");
247 strError = "User \"" + login + "\" not found in database";
248 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
256 IBPP::Timestamp creditExpire;
257 time_t2ts(conf.creditExpire, &creditExpire);
259 st->Prepare("update tb_users set \
265 disabled_detail_stat = ?, \
272 fk_tariff = (select pk_tariff from tb_tariffs \
274 fk_tariff_change = (select pk_tariff from tb_tariffs \
276 fk_corporation = (select pk_corporation from tb_corporations \
281 st->Set(1, conf.address);
282 st->Set(2, (bool)conf.alwaysOnline);
283 st->Set(3, conf.credit);
284 st->Set(4, creditExpire);
285 st->Set(5, (bool)conf.disabled);
286 st->Set(6, (bool)conf.disabledDetailStat);
287 st->Set(7, conf.email);
288 st->Set(8, conf.group);
289 st->Set(9, conf.note);
290 st->Set(10, (bool)conf.passive);
291 st->Set(11, conf.password);
292 st->Set(12, conf.phone);
293 st->Set(13, conf.tariffName);
294 st->Set(14, conf.nextTariff);
295 st->Set(15, conf.corp);
296 st->Set(16, conf.realName);
302 st->Prepare("delete from tb_users_services where fk_user = ?");
307 st->Prepare("insert into tb_users_services (fk_user, fk_service) \
308 values (?, (select pk_service from tb_services \
310 for(std::vector<std::string>::const_iterator it = conf.services.begin(); it != conf.services.end(); ++it)
318 st->Prepare("delete from tb_users_data where fk_user = ?");
324 st->Prepare("insert into tb_users_data (fk_user, data, num) values (?, ?, ?)");
325 for (std::vector<std::string>::const_iterator it = conf.userdata.begin(); it != conf.userdata.end(); ++it)
334 st->Prepare("delete from tb_allowed_ip where fk_user = ?");
338 st->Prepare("insert into tb_allowed_ip (fk_user, ip, mask) values (?, ?, ?)");
339 for(size_t j = 0; i < conf.ips.count(); j++)
342 st->Set(2, (int32_t)conf.ips[j].ip);
343 st->Set(3, (int32_t)conf.ips[j].mask);
348 catch (IBPP::Exception & ex)
351 strError = "IBPP exception";
352 printfd(__FILE__, ex.what());
358 //-----------------------------------------------------------------------------
359 int FIREBIRD_STORE::RestoreUserStat(STG::UserStat * stat,
360 const std::string & login) const
362 STG_LOCKER lock(&mutex);
364 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
365 IBPP::Statement st = IBPP::StatementFactory(db, tr);
370 st->Prepare("select pk_user from tb_users where name = ?");
375 strError = "User \"" + login + "\" not found in database";
376 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
383 st->Prepare("select first 1 pk_stat, cash, free_mb, last_activity_time, \
384 last_cash_add, last_cash_add_time, passive_time from tb_stats \
385 where fk_user = ? order by stats_date desc");
390 strError = "No stat info for user \"" + login + "\"";
391 printfd(__FILE__, "No stat info for user '%s'\n", login.c_str());
398 st->Get(2, stat->cash);
399 st->Get(3, stat->freeMb);
400 IBPP::Timestamp actTime;
402 st->Get(5, stat->lastCashAdd);
403 IBPP::Timestamp addTime;
406 st->Get(7, passiveTime);
408 stat->passiveTime = passiveTime;
410 stat->lastActivityTime = ts2time_t(actTime);
412 stat->lastCashAddTime = ts2time_t(addTime);
415 st->Prepare("select * from tb_stats_traffic where fk_stat = ?");
418 for(int i = 0; i < DIR_NUM; i++)
424 st->Get(5, (int64_t &)stat->monthUp[dir]);
425 st->Get(4, (int64_t &)stat->monthDown[dir]);
435 catch (IBPP::Exception & ex)
438 strError = "IBPP exception";
439 printfd(__FILE__, ex.what());
445 //-----------------------------------------------------------------------------
446 int FIREBIRD_STORE::RestoreUserConf(STG::UserConf * conf,
447 const std::string & login) const
449 STG_LOCKER lock(&mutex);
451 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
452 IBPP::Statement st = IBPP::StatementFactory(db, tr);
457 st->Prepare("select tb_users.pk_user, tb_users.address, tb_users.always_online, \
458 tb_users.credit, tb_users.credit_expire, tb_users.disabled, \
459 tb_users.disabled_detail_stat, tb_users.email, tb_users.grp, \
460 tb_users.note, tb_users.passive, tb_users.passwd, tb_users.phone, \
461 tb_users.real_name, tf1.name, tf2.name, tb_corporations.name \
462 from tb_users left join tb_tariffs tf1 \
463 on tf1.pk_tariff = tb_users.fk_tariff \
464 left join tb_tariffs tf2 \
465 on tf2.pk_tariff = tb_users.fk_tariff_change \
466 left join tb_corporations \
467 on tb_corporations.pk_corporation = tb_users.fk_corporation \
468 where tb_users.name = ?");
473 strError = "User \"" + login + "\" not found in database";
474 printfd(__FILE__, "User '%s' not found in database", login.c_str());
480 // Getting base config
481 st->Get(2, conf->address);
484 conf->alwaysOnline = test;
485 st->Get(4, conf->credit);
486 IBPP::Timestamp timestamp;
487 st->Get(5, timestamp);
489 conf->creditExpire = ts2time_t(timestamp);
492 conf->disabled = test;
494 conf->disabledDetailStat = test;
495 st->Get(8, conf->email);
496 st->Get(9, conf->group);
497 st->Get(10, conf->note);
499 conf->passive = test;
500 st->Get(12, conf->password);
501 st->Get(13, conf->phone);
502 st->Get(14, conf->realName);
503 st->Get(15, conf->tariffName);
504 st->Get(16, conf->nextTariff);
505 st->Get(17, conf->corp);
507 if (conf->tariffName == "")
508 conf->tariffName = NO_TARIFF_NAME;
509 if (conf->corp == "")
510 conf->corp = NO_CORP_NAME;
514 st->Prepare("select name from tb_services \
515 where pk_service in \
516 (select fk_service from tb_users_services \
517 where fk_user = ?)");
524 conf->services.push_back(name);
529 st->Prepare("select data, num from tb_users_data where fk_user = ? order by num");
536 st->Get(1, conf->userdata[i]);
541 st->Prepare("select ip, mask from tb_allowed_ip \
549 st->Get(1, (int32_t &)im.ip);
550 st->Get(2, (int32_t &)im.mask);
557 catch (IBPP::Exception & ex)
560 strError = "IBPP exception";
561 printfd(__FILE__, ex.what());
567 //-----------------------------------------------------------------------------
568 int FIREBIRD_STORE::WriteUserChgLog(const std::string & login,
569 const std::string & admLogin,
571 const std::string & paramName,
572 const std::string & oldValue,
573 const std::string & newValue,
574 const std::string & message = "") const
576 STG_LOCKER lock(&mutex);
578 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
579 IBPP::Statement st = IBPP::StatementFactory(db, tr);
583 std::string temp = ""; // Composed message for log
588 temp += "Admin \"" + admLogin + "\", ";
589 temp += inet_ntostring(admIP);
591 temp = temp + message;
592 //----------------------------------------------------------------------------------------
593 // Checking and inserting parameters in params table
594 st->Prepare("select pk_parameter from tb_parameters where name = ?");
595 st->Set(1, paramName);
600 st->Prepare("insert into tb_parameters (name) values (?)");
601 st->Set(1, paramName);
605 //----------------------------------------------------------------------------------------
606 st->Prepare("insert into tb_params_log \
607 (fk_user, fk_parameter, event_time, from_val, to_val, comment) \
608 values ((select pk_user from tb_users \
610 (select pk_parameter from tb_parameters \
614 st->Set(2, paramName);
616 st->Set(4, oldValue);
617 st->Set(5, newValue);
623 catch (IBPP::Exception & ex)
626 strError = "IBPP exception";
627 printfd(__FILE__, ex.what());
633 //-----------------------------------------------------------------------------
634 int FIREBIRD_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
636 STG_LOCKER lock(&mutex);
638 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
639 IBPP::Statement st = IBPP::StatementFactory(db, tr);
646 st->Prepare("execute procedure sp_append_session_log(?, ?, 'c', ?)");
649 st->Set(3, (int32_t)ip);
653 catch (IBPP::Exception & ex)
656 strError = "IBPP exception";
657 printfd(__FILE__, ex.what());
663 //-----------------------------------------------------------------------------
664 int FIREBIRD_STORE::WriteUserDisconnect(const std::string & login,
665 const STG::DirTraff & up,
666 const STG::DirTraff & down,
667 const STG::DirTraff & sessionUp,
668 const STG::DirTraff & sessionDown,
671 const std::string & /*reason*/) const
673 STG_LOCKER lock(&mutex);
675 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
676 IBPP::Statement st = IBPP::StatementFactory(db, tr);
683 st->Prepare("execute procedure sp_append_session_log(?, ?, 'd', 0)");
689 st->Prepare("insert into tb_sessions_data \
690 (fk_session_log, dir_num, session_upload, \
691 session_download, month_upload, month_download) \
692 values (?, ?, ?, ?, ?, ?)");
693 for(int i = 0; i < DIR_NUM; i++)
697 st->Set(3, (int64_t)sessionUp[i]);
698 st->Set(4, (int64_t)sessionDown[i]);
699 st->Set(5, (int64_t)up[i]);
700 st->Set(6, (int64_t)down[i]);
706 catch (IBPP::Exception & ex)
709 strError = "IBPP exception";
710 printfd(__FILE__, ex.what());
716 //-----------------------------------------------------------------------------
717 int FIREBIRD_STORE::WriteDetailedStat(const STG::TraffStat & statTree,
719 const std::string & login) const
721 STG_LOCKER lock(&mutex);
723 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
724 IBPP::Statement st = IBPP::StatementFactory(db, tr);
726 IBPP::Timestamp statTime, now;
729 time_t2ts(lastStat, &statTime);
734 STG::TraffStat::const_iterator it;
735 it = statTree.begin();
736 st->Prepare("insert into tb_detail_stats \
737 (till_time, from_time, fk_user, dir_num, \
738 ip, download, upload, cost) \
739 values (?, ?, (select pk_user from tb_users \
742 while (it != statTree.end())
745 st->Set(2, statTime);
747 st->Set(4, it->first.dir);
748 st->Set(5, (int32_t)it->first.ip);
749 st->Set(6, (int64_t)it->second.down);
750 st->Set(7, (int64_t)it->second.up);
751 st->Set(8, it->second.cash);
758 catch (IBPP::Exception & ex)
761 strError = "IBPP exception";
762 printfd(__FILE__, ex.what());
768 //-----------------------------------------------------------------------------
769 int FIREBIRD_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const
771 STG_LOCKER lock(&mutex);
773 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
774 IBPP::Statement st = IBPP::StatementFactory(db, tr);
781 if (SaveStat(stat, login, year, month))
790 st->Prepare("execute procedure sp_add_stat(?, 0, 0, ?, 0, ?, 0, ?)");
801 st->Prepare("insert into tb_stats_traffic \
802 (fk_stat, dir_num, upload, download) \
803 values (?, ?, 0, 0)");
805 for(int i = 0; i < DIR_NUM; i++)
814 catch (IBPP::Exception & ex)
817 strError = "IBPP exception";
818 printfd(__FILE__, ex.what());
824 //-----------------------------------------------------------------------------