EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
APIServiceDB.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Aknor Jaden
24 */
25 
26 #include "eve-server.h"
27 
28 #include "apiserver/APIServiceDB.h"
29 
31 {
32 }
33 
34 bool APIServiceDB::GetAccountIdFromUsername(std::string username, std::string * accountID)
35 {
36  DBQueryResult res;
37 
38  // Find accountID in 'account' table using accountName:
39  if( !sDatabase.RunQuery(res,
40  "SELECT"
41  " accountID "
42  " FROM account "
43  " WHERE accountName='%s'" , username.c_str() ))
44  {
45  sLog.Error( "APIServiceDB::GetAccountIdFromUsername()", "Cannot find accountID for username %s", username.c_str() );
46  return false;
47  }
48 
49  DBResultRow row;
50  if( !res.GetRow(row) )
51  {
52  sLog.Error( "APIServiceDB::GetAccountIdFromUsername()", "res.GetRow(row) failed for unknown reason." );
53  return false;
54  }
55 
56  *accountID = row.GetText(0); // Grab accountID from the retrieved row from the 'account' table
57  return true;
58 }
59 
60 bool APIServiceDB::GetAccountIdFromUserID(std::string userID, uint32 * accountID)
61 {
62  DBQueryResult res;
63 
64  // Find accountID in 'accountapi' table using userID:
65  if( !sDatabase.RunQuery(res,
66  "SELECT"
67  " accountID "
68  " FROM accountApi "
69  " WHERE userID='%s'" , userID.c_str() ))
70  {
71  sLog.Error( "APIServiceDB::GetAccountIdFromUserID()", "Cannot find accountID for userID %s", userID.c_str() );
72  return false;
73  }
74 
75  DBResultRow row;
76  if( !res.GetRow(row) )
77  {
78  sLog.Error( "APIServiceDB::GetAccountIdFromUserID()", "res.GetRow(row) failed for unknown reason." );
79  return false;
80  }
81 
82  *accountID = row.GetUInt(0); // Grab accountID from the retrieved row from the 'accountapi' table
83  return true;
84 }
85 
86 bool APIServiceDB::GetApiAccountInfoUsingAccountID(std::string accountID, uint32 * userID, std::string * apiFullKey,
87  std::string * apiLimitedKey, uint32 * apiRole)
88 {
89  DBQueryResult res;
90 
91  // Find userID, fullKey, limitedKey, and apiRole from 'accountApi' table using accountID obtained from 'account' table:
92  if( !sDatabase.RunQuery(res,
93  "SELECT"
94  " userID, fullKey, limitedKey, apiRole "
95  " FROM accountApi "
96  " WHERE accountID='%s'" , accountID.c_str() ))
97  {
98  sLog.Error( "APIServiceDB::GetApiAccountInfoUsingAccountID()", "Cannot find accountID '%s' in 'accountApi' table", accountID.c_str() );
99  return false;
100  }
101 
102  DBResultRow row;
103  if( !res.GetRow(row) )
104  {
105  sLog.Error( "APIServiceDB::GetApiAccountInfoUsingAccountID()", "res.GetRow(row) failed for unknown reason." );
106  return false;
107  }
108 
109  *userID = row.GetUInt(0); // Grab userID from retrieved row from the 'accountApi' table
110  *apiFullKey = row.GetText(1); // Grab Full API Key from retrieved row from the 'accountApi' table
111  *apiLimitedKey = row.GetText(2); // Grab Limited API Key from retrieved row from the 'accountApi' table
112  *apiRole = row.GetUInt(3); // Grab API Role from retrieved row from the 'accountApi' table
113  return true;
114 }
115 
116 bool APIServiceDB::GetApiAccountInfoUsingUserID(std::string userID, std::string * apiFullKey, std::string * apiLimitedKey, uint32 * apiRole)
117 {
118  DBQueryResult res;
119 
120  // Find fullKey, limitedKey, and apiRole from 'accountApi' table using userID supplied from an API query string:
121  if( !sDatabase.RunQuery(res,
122  "SELECT"
123  " fullKey, limitedKey, apiRole "
124  " FROM accountApi "
125  " WHERE userID='%s'" , userID.c_str() ))
126  {
127  sLog.Error( "APIServiceDB::GetApiAccountInfoUsingUserID()", "Cannot find userID '%s' in 'accountApi' table", userID.c_str() );
128  return false;
129  }
130 
131  DBResultRow row;
132  if( !res.GetRow(row) )
133  {
134  sLog.Error( "APIServiceDB::GetApiAccountInfoUsingUserID()", "res.GetRow(row) failed for unknown reason." );
135  return false;
136  }
137 
138  *apiFullKey = row.GetText(0); // Grab Full API Key from retrieved row from the 'accountApi' table
139  *apiLimitedKey = row.GetText(1); // Grab Limited API Key from retrieved row from the 'accountApi' table
140  *apiRole = row.GetUInt(2); // Grab API Role from retrieved row from the 'accountApi' table
141  return true;
142 }
143 
144 bool APIServiceDB::UpdateUserIdApiKeyDatabaseRow(uint32 userID, std::string apiFullKey, std::string apiLimitedKey)
145 {
146  // Check key lengths and report error and return if either are incorrect:
147  if( apiLimitedKey.length() != 64 )
148  {
149  sLog.Error( "APIServiceDB::UpdateUserIdApiKeyDatabaseRow()", "limitedApiKey length != 64, but rather %u", apiLimitedKey.length() );
150  return false;
151  }
152 
153  if( apiFullKey.length() != 64 )
154  {
155  sLog.Error( "APIServiceDB::UpdateUserIdApiKeyDatabaseRow()", "fullApiKey length != 64, but rather %u", apiFullKey.length() );
156  return false;
157  }
158 
159  // Update fullKey and limitedKey in the 'accountApi' table using userID:
160  DBerror err;
161 
162  if( !sDatabase.RunQuery(err,
163  "UPDATE"
164  " accountApi"
165  " SET fullKey = '%s', limitedKey = '%s'"
166  " WHERE userID = %u",
167  apiFullKey.c_str(), apiLimitedKey.c_str(), userID ))
168  {
169  sLog.Error( "", "Error in query: %s.", err.c_str());
170  return false;
171  }
172  else
173  return true;
174 }
175 
176 bool APIServiceDB::InsertNewUserIdApiKeyInfoToDatabase(uint32 accountID, std::string apiFullKey, std::string apiLimitedKey, uint32 apiRole)
177 {
178  // Check key lengths and report error and return if either are incorrect:
179  if( apiLimitedKey.length() != 64 )
180  {
181  sLog.Error( "APIServiceDB::UpdateUserIdApiKeyDatabaseRow()", "limitedApiKey length != 64, but rather %u", apiLimitedKey.length() );
182  return false;
183  }
184 
185  if( apiFullKey.length() != 64 )
186  {
187  sLog.Error( "APIServiceDB::UpdateUserIdApiKeyDatabaseRow()", "fullApiKey length != 64, but rather %u", apiFullKey.length() );
188  return false;
189  }
190 
191  DBerror err;
192 
193  if( !sDatabase.RunQuery(err,
194  "INSERT INTO"
195  " accountApi ("
196  " accountID, fullKey, limitedKey, apiRole"
197  " ) VALUES ("
198  " %u, '%s', '%s', %u"
199  " )",
200  accountID, apiFullKey.c_str(), apiLimitedKey.c_str(), apiRole
201  ))
202  {
203  sLog.Error( "", "Error in query: %s.", err.c_str());
204  return false;
205  }
206  else
207  return true;
208 }
209 
211 {
212  // Update fullKey and limitedKey in the 'accountApi' table using userID:
213  DBerror err;
214 
215  if( !sDatabase.RunQuery(err,
216  "UPDATE"
217  " accountApi"
218  " SET apiRole = %u"
219  " WHERE userID = %u",
220  apiRole, userID ))
221  {
222  sLog.Error( "", "Error in query: %s.", err.c_str());
223  return false;
224  }
225  else
226  return true;
227 }
228 
bool GetAccountIdFromUsername(std::string username, std::string *accountID)
?
#define sDatabase
Definition: dbcore.h:199
const char * GetText(uint32 index) const
Definition: dbcore.h:104
bool UpdateUserIdApiRole(uint32 userID, uint32 apiRole)
?
uint32 GetUInt(uint32 index) const
Definition: dbcore.cpp:658
bool InsertNewUserIdApiKeyInfoToDatabase(uint32 accountID, std::string apiFullKey, std::string apiLimitedKey, uint32 apiRole)
?
bool GetRow(DBResultRow &into)
Definition: dbcore.cpp:552
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
bool GetAccountIdFromUserID(std::string userID, uint32 *accountID)
?
const char * c_str() const
Definition: dbcore.h:48
bool GetApiAccountInfoUsingUserID(std::string userID, std::string *apiFullKey, std::string *apiLimitedKey, uint32 *apiRole)
?
unsigned __int32 uint32
Definition: eve-compat.h:50
bool GetApiAccountInfoUsingAccountID(std::string accountID, uint32 *userID, std::string *apiFullKey, std::string *apiLimitedKey, uint32 *apiRole)
?
Definition: dbcore.h:39
bool UpdateUserIdApiKeyDatabaseRow(uint32 userID, std::string apiFullKey, std::string apiLimitedKey)
?