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 -- Final checks
-- create instance if not present -- 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()) modpol.add_org("instance", modpol.list_users())
end end

View File

@ -7,7 +7,7 @@
-- preserve records -- if true, will store org ledgers when they are closed, for later reference. -- preserve records -- if true, will store org ledgers when they are closed, for later reference.
local preserve_records = true 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 {} modpol.default_org_policies = modpol.default_org_policies or {}
@ -45,7 +45,9 @@ end
--define the template of a ledger entry --define the template of a ledger entry
modpol.ledger_entry_temp = { modpol.ledger_entry_temp = function()
return {
timestamp = '', timestamp = '',
entrytype = nil, entrytype = nil,
action_msg = '', action_msg = '',
@ -53,10 +55,15 @@ modpol.ledger_entry_temp = {
org_id = nil, org_id = nil,
} }
end
-- define the basic template of an org. -- define the basic template of an org.
modpol.org_temp = {
modpol.org_temp = function()
return {
id = nil, id = nil,
name = nil, name = nil,
policies = {}, policies = {},
@ -69,6 +76,8 @@ modpol.org_temp = {
} }
end
-- =================================================================== -- ===================================================================
-- Function: modpol.create_ledger_entry -- Function: modpol.create_ledger_entry
-- Params: strings: action_msg, type, number: org_id -- Params: strings: action_msg, type, number: org_id
@ -78,7 +87,7 @@ modpol.org_temp = {
-- and returns a valid ledger entry table -- and returns a valid ledger entry table
modpol.create_ledger_entry = function(action_msg, org_id, entry_type) 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 if action_msg and type(action_msg) == 'string' then
entry.action_msg = action_msg entry.action_msg = action_msg
end end
@ -114,12 +123,22 @@ modpol.record = function (org_id, msg, type)
-- Record in global ledger -- Record in global ledger
table.insert (modpol.ledger, entry) table.insert (modpol.ledger, entry)
-- Record in "org"-specific ledger -- Record in "org"-specific ledger
if modpol.orgs [org_id] ~= nil then if modpol.orgs [org_id] ~= nil then
local org_ledger = modpol.orgs [org_id]["ledger"] local org_ledger = modpol.orgs [org_id]["ledger"]
if org_ledger == nil then if org_ledger == nil then
modpol.orgs [org_id]["ledger"] = { entry } modpol.orgs [org_id]["ledger"] = { entry }
print("Warning: Org ".. org_id .. " did not have a ledger. This is an error.")
else else
modpol.orgs [org_id]["ledger"] = table.insert (org_ledger, entry)
table.insert (org_ledger, entry)
modpol.orgs [org_id]["ledger"] = org_ledger
end end
end end
@ -167,6 +186,7 @@ end
-- with arbitrary properties -- with arbitrary properties
modpol.add_org = function(org_name, members, parent_id, properties) modpol.add_org = function(org_name, members, parent_id, properties)
local str local str
if modpol.ocutil.str_empty (org_name) then if modpol.ocutil.str_empty (org_name) then
@ -190,37 +210,39 @@ modpol.add_org = function(org_name, members, parent_id, properties)
return false,str return false,str
end end
-- assign a new id to the new org
local org_id = modpol.id_counter local org_id = modpol.id_counter
modpol.id_counter = modpol.id_counter + 1 modpol.id_counter = modpol.id_counter + 1
-- the instance will have a parent of nil if it is the instance. -- 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 -- otherwise, it will have a parent of instance's id if not otherwise defined
local parent_org_id = nil local parent_org_id = nil
local instance_id = modpol.get_org_id_by_name('instance')
if parent_id and not(modpol.get_org_id_by_name('instance') == org_id) then if parent_id and instance_id and not(instance_id == org_id) then
parent_org_id = parent_id parent_org_id = parent_id
end end
if not parent_id and not(modpol.get_org_id_by_name('instance') == org_id) then if not parent_id and instance_id and not ( instance_id == org_id ) then
parent_org_id = modpol.get_org_id_by_name ( 'instance' ) parent_org_id = modpol.get_org_id_by_name ( 'instance' )
end end
local props = properties or {}
-- actually add the org -- actually add the org
local org = modpol.org_temp local org = modpol.org_temp()
local default_policies = modpol.default_org_policies or {} local default_policies = modpol.default_org_policies or {}
org.id = org_id 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.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.members = members
org.parent = parent_org_id org.parent = parent_org_id
org.properties = props org.properties = properites or org.properties
org.policies = default_policies org.policies = default_policies
org.children = {}
modpol.orgs [org_id] = org modpol.orgs [org_id] = org
@ -350,6 +372,11 @@ modpol.list_orgs = function()
return outbuf return outbuf
end end
-- =================================================================== -- ===================================================================
-- Function: modpol.reset_orgs -- Function: modpol.reset_orgs
-- Params: None -- Params: None