added remove_org, get_org, and record functions to orgs
This commit is contained in:
parent
9a0680b07e
commit
0a16d85be2
@ -4,13 +4,26 @@ modpol.orgs =
|
|||||||
list = {}
|
list = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
-- sets modpol.orgs as it's own callback
|
-- sets modpol.orgs as it's own fallback
|
||||||
modpol.orgs.__index = modpol.orgs
|
modpol.orgs.__index = modpol.orgs
|
||||||
|
|
||||||
|
-- ==================================================
|
||||||
|
-- returns org when given its id
|
||||||
|
|
||||||
|
function modpol.orgs.get_org(id)
|
||||||
|
return modpol.orgs.list[id]
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ===================================================
|
||||||
|
-- initializes the instance (root org)
|
||||||
|
-- can only be run once, as only one instance can exist
|
||||||
|
|
||||||
function modpol.orgs:init_instance()
|
function modpol.orgs:init_instance()
|
||||||
|
local error_msg
|
||||||
if modpol.orgs.list[1] then
|
if modpol.orgs.list[1] then
|
||||||
print('Error: instance has already been initialized')
|
error_msg = 'Error: instance has already been initialized'
|
||||||
return false
|
modpol.ocutil.log(error_msg)
|
||||||
|
return false, error_msg
|
||||||
end
|
end
|
||||||
|
|
||||||
local instance = {
|
local instance = {
|
||||||
@ -31,17 +44,57 @@ function modpol.orgs:init_instance()
|
|||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
function modpol.orgs:add_org(name)
|
-- =======================================================
|
||||||
if self.id == nil then
|
-- records a log message to the modpol ledger
|
||||||
print('Error: add_org can only be run by a parent org')
|
|
||||||
|
function modpol.orgs:record(msg, entry_type)
|
||||||
|
local entry = {
|
||||||
|
timestamp = '',
|
||||||
|
entry_type = nil,
|
||||||
|
action_msg = '',
|
||||||
|
org_name = '',
|
||||||
|
org_id = nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
if type(msg) == 'string' and not(modpol.ocutil.str_empty(msg)) then
|
||||||
|
entry.action_msg = msg
|
||||||
|
else
|
||||||
|
print('Error: msg must be a non empty string')
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
if modpol.ocutil.str_empty(name) then
|
if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then
|
||||||
print('Error: name is required')
|
entry.entry_type = entry_type
|
||||||
|
else
|
||||||
|
print('Error: entry_type must be a non empty string')
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
entry.timestamp = os.time()
|
||||||
|
entry.org_id = self.id
|
||||||
|
entry.org_name = self.name
|
||||||
|
|
||||||
|
table.insert(modpol.ledger, entry)
|
||||||
|
modpol.store_data()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ==================================================
|
||||||
|
-- adds a new sub org to the org it is called on
|
||||||
|
-- ex: instance:add_org('town hall')
|
||||||
|
|
||||||
|
function modpol.orgs:add_org(name)
|
||||||
|
if self.id == nil then
|
||||||
|
error_msg = 'Error: add_org can only be called by another org'
|
||||||
|
modpol.ocutil.log(error_msg)
|
||||||
|
return false, error_msg
|
||||||
|
end
|
||||||
|
|
||||||
|
if modpol.ocutil.str_empty(name) then
|
||||||
|
error_msg = 'Error: org name is required'
|
||||||
|
modpol.ocutil.log(error_msg)
|
||||||
|
return false, error_msg
|
||||||
|
end
|
||||||
|
|
||||||
-- creating the child sub org
|
-- creating the child sub org
|
||||||
modpol.orgs.count = modpol.orgs.count + 1
|
modpol.orgs.count = modpol.orgs.count + 1
|
||||||
local child_org = {
|
local child_org = {
|
||||||
@ -49,11 +102,8 @@ function modpol.orgs:add_org(name)
|
|||||||
name = name,
|
name = name,
|
||||||
policies = {},
|
policies = {},
|
||||||
members = {},
|
members = {},
|
||||||
ledger = {},
|
|
||||||
parent = self.id,
|
parent = self.id,
|
||||||
children = {},
|
children = {},
|
||||||
properties = {},
|
|
||||||
old_names = {}
|
|
||||||
}
|
}
|
||||||
setmetatable(child_org, modpol.orgs)
|
setmetatable(child_org, modpol.orgs)
|
||||||
|
|
||||||
@ -62,13 +112,37 @@ function modpol.orgs:add_org(name)
|
|||||||
|
|
||||||
-- adding child to org list
|
-- adding child to org list
|
||||||
modpol.orgs.list[child_org.id] = child_org
|
modpol.orgs.list[child_org.id] = child_org
|
||||||
|
|
||||||
|
|
||||||
return child_org
|
return child_org
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- ========================================
|
||||||
|
-- recursively deletes an org and its suborgs
|
||||||
|
-- note: "reason" param was removed, can be added back
|
||||||
|
|
||||||
|
function modpol.orgs:delete()
|
||||||
|
if self.id == 1 then
|
||||||
|
return false, 'Error: cannot delete instance'
|
||||||
|
end
|
||||||
|
|
||||||
|
if #self.children > 0 then
|
||||||
|
for i, child_id in pairs(self.children) do
|
||||||
|
local child = modpol.orgs.get_org(child_id)
|
||||||
|
print(child_id, child)
|
||||||
|
child:delete()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
modpol.orgs.list[self.id] = nil
|
||||||
|
print('Removed ' .. self.name .. ': ' .. self.id)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
function modpol.orgs:add_member(user)
|
function modpol.orgs:add_member(user)
|
||||||
table.insert(self.members, user)
|
table.insert(self.members, user)
|
||||||
end
|
end
|
||||||
|
|
||||||
function modpol.orgs:list_member()
|
function modpol.orgs:list_member()
|
||||||
print(self.members[1])
|
print(self.members[1])
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user