]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_store_users.cpp
index 66e452a9b9e4128bf0974a7eb06983cdbbead05c..b4c449fb32b4603464b7267924988d625bdca8be 100644 (file)
@@ -33,6 +33,7 @@
 
 #include <libpq-fe.h>
 
+#include "stg/common.h"
 #include "stg/const.h"
 #include "stg/locker.h"
 #include "../../../stg_timer.h"
@@ -41,7 +42,7 @@
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::GetUsersList(std::vector<std::string> * usersList) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -97,7 +98,7 @@ return 0;
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::AddUser(const std::string & name) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -161,7 +162,7 @@ return 0;
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::DelUser(const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -225,7 +226,7 @@ return 0;
 int POSTGRESQL_STORE::SaveUserStat(const USER_STAT & stat,
                                    const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 return SaveStat(stat, login);
 }
@@ -270,9 +271,9 @@ std::ostringstream query;
 query << "UPDATE tb_users SET "
             "cash = " << stat.cash << ", "
             "free_mb = " << stat.freeMb << ", "
-            "last_activity_time = CAST('" << Int2TS(stat.lastActivityTime) << "' AS TIMESTAMP), "
+            "last_activity_time = CAST('" << formatTime(stat.lastActivityTime) << "' AS TIMESTAMP), "
             "last_cash_add = " << stat.lastCashAdd << ", "
-            "last_cash_add_time = CAST('" << Int2TS(stat.lastCashAddTime) << "' AS TIMESTAMP), "
+            "last_cash_add_time = CAST('" << formatTime(stat.lastCashAddTime) << "' AS TIMESTAMP), "
             "passive_time = " << stat.passiveTime << " "
          "WHERE name = '" << elogin << "'";
 
@@ -336,7 +337,7 @@ return 0;
 int POSTGRESQL_STORE::SaveUserConf(const USER_CONF & conf,
                                  const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -527,7 +528,7 @@ query << "UPDATE tb_users SET "
              "address = '" << eaddress << "', "
              "always_online = " << (conf.alwaysOnline ? "'t'" : "'f'") << ", "
              "credit = " << conf.credit << ", "
-             "credit_expire = CAST('" << Int2TS(conf.creditExpire) << "' AS TIMESTAMP), "
+             "credit_expire = CAST('" << formatTime(conf.creditExpire) << "' AS TIMESTAMP), "
              "disabled = " << (conf.disabled ? "'t'" : "'f'") << ", "
              "disabled_detail_stat = " << (conf.disabledDetailStat ? "'t'" : "'f'") << ", "
              "email = '" << eemail << "', "
@@ -607,7 +608,7 @@ return 0;
 int POSTGRESQL_STORE::RestoreUserStat(USER_STAT * stat,
                                     const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -681,9 +682,9 @@ if (tuples != 1)
     std::stringstream tuple;
     tuple << PQgetvalue(result, 0, 0) << " ";
     tuple << PQgetvalue(result, 0, 1) << " ";
-    stat->lastActivityTime = TS2Int(PQgetvalue(result, 0, 2));
+    stat->lastActivityTime = readTime(PQgetvalue(result, 0, 2));
     tuple << PQgetvalue(result, 0, 3) << " ";
-    stat->lastCashAddTime = TS2Int(PQgetvalue(result, 0, 4));
+    stat->lastCashAddTime = readTime(PQgetvalue(result, 0, 4));
     tuple << PQgetvalue(result, 0, 5) << " ";
 
     PQclear(result);
@@ -699,7 +700,7 @@ if (tuples != 1)
     query << "SELECT dir_num, upload, download "
              "FROM tb_stats_traffic "
              "WHERE fk_user IN (SELECT pk_user FROM tb_users WHERE name = '" << elogin << "') AND "
-                   "DATE_TRUNC('month', stats_date) = DATE_TRUNC('month', CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP))";
+                   "DATE_TRUNC('month', stats_date) = DATE_TRUNC('month', CAST('" << formatTime(stgTime) << "' AS TIMESTAMP))";
 
     result = PQexec(connection, query.str().c_str());
     }
@@ -747,7 +748,7 @@ return 0;
 int POSTGRESQL_STORE::RestoreUserConf(USER_CONF * conf,
                                     const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -832,7 +833,7 @@ uint32_t uid;
     conf->address = PQgetvalue(result, 0, 1);               // address
     conf->alwaysOnline = !strncmp(PQgetvalue(result, 0, 2), "t", 1);
     tuple << PQgetvalue(result, 0, 3) << " ";               // credit
-    conf->creditExpire = TS2Int(PQgetvalue(result, 0, 4));  // creditExpire
+    conf->creditExpire = readTime(PQgetvalue(result, 0, 4));  // creditExpire
     conf->disabled = !strncmp(PQgetvalue(result, 0, 5), "t", 1);
     conf->disabledDetailStat = !strncmp(PQgetvalue(result, 0, 6), "t", 1);
     conf->email = PQgetvalue(result, 0, 7);                 // email
@@ -953,26 +954,22 @@ if (PQresultStatus(result) != PGRES_TUPLES_OK)
 
 tuples = PQntuples(result);
 
-conf->ips.Erase();
+USER_IPS ips;
 for (int i = 0; i < tuples; ++i)
     {
-    IP_MASK ipm;
+    IP_MASK im;
 
-    int ip, mask;
+    im.ip = inet_strington(PQgetvalue(result, i, 0));
 
-    ip = inet_strington(PQgetvalue(result, i, 0));
-
-    if (str2x(PQgetvalue(result, i, 1), mask))
+    if (str2x(PQgetvalue(result, i, 1), im.mask))
         {
         printfd(__FILE__, "POSTGRESQL_STORE::RestoreUserConf(): 'Failed to fetch mask'\n");
         continue;
         }
 
-    ipm.ip = ip;
-    ipm.mask = mask;
-
-    conf->ips.Add(ipm);
+    ips.Add(im);
     }
+conf->ips = ips;
 
 PQclear(result);
 
@@ -994,7 +991,7 @@ int POSTGRESQL_STORE::WriteUserChgLog(const std::string & login,
                                     const std::string & newValue,
                                     const std::string & message = "") const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -1078,7 +1075,7 @@ query << "SELECT sp_add_param_log_entry("
             "'" << eadminLogin << "', CAST('"
             << inet_ntostring(admIP) << "/32' AS INET), "
             "'" << eparam << "', "
-            "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
+            "CAST('" << formatTime(stgTime) << "' AS TIMESTAMP), "
             "'" << eold << "', "
             "'" << enew << "', "
             "'" << emessage << "')";
@@ -1111,7 +1108,7 @@ return 0;
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -1149,7 +1146,7 @@ if (version < 6)
     {
     query << "SELECT sp_add_session_log_entry("
                  "'" << elogin << "', "
-                 "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
+                 "CAST('" << formatTime(stgTime) << "' AS TIMESTAMP), "
                  "'c', CAST('"
                  << inet_ntostring(ip) << "/32' AS INET), 0)";
     }
@@ -1157,7 +1154,7 @@ else
     {
     query << "SELECT sp_add_session_log_entry("
                  "'" << elogin << "', "
-                 "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
+                 "CAST('" << formatTime(stgTime) << "' AS TIMESTAMP), "
                  "'c', CAST('"
                  << inet_ntostring(ip) << "/32' AS INET), 0, 0, '')";
     }
@@ -1197,7 +1194,7 @@ int POSTGRESQL_STORE::WriteUserDisconnect(const std::string & login,
                     double freeMb,
                     const std::string & reason) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -1249,7 +1246,7 @@ if (EscapeString(ereason))
         // Old database version - no freeMb logging support
         query << "SELECT sp_add_session_log_entry("
                     "'" << elogin << "', "
-                    "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
+                    "CAST('" << formatTime(stgTime) << "' AS TIMESTAMP), "
                     "'d', CAST('0.0.0.0/0' AS INET), "
                     << cash << ")";
         }
@@ -1257,7 +1254,7 @@ if (EscapeString(ereason))
         {
         query << "SELECT sp_add_session_log_entry("
                     "'" << elogin << "', "
-                    "CAST('" << Int2TS(stgTime) << "' AS TIMESTAMP), "
+                    "CAST('" << formatTime(stgTime) << "' AS TIMESTAMP), "
                     "'d', CAST('0.0.0.0/0' AS INET), "
                     << cash << ", " << freeMb << ", '" << ereason << "')";
         }
@@ -1356,7 +1353,7 @@ int POSTGRESQL_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> &
                                       time_t lastStat,
                                       const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 if (PQstatus(connection) != CONNECTION_OK)
     {
@@ -1369,8 +1366,6 @@ if (PQstatus(connection) != CONNECTION_OK)
         }
     }
 
-PGresult * result;
-
 if (StartTransaction())
     {
     printfd(__FILE__, "POSTGRESQL_STORE::WriteDetailedStat(): 'Failed to start transaction'\n");
@@ -1399,8 +1394,8 @@ for (it = statTree.begin(); it != statTree.end(); ++it)
                 "(till_time, from_time, fk_user, "
                  "dir_num, ip, download, upload, cost) "
              "VALUES ("
-                "CAST('" << Int2TS(currTime) << "' AS TIMESTAMP), "
-                "CAST('" << Int2TS(lastStat) << "' AS TIMESTAMP), "
+                "CAST('" << formatTime(currTime) << "' AS TIMESTAMP), "
+                "CAST('" << formatTime(lastStat) << "' AS TIMESTAMP), "
                 "(SELECT pk_user FROM tb_users WHERE name = '" << elogin << "'), "
                 << it->first.dir << ", "
                 << "CAST('" << inet_ntostring(it->first.ip) << "' AS INET), "
@@ -1408,7 +1403,7 @@ for (it = statTree.begin(); it != statTree.end(); ++it)
                 << it->second.up << ", "
                 << it->second.cash << ")";
 
-    result = PQexec(connection, query.str().c_str());
+    PGresult * result = PQexec(connection, query.str().c_str());
 
     if (PQresultStatus(result) != PGRES_COMMAND_OK)
         {
@@ -1437,7 +1432,7 @@ return 0;
 //-----------------------------------------------------------------------------
 int POSTGRESQL_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const
 {
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+STG_LOCKER lock(&mutex);
 
 return SaveStat(stat, login, year, month);
 }