Refactored policy structure

Previously, all modules in an org were fully copied into that
org. Now, the only copy of the modules is at modpol.modules, and orgs
have a policy table at [org].policies, which overrides the config
table in any given module.
This commit is contained in:
Nathan Schneider
2022-02-09 22:14:26 -07:00
parent f950b5b94c
commit 2028f1ee85
7 changed files with 76 additions and 52 deletions

View File

@ -33,7 +33,7 @@ function change_modules:initiate(result)
for k, module in pairs(modpol.modules) do
if not modpol.modules[module.slug].hide then
local in_org = false
if self.org.modules[module.slug] then
if self.org.policies[module.slug] then
in_org = true
end
table.insert(
@ -97,14 +97,13 @@ end
function change_modules:implement_change()
for i,v in ipairs(self.data.add_modules) do
local slug = string.match(v,"%[(.+)%]")
self.org.modules[slug] =
modpol.util.copy_table(modpol.modules[slug])
table.sort(self.org.modules)
self.org.policies[slug] = {}
table.sort(self.org.policies)
end
for i,v in ipairs(self.data.remove_modules) do
local slug = string.match(v,"%[(.+)%]")
self.org.modules[slug] = nil
table.sort(self.org.modules)
self.org.policies[slug] = nil
table.sort(self.org.policies)
end
-- announce and shut down
modpol.interactions.message_org(

View File

@ -22,8 +22,9 @@ change_policy.config = {
function change_policy:initiate(result)
-- prepare module options
local available_modules = {}
for k,org_mod in pairs(self.org.modules) do
if not org_mod.hide then
for k,org_mod in pairs(modpol.modules) do
if not org_mod.hide and
self.org.policies[k] then
available_modules[org_mod.slug] = modpol.util.copy_table(org_mod)
end end
local modules_list = {}
@ -48,7 +49,7 @@ function change_policy:initiate(result)
self.initiator, "Choose a module to change policies for:",
modules_list,
function(mod_choice)
local this_module = self.org.modules[mod_choice]
local this_module = modpol.modules[mod_choice]
local module_policies = this_module.config
local policy_list = {}
for k,v in pairs(module_policies) do
@ -70,10 +71,9 @@ function change_policy:initiate(result)
function(policy_choice)
modpol.interactions.text_query(
self.initiator,
"Use carefully!\n" ..
"Current " .. policy_choice .. " value: " ..
tostring(self.org.modules[mod_choice][policy_choice])
.. "\nChange value to:",
tostring(modpol.modules[mod_choice][policy_choice])
.. "\nChange value to (be careful!):",
function(policy_input)
self:approve_change(
mod_choice,
@ -99,7 +99,7 @@ function change_policy:approve_change(module_slug, policy, input)
self.initiator,
"Updating " .. policy .. " policy on module " ..
module_slug .. " with: " .. input)
self.org.modules[module_slug].config[policy] = input
self.org.policies[module_slug][policy] = input
modpol.interactions.org_dashboard(
self.initiator, self.org.id)
if self.data.result then self.data.result() end

View File

@ -19,26 +19,30 @@ display_policies.config = {
-- @param result Callback if this module is embedded in other modules
function display_policies:initiate(result)
local display_table = {}
for k,v in pairs(self.org.modules) do
local input = v.name
table.insert(display_table, input)
if v.config
and modpol.util.num_pairs(v.config) > 0 then
for k2,v2 in pairs(v.config) do
local v2_string = ""
if type(v2) == "string" then
v2_string = v2
elseif type(v2) == "table"
or type(v2) == "number" then
v2_string = tostring(v2)
else
v2_string = "Could not render"
for k,v in pairs(self.org.policies) do
if v then -- check the module is allowed
local input = modpol.modules[k].name
table.insert(display_table, input)
if modpol.modules[k].config then
for k2,v2 in pairs(modpol.modules[k].config) do
if self.org.policies[k][k2] then
v2 = self.org.policies[k][k2]
end
local v2_string = ""
if type(v2) == "string" then
v2_string = v2
elseif type(v2) == "table"
or type(v2) == "number" then
v2_string = tostring(v2)
else
v2_string = "Could not render"
end
input = k2..": "..v2_string
table.insert(display_table, input)
end
input = k2..": "..v2_string
table.insert(display_table, input)
end
table.insert(display_table, "---")
end
table.insert(display_table, "---")
end
local output = table.concat(display_table,"\n")
if #display_table == 0 then