-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermissionService.cs
142 lines (134 loc) · 6.31 KB
/
PermissionService.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
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using CitizenFX.Core;
using CitizenFX.Core.Native;
using PoliceMP.Client.Services.Interfaces;
using PoliceMP.Core.Server.Communications.Interfaces;
using PoliceMP.Core.Shared;
using PoliceMP.Server.Extensions;
using PoliceMP.Shared.Constants;
using PoliceMP.Shared.Constants.States;
using PoliceMP.Shared.Enums;
using PoliceMP.Shared.Models;
namespace PoliceMP.Server.Services
{
public class PermissionService : IPermissionService
{
private static Dictionary<string, UserRole> _userRoles = new();
private readonly PlayerList _playerList;
private readonly ILogger<PermissionService> _logger;
public PermissionService(PlayerList playerList, ILogger<PermissionService> logger, IServerCommunicationsManager comms)
{
_playerList = playerList;
_logger = logger;
comms.On<UserRole>(ServerEvents.SendUserRole, SetUserRole);
}
public Task<List<Player>> GetAllAdmins()
{
return Task.FromResult(_playerList.Where(player => API.IsPlayerAceAllowed(player.Handle, "Police.adminAuth") || API.IsPlayerAceAllowed(player.Handle, "Police.adminAuth")).ToList());
}
public Task<UserAces> GetUserAces(Player player)
{
var userAces = new UserAces
{
IsWhiteListed = API.IsPlayerAceAllowed(player.Handle, "Police.whitelisted"),
IsAdmin = API.IsPlayerAceAllowed(player.Handle, "Police.adminAuth"),
IsDeveloper = API.IsPlayerAceAllowed(player.Handle, "Police.developer"),
IsModerator = API.IsPlayerAceAllowed(player.Handle, "Police.modAuth"),
IsAfoTrained = API.IsPlayerAceAllowed(player.Handle, "Police.afoTrained"),
IsRpuTrained = API.IsPlayerAceAllowed(player.Handle, "Police.rpuTrained"),
IsCidTrained = API.IsPlayerAceAllowed(player.Handle, "Police.cidTrained"),
IsNpasTrained = API.IsPlayerAceAllowed(player.Handle, "Police.npasTrained"),
IsMpuTrained = API.IsPlayerAceAllowed(player.Handle, "Police.mpuTrained"),
IsDsuTrained = API.IsPlayerAceAllowed(player.Handle, "Police.dogTrained"),
IsBtpTrained = API.IsPlayerAceAllowed(player.Handle, "Police.btpTrained"),
IsFireTrained = API.IsPlayerAceAllowed(player.Handle, "Fire.Trained"),
IsBasicDonator = API.IsPlayerAceAllowed(player.Handle, "Don.basic"),
IsProDonator = API.IsPlayerAceAllowed(player.Handle, "Don.pro"),
IsCivTrained = API.IsPlayerAceAllowed(player.Handle, "Civ.Trained"),
IsControl = API.IsPlayerAceAllowed(player.Handle, "control.trained"),
IsContentCreator = API.IsPlayerAceAllowed(player.Handle, "Content.Creator"),
IsRetired = API.IsPlayerAceAllowed(player.Handle, "Retired"),
IsHighwaysTrained = API.IsPlayerAceAllowed(player.Handle, "HETO.Trained"),
IsCollegeStaff = API.IsPlayerAceAllowed(player.Handle, "Police.collegeStaff"),
IsSeniorCiv = API.IsPlayerAceAllowed(player.Handle, "SeniorCiv.Trained"),
IsFireBoroughCommander = API.IsPlayerAceAllowed(player.Handle, "fire.boroughCommander"),
IsNhsHems = API.IsPlayerAceAllowed(player.Handle, "nhs.hems"),
IsNhsParamedic = API.IsPlayerAceAllowed(player.Handle, "nhs.paramedic"),
IsNhsClinicalAdv = API.IsPlayerAceAllowed(player.Handle, "nhs.clinicalADV"),
IsNhsClinicalTl = API.IsPlayerAceAllowed(player.Handle, "nhs.clinicalTL"),
IsNhsDoctor = API.IsPlayerAceAllowed(player.Handle, "nhs.doctor"),
IsNhsSectionLeader = API.IsPlayerAceAllowed(player.Handle, "nhs.sectionleader"),
IsNhsHemsTl = API.IsPlayerAceAllowed(player.Handle, "nhs.hemsTL"),
IsCivGunTrained = API.IsPlayerAceAllowed(player.Handle, "command.givegun"),
IsBandOne = API.IsPlayerAceAllowed(player.Handle, "staff.bandOne"),
IsBandTwo = API.IsPlayerAceAllowed(player.Handle, "staff.bandTwo"),
IsBandThree = API.IsPlayerAceAllowed(player.Handle, "staff.bandThree"),
IsBandFour = API.IsPlayerAceAllowed(player.Handle, "staff.bandFour"),
HasCityPoliceDlc = API.IsPlayerAceAllowed(player.Handle, "Don.colp"),
};
return Task.FromResult(userAces);
}
public Task<UserRole> GetUserRole(Player player)
{
var doesContain = _userRoles.TryGetValue(player.Handle, out var userRole);
if (!doesContain)
{
userRole = new UserRole
{
Branch = UserBranch.Police,
Division = UserDivision.Ert
};
}
return Task.FromResult(userRole);
}
public Task SetUserRole(Player player, UserRole userRole)
{
var doesContain = _userRoles.TryGetValue(player.Handle, out UserRole currentRole);
if (doesContain)
{
lock (_userRoles)
{
_userRoles.Remove(player.Handle);
_userRoles.Add(player.Handle, userRole);
}
}
else
{
lock (_userRoles)
{
_userRoles.Add(player.Handle, userRole);
}
}
return Task.CompletedTask;
}
public async Task<int> GetUserCountByBranch(UserBranch branch)
{
var count = 0;
foreach (var player in _playerList)
{
var role = await GetUserRole(player);
if (role.Branch == branch)
{
count++;
}
}
return count;
}
public async Task<int> GetUserCountByDivision(UserDivision division)
{
var count = 0;
foreach (var player in _playerList)
{
var role = await GetUserRole(player);
if (role.Division == division)
{
count++;
}
}
return count;
}
}
}