modpol/modpol/orgs/requests.lua

192 lines
6.2 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 req ~= "deleted" then
if str then
str = str .. '\n' .. req.type .. ' (' .. req.user .. ') '
else
str = req.type .. ' (' .. req.user .. ') '
end
end
end
return str
end
-- ===============================
-- if the request was approved, the associated function is called, otherwise it is deleted
function modpol.orgs:resolve_request(request_id, approve)
if approve then
local request = self.requests[request_id]
local p = request.params
-- 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(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
self.requests[request_id] = "deleted"
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 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]
-- 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
-- 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 by " .. request.user .. " to " .. request.type)
-- launching process tied to this request
local process_id = self:create_process(requested_policy.process_type, request_id)
-- returns process id of processes launched by this request
return process_id
end