19 m_DB(
"Ranks.sqlite", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE),
20 m_IsInitialized(false)
41 m_DB.exec(
"CREATE TABLE IF NOT EXISTS Rank (RankID INTEGER PRIMARY KEY, Name, MsgPrefix, MsgSuffix, MsgNameColorCode)");
42 m_DB.exec(
"CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID INTEGER)");
43 m_DB.exec(
"CREATE TABLE IF NOT EXISTS PermGroup (PermGroupID INTEGER PRIMARY KEY, Name)");
44 m_DB.exec(
"CREATE TABLE IF NOT EXISTS RankPermGroup (RankID INTEGER, PermGroupID INTEGER)");
45 m_DB.exec(
"CREATE TABLE IF NOT EXISTS PermissionItem (PermGroupID INTEGER, Permission)");
46 m_DB.exec(
"CREATE TABLE IF NOT EXISTS RestrictionItem (PermGroupID INTEGER, Permission)");
47 m_DB.exec(
"CREATE TABLE IF NOT EXISTS DefaultRank (RankID INTEGER)");
56 LOGINFO(
"Creating default ranks...");
58 LOGINFO(
"Default ranks created.");
64 SQLite::Statement stmt(
m_DB,
65 "SELECT Rank.Name FROM Rank "
66 "LEFT JOIN DefaultRank ON Rank.RankID = DefaultRank.RankID"
68 if (stmt.executeStep())
73 catch (
const SQLite::Exception & ex)
75 LOGWARNING(
"%s: Cannot load default rank: %s", __FUNCTION__, ex.what());
97 SQLite::Statement stmt(
m_DB,
"SELECT Rank.Name FROM Rank LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID WHERE PlayerRank.PlayerUUID = ?");
100 if (!stmt.executeStep())
105 return stmt.getColumn(0).getText();
107 catch (
const SQLite::Exception & ex)
109 LOGWARNING(
"%s: Cannot get player rank name: %s", __FUNCTION__, ex.what());
126 SQLite::Statement stmt(
m_DB,
"SELECT PlayerName FROM PlayerRank WHERE PlayerUUID = ?");
129 if (stmt.executeStep())
131 return stmt.getColumn(0).getText();
134 catch (SQLite::Exception & ex)
136 LOGWARNING(
"%s: Cannot get player name: %s", __FUNCTION__, ex.what());
154 SQLite::Statement stmt(
m_DB,
155 "SELECT PermGroup.Name FROM PermGroup "
156 "LEFT JOIN RankPermGroup ON PermGroup.PermGroupID = RankPermGroup.PermGroupID "
157 "LEFT JOIN PlayerRank ON PlayerRank.RankID = RankPermGroup.RankID "
158 "WHERE PlayerRank.PlayerUUID = ?"
163 while (stmt.executeStep())
165 res.push_back(stmt.getColumn(0).getText());
168 catch (
const SQLite::Exception & ex)
170 LOGWARNING(
"%s: Cannot get player groups: %s", __FUNCTION__, ex.what());
215 SQLite::Statement stmt(
m_DB,
216 "SELECT PermGroup.Name FROM PermGroup "
217 "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermGroup.PermGroupID "
218 "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
219 "WHERE Rank.Name = ?"
221 stmt.bind(1, a_RankName);
222 while (stmt.executeStep())
224 res.push_back(stmt.getColumn(0).getText());
227 catch (
const SQLite::Exception & ex)
229 LOGWARNING(
"%s: Failed to get rank groups from DB: %s", __FUNCTION__, ex.what());
246 SQLite::Statement stmt(
m_DB,
247 "SELECT PermissionItem.Permission FROM PermissionItem "
248 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = PermissionItem.PermGroupID "
249 "WHERE PermGroup.Name = ?"
251 stmt.bind(1, a_GroupName);
252 while (stmt.executeStep())
254 res.push_back(stmt.getColumn(0).getText());
257 catch (
const SQLite::Exception & ex)
259 LOGWARNING(
"%s: Failed to get group permissions from DB: %s", __FUNCTION__, ex.what());
276 SQLite::Statement stmt(
m_DB,
277 "SELECT RestrictionItem.Permission FROM RestrictionItem "
278 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = RestrictionItem.PermGroupID "
279 "WHERE PermGroup.Name = ?"
281 stmt.bind(1, a_GroupName);
282 while (stmt.executeStep())
284 res.push_back(stmt.getColumn(0).getText());
287 catch (
const SQLite::Exception & ex)
289 LOGWARNING(
"%s: Failed to get group restrictions from DB: %s", __FUNCTION__, ex.what());
306 SQLite::Statement stmt(
m_DB,
307 "SELECT PermissionItem.Permission FROM PermissionItem "
308 "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermissionItem.PermGroupID "
309 "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
310 "WHERE Rank.Name = ?"
312 stmt.bind(1, a_RankName);
313 while (stmt.executeStep())
315 res.push_back(stmt.getColumn(0).getText());
318 catch (
const SQLite::Exception & ex)
320 LOGWARNING(
"%s: Failed to get rank permissions from DB: %s", __FUNCTION__, ex.what());
337 SQLite::Statement stmt(
m_DB,
338 "SELECT RestrictionItem.Permission FROM RestrictionItem "
339 "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = RestrictionItem.PermGroupID "
340 "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
341 "WHERE Rank.Name = ?"
343 stmt.bind(1, a_RankName);
344 while (stmt.executeStep())
346 res.push_back(stmt.getColumn(0).getText());
349 catch (
const SQLite::Exception & ex)
351 LOGWARNING(
"%s: Failed to get rank restrictions from DB: %s", __FUNCTION__, ex.what());
366 std::vector<cUUID> res;
369 SQLite::Statement stmt(
m_DB,
"SELECT PlayerUUID FROM PlayerRank ORDER BY PlayerName COLLATE NOCASE");
370 while (stmt.executeStep())
372 if (!tempUUID.
FromString(stmt.getColumn(0).getText()))
377 res.push_back(tempUUID);
380 catch (
const SQLite::Exception & ex)
382 LOGWARNING(
"%s: Failed to get players from DB: %s", __FUNCTION__, ex.what());
399 SQLite::Statement stmt(
m_DB,
"SELECT Name FROM Rank");
400 while (stmt.executeStep())
402 res.push_back(stmt.getColumn(0).getText());
405 catch (
const SQLite::Exception & ex)
407 LOGWARNING(
"%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what());
424 SQLite::Statement stmt(
m_DB,
"SELECT Name FROM PermGroup");
425 while (stmt.executeStep())
427 res.push_back(stmt.getColumn(0).getText());
430 catch (
const SQLite::Exception & ex)
432 LOGWARNING(
"%s: Failed to get groups from DB: %s", __FUNCTION__, ex.what());
449 SQLite::Statement stmt(
m_DB,
"SELECT DISTINCT(Permission) FROM PermissionItem");
450 while (stmt.executeStep())
452 res.push_back(stmt.getColumn(0).getText());
455 catch (
const SQLite::Exception & ex)
457 LOGWARNING(
"%s: Failed to get permissions from DB: %s", __FUNCTION__, ex.what());
474 SQLite::Statement stmt(
m_DB,
"SELECT DISTINCT(Permission) FROM RestrictionItem");
475 while (stmt.executeStep())
477 res.push_back(stmt.getColumn(0).getText());
480 catch (
const SQLite::Exception & ex)
482 LOGWARNING(
"%s: Failed to get restrictions from DB: %s", __FUNCTION__, ex.what());
495 for (
auto & restriction: Restrictions)
497 Permissions.push_back(restriction);
507 const cUUID & a_PlayerUUID,
519 a_MsgNameColorCode.clear();
522 return GetRankVisuals(Rank, a_MsgPrefix, a_MsgSuffix, a_MsgNameColorCode);
533 const AString & a_MsgNameColorCode
543 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM Rank WHERE Name = ?");
544 stmt.bind(1, a_RankName);
545 if (stmt.executeStep())
547 if (stmt.getColumn(0).getInt() > 0)
556 SQLite::Statement stmt(
m_DB,
"INSERT INTO Rank (Name, MsgPrefix, MsgSuffix, MsgNameColorCode) VALUES (?, ?, ?, ?)");
557 stmt.bind(1, a_RankName);
558 stmt.bind(2, a_MsgPrefix);
559 stmt.bind(3, a_MsgSuffix);
560 stmt.bind(4, a_MsgNameColorCode);
561 if (stmt.exec() <= 0)
563 LOGWARNING(
"%s: Failed to add a new rank \"%s\".", __FUNCTION__, a_RankName.c_str());
567 catch (
const SQLite::Exception & ex)
569 LOGWARNING(
"%s: Failed to add a new rank \"%s\": %s", __FUNCTION__, a_RankName.c_str(), ex.what());
586 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM PermGroup WHERE Name = ?");
587 stmt.bind(1, a_GroupName);
588 if (stmt.executeStep())
590 if (stmt.getColumn(0).getInt() > 0)
599 SQLite::Statement stmt(
m_DB,
"INSERT INTO PermGroup (Name) VALUES (?)");
600 stmt.bind(1, a_GroupName);
601 if (stmt.exec() <= 0)
603 LOGWARNING(
"%s: Failed to add a new group \"%s\".", __FUNCTION__, a_GroupName.c_str());
607 catch (
const SQLite::Exception & ex)
609 LOGWARNING(
"%s: Failed to add a new group \"%s\": %s", __FUNCTION__, a_GroupName.c_str(), ex.what());
624 for (AStringVector::const_iterator itr = a_GroupNames.begin(), end = a_GroupNames.end(); itr != end; ++itr)
628 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM PermGroup WHERE Name = ?");
630 if (stmt.executeStep())
632 if (stmt.getColumn(0).getInt() > 0)
641 SQLite::Statement stmt(
m_DB,
"INSERT INTO PermGroup (Name) VALUES (?)");
643 if (stmt.exec() <= 0)
645 LOGWARNING(
"%s: Failed to add a new group \"%s\".", __FUNCTION__, itr->c_str());
650 catch (
const SQLite::Exception & ex)
652 LOGWARNING(
"%s: Failed to add new groups: %s", __FUNCTION__, ex.what());
670 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
671 stmt.bind(1, a_GroupName);
672 if (!stmt.executeStep())
674 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
677 GroupID = stmt.getColumn(0);
683 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
684 stmt.bind(1, a_RankName);
685 if (!stmt.executeStep())
687 LOGWARNING(
"%s: No such rank (%s), aborting.", __FUNCTION__, a_RankName.c_str());
690 RankID = stmt.getColumn(0);
695 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM RankPermGroup WHERE RankID = ? AND PermGroupID = ?");
696 stmt.bind(1, RankID);
697 stmt.bind(2, GroupID);
698 if (!stmt.executeStep())
700 LOGWARNING(
"%s: Failed to check binding between rank %s and group %s, aborting.", __FUNCTION__, a_RankName.c_str(), a_GroupName.c_str());
703 if (stmt.getColumn(0).getInt() > 0)
705 LOGD(
"%s: Group %s already present in rank %s, skipping and returning success.",
706 __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str()
714 SQLite::Statement stmt(
m_DB,
"INSERT INTO RankPermGroup (RankID, PermGroupID) VALUES (?, ?)");
715 stmt.bind(1, RankID);
716 stmt.bind(2, GroupID);
717 if (stmt.exec() <= 0)
719 LOGWARNING(
"%s: Failed to add group %s to rank %s, aborting.", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str());
727 catch (
const SQLite::Exception & ex)
729 LOGWARNING(
"%s: Failed to add group %s to rank %s: %s", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str(), ex.what());
748 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
749 stmt.bind(1, a_GroupName);
750 if (!stmt.executeStep())
752 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
755 GroupID = stmt.getColumn(0).getInt();
760 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?");
761 stmt.bind(1, GroupID);
762 stmt.bind(2, a_Permission);
763 if (!stmt.executeStep())
765 LOGWARNING(
"%s: Failed to check binding between permission %s and group %s, aborting.", __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str());
768 if (stmt.getColumn(0).getInt() > 0)
770 LOGD(
"%s: Permission %s is already present in group %s, skipping and returning success.",
771 __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str()
779 SQLite::Statement stmt(
m_DB,
"INSERT INTO PermissionItem (Permission, PermGroupID) VALUES (?, ?)");
780 stmt.bind(1, a_Permission);
781 stmt.bind(2, GroupID);
782 if (stmt.exec() <= 0)
784 LOGWARNING(
"%s: Failed to add permission %s to group %s, aborting.", __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str());
792 catch (
const SQLite::Exception & ex)
794 LOGWARNING(
"%s: Failed to add permission %s to group %s: %s",
795 __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str(), ex.what()
815 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
816 stmt.bind(1, a_GroupName);
817 if (!stmt.executeStep())
819 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
822 GroupID = stmt.getColumn(0).getInt();
827 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM RestrictionItem WHERE PermGroupID = ? AND Permission = ?");
828 stmt.bind(1, GroupID);
829 stmt.bind(2, a_Restriction);
830 if (!stmt.executeStep())
832 LOGWARNING(
"%s: Failed to check binding between restriction %s and group %s, aborting.", __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str());
835 if (stmt.getColumn(0).getInt() > 0)
837 LOGD(
"%s: Restriction %s is already present in group %s, skipping and returning success.",
838 __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str()
846 SQLite::Statement stmt(
m_DB,
"INSERT INTO RestrictionItem (Permission, PermGroupID) VALUES (?, ?)");
847 stmt.bind(1, a_Restriction);
848 stmt.bind(2, GroupID);
849 if (stmt.exec() <= 0)
851 LOGWARNING(
"%s: Failed to add restriction %s to group %s, aborting.", __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str());
859 catch (
const SQLite::Exception & ex)
861 LOGWARNING(
"%s: Failed to add restriction %s to group %s: %s",
862 __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str(), ex.what()
882 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
883 stmt.bind(1, a_GroupName);
884 if (!stmt.executeStep())
886 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
889 GroupID = stmt.getColumn(0).getInt();
892 for (AStringVector::const_iterator itr = a_Permissions.begin(), end = a_Permissions.end(); itr != end; ++itr)
896 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?");
897 stmt.bind(1, GroupID);
899 if (!stmt.executeStep())
901 LOGWARNING(
"%s: Failed to check binding between permission %s and group %s, aborting.", __FUNCTION__, itr->c_str(), a_GroupName.c_str());
904 if (stmt.getColumn(0).getInt() > 0)
906 LOGD(
"%s: Permission %s is already present in group %s, skipping and returning success.",
907 __FUNCTION__, itr->c_str(), a_GroupName.c_str()
915 SQLite::Statement stmt(
m_DB,
"INSERT INTO PermissionItem (Permission, PermGroupID) VALUES (?, ?)");
917 stmt.bind(2, GroupID);
918 if (stmt.exec() <= 0)
920 LOGWARNING(
"%s: Failed to add permission %s to group %s, skipping.", __FUNCTION__, itr->c_str(), a_GroupName.c_str());
929 catch (
const SQLite::Exception & ex)
931 LOGWARNING(
"%s: Failed to add permissions to group %s: %s",
932 __FUNCTION__, a_GroupName.c_str(), ex.what()
952 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
953 stmt.bind(1, a_GroupName);
954 if (!stmt.executeStep())
956 LOGWARNING(
"%s: No such group (%s), aborting.", __FUNCTION__, a_GroupName.c_str());
959 GroupID = stmt.getColumn(0).getInt();
962 for (
auto itr = a_Restrictions.cbegin(), end = a_Restrictions.cend(); itr != end; ++itr)
966 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM RestrictionItem WHERE PermGroupID = ? AND Permission = ?");
967 stmt.bind(1, GroupID);
969 if (!stmt.executeStep())
971 LOGWARNING(
"%s: Failed to check binding between restriction %s and group %s, aborting.", __FUNCTION__, itr->c_str(), a_GroupName.c_str());
974 if (stmt.getColumn(0).getInt() > 0)
976 LOGD(
"%s: Restriction %s is already present in group %s, skipping and returning success.",
977 __FUNCTION__, itr->c_str(), a_GroupName.c_str()
985 SQLite::Statement stmt(
m_DB,
"INSERT INTO RestrictionItem (Permission, PermGroupID) VALUES (?, ?)");
987 stmt.bind(2, GroupID);
988 if (stmt.exec() <= 0)
990 LOGWARNING(
"%s: Failed to add restriction %s to group %s, skipping.", __FUNCTION__, itr->c_str(), a_GroupName.c_str());
999 catch (
const SQLite::Exception & ex)
1001 LOGWARNING(
"%s: Failed to add restrictions to group %s: %s",
1002 __FUNCTION__, a_GroupName.c_str(), ex.what()
1020 LOGWARNING(
"%s: Cannot remove rank %s, it is the default rank and the replacement rank doesn't exist.", __FUNCTION__, a_RankName.c_str());
1030 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1031 stmt.bind(1, a_RankName);
1032 if (!stmt.executeStep())
1034 LOGINFO(
"%s: Rank %s was not found. Skipping.", __FUNCTION__, a_RankName.c_str());
1037 RemoveRankID = stmt.getColumn(0).getInt();
1041 int ReplacementRankID = -1;
1043 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1044 stmt.bind(1, a_ReplacementRankName);
1045 if (stmt.executeStep())
1047 ReplacementRankID = stmt.getColumn(0).getInt();
1053 SQLite::Statement stmt(
m_DB,
"DELETE FROM RankPermGroup WHERE RankID = ?");
1054 stmt.bind(1, RemoveRankID);
1059 if (ReplacementRankID == -1)
1062 SQLite::Statement stmt(
m_DB,
"DELETE FROM PlayerRank WHERE RankID = ?");
1063 stmt.bind(1, RemoveRankID);
1069 SQLite::Statement stmt(
m_DB,
"UPDATE PlayerRank SET RankID = ? WHERE RankID = ?");
1070 stmt.bind(1, ReplacementRankID);
1071 stmt.bind(2, RemoveRankID);
1077 SQLite::Statement stmt(
m_DB,
"DELETE FROM Rank WHERE RankID = ?");
1078 stmt.bind(1, RemoveRankID);
1088 catch (
const SQLite::Exception & ex)
1090 LOGWARNING(
"%s: Failed to remove rank from DB: %s", __FUNCTION__, ex.what());
1108 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
1109 stmt.bind(1, a_GroupName);
1110 if (!stmt.executeStep())
1112 LOGINFO(
"%s: Group %s was not found, skipping.", __FUNCTION__, a_GroupName.c_str());
1115 GroupID = stmt.getColumn(0).getInt();
1120 SQLite::Statement stmt(
m_DB,
"DELETE FROM PermissionItem WHERE PermGroupID = ?");
1121 stmt.bind(1, GroupID);
1127 SQLite::Statement stmt(
m_DB,
"DELETE FROM RankPermGroup WHERE PermGroupID = ?");
1128 stmt.bind(1, GroupID);
1134 SQLite::Statement stmt(
m_DB,
"DELETE FROM PermGroup WHERE PermGroupID = ?");
1135 stmt.bind(1, GroupID);
1139 catch (
const SQLite::Exception & ex)
1141 LOGWARNING(
"%s: Failed to remove group %s from DB: %s", __FUNCTION__, a_GroupName.c_str(), ex.what());
1157 int GroupID, RankID;
1159 SQLite::Statement stmt(
m_DB,
1160 "SELECT PermGroup.PermGroupID, Rank.RankID FROM PermGroup "
1161 "LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermGroup.PermGroupID "
1162 "LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
1163 "WHERE PermGroup.Name = ? AND Rank.Name = ?"
1165 stmt.bind(1, a_GroupName);
1166 stmt.bind(2, a_RankName);
1167 if (!stmt.executeStep())
1169 LOGINFO(
"%s: Group %s was not found in rank %s, skipping.", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str());
1172 GroupID = stmt.getColumn(0).getInt();
1173 RankID = stmt.getColumn(1).getInt();
1178 SQLite::Statement stmt(
m_DB,
"DELETE FROM RankPermGroup WHERE PermGroupID = ?");
1179 stmt.bind(1, GroupID);
1185 SQLite::Statement stmt(
m_DB,
"DELETE FROM RankPermGroup WHERE PermGroupID = ? AND RankID = ?");
1186 stmt.bind(1, GroupID);
1187 stmt.bind(1, RankID);
1191 catch (
const SQLite::Exception & ex)
1193 LOGWARNING(
"%s: Failed to remove group %s from rank %s in the DB: %s", __FUNCTION__, a_GroupName.c_str(), a_RankName.c_str(), ex.what());
1211 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
1212 stmt.bind(1, a_GroupName);
1213 if (!stmt.executeStep())
1215 LOGINFO(
"%s: Group %s was not found, skipping.", __FUNCTION__, a_GroupName.c_str());
1218 GroupID = stmt.getColumn(0).getInt();
1223 SQLite::Statement stmt(
m_DB,
"DELETE FROM PermissionItem WHERE PermGroupID = ? AND Permission = ?");
1224 stmt.bind(1, GroupID);
1225 stmt.bind(2, a_Permission);
1229 catch (
const SQLite::Exception & ex)
1231 LOGWARNING(
"%s: Failed to remove permission %s from group %s in DB: %s",
1232 __FUNCTION__, a_Permission.c_str(), a_GroupName.c_str(), ex.what()
1251 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
1252 stmt.bind(1, a_GroupName);
1253 if (!stmt.executeStep())
1255 LOGINFO(
"%s: Group %s was not found, skipping.", __FUNCTION__, a_GroupName.c_str());
1258 GroupID = stmt.getColumn(0).getInt();
1263 SQLite::Statement stmt(
m_DB,
"DELETE FROM RestrictionItem WHERE PermGroupID = ? AND Permission = ?");
1264 stmt.bind(1, GroupID);
1265 stmt.bind(2, a_Restriction);
1269 catch (
const SQLite::Exception & ex)
1271 LOGWARNING(
"%s: Failed to remove restriction %s from group %s in DB: %s",
1272 __FUNCTION__, a_Restriction.c_str(), a_GroupName.c_str(), ex.what()
1290 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1291 stmt.bind(1, a_NewName);
1292 if (stmt.executeStep())
1294 LOGINFO(
"%s: Rank %s is already present, cannot rename %s", __FUNCTION__, a_NewName.c_str(), a_OldName.c_str());
1301 SQLite::Statement stmt(
m_DB,
"UPDATE Rank SET Name = ? WHERE Name = ?");
1302 stmt.bind(1, a_NewName);
1303 stmt.bind(2, a_OldName);
1304 if (stmt.exec() <= 0)
1306 LOGINFO(
"%s: There is no rank %s, cannot rename to %s.", __FUNCTION__, a_OldName.c_str(), a_NewName.c_str());
1319 catch (
const SQLite::Exception & ex)
1321 LOGWARNING(
"%s: Failed to rename rank %s to %s in DB: %s",
1322 __FUNCTION__, a_OldName.c_str(), a_NewName.c_str(), ex.what());
1340 SQLite::Statement stmt(
m_DB,
"SELECT PermGroupID FROM PermGroup WHERE Name = ?");
1341 stmt.bind(1, a_NewName);
1342 if (stmt.executeStep())
1344 LOGD(
"%s: Group %s is already present, cannot rename %s", __FUNCTION__, a_NewName.c_str(), a_OldName.c_str());
1352 SQLite::Statement stmt(
m_DB,
"UPDATE PermGroup SET Name = ? WHERE Name = ?");
1353 stmt.bind(1, a_NewName);
1354 stmt.bind(2, a_OldName);
1355 res = (stmt.exec() > 0);
1360 catch (
const SQLite::Exception & ex)
1362 LOGWARNING(
"%s: Failed to rename group %s to %s in DB: %s",
1363 __FUNCTION__, a_OldName.c_str(), a_NewName.c_str(), ex.what());
1384 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1385 stmt.bind(1, a_RankName);
1386 if (!stmt.executeStep())
1388 LOGWARNING(
"%s: There is no rank %s, aborting.", __FUNCTION__, a_RankName.c_str());
1391 RankID = stmt.getColumn(0).getInt();
1396 SQLite::Statement stmt(
m_DB,
"UPDATE PlayerRank SET RankID = ?, PlayerName = ? WHERE PlayerUUID = ?");
1397 stmt.bind(1, RankID);
1398 stmt.bind(2, a_PlayerName);
1399 stmt.bind(3, StrUUID);
1400 if (stmt.exec() > 0)
1408 SQLite::Statement stmt(
m_DB,
"INSERT INTO PlayerRank (RankID, PlayerUUID, PlayerName) VALUES (?, ?, ?)");
1409 stmt.bind(1, RankID);
1410 stmt.bind(2, StrUUID);
1411 stmt.bind(3, a_PlayerName);
1412 if (stmt.exec() > 0)
1418 LOGWARNING(
"%s: Failed to set player UUID %s to rank %s.",
1419 __FUNCTION__, StrUUID.c_str(), a_RankName.c_str()
1422 catch (
const SQLite::Exception & ex)
1424 LOGWARNING(
"%s: Failed to set player UUID %s to rank %s: %s",
1425 __FUNCTION__, StrUUID.c_str(), a_RankName.c_str(), ex.what()
1443 SQLite::Statement stmt(
m_DB,
"DELETE FROM PlayerRank WHERE PlayerUUID = ?");
1444 stmt.bind(1, StrUUID);
1447 catch (
const SQLite::Exception & ex)
1449 LOGWARNING(
"%s: Failed to remove rank from player UUID %s: %s",
1450 __FUNCTION__, StrUUID.c_str(), ex.what()
1463 const AString & a_MsgNameColorCode
1471 SQLite::Statement stmt(
m_DB,
"UPDATE Rank SET MsgPrefix = ?, MsgSuffix = ?, MsgNameColorCode = ? WHERE Name = ?");
1472 stmt.bind(1, a_MsgPrefix);
1473 stmt.bind(2, a_MsgSuffix);
1474 stmt.bind(3, a_MsgNameColorCode);
1475 stmt.bind(4, a_RankName);
1476 if (stmt.exec() < 1)
1478 LOGINFO(
"%s: Rank %s not found, visuals not set.", __FUNCTION__, a_RankName.c_str());
1481 catch (
const SQLite::Exception & ex)
1483 LOGWARNING(
"%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what());
1503 SQLite::Statement stmt(
m_DB,
"SELECT MsgPrefix, MsgSuffix, MsgNameColorCode FROM Rank WHERE Name = ?");
1504 stmt.bind(1, a_RankName);
1505 if (!stmt.executeStep())
1510 a_MsgPrefix = stmt.getColumn(0).getText();
1511 a_MsgSuffix = stmt.getColumn(1).getText();
1512 a_MsgNameColorCode = stmt.getColumn(2).getText();
1515 catch (
const SQLite::Exception & ex)
1517 LOGWARNING(
"%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what());
1533 SQLite::Statement stmt(
m_DB,
"SELECT * FROM Rank WHERE Name = ?");
1534 stmt.bind(1, a_RankName);
1535 if (stmt.executeStep())
1541 catch (
const SQLite::Exception & ex)
1543 LOGWARNING(
"%s: Failed to query DB for rank %s: %s", __FUNCTION__, a_RankName.c_str(), ex.what());
1559 SQLite::Statement stmt(
m_DB,
"SELECT * FROM PermGroup WHERE Name = ?");
1560 stmt.bind(1, a_GroupName);
1561 if (stmt.executeStep())
1567 catch (
const SQLite::Exception & ex)
1569 LOGWARNING(
"%s: Failed to query DB for group %s: %s", __FUNCTION__, a_GroupName.c_str(), ex.what());
1587 SQLite::Statement stmt(
m_DB,
"SELECT * FROM PlayerRank WHERE PlayerUUID = ?");
1588 stmt.bind(1, StrUUID);
1589 if (stmt.executeStep())
1595 catch (
const SQLite::Exception & ex)
1597 LOGWARNING(
"%s: Failed to query DB for player UUID %s: %s", __FUNCTION__, StrUUID.c_str(), ex.what());
1613 SQLite::Statement stmt(
m_DB,
1614 "SELECT * FROM Rank "
1615 "LEFT JOIN RankPermGroup ON Rank.RankID = RankPermGroup.RankID "
1616 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = RankPermGroup.PermGroupID "
1617 "WHERE Rank.Name = ? AND PermGroup.Name = ?"
1619 stmt.bind(1, a_RankName);
1620 stmt.bind(2, a_GroupName);
1621 if (stmt.executeStep())
1627 catch (
const SQLite::Exception & ex)
1629 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1645 SQLite::Statement stmt(
m_DB,
1646 "SELECT * FROM PermissionItem "
1647 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = PermissionItem.PermGroupID "
1648 "WHERE PermissionItem.Permission = ? AND PermGroup.Name = ?"
1650 stmt.bind(1, a_Permission);
1651 stmt.bind(2, a_GroupName);
1652 if (stmt.executeStep())
1658 catch (
const SQLite::Exception & ex)
1660 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1676 SQLite::Statement stmt(
m_DB,
1677 "SELECT * FROM RestrictionItem "
1678 "LEFT JOIN PermGroup ON PermGroup.PermGroupID = RestrictionItem.PermGroupID "
1679 "WHERE RestrictionItem.Permission = ? AND PermGroup.Name = ?"
1681 stmt.bind(1, a_Restriction);
1682 stmt.bind(2, a_GroupName);
1683 if (stmt.executeStep())
1689 catch (
const SQLite::Exception & ex)
1691 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1707 SQLite::Statement stmt(
m_DB,
"UPDATE PlayerRank SET PlayerName = ? WHERE PlayerUUID = ?");
1708 stmt.bind(1, a_PlayerName);
1712 catch (
const SQLite::Exception & ex)
1714 LOGWARNING(
"%s: Failed to update DB: %s", __FUNCTION__, ex.what());
1732 SQLite::Statement stmt(
m_DB,
"SELECT RankID FROM Rank WHERE Name = ?");
1733 stmt.bind(1, a_RankName);
1734 if (!stmt.executeStep())
1736 LOGINFO(
"%s: Cannot set rank %s as the default, it does not exist.", __FUNCTION__, a_RankName.c_str());
1743 SQLite::Statement stmt(
m_DB,
"UPDATE DefaultRank SET RankID = ?");
1744 stmt.bind(1, RankID);
1745 if (stmt.exec() < 1)
1748 SQLite::Statement stmt2(
m_DB,
"INSERT INTO DefaultRank (RankID) VALUES (?)");
1749 stmt2.bind(1, RankID);
1750 if (stmt2.exec() < 1)
1752 LOGINFO(
"%s: Cannot update the default rank in the DB to %s.", __FUNCTION__, a_RankName.c_str());
1762 catch (
const SQLite::Exception & ex)
1764 LOGWARNING(
"%s: Failed to update DB: %s", __FUNCTION__, ex.what());
1780 SQLite::Statement stmt(
m_DB,
"DELETE FROM PlayerRank");
1783 catch (SQLite::Exception & ex)
1785 LOGWARNING(
"%s: Failed to remove / clear all players: %s", __FUNCTION__, ex.what());
1802 SQLite::Statement stmt(
m_DB,
"UPDATE PlayerRank SET PlayerName = ? WHERE PlayerUUID = ?");
1803 stmt.bind(1, a_NewPlayerName);
1804 stmt.bind(2, StrUUID);
1805 if (stmt.exec() > 0)
1811 catch (
const SQLite::Exception & ex)
1813 LOGWARNING(
"%s: Failed to update player name from UUID %s: %s", __FUNCTION__, StrUUID.c_str(), ex.what());
1842 SQLite::Statement stmt(
m_DB,
"SELECT COUNT(*) FROM " + a_TableName);
1843 return (stmt.executeStep() && (stmt.getColumn(0).getInt() == 0));
1845 catch (
const SQLite::Exception & ex)
1847 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1862 AddRank(
"Default",
"",
"",
"");
1864 AddRank(
"Operator",
"",
"",
"");
1899 SQLite::Statement stmt(
m_DB, fmt::format(FMT_STRING(
"PRAGMA table_info({})"), a_TableName));
1900 while (stmt.executeStep())
1902 int NumColumns = stmt.getColumnCount();
1903 for (
int i = 0; i < NumColumns; i++)
1905 auto column = stmt.getColumn(i);
1906 if (strcmp(column.getName(),
"name") == 0)
1917 catch (
const SQLite::Exception & ex)
1919 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, ex.what());
1939 m_DB.exec(fmt::format(FMT_STRING(
"ALTER TABLE {} ADD COLUMN {} {}"), a_TableName, a_ColumnName, a_ColumnType));
1941 catch (
const SQLite::Exception & exc)
1943 LOGWARNING(
"%s: Failed to query DB: %s", __FUNCTION__, exc.what());
void LOGWARNING(std::string_view a_Format, const Args &... args)
void LOGINFO(std::string_view a_Format, const Args &... args)
int NoCaseCompare(const AString &s1, const AString &s2)
Case-insensitive string comparison.
std::vector< AString > AStringVector
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
void SetRankManager(cRankManager *a_RankManager)
Sets the m_RankMgr that is used for name-uuid notifications.
AStringVector GetGroupPermissions(const AString &a_GroupName)
Returns the permissions that the specified group has assigned to it.
void AddGroups(const AStringVector &a_GroupNames)
Bulk-adds groups.
void AddGroup(const AString &a_GroupName)
Adds a new permission group.
AStringVector GetGroupRestrictions(const AString &a_GroupName)
Returns the restrictions that the specified group has assigned to it.
bool IsPermissionInGroup(const AString &a_Permission, const AString &a_GroupName)
Returns true iff the specified group contains the specified permission.
AStringVector GetAllPermissionsRestrictions(void)
Returns all the distinct permissions and restrictions that are stored in the DB.
void RemoveRestrictionFromGroup(const AString &a_Restriction, const AString &a_GroupName)
Removes the specified restriction from the specified group.
bool GroupExists(const AString &a_GroupName)
Returns true iff the specified group exists in the DB.
void SetRankVisuals(const AString &a_RankName, const AString &a_MsgPrefix, const AString &a_MsgSuffix, const AString &a_MsgNameColorCode)
Sets the message visuals of an existing rank.
bool AddGroupToRank(const AString &a_GroupName, const AString &a_RankName)
Adds the specified permission group to the specified rank.
cRankManager(void)
Creates the rank manager.
bool DoesColumnExist(const char *a_TableName, const char *a_ColumnName)
Returns true if the specified column exists in the specified table.
bool RenameGroup(const AString &a_OldName, const AString &a_NewName)
Renames the specified group.
bool SetDefaultRank(const AString &a_RankName)
Sets the specified rank as the default rank.
AString m_DefaultRank
The name of the default rank.
void RemovePlayerRank(const cUUID &a_PlayerUUID)
Removes the player's rank assignment.
bool AddPermissionToGroup(const AString &a_Permission, const AString &a_GroupName)
Adds the specified permission to the specified permission group.
bool m_IsInitialized
Set to true once the manager is initialized.
AStringVector GetPlayerPermissions(const cUUID &a_PlayerUUID)
Returns the permissions that the specified player has assigned to them.
AStringVector GetAllGroups(void)
Returns the names of all permission groups.
void AddRank(const AString &a_RankName, const AString &a_MsgPrefix, const AString &a_MsgSuffix, const AString &a_MsgNameColorCode)
Adds a new rank.
bool GetRankVisuals(const AString &a_RankName, AString &a_MsgPrefix, AString &a_MsgSuffix, AString &a_MsgNameColorCode)
Returns the message visuals of an existing rank.
bool IsDBTableEmpty(const AString &a_TableName)
Returns true iff the specified DB table is empty.
void CreateColumnIfNotExists(const char *a_TableName, const char *a_ColumnName, const char *a_ColumnType="")
If the specified table doesn't contain the specified column, it is added to the table.
bool RankExists(const AString &a_RankName)
Returns true iff the specified rank exists in the DB.
AStringVector GetRankPermissions(const AString &a_RankName)
Returns all permissions that the specified rank has assigned to it, through all its groups.
cCriticalSection m_CS
The mutex protecting m_DB and m_DefaultRank against multi-threaded access.
AString GetPlayerName(const cUUID &a_PlayerUUID)
Returns the last name that the specified player has.
bool IsPlayerRankSet(const cUUID &a_PlayerUUID)
Returns true iff the specified player has a rank assigned to them in the DB.
void RemovePermissionFromGroup(const AString &a_Permission, const AString &a_GroupName)
Removes the specified permission from the specified group.
void Initialize(cMojangAPI &a_MojangAPI)
Initializes the rank manager.
AStringVector GetPlayerGroups(const cUUID &a_PlayerUUID)
Returns the names of Groups that the specified player has assigned to them.
AStringVector GetAllRanks(void)
Returns the names of all defined ranks.
bool AreDBTablesEmpty(void)
Returns true if all the DB tables are empty, indicating a fresh new install.
bool AddRestrictionToGroup(const AString &a_Restriction, const AString &a_GroupName)
Adds the specified restriction to the specified group.
bool GetPlayerMsgVisuals(const cUUID &a_PlayerUUID, AString &a_MsgPrefix, AString &a_MsgSuffix, AString &a_MsgNameColorCode)
Returns the message visuals (prefix, postfix, color) for the specified player.
AString GetPlayerRankName(const cUUID &a_PlayerUUID)
Returns the name of the rank that the specified player has assigned to them.
void RemoveGroupFromRank(const AString &a_GroupName, const AString &a_RankName)
Removes the specified group from the specified rank.
void NotifyNameUUID(const AString &a_PlayerName, const cUUID &a_UUID)
Called by cMojangAPI whenever the playername-uuid pairing is discovered.
AStringVector GetRankGroups(const AString &a_RankName)
Returns the names of groups that the specified rank has assigned to it.
void RemoveGroup(const AString &a_GroupName)
Removes the specified group completely.
void SetPlayerRank(const cUUID &a_PlayerUUID, const AString &a_PlayerName, const AString &a_RankName)
Sets the specified player's rank.
bool UpdatePlayerName(const cUUID &a_PlayerUUID, const AString &a_NewPlayerName)
Updates the playername that is saved with this uuid.
AStringVector GetAllRestrictions(void)
Returns all the distinct restrictions that are stored in the DB.
void CreateDefaults(void)
Creates a default set of ranks / groups / permissions.
AStringVector GetRankRestrictions(const AString &a_RankName)
Returns all restrictions that the specified rank has assigned to it, through all its groups.
void RemoveRank(const AString &a_RankName, const AString &a_ReplacementRankName)
Removes the specified rank.
SQLite::Database m_DB
The database storage for all the data.
std::vector< cUUID > GetAllPlayerUUIDs(void)
Returns the uuids of all defined players.
AStringVector GetPlayerRestrictions(const cUUID &a_PlayerUUID)
Returns the restrictions that the specified player has assigned to them.
bool RenameRank(const AString &a_OldName, const AString &a_NewName)
Renames the specified rank.
bool AddPermissionsToGroup(const AStringVector &a_Permissions, const AString &a_GroupName)
Adds the specified permissions to the specified permission group.
AStringVector GetAllPermissions(void)
Returns all the distinct permissions that are stored in the DB.
void ClearPlayerRanks(void)
Removes all player ranks from the database.
bool IsGroupInRank(const AString &a_GroupName, const AString &a_RankName)
Returns true iff the specified rank contains the specified group.
bool IsRestrictionInGroup(const AString &a_Restriction, const AString &a_GroupName)
Returns true iff the specified group contains the specified restriction.
bool AddRestrictionsToGroup(const AStringVector &a_Restrictions, const AString &a_GroupName)
Adds the specified restrictions to the specified group.
Acquire this lock to perform mass changes.
AString ToShortString() const
Converts the UUID to a short form string (i.e without dashes).
bool FromString(const AString &a_StringUUID)
Tries to interpret the string as a short or long form UUID and assign from it.