Merge branch 'join-created-orgs' into 'master'

created orgs start with a member

See merge request medlabboulder/modpol!23
This commit is contained in:
Nathan Schneider 2021-06-06 04:03:25 +00:00
commit f837e99077
4 changed files with 68 additions and 30 deletions

View File

@ -75,6 +75,10 @@ function modpol.orgs.reset()
end
end
modpol.orgs.array[1] = nil
modpol.instance = modpol.orgs.init_instance()
modpol.ocutil.log('Reset all orgs')
modpol.orgs:record('Resetting all orgs', 'org_reset')
end
@ -144,7 +148,7 @@ 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)
function modpol.orgs:add_org(name, user)
if self.id == nil then
modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> add_org can only be called by another org')
return false
@ -175,6 +179,9 @@ function modpol.orgs:add_org(name)
-- adding child to org list
modpol.orgs.array[child_org.id] = child_org
-- adding creator of org as the first member
child_org:add_member(user)
self:record('created sub org ' .. name, 'add_org')
modpol.ocutil.log('Created ' .. name .. ' (suborg of ' .. self.name .. ')')

View File

@ -79,17 +79,18 @@ function modpol.orgs:resolve_request(request_id, approve)
-- there's probably a way to clean this up, the issue is the varying number of commands
-- ex: self['add_member'](self, 'member_name')
-- not sure if this is safe, more testing to do
self[request.type](self, p[1], p[2], p[3])
-- if request.type == "add_org" then
-- self:add_org(p[1])
-- elseif request.type == "delete" then
-- self:delete()
-- elseif request.type == "add_member" then
-- self:add_member(p[1])
-- elseif request.type == "remove_member" then
-- self:remove_member(p[1])
-- end
-- self[request.type](self, p[1], p[2], p[3])
if request.type == "add_org" then
self:add_org(request.params[1], request.user)
elseif request.type == "delete" then
self:delete()
elseif request.type == "add_member" then
self:add_member(request.params[1])
elseif request.type == "remove_member" then
self:remove_member(request.params[1])
end
end
@ -127,15 +128,26 @@ function modpol.orgs:make_request(request)
-- checking to see if user is able to make request
local requested_policy = self.policies[request.type]
local parent_policy = modpol.orgs.get_org(self.parent).policies[request.type]
-- tries to use org's policy table, defers to parent otherwise
if not requested_policy then
modpol.ocutil.log(request.type .. ' policy not found, deferring to parent org')
requested_policy = parent_policy
-- if not the instance org (instance's don't have parents)
if self.id ~= 1 then
local parent_policy = modpol.orgs.get_org(self.parent).policies[request.type]
if not parent_policy then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined')
-- tries to use org's policy table, defers to parent otherwise
if not requested_policy then
modpol.ocutil.log(request.type .. ' policy not found, deferring to parent org')
requested_policy = parent_policy
if not parent_policy then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined')
return false
end
end
-- fails if instance policy undefined
else
if not requested_policy then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> policy undefined')
return false
end
end

View File

@ -4,20 +4,20 @@ print('\nRemoving existing orgs')
modpol.orgs.reset()
print('\nCreating an org called "test_org"')
test_org = modpol.instance:add_org('test_org')
test_org = modpol.instance:add_org('test_org', 'luke')
print('\nTrying to create an org with the same name')
duplicate = modpol.instance:add_org('test_org')
duplicate = modpol.instance:add_org('test_org', 'luke')
print('\nAdding user "luke" to test_org')
test_org:add_member('luke')
print('\nAdding user "nathan" to test_org')
test_org:add_member('nathan')
print('\nTrying to add duplicate user to test_org')
test_org:add_member('luke')
test_org:add_member('nathan')
print('\nRemoving user "luke" from test_org')
test_org:remove_member('luke')
print('\nRemoving user "nathan" from test_org')
test_org:remove_member('nathan')
print('\nTrying to remove user "luke" from empty member list')
test_org:remove_member('luke')
print('\nTrying to remove user "nathan" from empty member list')
test_org:remove_member('nathan')

View File

@ -2,7 +2,7 @@ dofile('../modpol.lua');
modpol.orgs.reset()
test_org = modpol.instance:add_org('test_org')
test_org = modpol.instance:add_org('test_org', 'lukvmil')
test_org:add_member('luke')
test_org:add_member('nathan')
@ -23,4 +23,23 @@ for id, process in ipairs(test_org.processes) do
end
-- process = test_org.processes[process_id]
-- process:approve("luke", true)
-- process:approve("nathan", true)
-- process:approve("nathan", true)
modpol.instance:set_policy("add_org", "consent", false);
new_request = {
user = "lukvmil",
type = "add_org",
params = {"new_org"}
}
request_id = modpol.instance:make_request(new_request)
modpol.instance:add_member('luke')
modpol.instance:add_member('josh')
modpol.instance:add_member('nathan')
for id, process in ipairs(modpol.instance.processes) do
process:approve('luke', true)
process:approve('josh', true)
end