forked from cuberite/cuberite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRankManager.cpp
2331 lines (1884 loc) · 53.7 KB
/
RankManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// RankManager.cpp
// Implements the cRankManager class that represents the rank manager responsible for assigning permissions and message visuals to players
#include "Globals.h"
#include "RankManager.h"
#include "IniFile.h"
#include "Protocol/MojangAPI.h"
#include "ClientHandle.h"
////////////////////////////////////////////////////////////////////////////////
// cRankManagerIniMigrator:
/** Migrates from groups.ini and users.ini into the rankmanager DB */
class cRankManagerIniMigrator
{
public:
cRankManagerIniMigrator(cRankManager & a_RankManager, cMojangAPI & a_MojangAPI) :
m_RankManager(a_RankManager),
m_MojangAPI(a_MojangAPI)
{
}
/** Performs the complete migration from INI files to DB. */
bool Migrate(void)
{
cRankManager::cMassChangeLock Lock(m_RankManager);
LOGD("Reading groups...");
if (!ReadGroups())
{
return false;
}
LOGD("Cleaning groups inheritance...");
CleanGroupInheritance();
LOGD("Creating groups...");
CreateGroups();
LOGD("Reading users...");
if (!ReadUsers())
{
return false;
}
LOGD("Cleaning user groups...");
CleanUserGroups();
LOGD("Resolving user UUIDs...");
ResolveUserUUIDs();
LOGD("Setting ranks...");
SetRanks();
LOGD("Creating defaults...");
CreateDefaults();
return true;
}
protected:
/** Container for a group read from an INI file. */
struct sGroup
{
AString m_Name;
AString m_Color;
AStringVector m_Inherits;
AStringVector m_Permissions;
sGroup(void) {}
sGroup(const AString & a_Name, const AString & a_Color, const AStringVector & a_Inherits, const AStringVector & a_Permissions):
m_Name(a_Name),
m_Color(a_Color),
m_Inherits(a_Inherits),
m_Permissions(a_Permissions)
{
}
};
typedef std::map<AString, sGroup> sGroupMap;
/** Container for a single user read from an INI file. */
struct sUser
{
AString m_Name;
AStringVector m_Groups;
/** Assigned by ResolveUserUUIDs(), contains the online (Mojang) UUID of the player. */
cUUID m_UUID;
/** Assigned by ResolveUserUUIDs(), contains the offline (generated) UUID of the player. */
cUUID m_OfflineUUID;
sUser(void) {}
sUser(const AString & a_Name, const AStringVector & a_Groups):
m_Name(a_Name),
m_Groups(a_Groups)
{
}
};
typedef std::map<AString, sUser> sUserMap;
typedef std::map<AString, AString> cStringMap;
/** The parent Rank manager where we will create the groups, ranks and players */
cRankManager & m_RankManager;
/** The player name to UUID resolver */
cMojangAPI & m_MojangAPI;
/** List of all groups read from the ini file */
sGroupMap m_Groups;
/** List of all players read from the ini file. */
sUserMap m_Users;
/** Maps lists of groups to rank names.
Each group list is either a simple "<Group>" if there's only one group,
or "<PrimaryGroup>, <FirstSecondaryGroup>, <SecondSecondaryGroup>...", where the secondary groups are
lowercased and alpha-sorted. This makes the group lists comparable for equivalence, simply by comparing
their string names.
The ranks are named "<Group>" for single-group players, and "AutoMigratedRank_N" for the composite ranks,
where N is a unique number. */
cStringMap m_GroupsToRanks;
/** Reads the groups from the "groups.ini" file into m_Groups */
bool ReadGroups(void)
{
// Read the file:
cIniFile Groups;
if (!Groups.ReadFile("groups.ini"))
{
return false;
}
// Read all the groups into a map:
int NumGroups = Groups.GetNumKeys();
for (int i = 0; i < NumGroups; i++)
{
AString GroupName = Groups.GetKeyName(i);
AString lcGroupName = StrToLower(GroupName);
if (m_Groups.find(lcGroupName) != m_Groups.end())
{
LOGINFO("groups.ini contains a duplicate definition of group %s, ignoring the latter.", GroupName.c_str());
continue;
}
m_Groups[lcGroupName] = sGroup(
GroupName,
Groups.GetValue(GroupName, "Color", ""),
StringSplitAndTrim(Groups.GetValue(GroupName, "Inherits"), ","),
StringSplitAndTrim(Groups.GetValue(GroupName, "Permissions"), ",")
);
} // for i - Groups' keys
return true;
}
/** Removes non-existent groups from all the groups' inheritance */
void CleanGroupInheritance(void)
{
for (sGroupMap::iterator itrG = m_Groups.begin(), endG = m_Groups.end(); itrG != endG; ++itrG)
{
AStringVector & Inherits = itrG->second.m_Inherits;
for (AStringVector::iterator itrI = Inherits.begin(); itrI != Inherits.end();)
{
AString lcInherits = StrToLower(*itrI);
if (m_Groups.find(lcInherits) != m_Groups.end())
{
// Inherited group exists, continue checking the next one
++itrI;
continue;
}
// Inherited group doesn't exist, remove it from the list:
LOGWARNING("RankMigrator: Group \"%s\" inherits from a non-existent group \"%s\", this inheritance will be ignored.",
itrG->second.m_Name.c_str(), itrI->c_str()
);
AStringVector::iterator itrI2 = itrI;
++itrI2;
Inherits.erase(itrI);
itrI = itrI2;
} // for itrI - Inherits[]
} // for itrG - m_Groups[]
}
/** Reads the users from the "users.ini" file into m_Users */
bool ReadUsers(void)
{
// Read the file:
cIniFile Users;
if (!Users.ReadFile("users.ini"))
{
return false;
}
// Read all the users into a map:
int NumUsers = Users.GetNumKeys();
for (int i = 0; i < NumUsers; i++)
{
AString UserName = Users.GetKeyName(i);
AString lcUserName = StrToLower(UserName);
if (m_Users.find(lcUserName) != m_Users.end())
{
LOGINFO("users.ini contains a duplicate definition of user %s, ignoring the latter.", UserName.c_str());
continue;
}
m_Users[lcUserName] = sUser(
UserName,
StringSplitAndTrim(Users.GetValue(UserName, "Groups", ""), ",")
);
} // for i - Users' keys
return true;
}
/** Removes non-existent groups from each user's definition. */
void CleanUserGroups(void)
{
for (sUserMap::iterator itrU = m_Users.begin(), endU = m_Users.end(); itrU != endU; ++itrU)
{
AStringVector & Groups = itrU->second.m_Groups;
for (AStringVector::iterator itrG = Groups.begin(); itrG != Groups.end();)
{
AString lcGroup = StrToLower(*itrG);
if (m_Groups.find(lcGroup) != m_Groups.end())
{
// Assigned group exists, continue checking the next one
++itrG;
continue;
}
// Assigned group doesn't exist, remove it from the list:
LOGWARNING("RankMigrator: User \"%s\" is assigned a non-existent group \"%s\", this assignment will be ignored.",
itrU->second.m_Name.c_str(), itrG->c_str()
);
AStringVector::iterator itrG2 = itrG;
++itrG2;
Groups.erase(itrG);
itrG = itrG2;
} // for itrG - Groups[]
} // for itrU - m_Users[]
}
/** Creates groups based on m_Groups.
Ignores group inheritance. */
void CreateGroups(void)
{
// Create each group, with its permissions:
for (sGroupMap::const_iterator itr = m_Groups.begin(), end = m_Groups.end(); itr != end; ++itr)
{
m_RankManager.AddGroup(itr->second.m_Name);
m_RankManager.AddPermissionsToGroup(itr->second.m_Permissions, itr->second.m_Name);
} // for itr - m_Groups[]
}
/** Resolves the UUID of each user in m_Users.
If a user doesn't resolve, they are removed and logged in the console. */
void ResolveUserUUIDs(void)
{
// Resolve all PlayerNames at once (the API doesn't like single-name queries):
AStringVector PlayerNames;
for (sUserMap::const_iterator itr = m_Users.begin(), end = m_Users.end(); itr != end; ++itr)
{
PlayerNames.push_back(itr->second.m_Name);
}
m_MojangAPI.GetUUIDsFromPlayerNames(PlayerNames);
// Assign the UUIDs back to players, remove those not resolved:
for (auto & User : m_Users)
{
cUUID UUID = m_MojangAPI.GetUUIDFromPlayerName(User.second.m_Name);
if (UUID.IsNil())
{
LOGWARNING("RankMigrator: Cannot resolve player %s to online UUID, player will be left unranked in online mode", User.second.m_Name.c_str());
}
User.second.m_UUID = UUID;
User.second.m_OfflineUUID = cClientHandle::GenerateOfflineUUID(User.second.m_Name);
}
}
/** Adds the specified groups to the specified ranks. Recurses on the groups' inheritance. */
void AddGroupsToRank(const AStringVector & a_Groups, const AString & a_RankName)
{
for (AStringVector::const_iterator itr = a_Groups.begin(), end = a_Groups.end(); itr != end; ++itr)
{
// Normalize the group name:
sGroup & Group = m_Groups[StrToLower(*itr)];
// Avoid loops, check if the group is already added:
if (m_RankManager.IsGroupInRank(Group.m_Name, a_RankName))
{
continue;
}
// Add the group, and all the groups it inherits from recursively:
m_RankManager.AddGroupToRank(Group.m_Name, a_RankName);
AddGroupsToRank(Group.m_Inherits, a_RankName);
} // for itr - a_Groups[]
}
/** Creates a rank for each player, based on the master groups they are assigned. */
void SetRanks(void)
{
for (sUserMap::const_iterator itr = m_Users.begin(), end = m_Users.end(); itr != end; ++itr)
{
// Ignore users with no groups:
const AStringVector & Groups = itr->second.m_Groups;
if (Groups.empty())
{
LOGWARNING("RankMigrator: Player %s has no groups assigned to them, skipping the player.", itr->second.m_Name.c_str());
continue;
}
// Compose the rank name out of group names:
AString RankName;
for (AStringVector::const_iterator itrG = Groups.begin(), endG = Groups.end(); itrG != endG; ++itrG)
{
AString GroupName = m_Groups[StrToLower(*itrG)].m_Name; // Normalize group name
if (!RankName.empty())
{
RankName.push_back(',');
}
RankName.append(GroupName);
} // for itrG - Groups[]
// Create the rank, with al its groups:
if (!m_RankManager.RankExists(RankName))
{
m_RankManager.AddRank(RankName, "", "", m_Groups[StrToLower(Groups[0])].m_Color);
AddGroupsToRank(Groups, RankName);
}
// Set the rank to the user, using both the online and offline UUIDs:
m_RankManager.SetPlayerRank(itr->second.m_UUID, itr->second.m_Name, RankName);
m_RankManager.SetPlayerRank(itr->second.m_OfflineUUID, itr->second.m_Name, RankName);
} // for itr - m_Users[]
}
/** Creates the Default rank that contains the Default group, if it exists.
Sets the RankManager's default rank. */
void CreateDefaults(void)
{
if (!m_RankManager.RankExists("Default"))
{
m_RankManager.AddRank("Default", "", "", "");
if (!m_RankManager.IsGroupInRank("Default", "Default"))
{
m_RankManager.AddGroupToRank("Default", "Default");
}
}
m_RankManager.SetDefaultRank("Default");
}
};
////////////////////////////////////////////////////////////////////////////////
// cRankManager:
cRankManager::cRankManager(void) :
m_DB("Ranks.sqlite", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE),
m_IsInitialized(false),
m_MojangAPI(nullptr)
{
}
cRankManager::~cRankManager()
{
if (m_MojangAPI != nullptr)
{
m_MojangAPI->SetRankManager(nullptr);
}
}
void cRankManager::Initialize(cMojangAPI & a_MojangAPI)
{
ASSERT(!m_IsInitialized); // Calling Initialize for the second time?
// Create the DB tables, if they don't exist:
m_DB.exec("CREATE TABLE IF NOT EXISTS Rank (RankID INTEGER PRIMARY KEY, Name, MsgPrefix, MsgSuffix, MsgNameColorCode)");
m_DB.exec("CREATE TABLE IF NOT EXISTS PlayerRank (PlayerUUID, PlayerName, RankID INTEGER)");
m_DB.exec("CREATE TABLE IF NOT EXISTS PermGroup (PermGroupID INTEGER PRIMARY KEY, Name)");
m_DB.exec("CREATE TABLE IF NOT EXISTS RankPermGroup (RankID INTEGER, PermGroupID INTEGER)");
m_DB.exec("CREATE TABLE IF NOT EXISTS PermissionItem (PermGroupID INTEGER, Permission)");
m_DB.exec("CREATE TABLE IF NOT EXISTS RestrictionItem (PermGroupID INTEGER, Permission)");
m_DB.exec("CREATE TABLE IF NOT EXISTS DefaultRank (RankID INTEGER)");
m_IsInitialized = true;
a_MojangAPI.SetRankManager(this);
// Check if tables empty, migrate from ini files then
if (AreDBTablesEmpty())
{
LOGINFO("There are no ranks, migrating old-style INI files to new DB ranks...");
LOGINFO("(This might take a while)");
cRankManagerIniMigrator Migrator(*this, a_MojangAPI);
if (Migrator.Migrate())
{
LOGINFO("Ranks migrated.");
// The default rank has been set by the migrator
return;
}
// Migration failed. Add some defaults
LOGINFO("Rank migration failed, creating default ranks...");
CreateDefaults();
LOGINFO("Default ranks created.");
}
// Load the default rank:
try
{
SQLite::Statement stmt(m_DB,
"SELECT Rank.Name FROM Rank "
"LEFT JOIN DefaultRank ON Rank.RankID = DefaultRank.RankID"
);
if (stmt.executeStep())
{
m_DefaultRank = stmt.getColumn(0).getText();
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Cannot load default rank: %s", __FUNCTION__, ex.what());
return;
}
// If the default rank cannot be loaded, use the first rank:
if (m_DefaultRank.empty())
{
SetDefaultRank(GetAllRanks()[0]);
}
}
AString cRankManager::GetPlayerRankName(const cUUID & a_PlayerUUID)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
try
{
SQLite::Statement stmt(m_DB, "SELECT Rank.Name FROM Rank LEFT JOIN PlayerRank ON Rank.RankID = PlayerRank.RankID WHERE PlayerRank.PlayerUUID = ?");
stmt.bind(1, a_PlayerUUID.ToShortString());
// executeStep returns false on no data
if (!stmt.executeStep())
{
// No data returned from the DB
return AString();
}
return stmt.getColumn(0).getText();
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Cannot get player rank name: %s", __FUNCTION__, ex.what());
}
return AString();
}
AString cRankManager::GetPlayerName(const cUUID & a_PlayerUUID)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
try
{
// Prepare the DB statement:
SQLite::Statement stmt(m_DB, "SELECT PlayerName FROM PlayerRank WHERE PlayerUUID = ?");
stmt.bind(1, a_PlayerUUID.ToShortString());
if (stmt.executeStep())
{
return stmt.getColumn(0).getText();
}
}
catch (SQLite::Exception & ex)
{
LOGWARNING("%s: Cannot get player name: %s", __FUNCTION__, ex.what());
}
return AString();
}
AStringVector cRankManager::GetPlayerGroups(const cUUID & a_PlayerUUID)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
// Prepare the DB statement:
SQLite::Statement stmt(m_DB,
"SELECT PermGroup.Name FROM PermGroup "
"LEFT JOIN RankPermGroup ON PermGroup.PermGroupID = RankPermGroup.PermGroupID "
"LEFT JOIN PlayerRank ON PlayerRank.RankID = RankPermGroup.RankID "
"WHERE PlayerRank.PlayerUUID = ?"
);
stmt.bind(1, a_PlayerUUID.ToShortString());
// Execute and get results:
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Cannot get player groups: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetPlayerPermissions(const cUUID & a_PlayerUUID)
{
AString Rank = GetPlayerRankName(a_PlayerUUID);
if (Rank.empty())
{
Rank = m_DefaultRank;
}
return GetRankPermissions(Rank);
}
AStringVector cRankManager::GetPlayerRestrictions(const cUUID & a_PlayerUUID)
{
AString Rank = GetPlayerRankName(a_PlayerUUID);
if (Rank.empty())
{
Rank = m_DefaultRank;
}
return GetRankRestrictions(Rank);
}
AStringVector cRankManager::GetRankGroups(const AString & a_RankName)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB,
"SELECT PermGroup.Name FROM PermGroup "
"LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermGroup.PermGroupID "
"LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
"WHERE Rank.Name = ?"
);
stmt.bind(1, a_RankName);
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get rank groups from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetGroupPermissions(const AString & a_GroupName)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB,
"SELECT PermissionItem.Permission FROM PermissionItem "
"LEFT JOIN PermGroup ON PermGroup.PermGroupID = PermissionItem.PermGroupID "
"WHERE PermGroup.Name = ?"
);
stmt.bind(1, a_GroupName);
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get group permissions from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetGroupRestrictions(const AString & a_GroupName)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB,
"SELECT RestrictionItem.Permission FROM RestrictionItem "
"LEFT JOIN PermGroup ON PermGroup.PermGroupID = RestrictionItem.PermGroupID "
"WHERE PermGroup.Name = ?"
);
stmt.bind(1, a_GroupName);
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get group restrictions from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetRankPermissions(const AString & a_RankName)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB,
"SELECT PermissionItem.Permission FROM PermissionItem "
"LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = PermissionItem.PermGroupID "
"LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
"WHERE Rank.Name = ?"
);
stmt.bind(1, a_RankName);
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get rank permissions from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetRankRestrictions(const AString & a_RankName)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB,
"SELECT RestrictionItem.Permission FROM RestrictionItem "
"LEFT JOIN RankPermGroup ON RankPermGroup.PermGroupID = RestrictionItem.PermGroupID "
"LEFT JOIN Rank ON Rank.RankID = RankPermGroup.RankID "
"WHERE Rank.Name = ?"
);
stmt.bind(1, a_RankName);
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get rank restrictions from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
std::vector<cUUID> cRankManager::GetAllPlayerUUIDs(void)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
cUUID tempUUID;
std::vector<cUUID> res;
try
{
SQLite::Statement stmt(m_DB, "SELECT PlayerUUID FROM PlayerRank ORDER BY PlayerName COLLATE NOCASE");
while (stmt.executeStep())
{
if (!tempUUID.FromString(stmt.getColumn(0).getText()))
{
// Invalid UUID, ignore
continue;
}
res.push_back(tempUUID);
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get players from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetAllRanks(void)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB, "SELECT Name FROM Rank");
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get ranks from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetAllGroups(void)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB, "SELECT Name FROM PermGroup");
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get groups from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetAllPermissions(void)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB, "SELECT DISTINCT(Permission) FROM PermissionItem");
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get permissions from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetAllRestrictions(void)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
AStringVector res;
try
{
SQLite::Statement stmt(m_DB, "SELECT DISTINCT(Permission) FROM RestrictionItem");
while (stmt.executeStep())
{
res.push_back(stmt.getColumn(0).getText());
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to get restrictions from DB: %s", __FUNCTION__, ex.what());
}
return res;
}
AStringVector cRankManager::GetAllPermissionsRestrictions(void)
{
AStringVector Permissions = GetAllPermissions();
AStringVector Restrictions = GetAllRestrictions();
for (auto & restriction: Restrictions)
{
Permissions.push_back(restriction);
}
return Permissions;
}
bool cRankManager::GetPlayerMsgVisuals(
const cUUID & a_PlayerUUID,
AString & a_MsgPrefix,
AString & a_MsgSuffix,
AString & a_MsgNameColorCode
)
{
AString Rank = GetPlayerRankName(a_PlayerUUID);
if (Rank.empty())
{
// Rank not found, return failure:
a_MsgPrefix.clear();
a_MsgSuffix.clear();
a_MsgNameColorCode.clear();
return false;
}
return GetRankVisuals(Rank, a_MsgPrefix, a_MsgSuffix, a_MsgNameColorCode);
}
void cRankManager::AddRank(
const AString & a_RankName,
const AString & a_MsgPrefix,
const AString & a_MsgSuffix,
const AString & a_MsgNameColorCode
)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
try
{
// Check if such a rank name is already used:
{
SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM Rank WHERE Name = ?");
stmt.bind(1, a_RankName);
if (stmt.executeStep())
{
if (stmt.getColumn(0).getInt() > 0)
{
// Rank already exists, do nothing:
return;
}
}
}
// Insert a new rank:
SQLite::Statement stmt(m_DB, "INSERT INTO Rank (Name, MsgPrefix, MsgSuffix, MsgNameColorCode) VALUES (?, ?, ?, ?)");
stmt.bind(1, a_RankName);
stmt.bind(2, a_MsgPrefix);
stmt.bind(3, a_MsgSuffix);
stmt.bind(4, a_MsgNameColorCode);
if (stmt.exec() <= 0)
{
LOGWARNING("%s: Failed to add a new rank \"%s\".", __FUNCTION__, a_RankName.c_str());
return;
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to add a new rank \"%s\": %s", __FUNCTION__, a_RankName.c_str(), ex.what());
}
}
void cRankManager::AddGroup(const AString & a_GroupName)
{
ASSERT(m_IsInitialized);
cCSLock Lock(m_CS);
try
{
// Check if such a group name is already used:
{
SQLite::Statement stmt(m_DB, "SELECT COUNT(*) FROM PermGroup WHERE Name = ?");
stmt.bind(1, a_GroupName);
if (stmt.executeStep())
{
if (stmt.getColumn(0).getInt() > 0)
{
// Group already exists, do nothing:
return;
}
}
}
// Insert a new group:
SQLite::Statement stmt(m_DB, "INSERT INTO PermGroup (Name) VALUES (?)");
stmt.bind(1, a_GroupName);
if (stmt.exec() <= 0)
{
LOGWARNING("%s: Failed to add a new group \"%s\".", __FUNCTION__, a_GroupName.c_str());
return;
}
}
catch (const SQLite::Exception & ex)
{
LOGWARNING("%s: Failed to add a new group \"%s\": %s", __FUNCTION__, a_GroupName.c_str(), ex.what());
}
}
void cRankManager::AddGroups(const AStringVector & a_GroupNames)
{