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);
52 st->Execute("select name from tb_users");
56 usersList->push_back(name);
61 catch (IBPP::Exception & ex)
64 strError = "IBPP exception";
65 printfd(__FILE__, ex.what());
71 //-----------------------------------------------------------------------------
72 int FIREBIRD_STORE::AddUser(const std::string & name) const
74 STG_LOCKER lock(&mutex);
76 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
77 IBPP::Statement st = IBPP::StatementFactory(db, tr);
82 st->Prepare("execute procedure sp_add_user(?, ?)");
89 catch (IBPP::Exception & ex)
92 strError = "IBPP exception";
93 printfd(__FILE__, ex.what());
99 //-----------------------------------------------------------------------------
100 int FIREBIRD_STORE::DelUser(const std::string & login) const
102 STG_LOCKER lock(&mutex);
104 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
105 IBPP::Statement st = IBPP::StatementFactory(db, tr);
110 st->Prepare("execute procedure sp_delete_user(?)");
116 catch (IBPP::Exception & ex)
119 strError = "IBPP exception";
120 printfd(__FILE__, ex.what());
126 //-----------------------------------------------------------------------------
127 int FIREBIRD_STORE::SaveUserStat(const STG::UserStat & stat,
128 const std::string & login) const
130 STG_LOCKER lock(&mutex);
132 return SaveStat(stat, login);
134 //-----------------------------------------------------------------------------
135 int FIREBIRD_STORE::SaveStat(const STG::UserStat & stat,
136 const std::string & login,
140 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
141 IBPP::Statement st = IBPP::StatementFactory(db, tr);
146 st->Prepare("select pk_user from tb_users where name = ?");
151 strError = "User \"" + login + "\" not found in database";
152 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
156 int32_t uid = Get<int32_t>(st, 1);
158 st->Prepare("select first 1 pk_stat from tb_stats where fk_user = ? order by stats_date desc");
164 strError = "No stat info for user \"" + login + "\"";
165 printfd(__FILE__, "No stat info for user '%s'\n", login.c_str());
172 IBPP::Timestamp actTime;
173 time_t2ts(stat.lastActivityTime, &actTime);
174 IBPP::Timestamp addTime;
175 time_t2ts(stat.lastCashAddTime, &addTime);
178 ym2date(year, month, &dt);
182 st->Prepare("update tb_stats set \
185 last_activity_time = ?, \
187 last_cash_add_time = ?, \
192 st->Set(1, stat.cash);
193 st->Set(2, stat.freeMb);
195 st->Set(4, stat.lastCashAdd);
197 st->Set(6, (int32_t)stat.passiveTime);
204 for(int i = 0; i < DIR_NUM; i++)
206 st->Prepare("update tb_stats_traffic set \
209 where fk_stat = ? and dir_num = ?");
210 st->Set(1, (int64_t)stat.monthUp[i]);
211 st->Set(2, (int64_t)stat.monthDown[i]);
221 catch (IBPP::Exception & ex)
224 strError = "IBPP exception";
225 printfd(__FILE__, ex.what());
231 //-----------------------------------------------------------------------------
232 int FIREBIRD_STORE::SaveUserConf(const STG::UserConf & conf,
233 const std::string & login) const
235 STG_LOCKER lock(&mutex);
237 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
238 IBPP::Statement st = IBPP::StatementFactory(db, tr);
243 st->Prepare("select pk_user from tb_users where name = ?");
248 strError = "User \"" + login + "\" not found in database";
249 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
257 IBPP::Timestamp creditExpire;
258 time_t2ts(conf.creditExpire, &creditExpire);
260 st->Prepare("update tb_users set \
266 disabled_detail_stat = ?, \
273 fk_tariff = (select pk_tariff from tb_tariffs \
275 fk_tariff_change = (select pk_tariff from tb_tariffs \
277 fk_corporation = (select pk_corporation from tb_corporations \
282 st->Set(1, conf.address);
283 st->Set(2, (bool)conf.alwaysOnline);
284 st->Set(3, conf.credit);
285 st->Set(4, creditExpire);
286 st->Set(5, (bool)conf.disabled);
287 st->Set(6, (bool)conf.disabledDetailStat);
288 st->Set(7, conf.email);
289 st->Set(8, conf.group);
290 st->Set(9, conf.note);
291 st->Set(10, (bool)conf.passive);
292 st->Set(11, conf.password);
293 st->Set(12, conf.phone);
294 st->Set(13, conf.tariffName);
295 st->Set(14, conf.nextTariff);
296 st->Set(15, conf.corp);
297 st->Set(16, conf.realName);
303 st->Prepare("delete from tb_users_services where fk_user = ?");
308 st->Prepare("insert into tb_users_services (fk_user, fk_service) \
309 values (?, (select pk_service from tb_services \
311 for(std::vector<std::string>::const_iterator it = conf.services.begin(); it != conf.services.end(); ++it)
319 st->Prepare("delete from tb_users_data where fk_user = ?");
325 st->Prepare("insert into tb_users_data (fk_user, data, num) values (?, ?, ?)");
326 for (std::vector<std::string>::const_iterator it = conf.userdata.begin(); it != conf.userdata.end(); ++it)
335 st->Prepare("delete from tb_allowed_ip where fk_user = ?");
339 st->Prepare("insert into tb_allowed_ip (fk_user, ip, mask) values (?, ?, ?)");
340 for(size_t i = 0; i < conf.ips.count(); i++)
343 st->Set(2, (int32_t)conf.ips[i].ip);
344 st->Set(3, (int32_t)conf.ips[i].mask);
349 catch (IBPP::Exception & ex)
352 strError = "IBPP exception";
353 printfd(__FILE__, ex.what());
359 //-----------------------------------------------------------------------------
360 int FIREBIRD_STORE::RestoreUserStat(STG::UserStat * stat,
361 const std::string & login) const
363 STG_LOCKER lock(&mutex);
365 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
366 IBPP::Statement st = IBPP::StatementFactory(db, tr);
371 st->Prepare("select pk_user from tb_users where name = ?");
376 strError = "User \"" + login + "\" not found in database";
377 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
384 st->Prepare("select first 1 pk_stat, cash, free_mb, last_activity_time, \
385 last_cash_add, last_cash_add_time, passive_time from tb_stats \
386 where fk_user = ? order by stats_date desc");
391 strError = "No stat info for user \"" + login + "\"";
392 printfd(__FILE__, "No stat info for user '%s'\n", login.c_str());
399 st->Get(2, stat->cash);
400 st->Get(3, stat->freeMb);
401 IBPP::Timestamp actTime;
403 st->Get(5, stat->lastCashAdd);
404 IBPP::Timestamp addTime;
407 st->Get(7, passiveTime);
409 stat->passiveTime = passiveTime;
411 stat->lastActivityTime = ts2time_t(actTime);
413 stat->lastCashAddTime = ts2time_t(addTime);
416 st->Prepare("select * from tb_stats_traffic where fk_stat = ?");
419 for(int i = 0; i < DIR_NUM; i++)
425 st->Get(5, (int64_t &)stat->monthUp[dir]);
426 st->Get(4, (int64_t &)stat->monthDown[dir]);
436 catch (IBPP::Exception & ex)
439 strError = "IBPP exception";
440 printfd(__FILE__, ex.what());
446 //-----------------------------------------------------------------------------
447 int FIREBIRD_STORE::RestoreUserConf(STG::UserConf * conf,
448 const std::string & login) const
450 STG_LOCKER lock(&mutex);
452 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
453 IBPP::Statement st = IBPP::StatementFactory(db, tr);
458 st->Prepare("select tb_users.pk_user, tb_users.address, tb_users.always_online, \
459 tb_users.credit, tb_users.credit_expire, tb_users.disabled, \
460 tb_users.disabled_detail_stat, tb_users.email, tb_users.grp, \
461 tb_users.note, tb_users.passive, tb_users.passwd, tb_users.phone, \
462 tb_users.real_name, tf1.name, tf2.name, tb_corporations.name \
463 from tb_users left join tb_tariffs tf1 \
464 on tf1.pk_tariff = tb_users.fk_tariff \
465 left join tb_tariffs tf2 \
466 on tf2.pk_tariff = tb_users.fk_tariff_change \
467 left join tb_corporations \
468 on tb_corporations.pk_corporation = tb_users.fk_corporation \
469 where tb_users.name = ?");
474 strError = "User \"" + login + "\" not found in database";
475 printfd(__FILE__, "User '%s' not found in database", login.c_str());
481 // Getting base config
482 st->Get(2, conf->address);
485 conf->alwaysOnline = test;
486 st->Get(4, conf->credit);
487 IBPP::Timestamp timestamp;
488 st->Get(5, timestamp);
490 conf->creditExpire = ts2time_t(timestamp);
493 conf->disabled = test;
495 conf->disabledDetailStat = test;
496 st->Get(8, conf->email);
497 st->Get(9, conf->group);
498 st->Get(10, conf->note);
500 conf->passive = test;
501 st->Get(12, conf->password);
502 st->Get(13, conf->phone);
503 st->Get(14, conf->realName);
504 st->Get(15, conf->tariffName);
505 st->Get(16, conf->nextTariff);
506 st->Get(17, conf->corp);
508 if (conf->tariffName == "")
509 conf->tariffName = NO_TARIFF_NAME;
510 if (conf->corp == "")
511 conf->corp = NO_CORP_NAME;
515 st->Prepare("select name from tb_services \
516 where pk_service in \
517 (select fk_service from tb_users_services \
518 where fk_user = ?)");
525 conf->services.push_back(name);
530 st->Prepare("select data, num from tb_users_data where fk_user = ? order by num");
537 st->Get(1, conf->userdata[i]);
542 st->Prepare("select ip, mask from tb_allowed_ip \
550 st->Get(1, (int32_t &)im.ip);
551 st->Get(2, (int32_t &)im.mask);
558 catch (IBPP::Exception & ex)
561 strError = "IBPP exception";
562 printfd(__FILE__, ex.what());
568 //-----------------------------------------------------------------------------
569 int FIREBIRD_STORE::WriteUserChgLog(const std::string & login,
570 const std::string & admLogin,
572 const std::string & paramName,
573 const std::string & oldValue,
574 const std::string & newValue,
575 const std::string & message = "") const
577 STG_LOCKER lock(&mutex);
579 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
580 IBPP::Statement st = IBPP::StatementFactory(db, tr);
584 std::string temp = ""; // Composed message for log
589 temp += "Admin \"" + admLogin + "\", ";
590 temp += inet_ntostring(admIP);
592 temp = temp + message;
593 //----------------------------------------------------------------------------------------
594 // Checking and inserting parameters in params table
595 st->Prepare("select pk_parameter from tb_parameters where name = ?");
596 st->Set(1, paramName);
601 st->Prepare("insert into tb_parameters (name) values (?)");
602 st->Set(1, paramName);
606 //----------------------------------------------------------------------------------------
607 st->Prepare("insert into tb_params_log \
608 (fk_user, fk_parameter, event_time, from_val, to_val, comment) \
609 values ((select pk_user from tb_users \
611 (select pk_parameter from tb_parameters \
615 st->Set(2, paramName);
617 st->Set(4, oldValue);
618 st->Set(5, newValue);
624 catch (IBPP::Exception & ex)
627 strError = "IBPP exception";
628 printfd(__FILE__, ex.what());
634 //-----------------------------------------------------------------------------
635 int FIREBIRD_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
637 STG_LOCKER lock(&mutex);
639 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
640 IBPP::Statement st = IBPP::StatementFactory(db, tr);
647 st->Prepare("execute procedure sp_append_session_log(?, ?, 'c', ?)");
650 st->Set(3, (int32_t)ip);
654 catch (IBPP::Exception & ex)
657 strError = "IBPP exception";
658 printfd(__FILE__, ex.what());
664 //-----------------------------------------------------------------------------
665 int FIREBIRD_STORE::WriteUserDisconnect(const std::string & login,
666 const STG::DirTraff & up,
667 const STG::DirTraff & down,
668 const STG::DirTraff & sessionUp,
669 const STG::DirTraff & sessionDown,
672 const std::string & /*reason*/) const
674 STG_LOCKER lock(&mutex);
676 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
677 IBPP::Statement st = IBPP::StatementFactory(db, tr);
684 st->Prepare("execute procedure sp_append_session_log(?, ?, 'd', 0)");
690 st->Prepare("insert into tb_sessions_data \
691 (fk_session_log, dir_num, session_upload, \
692 session_download, month_upload, month_download) \
693 values (?, ?, ?, ?, ?, ?)");
694 for(int i = 0; i < DIR_NUM; i++)
698 st->Set(3, (int64_t)sessionUp[i]);
699 st->Set(4, (int64_t)sessionDown[i]);
700 st->Set(5, (int64_t)up[i]);
701 st->Set(6, (int64_t)down[i]);
707 catch (IBPP::Exception & ex)
710 strError = "IBPP exception";
711 printfd(__FILE__, ex.what());
717 //-----------------------------------------------------------------------------
718 int FIREBIRD_STORE::WriteDetailedStat(const STG::TraffStat & statTree,
720 const std::string & login) const
722 STG_LOCKER lock(&mutex);
724 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
725 IBPP::Statement st = IBPP::StatementFactory(db, tr);
727 IBPP::Timestamp statTime, now;
730 time_t2ts(lastStat, &statTime);
735 STG::TraffStat::const_iterator it;
736 it = statTree.begin();
737 st->Prepare("insert into tb_detail_stats \
738 (till_time, from_time, fk_user, dir_num, \
739 ip, download, upload, cost) \
740 values (?, ?, (select pk_user from tb_users \
743 while (it != statTree.end())
746 st->Set(2, statTime);
748 st->Set(4, it->first.dir);
749 st->Set(5, (int32_t)it->first.ip);
750 st->Set(6, (int64_t)it->second.down);
751 st->Set(7, (int64_t)it->second.up);
752 st->Set(8, it->second.cash);
759 catch (IBPP::Exception & ex)
762 strError = "IBPP exception";
763 printfd(__FILE__, ex.what());
769 //-----------------------------------------------------------------------------
770 int FIREBIRD_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const
772 STG_LOCKER lock(&mutex);
774 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
775 IBPP::Statement st = IBPP::StatementFactory(db, tr);
782 if (SaveStat(stat, login, year, month))
791 st->Prepare("execute procedure sp_add_stat(?, 0, 0, ?, 0, ?, 0, ?)");
802 st->Prepare("insert into tb_stats_traffic \
803 (fk_stat, dir_num, upload, download) \
804 values (?, ?, 0, 0)");
806 for(int i = 0; i < DIR_NUM; i++)
815 catch (IBPP::Exception & ex)
818 strError = "IBPP exception";
819 printfd(__FILE__, ex.what());
825 //-----------------------------------------------------------------------------