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) 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 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_print_privileges", function(ply,cmd,args) if not ply:IsSuperAdmin() then return end PrintTable(privileges) end) concommand.Add("sv_print_groups", function(ply,cmd,args) if not ply:IsSuperAdmin() then return end PrintTable(groups) end) concommand.Add("sv_print_players", function(ply,cmd,args) if not ply:IsSuperAdmin() then return end PrintTable(players) 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