requests.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 str then
  56. str = str .. '\n' .. req.type .. ' (' .. req.user .. ') '
  57. else
  58. str = req.type .. ' (' .. req.user .. ') '
  59. end
  60. end
  61. return str
  62. end
  63. -- ================================
  64. -- tries to make a request to the org
  65. function modpol.orgs:make_request(request)
  66. -- makes sure the request has the valid number of parameters
  67. local num_params = modpol.orgs.request_params[request.type]
  68. if num_params == nil then
  69. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request type is invalid')
  70. return false
  71. end
  72. -- num_params should equal zero at the end if request.params matches the num of params for that type
  73. for k, v in ipairs(request.params) do
  74. num_params = num_params - 1
  75. end
  76. if num_params ~= 0 then
  77. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has invalid number of parameters')
  78. return false
  79. end
  80. -- checking to see if identical request already exists
  81. for k, v in ipairs(self.requests) do
  82. if self.comp_req(request, v) == true then
  83. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has already been made')
  84. return false
  85. end
  86. end
  87. -- checking to see if user is able to make request
  88. local requested_policy = self.policies[request.type]
  89. local parent_policy = modpol.orgs.get_org(self.parent).policies[request.type]
  90. -- tries to use org's policy table, defers to parent otherwise
  91. if not requested_policy then
  92. modpol.ocutil.log(request.type .. 'policy not found, deferring to parent org')
  93. requested_policy = parent_policy
  94. if not parent_policy then
  95. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined')
  96. return false
  97. end
  98. end
  99. -- make sure user is allowed to make request
  100. if requested_policy.must_be_member and not self:has_member(request.user) then
  101. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> user must be org member to make this request')
  102. return false
  103. end
  104. -- linear search for empty process slots (lazy deletion)
  105. for k, v in ipairs(self.requests) do
  106. if v == 'deleted' then
  107. local empty_index = k
  108. break
  109. end
  110. end
  111. -- attempts to fill empty spots in list, otherwise appends to end
  112. local request_id = nil
  113. if empty_index then
  114. self.requests[empty_index] = request
  115. request_id = empty_index
  116. else
  117. table.insert(self.requests, request)
  118. -- finds end of list to return current request's id
  119. local count = 0
  120. for k, v in ipairs(self.requests) do
  121. count = count + 1
  122. end
  123. request_id = count
  124. end
  125. modpol.ocutil.log("Request made")
  126. return request_id
  127. end