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 "stg/const.h"
30 #include "firebird_store.h"
33 //-----------------------------------------------------------------------------
34 int FIREBIRD_STORE::GetUsersList(std::vector<std::string> * usersList) const
36 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
38 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
39 IBPP::Statement st = IBPP::StatementFactory(db, tr);
46 st->Execute("select name from tb_users");
50 usersList->push_back(name);
55 catch (IBPP::Exception & ex)
58 strError = "IBPP exception";
59 printfd(__FILE__, ex.what());
65 //-----------------------------------------------------------------------------
66 int FIREBIRD_STORE::AddUser(const std::string & name) const
68 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
70 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
71 IBPP::Statement st = IBPP::StatementFactory(db, tr);
76 st->Prepare("execute procedure sp_add_user(?, ?)");
83 catch (IBPP::Exception & ex)
86 strError = "IBPP exception";
87 printfd(__FILE__, ex.what());
93 //-----------------------------------------------------------------------------
94 int FIREBIRD_STORE::DelUser(const std::string & login) const
96 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
98 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
99 IBPP::Statement st = IBPP::StatementFactory(db, tr);
104 st->Prepare("execute procedure sp_delete_user(?)");
110 catch (IBPP::Exception & ex)
113 strError = "IBPP exception";
114 printfd(__FILE__, ex.what());
120 //-----------------------------------------------------------------------------
121 int FIREBIRD_STORE::SaveUserStat(const USER_STAT & stat,
122 const std::string & login) const
124 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
126 return SaveStat(stat, login);
128 //-----------------------------------------------------------------------------
129 int FIREBIRD_STORE::SaveStat(const USER_STAT & stat,
130 const std::string & login,
135 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
136 IBPP::Statement st = IBPP::StatementFactory(db, tr);
138 IBPP::Timestamp actTime;
139 IBPP::Timestamp addTime;
147 st->Prepare("select pk_user from tb_users where name = ?");
152 strError = "User \"" + login + "\" not found in database";
153 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
159 st->Prepare("select first 1 pk_stat from tb_stats where fk_user = ? order by stats_date desc");
165 strError = "No stat info for user \"" + login + "\"";
166 printfd(__FILE__, "No stat info for user '%s'\n", login.c_str());
172 time_t2ts(stat.lastActivityTime, &actTime);
173 time_t2ts(stat.lastCashAddTime, &addTime);
175 ym2date(year, month, &dt);
179 st->Prepare("update tb_stats set \
182 last_activity_time = ?, \
184 last_cash_add_time = ?, \
189 st->Set(1, stat.cash);
190 st->Set(2, stat.freeMb);
192 st->Set(4, stat.lastCashAdd);
194 st->Set(6, (int32_t)stat.passiveTime);
201 for(i = 0; i < DIR_NUM; i++)
203 st->Prepare("update tb_stats_traffic set \
206 where fk_stat = ? and dir_num = ?");
207 st->Set(1, (int64_t)stat.up[i]);
208 st->Set(2, (int64_t)stat.down[i]);
218 catch (IBPP::Exception & ex)
221 strError = "IBPP exception";
222 printfd(__FILE__, ex.what());
228 //-----------------------------------------------------------------------------
229 int FIREBIRD_STORE::SaveUserConf(const USER_CONF & conf,
230 const std::string & login) const
232 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
234 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
235 IBPP::Statement st = IBPP::StatementFactory(db, tr);
239 IBPP::Timestamp creditExpire;
240 std::vector<std::string>::const_iterator it;
245 st->Prepare("select pk_user from tb_users where name = ?");
250 strError = "User \"" + login + "\" not found in database";
251 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
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(it = conf.service.begin(); it != conf.service.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 (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);
350 catch (IBPP::Exception & ex)
353 strError = "IBPP exception";
354 printfd(__FILE__, ex.what());
360 //-----------------------------------------------------------------------------
361 int FIREBIRD_STORE::RestoreUserStat(USER_STAT * stat,
362 const std::string & login) const
364 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
366 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
367 IBPP::Statement st = IBPP::StatementFactory(db, tr);
369 IBPP::Timestamp actTime, addTime;
371 int32_t uid, sid, passiveTime;
376 st->Prepare("select pk_user from tb_users where name = ?");
381 strError = "User \"" + login + "\" not found in database";
382 printfd(__FILE__, "User '%s' not found in database\n", login.c_str());
388 st->Prepare("select first 1 pk_stat, cash, free_mb, last_activity_time, \
389 last_cash_add, last_cash_add_time, passive_time from tb_stats \
390 where fk_user = ? order by stats_date desc");
395 strError = "No stat info for user \"" + login + "\"";
396 printfd(__FILE__, "No stat info for user '%s'\n", login.c_str());
402 st->Get(2, stat->cash);
403 st->Get(3, stat->freeMb);
405 st->Get(5, stat->lastCashAdd);
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(i = 0; i < DIR_NUM; i++)
424 st->Get(5, (int64_t &)stat->up[dir]);
425 st->Get(4, (int64_t &)stat->down[dir]);
435 catch (IBPP::Exception & ex)
438 strError = "IBPP exception";
439 printfd(__FILE__, ex.what());
445 //-----------------------------------------------------------------------------
446 int FIREBIRD_STORE::RestoreUserConf(USER_CONF * conf,
447 const std::string & login) const
449 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
451 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
452 IBPP::Statement st = IBPP::StatementFactory(db, tr);
456 IBPP::Timestamp timestamp;
464 st->Prepare("select tb_users.pk_user, tb_users.address, tb_users.always_online, \
465 tb_users.credit, tb_users.credit_expire, tb_users.disabled, \
466 tb_users.disabled_detail_stat, tb_users.email, tb_users.grp, \
467 tb_users.note, tb_users.passive, tb_users.passwd, tb_users.phone, \
468 tb_users.real_name, tf1.name, tf2.name, tb_corporations.name \
469 from tb_users left join tb_tariffs tf1 \
470 on tf1.pk_tariff = tb_users.fk_tariff \
471 left join tb_tariffs tf2 \
472 on tf2.pk_tariff = tb_users.fk_tariff_change \
473 left join tb_corporations \
474 on tb_corporations.pk_corporation = tb_users.fk_corporation \
475 where tb_users.name = ?");
480 strError = "User \"" + login + "\" not found in database";
481 printfd(__FILE__, "User '%s' not found in database", login.c_str());
486 // Getting base config
487 st->Get(2, conf->address);
489 conf->alwaysOnline = test;
490 st->Get(4, conf->credit);
491 st->Get(5, timestamp);
493 conf->creditExpire = ts2time_t(timestamp);
496 conf->disabled = test;
498 conf->disabledDetailStat = test;
499 st->Get(8, conf->email);
500 st->Get(9, conf->group);
501 st->Get(10, conf->note);
503 conf->passive = test;
504 st->Get(12, conf->password);
505 st->Get(13, conf->phone);
506 st->Get(14, conf->realName);
507 st->Get(15, conf->tariffName);
508 st->Get(16, conf->nextTariff);
509 st->Get(17, conf->corp);
511 if (conf->tariffName == "")
512 conf->tariffName = NO_TARIFF_NAME;
513 if (conf->corp == "")
514 conf->corp = NO_CORP_NAME;
518 st->Prepare("select name from tb_services \
519 where pk_service in \
520 (select fk_service from tb_users_services \
521 where fk_user = ?)");
527 conf->service.push_back(name);
532 st->Prepare("select data, num from tb_users_data where fk_user = ? order by num");
538 st->Get(1, conf->userdata[i]);
543 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, __FILE__, __LINE__);
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, __FILE__, __LINE__);
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 DIR_TRAFF & up,
667 const DIR_TRAFF & down,
668 const DIR_TRAFF & sessionUp,
669 const DIR_TRAFF & sessionDown,
672 const std::string & /*reason*/) const
674 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
676 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
677 IBPP::Statement st = IBPP::StatementFactory(db, tr);
687 st->Prepare("execute procedure sp_append_session_log(?, ?, 'd', 0)");
692 st->Prepare("insert into tb_sessions_data \
693 (fk_session_log, dir_num, session_upload, \
694 session_download, month_upload, month_download) \
695 values (?, ?, ?, ?, ?, ?)");
696 for(i = 0; i < DIR_NUM; i++)
700 st->Set(3, (int64_t)sessionUp[i]);
701 st->Set(4, (int64_t)sessionDown[i]);
702 st->Set(5, (int64_t)up[i]);
703 st->Set(6, (int64_t)down[i]);
709 catch (IBPP::Exception & ex)
712 strError = "IBPP exception";
713 printfd(__FILE__, ex.what());
719 //-----------------------------------------------------------------------------
720 int FIREBIRD_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
722 const std::string & login) const
724 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
726 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
727 IBPP::Statement st = IBPP::StatementFactory(db, tr);
729 IBPP::Timestamp statTime, now;
732 time_t2ts(lastStat, &statTime);
737 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator it;
738 it = statTree.begin();
739 st->Prepare("insert into tb_detail_stats \
740 (till_time, from_time, fk_user, dir_num, \
741 ip, download, upload, cost) \
742 values (?, ?, (select pk_user from tb_users \
745 while (it != statTree.end())
748 st->Set(2, statTime);
750 st->Set(4, it->first.dir);
751 st->Set(5, (int32_t)it->first.ip);
752 st->Set(6, (int64_t)it->second.down);
753 st->Set(7, (int64_t)it->second.up);
754 st->Set(8, it->second.cash);
761 catch (IBPP::Exception & ex)
764 strError = "IBPP exception";
765 printfd(__FILE__, ex.what());
771 //-----------------------------------------------------------------------------
772 int FIREBIRD_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const
774 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
776 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
777 IBPP::Statement st = IBPP::StatementFactory(db, tr);
786 if (SaveStat(stat, login, year, month))
795 st->Prepare("execute procedure sp_add_stat(?, 0, 0, ?, 0, ?, 0, ?)");
805 st->Prepare("insert into tb_stats_traffic \
806 (fk_stat, dir_num, upload, download) \
807 values (?, ?, 0, 0)");
809 for(i = 0; i < DIR_NUM; i++)
819 catch (IBPP::Exception & ex)
822 strError = "IBPP exception";
823 printfd(__FILE__, ex.what());
829 //-----------------------------------------------------------------------------