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
|
if CLIENT then return end
include("sh_cami.lua")
local nadmin = include("sh_nadmin.lua")
--Override what print() does, just for this file
local oldprint = print
print = function(...)
local args = {...}
table.insert(args,1,"[NADMIN]")
oldprint(unpack(args))
end
for _,v in pairs({
--Server to client
"nadmin_update_privilege", -- (exists :: bool, privilege :: CAMI_PRIVILEGE)
"nadmin_create_group", -- (name :: string, inherits :: string, source :: string)
"nadmin_update_group", -- (name :: string, privilege :: string, exists :: bool)
"nadmin_delete_group", -- (name :: string)
"nadmin_update_player", -- (entity :: player, group :: string)
"nadmin_init_privileges", -- (privileges :: table)
"nadmin_init_players", -- (players :: table)
"nadmin_init_groups", -- (groups :: table)
--Client to server
"nadmin_request_set_user_group", --(player :: entity, group :: string)
"nadmin_request_create_group", --(group :: string, inherits :: string)
"nadmin_request_update_group", --(group :: string, privilege :: string, set :: bool)
"nadmin_request_delete_group", --(group :: string)
}) do
util.AddNetworkString(v)
end
--Load saved groups & privileges
local groups = nadmin.groups
local players = nadmin.players
local privileges = nadmin.privileges
--Delete the HasAccess functions
for _,privilege in pairs(privileges) do
privilege.HasAccess = nil
end
if file.Exists("nadmin.txt","DATA") then
local func, err = CompileString(file.Read("nadmin.txt","DATA"), "nadmin.txt")
local func_sandboxed = setfenv(func,{}) --Empty environment, you can't even print from inside (you could do an infinite loop, I guess)
local succ, nadmindata = pcall(func_sandboxed)
if not succ then
error("Failed to load groups:", nadmindata)
return --stop loading the file
end
local mygroups, myplayers = nadmindata.groups, nadmindata.players
for groupname, group in pairs(mygroups) do
local camigroup = {
Name = group.name,
Inherits = group.inherits,
}
CAMI.RegisterUsergroup(camigroup)
groups[groupname] = {
nadmin = true,
group = camigroup,
src = "nadmin",
privileges = group.privileges
}
end
end
--[[
Creates a new group that players can be
added to. All groups must inherit from a
group, except for the "user" "admin" and
"superadmin" groups.
]]
--(group :: string, inherits :: string)
local function create_usergroup(name,inherits)
local group = {
Name = name,
Inherits = inherits
}
CAMI.RegisterUsergroup(group, "nadmin")
nadmin.save_config()
end
--[[
Edits the privileges on a usergroup.
Players added to the usergroup will
now have the privilege set.
]]
--name :: string, privilegename :: string, set :: bool
local function edit_usergroup(name,privilegename,set)
if set then
groups[name].privileges[privilegename] = set
else
groups[name].privileges[privilegename] = nil
end
net.Start("nadmin_update_group")
net.WriteString(name)
net.WriteString(privilegename)
net.WriteBool(set)
net.Broadcast()
nadmin.save_config()
end
--[[
Deletes a usergroup. Any players assigned
to the deleted group will be reassigned
to the usergroup that the deleted group
inherited from. Any groups that inherited
from the usergroup in the heiarchy will
inherit from the group the usergroup
inherited from.
]]
--(group :: string)
local function delete_usergroup(name)
assert(groups[name], "Tried to delete a group that doesn't exist:\"" .. name .. "\"")
local inherited_from_name = groups[name].group.Inherits
local inherited_from = groups[inherited_from_name]
local group = groups[name]
for _,v in pairs(players) do
if v.group == groups[name] then
v.group = inherited_from
end
end
for _,v in pairs(groups) do
if v.group.Inherits == group then
v.group.Inherits = inherited_from
end
end
CAMI.UnregisterUsergroup(group, "nadmin")
groups[name] = nil
nadmin.save_config()
end
local function set_user_to_group(ply,group)
local id,name = ply:SteamID64(), ply:Name()
players[id] = {
group = group,
last_name = name
}
net.Start("nadmin_update_player")
net.WriteEntity(ply)
net.WriteString(group)
net.Broadcast()
nadmin.save_config()
end
local function send_init_info(ply)
net.Start("nadmin_init_privileges")
local light_privileges = {}
for privilege_name,privilege in pairs(privileges) do
light_privileges[privilege_name] = {
Description = privilege.Description,
MinAccess = privilege.MinAccess,
Name = privilege.Name
}
end
net.WriteTable(light_privileges)
net.Send(ply)
net.Start("nadmin_init_players")
net.WriteTable(players)
net.Send(ply)
net.Start("nadmin_init_groups")
net.WriteTable(groups)
net.Send(ply)
end
net.Receive("nadmin_request_set_user_group",function(len,ply)
if not ply:IsSuperAdmin() then return end
local player = net.ReadEntity()
local group = net.ReadString()
set_user_to_group(player,group)
end)
net.Receive("nadmin_request_create_group",function(len,ply)
if not ply:IsSuperAdmin() then return end
local groupname = net.ReadString()
local inherits = net.ReadString()
create_usergroup(groupname,inherits)
end)
net.Receive("nadmin_request_update_group",function(len,ply)
if not ply:IsSuperAdmin() then return end
local groupname = net.ReadString()
local privilegename = net.ReadString()
local set = net.ReadBool()
edit_usergroup(groupname,privilegename,set)
end)
net.Receive("nadmin_request_delete_group", function(len,ply)
if not ply:IsSuperAdmin() then return end
local group = net.ReadString()
delete_usergroup(group)
end)
hook.Add("PlayerInitialSpawn","nadmin_init_tables",function(ply)
timer.Simple(5,function()
if players[ply:SteamID64()] == nil then
local igroup = "user"
if ply:IsSuperAdmin() then igroup = "superadmin"
elseif ply:IsAdmin() then igroup = "admin" end
players[ply:SteamID64()] = {
group = igroup,
last_name = ply:Name()
}
set_user_to_group(ply,igroup)
end
if ply:IsSuperAdmin() then
send_init_info(ply)
end
end)
end)
concommand.Add("sv_resave", function(ply,cmd,args)
if not ply:IsSuperAdmin() then return end
nadmin.save_config()
end)
concommand.Add("sv_resend_init",function(ply,cmd,args)
if not ply:IsSuperAdmin() then return end
send_init_info(ply)
end)
--Reset the print function
print = oldprint
|