/
PermissionsManager.cs
569 lines (526 loc) · 17.4 KB
/
PermissionsManager.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;
namespace vMenuShared
{
public static class PermissionsManager
{
public enum Permission
{
// Global
Everything,
DontKickMe,
DontBanMe,
NoClip,
Staff,
// Online Players
OPMenu,
OPAll,
OPTeleport,
OPWaypoint,
OPSpectate,
OPIdentifiers,
OPSummon,
OPKill,
OPKick,
OPPermBan,
OPTempBan,
OPUnban,
OPViewBannedPlayers,
OPSeePrivateMessages,
// Player Options
POMenu,
POAll,
POGod,
POInvisible,
POFastRun,
POFastSwim,
POSuperjump,
PONoRagdoll,
PONeverWanted,
POSetWanted,
POIgnored,
POStayInVehicle,
POMaxHealth,
POMaxArmor,
POCleanPlayer,
PODryPlayer,
POWetPlayer,
POVehicleAutoPilotMenu,
POFreeze,
POScenarios,
POUnlimitedStamina,
// Vehicle Options
VOMenu,
VOAll,
VOGod,
VOKeepClean,
VORepair,
VOWash,
VOEngine,
VOBikeSeatbelt,
VOSpeedLimiter,
VOChangePlate,
VOMod,
VOColors,
VOLiveries,
VOComponents,
VODoors,
VOWindows,
VOFreeze,
VOInvisible,
VOTorqueMultiplier,
VOPowerMultiplier,
VOFlip,
VOAlarm,
VOCycleSeats,
VOEngineAlwaysOn,
VONoSiren,
VONoHelmet,
VOLights,
VOFixOrDestroyTires,
VODelete,
VOUnderglow,
VOFlashHighbeamsOnHonk,
VODisableTurbulence,
VOInfiniteFuel,
VOFlares,
VOPlaneBombs,
// Vehicle Spawner
VSMenu,
VSAll,
VSDisableReplacePrevious,
VSSpawnByName,
VSAddon,
VSCompacts,
VSSedans,
VSSUVs,
VSCoupes,
VSMuscle,
VSSportsClassic,
VSSports,
VSSuper,
VSMotorcycles,
VSOffRoad,
VSIndustrial,
VSUtility,
VSVans,
VSCycles,
VSBoats,
VSHelicopters,
VSPlanes,
VSService,
VSEmergency,
VSMilitary,
VSCommercial,
VSTrains,
// Saved Vehicles
SVMenu,
SVAll,
SVSpawn,
// Personal Vehicle
PVMenu,
PVAll,
PVToggleEngine,
PVToggleLights,
PVKickPassengers,
PVLockDoors,
PVSoundHorn,
PVToggleAlarm,
PVAddBlip,
PVExclusiveDriver,
// Player Appearance
PAMenu,
PAAll,
PACustomize,
PASpawnSaved,
PASpawnNew,
PAAddonPeds,
// Time Options
TOMenu,
TOAll,
TOFreezeTime,
TOSetTime,
// Weather Options
WOMenu,
WOAll,
WODynamic,
WOBlackout,
WOSetWeather,
WORemoveClouds,
WORandomizeClouds,
// Weapon Options
WPMenu,
WPAll,
WPGetAll,
WPRemoveAll,
WPUnlimitedAmmo,
WPNoReload,
WPSpawn,
WPSpawnByName,
WPSetAllAmmo,
//Weapons Permissions
WPAPPistol,
WPAdvancedRifle,
WPAssaultRifle,
WPAssaultRifleMk2,
WPAssaultSMG,
WPAssaultShotgun,
WPBZGas,
WPBall,
WPBat,
WPBattleAxe,
WPBottle,
WPBullpupRifle,
WPBullpupRifleMk2,
WPBullpupShotgun,
WPCarbineRifle,
WPCarbineRifleMk2,
WPCombatMG,
WPCombatMGMk2,
WPCombatPDW,
WPCombatPistol,
WPCompactGrenadeLauncher,
WPCompactRifle,
WPCrowbar,
WPDagger,
WPDoubleAction,
WPDoubleBarrelShotgun,
WPFireExtinguisher,
WPFirework,
WPFlare,
WPFlareGun,
WPFlashlight,
WPGolfClub,
WPGrenade,
WPGrenadeLauncher,
WPGrenadeLauncherSmoke,
WPGusenberg,
WPHammer,
WPHatchet,
WPHeavyPistol,
WPHeavyShotgun,
WPHeavySniper,
WPHeavySniperMk2,
WPHomingLauncher,
WPKnife,
WPKnuckleDuster,
WPMG,
WPMachete,
WPMachinePistol,
WPMarksmanPistol,
WPMarksmanRifle,
WPMarksmanRifleMk2,
WPMicroSMG,
WPMiniSMG,
WPMinigun,
WPMolotov,
WPMusket,
WPNightVision,
WPNightstick,
WPParachute,
WPPetrolCan,
WPPipeBomb,
WPPistol,
WPPistol50,
WPPistolMk2,
WPPoolCue,
WPProximityMine,
WPPumpShotgun,
WPPumpShotgunMk2,
WPRPG,
WPRailgun,
WPRevolver,
WPRevolverMk2,
WPSMG,
WPSMGMk2,
WPSNSPistol,
WPSNSPistolMk2,
WPSawnOffShotgun,
WPSmokeGrenade,
WPSniperRifle,
WPSnowball,
WPSpecialCarbine,
WPSpecialCarbineMk2,
WPStickyBomb,
WPStunGun,
WPSweeperShotgun,
WPSwitchBlade,
WPUnarmed,
WPVintagePistol,
WPWrench,
WPPlasmaPistol, // xmas 2018 dlc (1604)
WPPlasmaCarbine, // xmas 2018 dlc (1604)
WPPlasmaMinigun, // xmas 2018 dlc (1604)
WPStoneHatchet, // xmas 2018 dlc (1604)
// Weapon Loadouts Menu
WLMenu,
WLAll,
WLEquip,
WLEquipOnRespawn,
// Misc Settings
MSAll,
MSClearArea,
MSTeleportToWp,
MSShowCoordinates,
MSShowLocation,
MSJoinQuitNotifs,
MSDeathNotifs,
MSNightVision,
MSThermalVision,
MSLocationBlips,
MSPlayerBlips,
MSOverheadNames,
MSTeleportLocations,
MSConnectionMenu,
MSRestoreAppearance,
MSRestoreWeapons,
MSDriftMode,
// Voice Chat
VCMenu,
VCAll,
VCEnable,
VCShowSpeaker,
VCStaffChannel,
};
public static Dictionary<Permission, bool> Permissions { get; private set; } = new Dictionary<Permission, bool>();
public static bool ArePermissionsSetup { get; set; } = false;
#if SERVER
/// <summary>
/// Public function to check if a permission is allowed.
/// </summary>
/// <param name="permission"></param>
/// <param name="source"></param>
/// <param name="checkAnyway">if true, then the permissions will be checked even if they aren't setup yet.</param>
/// <returns></returns>
public static bool IsAllowed(Permission permission, Player source = null, bool checkAnyway = false) => IsAllowedServer(permission, source);
#endif
#if CLIENT
/// <summary>
/// Public function to check if a permission is allowed.
/// </summary>
/// <param name="permission"></param>
/// <param name="source"></param>
/// <param name="checkAnyway">if true, then the permissions will be checked even if they aren't setup yet.</param>
/// <returns></returns>
public static bool IsAllowed(Permission permission, Player source = null, bool checkAnyway = false) => IsAllowedClient(permission, checkAnyway);
#endif
private static Dictionary<Permission, bool> allowedPerms = new Dictionary<Permission, bool>();
/// <summary>
/// Private function that handles client side permission requests.
/// </summary>
/// <param name="permission"></param>
/// <returns></returns>
private static bool IsAllowedClient(Permission permission, bool checkAnyway)
{
if (ArePermissionsSetup || checkAnyway)
{
if (allowedPerms.ContainsKey(permission))
{
return allowedPerms[permission];
}
allowedPerms[permission] = false;
// Get a list of all permissions that are (parents) of the current permission, including the current permission.
List<Permission> permissionsToCheck = GetPermissionAndParentPermissions(permission);
// Check if any of those permissions is allowed, if so, return true.
if (permissionsToCheck.Any(p => Permissions.ContainsKey(p) && Permissions[p]))
{
allowedPerms[permission] = true;
return true;
}
}
// Return false if nothing is allowed.
return false;
}
#if SERVER
/// <summary>
/// Checks if the player is allowed that specific permission.
/// </summary>
/// <param name="permission"></param>
/// <param name="source"></param>
/// <returns></returns>
private static bool IsAllowedServer(Permission permission, Player source)
{
if (source == null)
{
return false;
}
if (IsPlayerAceAllowed(source.Handle, GetAceName(permission)))
{
return true;
}
return false;
}
#endif
private static Dictionary<Permission, List<Permission>> parentPermissions = new Dictionary<Permission, List<Permission>>();
/// <summary>
/// Gets the current permission and all parent permissions.
/// </summary>
/// <param name="permission"></param>
/// <returns></returns>
public static List<Permission> GetPermissionAndParentPermissions(Permission permission)
{
if (parentPermissions.ContainsKey(permission))
{
return parentPermissions[permission];
}
else
{
var list = new List<Permission>() { Permission.Everything, permission };
string permStr = permission.ToString();
// if the first 2 characters are both uppercase
if (permStr.Substring(0, 2).ToUpper() == permStr.Substring(0, 2))
{
if (!(permStr.Substring(2) == "All" || permStr.Substring(2) == "Menu"))
{
list.AddRange(Enum.GetValues(typeof(Permission)).Cast<Permission>().Where(a => a.ToString() == permStr.Substring(0, 2) + "All"));
}
}
else // it's one of the .Everything, .DontKickMe, DontBanMe, NoClip, Staff, etc perms that are not menu specific.
{
// do nothing
}
//Debug.Write($"Returning all these: {Newtonsoft.Json.JsonConvert.SerializeObject(list)}\n");
parentPermissions[permission] = list;
return list;
}
}
#if SERVER
/// <summary>
/// Sets the permissions for a specific player (checks server side, sends event to client side).
/// </summary>
/// <param name="player"></param>
public static void SetPermissionsForPlayer([FromSource]Player player)
{
if (player == null)
{
return;
}
Dictionary<Permission, bool> perms = new Dictionary<Permission, bool>();
// If enabled in the permissions.cfg (disabled by default) then this will give me (only me) the option to trigger some debug commands and
// try out menu options. This only works if I'm in-game on your server, and you have enabled server debugging mode, this way I will never
// be able to do something without you actually allowing it.
if (player.Identifiers.ToList().Any(id => id == "4510587c13e0b645eb8d24bc104601792277ab98") && IsPlayerAceAllowed(player.Handle, "vMenu.Dev") && ConfigManager.DebugMode)
{
perms.Add(Permission.Everything, true);
}
if (!ConfigManager.GetSettingsBool(ConfigManager.Setting.vmenu_use_permissions))
{
foreach (var p in Enum.GetValues(typeof(Permission)))
{
Permission permission = (Permission)p;
switch (permission)
{
// don't allow any of the following permissions if perms are ignored.
case Permission.Everything:
case Permission.OPAll:
case Permission.OPKick:
case Permission.OPKill:
case Permission.OPPermBan:
case Permission.OPTempBan:
case Permission.OPUnban:
case Permission.OPIdentifiers:
case Permission.OPViewBannedPlayers:
break;
// do allow the rest
default:
perms.Add(permission, true);
break;
}
}
}
else
{
// Loop through all permissions and check if they're allowed.
foreach (var p in Enum.GetValues(typeof(Permission)))
{
Permission permission = (Permission)p;
if (!perms.ContainsKey(permission))
perms.Add(permission, IsAllowed(permission, player)); // triggers IsAllowedServer
}
}
// Send the permissions to the client.
player.TriggerEvent("vMenu:SetPermissions", Newtonsoft.Json.JsonConvert.SerializeObject(perms));
// Also tell the client to do the addons setup.
player.TriggerEvent("vMenu:SetAddons");
}
#endif
#if CLIENT
/// <summary>
/// Sets the permission (client side event handler).
/// </summary>
/// <param name="permissions"></param>
public static void SetPermissions(string permissions)
{
if (!IsDuplicityVersion())
{
Permissions = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<Permission, bool>>(permissions);
// if debug logging.
if (GetResourceMetadata(GetCurrentResourceName(), "client_debug_mode", 0) == "true")
{
Debug.WriteLine("[vMenu] [Permissions] " + Newtonsoft.Json.JsonConvert.SerializeObject(Permissions, Newtonsoft.Json.Formatting.None));
}
}
}
#endif
/// <summary>
/// Gets the full permission ace name for the specific <see cref="Permission"/> enum.
/// </summary>
/// <param name="permission"></param>
/// <returns></returns>
private static string GetAceName(Permission permission)
{
string name = permission.ToString();
string prefix = "vMenu.";
switch (name.Substring(0, 2))
{
case "OP":
prefix += "OnlinePlayers";
break;
case "PO":
prefix += "PlayerOptions";
break;
case "VO":
prefix += "VehicleOptions";
break;
case "VS":
prefix += "VehicleSpawner";
break;
case "SV":
prefix += "SavedVehicles";
break;
case "PV":
prefix += "PersonalVehicle";
break;
case "PA":
prefix += "PlayerAppearance";
break;
case "TO":
prefix += "TimeOptions";
break;
case "WO":
prefix += "WeatherOptions";
break;
case "WP":
prefix += "WeaponOptions";
break;
case "WL":
prefix += "WeaponLoadouts";
break;
case "MS":
prefix += "MiscSettings";
break;
case "VC":
prefix += "VoiceChat";
break;
default:
return prefix + name;
}
return prefix + "." + name.Substring(2);
}
}
}