Merge branch 'Redo_orgs' into 'master'

bugfix orgs

See merge request medlabboulder/modpol!12
This commit is contained in:
Nathan Schneider 2021-03-25 22:24:09 +00:00
commit fc5e117b63
2 changed files with 86 additions and 59 deletions

View File

@ -77,7 +77,7 @@ dofile (topdir .. "/api.lua")
-- Final checks
-- create instance if not present
if (modpol.orgs["instance"] == nil) then
if not modpol.get_org_id_by_name('instance') then
modpol.add_org("instance", modpol.list_users())
end

View File

@ -7,7 +7,7 @@
-- preserve records -- if true, will store org ledgers when they are closed, for later reference.
local preserve_records = true
-- policies assigned to any org can go here
-- policies to be assigned to every org can go here
modpol.default_org_policies = modpol.default_org_policies or {}
@ -45,29 +45,38 @@ end
--define the template of a ledger entry
modpol.ledger_entry_temp = {
timestamp = '',
entrytype = nil,
action_msg = '',
org_name = nil,
org_id = nil,
}
modpol.ledger_entry_temp = function()
return {
timestamp = '',
entrytype = nil,
action_msg = '',
org_name = nil,
org_id = nil,
}
end
-- define the basic template of an org.
modpol.org_temp = {
id = nil,
name = nil,
policies = {},
members = {},
ledger = {},
parent = nil,
children = {},
properties = {},
old_names = {},
}
modpol.org_temp = function()
return {
id = nil,
name = nil,
policies = {},
members = {},
ledger = {},
parent = nil,
children = {},
properties = {},
old_names = {},
}
end
-- ===================================================================
-- Function: modpol.create_ledger_entry
@ -78,7 +87,7 @@ modpol.org_temp = {
-- and returns a valid ledger entry table
modpol.create_ledger_entry = function(action_msg, org_id, entry_type)
local entry = modpol.ledger_entry_temp
local entry = modpol.ledger_entry_temp()
if action_msg and type(action_msg) == 'string' then
entry.action_msg = action_msg
end
@ -114,13 +123,23 @@ modpol.record = function (org_id, msg, type)
-- Record in global ledger
table.insert (modpol.ledger, entry)
-- Record in "org"-specific ledger
if modpol.orgs [org_id] ~= nil then
local org_ledger = modpol.orgs [org_id]["ledger"]
if org_ledger == nil then
modpol.orgs [org_id]["ledger"] = { entry }
else
modpol.orgs [org_id]["ledger"] = table.insert (org_ledger, entry)
end
local org_ledger = modpol.orgs [org_id]["ledger"]
if org_ledger == nil then
modpol.orgs [org_id]["ledger"] = { entry }
print("Warning: Org ".. org_id .. " did not have a ledger. This is an error.")
else
table.insert (org_ledger, entry)
modpol.orgs [org_id]["ledger"] = org_ledger
end
end
modpol.store_data() -- Copy data to disk
@ -167,60 +186,63 @@ end
-- with arbitrary properties
modpol.add_org = function(org_name, members, parent_id, properties)
local str
if modpol.ocutil.str_empty (org_name) then
return false,"Error: Org needs a name"
end
local str
if parent_id and not modpol.orgs[parent_id] then
return false,"Error: Parent_id must be a valid existing org id"
end
if modpol.ocutil.str_empty (org_name) then
return false,"Error: Org needs a name"
end
if properties and not type(properties) == 'table' then
return false,"Error: Properties must be a table"
end
if parent_id and not modpol.orgs[parent_id] then
return false,"Error: Parent_id must be a valid existing org id"
end
modpol.ocutil.log ("Attempting to add new org " .. org_name)
if modpol.get_org_id_by_name(org_name) ~= nil then
str = "Error: Org " .. org_name .. " already exists"
modpol.ocutil.log (str)
return false,str
end
if properties and not type(properties) == 'table' then
return false,"Error: Properties must be a table"
end
local org_id = modpol.id_counter
modpol.id_counter = modpol.id_counter + 1
modpol.ocutil.log ("Attempting to add new org " .. org_name)
if modpol.get_org_id_by_name(org_name) ~= nil then
str = "Error: Org " .. org_name .. " already exists"
modpol.ocutil.log (str)
return false,str
end
-- assign a new id to the new org
local org_id = modpol.id_counter
modpol.id_counter = modpol.id_counter + 1
-- 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
-- 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
if parent_id and not(modpol.get_org_id_by_name('instance') == org_id) then
local parent_org_id = nil
local instance_id = modpol.get_org_id_by_name('instance')
if parent_id and instance_id and not(instance_id == org_id) then
parent_org_id = parent_id
end
end
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')
if not parent_id and instance_id and not ( instance_id == 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 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.properties = properites or org.properties
org.policies = default_policies
org.children = {}
modpol.orgs [org_id] = org
@ -350,6 +372,11 @@ modpol.list_orgs = function()
return outbuf
end
-- ===================================================================
-- Function: modpol.reset_orgs
-- Params: None