split up orgs file

This commit is contained in:
Luke Miller 2021-05-02 22:38:27 -04:00
parent c975f114b3
commit 10f6adc544
3 changed files with 115 additions and 114 deletions

View File

@ -6,7 +6,8 @@ local localdir = modpol.topdir
dofile (localdir .. "/users/users.lua")
--orgs
dofile (localdir .. "/orgs/orgs.lua")
dofile (localdir .. "/orgs/base.lua")
dofile (localdir .. "/orgs/requests.lua")
--interactions
dofile (localdir .. "/interactions/interactions.lua")

View File

@ -20,13 +20,6 @@ function temp_org()
}
end
modpol.orgs.request_params = {
add_org = 1,
delete = 0,
add_member = 1,
remove_member = 1
}
-- ==================================================
-- returns org when given its id or name
function modpol.orgs.get_org(arg)
@ -299,109 +292,3 @@ function modpol.orgs:set_policy(policy_type, process, must_be_member)
end
-- ================================
-- creates a new process linked to a request id
function modpol.orgs:create_process(request_id)
local new_process = {
object = "I am a process",
request_id = request_id
}
-- linear search for empty process slots (lazy deletion)
for k, v in ipairs(self.processes) do
if v == 'deleted' then
local empty_index = k
break
end
end
-- attempts to fill empty spots in list, otherwise appends to end
if empty_index then
self.processes[empty_index] = new_process
else
table.insert(self.processes, new_process)
end
end
-- ===========================
-- compares to requests to see if they are identical
function modpol.orgs.comp_req(req1, req2)
-- compares request type
if req1.type ~= req2.type then
return false
else
-- comparing parameters
-- we can assume the number of params is the same as this is checked in the make_request func
for k, v in ipairs(req1.params) do
if v ~= req2.params[k] then
return false
end
end
end
return true
end
-- ================================
-- tries to make a request to the org
function modpol.orgs:make_request(request)
-- makes sure the request has the valid number of parameters
local num_params = modpol.orgs.request_params[request.type]
if num_params == nil then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request type is invalid')
return false
end
-- num_params should equal zero at the end if request.params matches the num of params for that type
for k, v in ipairs(request.params) do
num_params = num_params - 1
end
if num_params ~= 0 then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has invalid number of parameters')
return false
end
-- checking to see if identical request already exists
for k, v in ipairs(self.requests) do
if self.comp_req(request, v) == true then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has already been made')
return false
end
end
-- checking to see if user is able to make request
local requested_policy = self.policies[request.type]
if requested_policy then
if requested_policy.must_be_member and not self:has_member(request.user) then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> user must be org member to make this request')
return false
end
end
-- linear search for empty process slots (lazy deletion)
for k, v in ipairs(self.requests) do
if v == 'deleted' then
local empty_index = k
break
end
end
-- attempts to fill empty spots in list, otherwise appends to end
local request_id = nil
if empty_index then
self.requests[empty_index] = request
request_id = empty_index
else
table.insert(self.requests, request)
-- finds end of list to return current request's id
local count = 0
for k, v in ipairs(self.requests) do
count = count + 1
end
request_id = count
end
modpol.ocutil.log("Request made")
return request_id
end

113
modpol/orgs/requests.lua Normal file
View File

@ -0,0 +1,113 @@
modpol.orgs.request_params = {
add_org = 1,
delete = 0,
add_member = 1,
remove_member = 1
}
-- ================================
-- creates a new process linked to a request id
function modpol.orgs:create_process(request_id)
local new_process = {
object = "I am a process",
request_id = request_id
}
-- linear search for empty process slots (lazy deletion)
for k, v in ipairs(self.processes) do
if v == 'deleted' then
local empty_index = k
break
end
end
-- attempts to fill empty spots in list, otherwise appends to end
if empty_index then
self.processes[empty_index] = new_process
else
table.insert(self.processes, new_process)
end
end
-- ===========================
-- compares to requests to see if they are identical
function modpol.orgs.comp_req(req1, req2)
-- compares request type
if req1.type ~= req2.type then
return false
else
-- comparing parameters
-- we can assume the number of params is the same as this is checked in the make_request func
for k, v in ipairs(req1.params) do
if v ~= req2.params[k] then
return false
end
end
end
return true
end
-- ================================
-- tries to make a request to the org
function modpol.orgs:make_request(request)
-- makes sure the request has the valid number of parameters
local num_params = modpol.orgs.request_params[request.type]
if num_params == nil then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request type is invalid')
return false
end
-- num_params should equal zero at the end if request.params matches the num of params for that type
for k, v in ipairs(request.params) do
num_params = num_params - 1
end
if num_params ~= 0 then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has invalid number of parameters')
return false
end
-- checking to see if identical request already exists
for k, v in ipairs(self.requests) do
if self.comp_req(request, v) == true then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has already been made')
return false
end
end
-- checking to see if user is able to make request
local requested_policy = self.policies[request.type]
if requested_policy then
if requested_policy.must_be_member and not self:has_member(request.user) then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> user must be org member to make this request')
return false
end
end
-- linear search for empty process slots (lazy deletion)
for k, v in ipairs(self.requests) do
if v == 'deleted' then
local empty_index = k
break
end
end
-- attempts to fill empty spots in list, otherwise appends to end
local request_id = nil
if empty_index then
self.requests[empty_index] = request
request_id = empty_index
else
table.insert(self.requests, request)
-- finds end of list to return current request's id
local count = 0
for k, v in ipairs(self.requests) do
count = count + 1
end
request_id = count
end
modpol.ocutil.log("Request made")
return request_id
end