summaryrefslogtreecommitdiff
path: root/lua/autorun/sh_nadmin.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/autorun/sh_nadmin.lua')
-rw-r--r--lua/autorun/sh_nadmin.lua340
1 files changed, 166 insertions, 174 deletions
diff --git a/lua/autorun/sh_nadmin.lua b/lua/autorun/sh_nadmin.lua
index 392daec..46f0cce 100644
--- a/lua/autorun/sh_nadmin.lua
+++ b/lua/autorun/sh_nadmin.lua
@@ -1,174 +1,166 @@
-include("sh_cami.lua")
-AddCSLuaFile()
-local nadmin = {}
-
---[[
-Groups is a table of
-["group_name"] = {
- nadmin :: bool --Wether the group was created by nAdmin or not
- --nAdmin will only allow you to delete groups
- --that were created with it. You cannot delete
- --groups that were created with another mod.
-
- group :: CAMI_USERGROUP -- the usergroup table given by CAMI,
- -- see sh_cami.lua for details.
-
- src :: string --The source of the usergroup, can be "cami", nAdmin,
- --or the name of the mod that introduced the usergroup.
-
- privileges :: table --An array of ["privilege_name"] = true, for
- --each privilege the group has
-}
-]]
-nadmin.groups = {}
-for _,v in pairs(CAMI.GetUsergroups()) do
- print("Initalizing usergroups, this usergroup was",v)
- PrintTable(v)
- nadmin.groups[v.Name] = {
- nadmin = false,
- group = v,
- src = "cami",
- privileges = {},
- }
- for _,privilege in pairs(CAMI.GetPrivileges()) do
- if privilege.MinAccess == v.Name then
- nadmin.groups[v.Name].privileges[privilege.Name] = true
- end
- end
-end
---[[
-Players is a table of
-[player_steamid] = {
- group :: CAMI_USERGROUP --The usergroup table given by CAMI that
- --the player is a part of, see sh_cami.lua for
- --details
-
- last_name :: string --The last name that the player joined the server
- --with, this is used to set player's permissions
- --even when the player is offline.
-]]
-nadmin.players = {}
-nadmin.privileges = CAMI.GetPrivileges()
-
-function nadmin.AddUserGroup(src,usergroup)
- nadmin.groups[usergroup.Name] = {
- nadmin = src == "nadmin",
- group = usergroup,
- src = src,
- privileges = {}
- }
-end
-
-function nadmin.DeleteUserGroup(name,src)
- nadmin.groups[name] = nil
-end
-
-function nadmin.save_config()
- if not SERVER then return end
- local file_parts = {"return {"}
-
- --Save groups
- table.insert(file_parts,"groups = {")
- for groupname, group in pairs(nadmin.groups) do
- if group.nadmin then
- local privileges_tbl = {}
- for privilege_name,_ in pairs(group.privileges) do
- table.insert(privileges_tbl,string.format("[%q] = true",privilege_name))
- end
- table.insert(file_parts,string.format([[
- [%q] = {
- name = %q,
- inherits = %q,
- privileges = {%s}
- },]],groupname, group.group.Name, group.group.Inherits, table.concat(privileges_tbl,",")))
- end
- end
- table.insert(file_parts,"},")
-
- --Save players
- table.insert(file_parts,"players = {")
- for playerid, player in pairs(nadmin.players) do
- table.insert(file_parts,string.format([[
- [%q] = {
- last_name = %q,
- group = %q
- },]], playerid, player.last_name, player.group))
- end
- table.insert(file_parts,"},")
-
- --Finish off
- table.insert(file_parts,"}")
- --And save to file
- file.Write("nadmin.txt",table.concat(file_parts,"\n"))
-end
-
---Notify client of changes to the groups table
-hook.Add("CAMI.OnUsergroupRegistered","nadmin_OnUserGroupRegistered",function(usergroup,src)
- print("hook nadmin_OnUserGroupRegistered called, src was", src)
- src = src or ""
- print("Usergroup was:",usergroup, usergroup.Name, usergroup.Inherits)
- nadmin.AddUserGroup(src,usergroup)
- if SERVER then
- net.Start("nadmin_create_group")
- net.WriteString(usergroup.Name)
- net.WriteString(usergroup.Inherits or "users") --Some mods call this hook without an "Inherits"
- net.WriteString(src)
- net.Broadcast()
- nadmin.save_config()
- end
-end)
-
-hook.Add("CAMI.OnUsergroupUnregistered","nadmin_OnUserGroupUnregistered",function(usergroup,src)
- nadmin.DeleteUserGroup(usergroup.Name, src)
- if SERVER then
- net.Start("nadmin_delete_group")
- net.WriteString(usergroup.Name)
- net.Broadcast()
- nadmin.save_config()
- end
-end)
-
-hook.Add("CAMI.PlayerUsergroupChanged","nadmin_PlayerUsergroupChanged",function(ply,from,to,src)
- local sid = ply:SteamID64()
- nadmin.players[sid] = nadmin.players[sid] or {}
- nadmin.players[sid].group = to
- if SERVER then
- net.Start("nadmin_update_player")
- net.WriteEntity(ply)
- net.WriteString(to)
- net.Broadcast()
- nadmin.save_config()
- end
-end)
-
-hook.Add("CAMI.OnPrivilegeRegistered", "nadmin_OnPrivilegeRegistered", function(privilege)
- nadmin.privileges[privilege.Name] = privilege
- nadmin.save_config()
-end)
-
-hook.Add("CAMI.OnPrivilegeUnregistered", "nadmin_OnPrivilegeUnregistered", function(privilege)
- nadmin.privileges[privilege.Name] = nil
-end)
-
-hook.Add("CAMI.PlayerHasAccess","nadmin_PlayerHasAccess",function(ply,name,callback,target,extra)
- if (not ply) or (not ply:IsValid()) then return end
- print("At time of PlayerHasAccess call, players was",nadmin.players, "for", ply)
- local sid = ply:SteamID64()
- if not nadmin.players[sid] then return end
- local group = nadmin.players[ply:SteamID64()].group
- if group and type(group) == "table" and group.nadmin and group.privileges[name] then
- return true
- end
-end)
-
-hook.Add("CAMI.SteamIDHasAccess","nadmin_SteamIDHasAccess",function(id,privilege,callback,target,extra)
- local group = nadmin.players[id].group
- if group and group.nadmin and group.privileges[privilege] then
- return true
- end
-end)
-
---nadmin.groups = groups
---nadmin.players = players
---nadmin.privileges = privileges
-return nadmin
+include("sh_cami.lua")
+AddCSLuaFile()
+local nadmin = {}
+
+--[[
+Groups is a table of
+["group_name"] = {
+ nadmin :: bool --Wether the group was created by nAdmin or not
+ --nAdmin will only allow you to delete groups
+ --that were created with it. You cannot delete
+ --groups that were created with another mod.
+
+ group :: CAMI_USERGROUP -- the usergroup table given by CAMI,
+ -- see sh_cami.lua for details.
+
+ src :: string --The source of the usergroup, can be "cami", nAdmin,
+ --or the name of the mod that introduced the usergroup.
+
+ privileges :: table --An array of ["privilege_name"] = true, for
+ --each privilege the group has
+}
+]]
+nadmin.groups = {}
+for _,v in pairs(CAMI.GetUsergroups()) do
+ nadmin.groups[v.Name] = {
+ nadmin = false,
+ group = v,
+ src = "cami",
+ privileges = {},
+ }
+ for _,privilege in pairs(CAMI.GetPrivileges()) do
+ if privilege.MinAccess == v.Name then
+ nadmin.groups[v.Name].privileges[privilege.Name] = true
+ end
+ end
+end
+--[[
+Players is a table of
+[player_steamid] = {
+ group :: CAMI_USERGROUP --The usergroup table given by CAMI that
+ --the player is a part of, see sh_cami.lua for
+ --details
+
+ last_name :: string --The last name that the player joined the server
+ --with, this is used to set player's permissions
+ --even when the player is offline.
+]]
+nadmin.players = {}
+nadmin.privileges = CAMI.GetPrivileges()
+
+function nadmin.AddUserGroup(src,usergroup)
+ nadmin.groups[usergroup.Name] = {
+ nadmin = src == "nadmin",
+ group = usergroup,
+ src = src,
+ privileges = {}
+ }
+end
+
+function nadmin.DeleteUserGroup(name,src)
+ nadmin.groups[name] = nil
+end
+
+function nadmin.save_config()
+ if not SERVER then return end
+ local file_parts = {"return {"}
+
+ --Save groups
+ table.insert(file_parts,"groups = {")
+ for groupname, group in pairs(nadmin.groups) do
+ if group.nadmin then
+ local privileges_tbl = {}
+ for privilege_name,_ in pairs(group.privileges) do
+ table.insert(privileges_tbl,string.format("[%q] = true",privilege_name))
+ end
+ table.insert(file_parts,string.format([[
+ [%q] = {
+ name = %q,
+ inherits = %q,
+ privileges = {%s}
+ },]],groupname, group.group.Name, group.group.Inherits, table.concat(privileges_tbl,",")))
+ end
+ end
+ table.insert(file_parts,"},")
+
+ --Save players
+ table.insert(file_parts,"players = {")
+ for playerid, player in pairs(nadmin.players) do
+ table.insert(file_parts,string.format([[
+ [%q] = {
+ last_name = %q,
+ group = %q
+ },]], playerid, player.last_name, player.group))
+ end
+ table.insert(file_parts,"},")
+
+ --Finish off
+ table.insert(file_parts,"}")
+ --And save to file
+ file.Write("nadmin.txt",table.concat(file_parts,"\n"))
+end
+
+--Notify client of changes to the groups table
+hook.Add("CAMI.OnUsergroupRegistered","nadmin_OnUserGroupRegistered",function(usergroup,src)
+ src = src or ""
+ nadmin.AddUserGroup(src,usergroup)
+ if SERVER then
+ net.Start("nadmin_create_group")
+ net.WriteString(usergroup.Name)
+ net.WriteString(usergroup.Inherits or "users") --Some mods call this hook without an "Inherits"
+ net.WriteString(src)
+ net.Broadcast()
+ nadmin.save_config()
+ end
+end)
+
+hook.Add("CAMI.OnUsergroupUnregistered","nadmin_OnUserGroupUnregistered",function(usergroup,src)
+ nadmin.DeleteUserGroup(usergroup.Name, src)
+ if SERVER then
+ net.Start("nadmin_delete_group")
+ net.WriteString(usergroup.Name)
+ net.Broadcast()
+ nadmin.save_config()
+ end
+end)
+
+hook.Add("CAMI.PlayerUsergroupChanged","nadmin_PlayerUsergroupChanged",function(ply,from,to,src)
+ local sid = ply:SteamID64()
+ nadmin.players[sid] = nadmin.players[sid] or {}
+ nadmin.players[sid].group = to
+ if SERVER then
+ net.Start("nadmin_update_player")
+ net.WriteEntity(ply)
+ net.WriteString(to)
+ net.Broadcast()
+ nadmin.save_config()
+ end
+end)
+
+hook.Add("CAMI.OnPrivilegeRegistered", "nadmin_OnPrivilegeRegistered", function(privilege)
+ nadmin.privileges[privilege.Name] = privilege
+ nadmin.save_config()
+end)
+
+hook.Add("CAMI.OnPrivilegeUnregistered", "nadmin_OnPrivilegeUnregistered", function(privilege)
+ nadmin.privileges[privilege.Name] = nil
+end)
+
+hook.Add("CAMI.PlayerHasAccess","nadmin_PlayerHasAccess",function(ply,name,callback,target,extra)
+ if (not ply) or (not ply:IsValid()) then return end
+ local sid = ply:SteamID64()
+ if not nadmin.players[sid] then return end
+ local group = nadmin.players[ply:SteamID64()].group
+ if group and type(group) == "table" and group.nadmin and group.privileges[name] then
+ return true
+ end
+end)
+
+hook.Add("CAMI.SteamIDHasAccess","nadmin_SteamIDHasAccess",function(id,privilege,callback,target,extra)
+ local group = nadmin.players[id].group
+ if group and group.nadmin and group.privileges[privilege] then
+ return true
+ end
+end)
+
+return nadmin