requests.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. modpol.orgs.request_params = {
  2. add_org = 1,
  3. delete = 0,
  4. add_member = 1,
  5. remove_member = 1
  6. }
  7. -- ================================
  8. -- creates a new process linked to a request id
  9. function modpol.orgs:create_process(process_type, request_id)
  10. if not modpol.modules[process_type] then
  11. modpol.ocutil.log('Process type "' .. process_type .. '" does not exist')
  12. return
  13. end
  14. -- retrieving requested module
  15. local module = modpol.modules[process_type]
  16. local new_process = module:new_process(request_id, self.id)
  17. -- linear search for empty process slots (lazy deletion)
  18. for k, v in ipairs(self.processes) do
  19. if v == 'deleted' then
  20. local empty_index = k
  21. break
  22. end
  23. end
  24. -- attempts to fill empty spots in list, otherwise appends to end
  25. if empty_index then
  26. self.processes[empty_index] = new_process
  27. return empty_index
  28. else
  29. table.insert(self.processes, new_process)
  30. return #self.processes
  31. end
  32. end
  33. -- ===========================
  34. -- compares to requests to see if they are identical
  35. function modpol.orgs.comp_req(req1, req2)
  36. -- compares request type
  37. if req1.type ~= req2.type then
  38. return false
  39. else
  40. -- comparing parameters
  41. -- we can assume the number of params is the same as this is checked in the make_request func
  42. for k, v in ipairs(req1.params) do
  43. if v ~= req2.params[k] then
  44. return false
  45. end
  46. end
  47. end
  48. return true
  49. end
  50. -- ===============================
  51. -- returns string of all active requests
  52. function modpol.orgs:list_request()
  53. local str
  54. for id, req in ipairs(self.requests) do
  55. if req ~= "deleted" then
  56. if str then
  57. str = str .. '\n' .. req.type .. ' (' .. req.user .. ') '
  58. else
  59. str = req.type .. ' (' .. req.user .. ') '
  60. end
  61. end
  62. end
  63. return str
  64. end
  65. -- ===============================
  66. -- if the request was approved, the associated function is called, otherwise it is deleted
  67. function modpol.orgs:resolve_request(request_id, approve)
  68. if approve then
  69. local request = self.requests[request_id]
  70. local p = request.params
  71. -- there's probably a way to clean this up, the issue is the varying number of commands
  72. -- ex: self['add_member'](self, 'member_name')
  73. -- not sure if this is safe, more testing to do
  74. self[request.type](self, p[1], p[2], p[3])
  75. -- if request.type == "add_org" then
  76. -- self:add_org(p[1])
  77. -- elseif request.type == "delete" then
  78. -- self:delete()
  79. -- elseif request.type == "add_member" then
  80. -- self:add_member(p[1])
  81. -- elseif request.type == "remove_member" then
  82. -- self:remove_member(p[1])
  83. -- end
  84. end
  85. self.requests[request_id] = "deleted"
  86. end
  87. -- ================================
  88. -- tries to make a request to the org
  89. function modpol.orgs:make_request(request)
  90. -- makes sure the request has the valid number of parameters
  91. local num_params = modpol.orgs.request_params[request.type]
  92. if num_params == nil then
  93. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request type is invalid')
  94. return false
  95. end
  96. -- num_params should equal zero at the end if request.params matches the num of params for that type
  97. for k, v in ipairs(request.params) do
  98. num_params = num_params - 1
  99. end
  100. if num_params ~= 0 then
  101. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has invalid number of parameters')
  102. return false
  103. end
  104. -- checking to see if identical request already exists
  105. for k, v in ipairs(self.requests) do
  106. if self.comp_req(request, v) == true then
  107. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has already been made')
  108. return false
  109. end
  110. end
  111. -- checking to see if user is able to make request
  112. local requested_policy = self.policies[request.type]
  113. local parent_policy = modpol.orgs.get_org(self.parent).policies[request.type]
  114. -- tries to use org's policy table, defers to parent otherwise
  115. if not requested_policy then
  116. modpol.ocutil.log(request.type .. ' policy not found, deferring to parent org')
  117. requested_policy = parent_policy
  118. if not parent_policy then
  119. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined')
  120. return false
  121. end
  122. end
  123. -- make sure user is allowed to make request
  124. if requested_policy.must_be_member and not self:has_member(request.user) then
  125. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> user must be org member to make this request')
  126. return false
  127. end
  128. -- linear search for empty process slots (lazy deletion)
  129. for k, v in ipairs(self.requests) do
  130. if v == 'deleted' then
  131. local empty_index = k
  132. break
  133. end
  134. end
  135. -- attempts to fill empty spots in list, otherwise appends to end
  136. local request_id = nil
  137. if empty_index then
  138. self.requests[empty_index] = request
  139. request_id = empty_index
  140. else
  141. table.insert(self.requests, request)
  142. -- finds end of list to return current request's id
  143. local count = 0
  144. for k, v in ipairs(self.requests) do
  145. count = count + 1
  146. end
  147. request_id = count
  148. end
  149. modpol.ocutil.log("Request made by " .. request.user .. " to " .. request.type)
  150. -- launching process tied to this request
  151. local process_id = self:create_process(requested_policy.process_type, request_id)
  152. -- returns process id of processes launched by this request
  153. return process_id
  154. end