2021-03-01 19:11:20 -05:00
|
|
|
|
2021-01-28 23:22:06 -07:00
|
|
|
-- ===================================================================
|
|
|
|
|
-- /orgs.lua
|
|
|
|
|
-- Org-related functions for Modular Politics
|
2021-02-10 23:40:28 -05:00
|
|
|
-- Called by modpol/modpol/api.lua
|
2021-03-01 19:11:20 -05:00
|
|
|
-- ===================================================================
|
|
|
|
|
-- preserve records -- if true, will store org ledgers when they are closed, for later reference.
|
|
|
|
|
local preserve_records = true
|
2021-03-08 14:06:15 -05:00
|
|
|
|
|
|
|
|
-- policies assigned to any org can go here
|
2021-03-01 19:11:20 -05:00
|
|
|
modpol.default_org_policies = modpol.default_org_policies or {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
-- initiate the table of orgs, if it doesnt already exist from storage!
|
|
|
|
|
modpol.orgs = modpol.orgs or {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-01 19:11:20 -05:00
|
|
|
if preserve_records then
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.old_ledgers = modpol.old_ledgers or {} -- a backup of all legders of all closed orgs.
|
2021-03-01 19:11:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
-- this counter will increment every time a new org is created, and the current value will be used as the org id
|
|
|
|
|
-- we set it to 1 for init, and then run through all existing orgs and take only the greatest... which we then add 1 to.
|
|
|
|
|
-- this ensures a unique id for each org.
|
|
|
|
|
modpol.id_counter = 1
|
|
|
|
|
if #modpol.orgs > 0 then
|
|
|
|
|
for id,org in pairs(modpol.orgs) do
|
|
|
|
|
if id > modpol.id_counter then
|
|
|
|
|
modpol.id_counter = id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
for id,ledg in pairs(modpol.old_ledgers) do
|
|
|
|
|
if id > modpol.id_counter then
|
|
|
|
|
modpol.id_counter = id
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
modpol.id_counter = modpol.id_counter +1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-01 19:11:20 -05:00
|
|
|
--define the template of a ledger entry
|
|
|
|
|
modpol.ledger_entry_temp = {
|
|
|
|
|
timestamp = '',
|
|
|
|
|
entrytype = nil,
|
|
|
|
|
action_msg = '',
|
|
|
|
|
org_name = nil,
|
2021-03-08 14:06:15 -05:00
|
|
|
org_id = nil,
|
2021-03-01 19:11:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- define the basic template of an org.
|
|
|
|
|
|
|
|
|
|
modpol.org_temp = {
|
2021-03-08 14:06:15 -05:00
|
|
|
id = nil,
|
2021-03-01 19:11:20 -05:00
|
|
|
name = nil,
|
|
|
|
|
policies = {},
|
|
|
|
|
members = {},
|
|
|
|
|
ledger = {},
|
|
|
|
|
parent = nil,
|
|
|
|
|
children = {},
|
|
|
|
|
properties = {},
|
|
|
|
|
old_names = {},
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.create_ledger_entry
|
2021-03-08 14:06:15 -05:00
|
|
|
-- Params: strings: action_msg, type, number: org_id
|
2021-03-01 19:11:20 -05:00
|
|
|
-- Outputs:
|
|
|
|
|
-- returns a ledger entry table, with a timestamp
|
2021-03-08 14:06:15 -05:00
|
|
|
-- This function accepts a message and an optional org_id and type specification
|
2021-03-01 19:11:20 -05:00
|
|
|
-- and returns a valid ledger entry table
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.create_ledger_entry = function(action_msg, org_id, entry_type)
|
2021-03-01 19:11:20 -05:00
|
|
|
local entry = modpol.ledger_entry_temp
|
|
|
|
|
if action_msg and type(action_msg) == 'string' then
|
|
|
|
|
entry.action_msg = action_msg
|
|
|
|
|
end
|
2021-03-08 14:06:15 -05:00
|
|
|
if org_id and type(org_id) == 'number' and modpol.orgs [org_id] then
|
|
|
|
|
entry.org_id = org_id
|
|
|
|
|
if modpol.orgs[org_id].name then
|
|
|
|
|
entry.org_name = modpol.orgs[org_id].name
|
|
|
|
|
end
|
2021-03-01 19:11:20 -05:00
|
|
|
end
|
|
|
|
|
if entry_type and type(entry_type) == 'string' then
|
|
|
|
|
entry.entrytype = entry_type
|
|
|
|
|
end
|
|
|
|
|
entry.timestamp = os.time()
|
|
|
|
|
return entry
|
|
|
|
|
end
|
|
|
|
|
|
2021-01-28 23:22:06 -07:00
|
|
|
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.record
|
2021-03-08 14:06:15 -05:00
|
|
|
-- Params: strings msg, type, number org_id
|
2021-01-28 23:22:06 -07:00
|
|
|
-- Outputs:
|
|
|
|
|
-- "msg" specifies an event and/or status message.
|
2021-03-08 14:06:15 -05:00
|
|
|
-- "org" specifies an "org" id.
|
2021-03-01 19:11:20 -05:00
|
|
|
-- "type" -- optional type of legder entry. Could be e.g. 'election_result', 'add_member', etc
|
2021-01-28 23:22:06 -07:00
|
|
|
-- This function adds the message to a global ledger and, if "org"
|
|
|
|
|
-- specifies a valid "org", to an "org"-specific ledger. Both the mem-
|
|
|
|
|
-- ory-resident and on-disk copies of the data structures used are up-
|
|
|
|
|
-- dated.
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.record = function (org_id, msg, type)
|
2021-03-01 19:11:20 -05:00
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
local entry = modpol.create_ledger_entry(msg, org_id, type)
|
2021-01-28 23:22:06 -07:00
|
|
|
-- Record in global ledger
|
2021-03-01 19:11:20 -05:00
|
|
|
table.insert (modpol.ledger, entry)
|
2021-01-28 23:22:06 -07:00
|
|
|
-- Record in "org"-specific ledger
|
2021-03-08 14:06:15 -05:00
|
|
|
if modpol.orgs [org_id] ~= nil then
|
|
|
|
|
local org_ledger = modpol.orgs [org_id]["ledger"]
|
2021-01-28 23:22:06 -07:00
|
|
|
if org_ledger == nil then
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.orgs [org_id]["ledger"] = { entry }
|
2021-01-28 23:22:06 -07:00
|
|
|
else
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.orgs [org_id]["ledger"] = table.insert (org_ledger, entry)
|
2021-01-28 23:22:06 -07:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
modpol.store_data() -- Copy data to disk
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.get_org_id_by_name(org_name)
|
|
|
|
|
-- Params: org_name
|
|
|
|
|
|
|
|
|
|
-- Output: an org id or nil
|
|
|
|
|
--
|
|
|
|
|
modpol.get_org_id_by_name = function(org_name)
|
|
|
|
|
local id = nil
|
|
|
|
|
for org_id,org in pairs(modpol.orgs) do
|
|
|
|
|
if org.name == org_name then
|
|
|
|
|
id = org_id
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-28 23:22:06 -07:00
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.add_org
|
2021-03-01 19:11:20 -05:00
|
|
|
-- Params: string name, table members, string parent, table properties
|
|
|
|
|
|
2021-01-28 23:22:06 -07:00
|
|
|
-- Output:
|
|
|
|
|
--
|
2021-03-01 19:11:20 -05:00
|
|
|
-- This function creates an "org". It returns a boolean success indicator and
|
|
|
|
|
-- either an error message starting with "Error:" or a success message.
|
2021-01-28 23:22:06 -07:00
|
|
|
--
|
|
|
|
|
-- The string parameter specifies the "org" name.
|
|
|
|
|
--
|
2021-03-01 19:11:20 -05:00
|
|
|
-- The members table should be an integer-indexed array of member
|
2021-01-28 23:22:06 -07:00
|
|
|
-- names.
|
|
|
|
|
|
2021-03-01 19:11:20 -05:00
|
|
|
-- parent should be nil or a valid existing org name. If nil, defaults to
|
|
|
|
|
-- 'instance'
|
|
|
|
|
|
|
|
|
|
-- properties defaults to an empty table. Use it to initialize the org
|
|
|
|
|
-- with arbitrary properties
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.add_org = function(org_name, members, parent_id, properties)
|
2021-01-28 23:22:06 -07:00
|
|
|
local str
|
|
|
|
|
|
2021-02-10 23:40:28 -05:00
|
|
|
if modpol.ocutil.str_empty (org_name) then
|
2021-03-01 19:11:20 -05:00
|
|
|
return false,"Error: Org needs a name"
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
if parent_id and not modpol.orgs[parent_id] then
|
|
|
|
|
return false,"Error: Parent_id must be a valid existing org id"
|
2021-03-01 19:11:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if properties and not type(properties) == 'table' then
|
|
|
|
|
return false,"Error: Properties must be a table"
|
2021-01-28 23:22:06 -07:00
|
|
|
end
|
2021-03-08 14:06:15 -05:00
|
|
|
|
2021-01-28 23:22:06 -07:00
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.ocutil.log ("Attempting to add new org " .. org_name)
|
|
|
|
|
|
|
|
|
|
if modpol.get_org_id_by_name(org_name) ~= nil then
|
2021-01-28 23:22:06 -07:00
|
|
|
str = "Error: Org " .. org_name .. " already exists"
|
2021-02-10 23:40:28 -05:00
|
|
|
modpol.ocutil.log (str)
|
2021-03-01 19:11:20 -05:00
|
|
|
return false,str
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
local org_id = modpol.id_counter
|
|
|
|
|
modpol.id_counter = modpol.id_counter + 1
|
2021-03-01 19:11:20 -05:00
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
|
|
|
|
|
-- the instance will have a parent of nil if it is the instance.
|
|
|
|
|
-- otherwise, it will have a parent of instance's id if not otherwise defined
|
|
|
|
|
local parent_org_id = nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if parent_id and not(modpol.get_org_id_by_name('instance') == org_id) then
|
|
|
|
|
parent_org_id = parent_id
|
2021-01-28 23:22:06 -07:00
|
|
|
end
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
if not parent_id and not(modpol.get_org_id_by_name('instance') == org_id) then
|
|
|
|
|
parent_org_id = modpol.get_org_id_by_name('instance')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local props = properties or {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- actually add the org
|
|
|
|
|
local org = modpol.org_temp
|
|
|
|
|
local default_policies = modpol.default_org_policies or {}
|
|
|
|
|
org.id = org_id
|
|
|
|
|
org.name = org_name -- do not change this without the proper api function. This is only a reference, not the only place this is recorded.
|
|
|
|
|
org.members = members
|
|
|
|
|
org.parent = parent_org_id
|
|
|
|
|
org.properties = props
|
|
|
|
|
org.policies = default_policies
|
|
|
|
|
|
|
|
|
|
modpol.orgs [org_id] = org
|
|
|
|
|
|
|
|
|
|
--record the child org in the parent's children table
|
|
|
|
|
if parent_org_id then
|
|
|
|
|
|
|
|
|
|
if modpol.orgs[parent_org_id].children then
|
|
|
|
|
table.insert(modpol.orgs[parent_org_id].children, org_id)
|
|
|
|
|
else
|
|
|
|
|
modpol.orgs[parent_org_id].children = {org_id}
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-01-28 23:22:06 -07:00
|
|
|
|
2021-03-10 01:40:04 -05:00
|
|
|
local parent_id_str = "none"
|
|
|
|
|
if parent_org_id then
|
|
|
|
|
parent_id_str = tostring(parent_org_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local msg = "New org: " .. org_name .. " ["..org_id.."], parent = ".. parent_id_str ..", members = "..
|
2021-03-08 14:06:15 -05:00
|
|
|
" (" .. table.concat (members, ", ") .. ")"
|
|
|
|
|
|
|
|
|
|
modpol.record (org_id, msg, 'org_init')
|
2021-03-01 19:11:20 -05:00
|
|
|
return true, msg
|
2021-01-28 23:22:06 -07:00
|
|
|
end
|
|
|
|
|
|
2021-03-01 19:11:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.remove_org
|
2021-03-08 14:06:15 -05:00
|
|
|
-- Params: number id, opt string reason
|
2021-03-01 19:11:20 -05:00
|
|
|
-- Output:
|
|
|
|
|
--
|
|
|
|
|
-- This function removes an "org". It returns a boolean
|
|
|
|
|
-- success indicator and either an error message
|
|
|
|
|
-- starting with "Error:" or a success message. It also recursively
|
|
|
|
|
-- removes all child orgs, with 'remove parent org' as reason. If
|
|
|
|
|
-- preserve_records is enabled, it logs the removal in the org ledger,
|
2021-03-08 14:06:15 -05:00
|
|
|
-- and stores the ledger in modpol.old_ledgers
|
2021-03-01 19:11:20 -05:00
|
|
|
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
-- The number parameter specifies the "org" id.
|
2021-03-01 19:11:20 -05:00
|
|
|
--
|
|
|
|
|
-- The reason is an optional string to additionally include in the
|
|
|
|
|
-- log as reason for removal
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.remove_org = function (org_id, reason)
|
2021-03-01 19:11:20 -05:00
|
|
|
local str
|
|
|
|
|
if not reason then reason = '' end
|
2021-03-08 14:06:15 -05:00
|
|
|
if not type(org_id) == 'number' or not modpol.orgs[org_id] then
|
|
|
|
|
str = "Error: Specify an existing org id to remove"
|
|
|
|
|
modpol.ocutil.log (str)
|
|
|
|
|
return false,str
|
2021-03-01 19:11:20 -05:00
|
|
|
end
|
2021-03-08 14:06:15 -05:00
|
|
|
local org_name = modpol.orgs[org_id]
|
2021-03-01 19:11:20 -05:00
|
|
|
if org_name == 'instance' then
|
|
|
|
|
return false,"Error: You cannot remove instance"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--log msg
|
2021-03-08 14:06:15 -05:00
|
|
|
local msg = 'Removing org '..org_name ..' ['..org_id..'], reason: '.. reason
|
|
|
|
|
|
|
|
|
|
modpol.ocutil.log (msg)
|
2021-03-01 19:11:20 -05:00
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
|
2021-03-01 19:11:20 -05:00
|
|
|
-- actually remove the org
|
|
|
|
|
|
|
|
|
|
-- TODO: if callbacks are implemented, do so here --
|
2021-03-08 14:06:15 -05:00
|
|
|
|
2021-03-01 19:11:20 -05:00
|
|
|
if preserve_records then
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.old_ledgers[org_id] = modpol.orgs[org_id].ledger
|
2021-03-01 19:11:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- also remove all children
|
2021-03-08 14:06:15 -05:00
|
|
|
if modpol.orgs[org_id].children and #modpol.orgs[org_id].children > 0 then
|
|
|
|
|
for _,child_name in pairs(modpol.orgs[org_id].children) do
|
|
|
|
|
--this is recursion. If it gets to be a problem, we will need a separate function
|
|
|
|
|
modpol.remove_org(child_name, 'remove parent org')
|
2021-03-01 19:11:20 -05:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
--also remove references to this org in parent orgs
|
|
|
|
|
if modpol.orgs[org_id].parent then
|
|
|
|
|
local parent_id = modpol.orgs[org_id].parent
|
|
|
|
|
modpol.orgs[parent_id].children[org_id] = nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
modpol.orgs[org_id] = nil
|
|
|
|
|
|
|
|
|
|
modpol.record (org_id, msg, 'org_remove')
|
2021-03-01 19:11:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
return true,msg
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-28 23:22:06 -07:00
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.list_orgs
|
|
|
|
|
-- Params: None
|
|
|
|
|
-- Output:
|
|
|
|
|
-- This function returns a text-format list of "orgs". The list shows
|
|
|
|
|
-- one "org" per line in the following format:
|
|
|
|
|
-- org_name (member, member, ...)
|
|
|
|
|
-- If there are no "orgs", the output is an empty string.
|
|
|
|
|
|
|
|
|
|
modpol.list_orgs = function()
|
|
|
|
|
local outbuf = ""
|
|
|
|
|
local str
|
2021-03-08 14:06:15 -05:00
|
|
|
for org_id, org_data in pairs (modpol.orgs) do
|
2021-01-28 23:22:06 -07:00
|
|
|
-- Process next "org"
|
|
|
|
|
-- Build string version of member list
|
|
|
|
|
local memcat = org_data ["members"]
|
2021-03-08 14:06:15 -05:00
|
|
|
local org_name = org_data ["name"].."["..org_id.."]"
|
2021-02-10 23:40:28 -05:00
|
|
|
if modpol.ocutil.str_empty (memcat) then
|
2021-01-28 23:22:06 -07:00
|
|
|
memcat = "(empty)"
|
|
|
|
|
else
|
|
|
|
|
memcat = "(" .. table.concat (memcat, ", ") .. ")"
|
|
|
|
|
end
|
|
|
|
|
-- Build output string
|
|
|
|
|
outbuf = outbuf .. org_name .. " " .. memcat .. "\n"
|
|
|
|
|
end
|
|
|
|
|
return outbuf
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.reset_orgs
|
|
|
|
|
-- Params: None
|
|
|
|
|
-- Removes all orgs and recreates blank org "instance" with all
|
|
|
|
|
-- current users
|
|
|
|
|
-- returns confirmation message
|
|
|
|
|
|
|
|
|
|
modpol.reset_orgs = function()
|
2021-01-31 22:47:28 -07:00
|
|
|
local users = modpol.list_users()
|
2021-03-01 19:11:20 -05:00
|
|
|
--store the ledgers in modpol.old_ledgers, and note that each org was closed in each record.
|
|
|
|
|
if preserve_records then
|
2021-03-08 14:06:15 -05:00
|
|
|
for id, org in pairs(modpol.orgs) do
|
2021-03-01 19:11:20 -05:00
|
|
|
local old_ledger = org.ledger
|
2021-03-21 23:00:03 -04:00
|
|
|
local entry = modpol.create_ledger_entry('Removing org '.. id, id, 'org_purge')
|
|
|
|
|
|
|
|
|
|
if old_ledger == nil then
|
|
|
|
|
old_ledger = { entry }
|
|
|
|
|
else
|
|
|
|
|
table.insert(old_ledger, entry)
|
|
|
|
|
end
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.old_ledgers[id] = old_ledger
|
2021-03-01 19:11:20 -05:00
|
|
|
end
|
|
|
|
|
end
|
2021-01-28 23:22:06 -07:00
|
|
|
modpol.orgs = {}
|
|
|
|
|
local message = "Orgs purged"
|
2021-03-01 19:11:20 -05:00
|
|
|
modpol.record(nil, message, 'org_purge')
|
2021-01-28 23:22:06 -07:00
|
|
|
modpol.add_org("instance", users)
|
|
|
|
|
return message
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.add_member
|
2021-03-08 14:06:15 -05:00
|
|
|
-- Params: org_id (number), member (string)
|
2021-01-28 23:22:06 -07:00
|
|
|
-- Output:
|
|
|
|
|
-- Adds the specified member to the specified org
|
2021-03-01 19:11:20 -05:00
|
|
|
-- Returns a boolean success indicator and
|
|
|
|
|
-- either a confirmation or error message
|
2021-01-28 23:22:06 -07:00
|
|
|
|
2021-03-01 19:11:20 -05:00
|
|
|
|
|
|
|
|
-- TODO, check that member is a user, if the org_name is not instance
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.add_member = function(org_id, member)
|
|
|
|
|
|
|
|
|
|
if (modpol.orgs[org_id] == nil) then
|
2021-03-01 19:11:20 -05:00
|
|
|
return false,"Error: No such org"
|
|
|
|
|
elseif type(member) ~= 'string' then
|
|
|
|
|
return false,"Error: member is wrong type"
|
2021-01-28 23:22:06 -07:00
|
|
|
else
|
2021-03-08 14:06:15 -05:00
|
|
|
local org_name = modpol.orgs [org_id].name
|
|
|
|
|
if modpol.is_member(org_id,member) then
|
2021-03-01 19:11:20 -05:00
|
|
|
return false,"Error: " .. member .. " is already a member of " .. org_name
|
2021-02-10 23:40:28 -05:00
|
|
|
else
|
2021-03-08 14:06:15 -05:00
|
|
|
table.insert(modpol.orgs[org_id]["members"], member)
|
|
|
|
|
local message = member .. " added to org " .. org_name .. " ["..org_id.."]"
|
|
|
|
|
modpol.record(org_id, message, 'add_member')
|
2021-03-01 19:11:20 -05:00
|
|
|
return true,message
|
2021-02-10 23:40:28 -05:00
|
|
|
end
|
2021-01-28 23:22:06 -07:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.is_member
|
2021-03-08 14:06:15 -05:00
|
|
|
-- Params: org (number), member (string)
|
2021-01-28 23:22:06 -07:00
|
|
|
-- Output: Boolean depending on membership or nil/error message
|
|
|
|
|
|
|
|
|
|
modpol.is_member = function(org, member)
|
|
|
|
|
if (modpol.orgs[org] == nil) then
|
|
|
|
|
return nil, "Error: No such org"
|
|
|
|
|
else
|
|
|
|
|
local member_table = modpol.orgs[org]["members"]
|
|
|
|
|
if member_table then
|
|
|
|
|
for index, value in pairs(member_table) do
|
|
|
|
|
if value == member then
|
|
|
|
|
-- match found
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
-- no match found
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
-- Function: modpol.remove_member
|
2021-03-08 14:06:15 -05:00
|
|
|
-- Params: org (number), member (string)
|
2021-01-28 23:22:06 -07:00
|
|
|
-- Output:
|
|
|
|
|
-- Removes the specified member from the specified org
|
|
|
|
|
-- Returns confirmation or error message
|
|
|
|
|
|
|
|
|
|
function modpol.remove_member(org, member)
|
|
|
|
|
local message = "Error: No such org"
|
|
|
|
|
if (modpol.orgs[org] == nil) then
|
2021-03-01 19:11:20 -05:00
|
|
|
return false, message
|
2021-01-28 23:22:06 -07:00
|
|
|
else
|
|
|
|
|
local member_table = modpol.orgs[org]["members"]
|
|
|
|
|
if member_table then
|
|
|
|
|
for index, value in pairs(member_table) do
|
|
|
|
|
if value == member then
|
|
|
|
|
-- match found, set to nil
|
2021-03-01 19:11:20 -05:00
|
|
|
|
|
|
|
|
modpol.orgs[org]["members"][value] = nil
|
|
|
|
|
|
2021-03-08 14:06:15 -05:00
|
|
|
-- TODO: add callbacks here, for such as revoking privs, etc --
|
|
|
|
|
|
2021-01-28 23:22:06 -07:00
|
|
|
message = member .. " removed from org " .. org
|
2021-03-08 14:06:15 -05:00
|
|
|
modpol.record(org, message, 'remove_member')
|
2021-03-01 19:11:20 -05:00
|
|
|
return true, message
|
2021-01-28 23:22:06 -07:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
-- no match found (or org has no members)
|
|
|
|
|
message = member .. " not found in org " .. org
|
2021-03-01 19:11:20 -05:00
|
|
|
return false, message
|
2021-01-28 23:22:06 -07:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- ===================================================================
|
2021-02-11 21:59:17 +00:00
|
|
|
-- TKTK
|
|
|
|
|
-- rename_org(old_name, new_name)
|
|
|
|
|
-- Renames an org
|
2021-01-28 23:22:06 -07:00
|
|
|
|
2021-03-01 19:11:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-02-20 22:48:17 -07:00
|
|
|
-- ===================================================================
|
|
|
|
|
-- POLICIES
|
|
|
|
|
-- Each policy is a table with a string for a key. Contents:
|
|
|
|
|
-- KEY - name of the policy (string)
|
|
|
|
|
-- [member orgs]: external orgs allowed to call policy
|
|
|
|
|
-- [function]: the function that initiates the policy
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
|
2021-02-11 21:59:17 +00:00
|
|
|
-- ===================================================================
|
|
|
|
|
-- modpol.add_policy
|
|
|
|
|
-- Adds a policy to an org's [org].policies table
|
2021-02-20 22:48:17 -07:00
|
|
|
-- Params: org_name (string),
|
|
|
|
|
-- policy_name (string),
|
|
|
|
|
-- policy_table (table)
|
2021-02-11 21:59:17 +00:00
|
|
|
-- Output: true if successful, nil if error
|
2021-02-20 22:48:17 -07:00
|
|
|
|
|
|
|
|
-- TKTK NEEDS TO BE REWRITTEN
|
|
|
|
|
|
|
|
|
|
modpol.add_policy = function(org_name, policy_name, policy_table)
|
2021-02-11 21:59:17 +00:00
|
|
|
-- first, basic checks
|
|
|
|
|
local message = "Error: No such org"
|
|
|
|
|
if (modpol.orgs[org_name] == nil) then
|
|
|
|
|
return nil, message
|
|
|
|
|
elseif (modpol[target_function] == nil) then
|
|
|
|
|
message = "Error: No such target function"
|
|
|
|
|
return nil, message
|
|
|
|
|
elseif (modpol[policy_function]) then
|
|
|
|
|
message = "Error: No such policy function"
|
|
|
|
|
return nil, message
|
|
|
|
|
else
|
|
|
|
|
-- okay, proceed
|
|
|
|
|
modpol.orgs[org_name].policies[target_function] = policy_function
|
|
|
|
|
message = "In org " .. org_name .. ", policy for " .. target_function
|
|
|
|
|
.. " set to " .. policy_function
|
|
|
|
|
record(org_name, message)
|
|
|
|
|
return true, message
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-01-28 23:22:06 -07:00
|
|
|
|
|
|
|
|
-- ===================================================================
|
|
|
|
|
-- End of file.
|