requests.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(request_id)
  10. local new_process = {
  11. object = "I am a process",
  12. request_id = request_id
  13. }
  14. -- linear search for empty process slots (lazy deletion)
  15. for k, v in ipairs(self.processes) do
  16. if v == 'deleted' then
  17. local empty_index = k
  18. break
  19. end
  20. end
  21. -- attempts to fill empty spots in list, otherwise appends to end
  22. if empty_index then
  23. self.processes[empty_index] = new_process
  24. else
  25. table.insert(self.processes, new_process)
  26. end
  27. end
  28. -- ===========================
  29. -- compares to requests to see if they are identical
  30. function modpol.orgs.comp_req(req1, req2)
  31. -- compares request type
  32. if req1.type ~= req2.type then
  33. return false
  34. else
  35. -- comparing parameters
  36. -- we can assume the number of params is the same as this is checked in the make_request func
  37. for k, v in ipairs(req1.params) do
  38. if v ~= req2.params[k] then
  39. return false
  40. end
  41. end
  42. end
  43. return true
  44. end
  45. -- ===============================
  46. -- returns string of all active requests
  47. function modpol.orgs:list_request()
  48. local str
  49. for id, req in ipairs(self.requests) do
  50. if str then
  51. str = str .. '\n' .. req.type .. ' (' .. req.user .. ') '
  52. else
  53. str = req.type .. ' (' .. req.user .. ') '
  54. end
  55. end
  56. return str
  57. end
  58. -- ================================
  59. -- tries to make a request to the org
  60. function modpol.orgs:make_request(request)
  61. -- makes sure the request has the valid number of parameters
  62. local num_params = modpol.orgs.request_params[request.type]
  63. if num_params == nil then
  64. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request type is invalid')
  65. return false
  66. end
  67. -- num_params should equal zero at the end if request.params matches the num of params for that type
  68. for k, v in ipairs(request.params) do
  69. num_params = num_params - 1
  70. end
  71. if num_params ~= 0 then
  72. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has invalid number of parameters')
  73. return false
  74. end
  75. -- checking to see if identical request already exists
  76. for k, v in ipairs(self.requests) do
  77. if self.comp_req(request, v) == true then
  78. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has already been made')
  79. return false
  80. end
  81. end
  82. -- checking to see if user is able to make request
  83. local requested_policy = self.policies[request.type]
  84. local parent_policy = modpol.orgs.get_org(self.parent).policies[request.type]
  85. -- tries to use org's policy table, defers to parent otherwise
  86. if not requested_policy then
  87. modpol.ocutil.log(request.type .. 'policy not found, deferring to parent org')
  88. requested_policy = parent_policy
  89. if not parent_policy then
  90. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined')
  91. return false
  92. end
  93. end
  94. -- make sure user is allowed to make request
  95. if requested_policy.must_be_member and not self:has_member(request.user) then
  96. modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> user must be org member to make this request')
  97. return false
  98. end
  99. -- linear search for empty process slots (lazy deletion)
  100. for k, v in ipairs(self.requests) do
  101. if v == 'deleted' then
  102. local empty_index = k
  103. break
  104. end
  105. end
  106. -- attempts to fill empty spots in list, otherwise appends to end
  107. local request_id = nil
  108. if empty_index then
  109. self.requests[empty_index] = request
  110. request_id = empty_index
  111. else
  112. table.insert(self.requests, request)
  113. -- finds end of list to return current request's id
  114. local count = 0
  115. for k, v in ipairs(self.requests) do
  116. count = count + 1
  117. end
  118. request_id = count
  119. end
  120. modpol.ocutil.log("Request made")
  121. return request_id
  122. end