Files
modpol/modpol/orgs/requests.lua
T
2021-05-06 01:05:57 -04:00

147 lines
4.5 KiB
Lua

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(process_type, request_id)
if not modpol.modules[process_type] then
modpol.ocutil.log('Process type "' .. process_type .. '" does not exist')
return
end
-- retrieving requested module
local module = modpol.modules[process_type]
local new_process = module:new_process(request_id, self.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
return empty_index
else
table.insert(self.processes, new_process)
return #self.processes
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
-- ===============================
-- returns string of all active requests
function modpol.orgs:list_request()
local str
for id, req in ipairs(self.requests) do
if str then
str = str .. '\n' .. req.type .. ' (' .. req.user .. ') '
else
str = req.type .. ' (' .. req.user .. ') '
end
end
return str
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]
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 parent_policy then
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined')
return false
end
end
-- make sure user is allowed to make request
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
-- 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