orgs.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. modpol.orgs = modpol.orgs or
  2. {
  3. count = 1,
  4. array = {}
  5. }
  6. -- sets modpol.orgs as its own fallback
  7. modpol.orgs.__index = modpol.orgs
  8. function temp_org()
  9. return {
  10. id = nil,
  11. name = nil,
  12. policies = {},
  13. processes = {},
  14. requests = {},
  15. request_count = 0,
  16. members = {},
  17. parent = nil,
  18. children = {}
  19. }
  20. end
  21. modpol.orgs.request_params = {
  22. add_org = 1,
  23. delete = 0,
  24. add_member = 1,
  25. remove_member = 1
  26. }
  27. -- ==================================================
  28. -- returns org when given its id or name
  29. function modpol.orgs.get_org(arg)
  30. if type(arg) == 'string' then
  31. for id, org in ipairs(modpol.orgs.array) do
  32. if org.name == arg then
  33. return org
  34. end
  35. end
  36. elseif type(arg) == 'number' then
  37. return modpol.orgs.array[arg]
  38. end
  39. return nil
  40. end
  41. -- ===============================================
  42. -- returns a table list of all org names
  43. function modpol.orgs.list_all()
  44. local org_table
  45. for k, v in ipairs(modpol.orgs.array) do
  46. if type(v) == 'table' then
  47. if org_table then
  48. table.insert(org_table, v.name)
  49. else
  50. org_table = {v.name}
  51. end
  52. end
  53. end
  54. return org_table
  55. end
  56. -- Function: modpol.orgs.user_orgs(user)
  57. -- input: user (string)
  58. -- output: table of strings of org names
  59. function modpol.orgs.user_orgs(user)
  60. local all_orgs = modpol.orgs.list_all()
  61. local user_orgs = {}
  62. for i,v in ipairs(all_orgs) do
  63. local this_table = modpol.orgs.get_org(v)
  64. if this_table:has_member(user) then
  65. table.insert(user_orgs,v)
  66. end
  67. end
  68. return user_orgs
  69. end
  70. -- ===========================================
  71. -- deletes all orgs except for the instance
  72. function modpol.orgs.reset()
  73. for id, org in ipairs(modpol.orgs.array) do
  74. if id > 1 then
  75. modpol.orgs.array[id] = nil
  76. end
  77. end
  78. modpol.orgs:record('Resetting all orgs', 'org_reset')
  79. end
  80. -- ===================================================
  81. -- initializes the instance (root org)
  82. -- can only be run once, as only one instance can exist
  83. function modpol.orgs.init_instance()
  84. local error_msg
  85. if modpol.orgs.array[1] then
  86. modpol.ocutil.log('Error: instance has already been initialized')
  87. return false
  88. end
  89. local instance = temp_org()
  90. instance.id = 1
  91. instance.name = "instance"
  92. setmetatable(instance, modpol.orgs)
  93. -- adding instance to org list
  94. modpol.orgs.array[1] = instance
  95. modpol.orgs:record('Initialized the instance org', 'create_instance')
  96. return instance
  97. end
  98. -- FUNCTIONS BEYOND HERE OPERATE ON ORG OBJECTS
  99. -- =======================================================
  100. -- records a log message to the modpol ledger
  101. function modpol.orgs:record(msg, entry_type)
  102. local entry = {
  103. timestamp = '',
  104. entry_type = nil,
  105. action_msg = '',
  106. org_name = '',
  107. org_id = nil,
  108. }
  109. if type(msg) == 'string' and not(modpol.ocutil.str_empty(msg)) then
  110. entry.action_msg = msg
  111. else
  112. modpol.ocutil.log('Error: msg must be a non empty string')
  113. return false
  114. end
  115. if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then
  116. entry.entry_type = entry_type
  117. else
  118. modpol.ocutil.log('Error: entry_type must be a non empty string')
  119. print(msg, entry_type)
  120. return false
  121. end
  122. entry.timestamp = os.time()
  123. entry.org_id = self.id
  124. entry.org_name = self.name
  125. table.insert(modpol.ledger, entry)
  126. modpol.store_data()
  127. end
  128. -- ==================================================
  129. -- adds a new sub org to the org it is called on
  130. -- ex: instance:add_org('town hall')
  131. function modpol.orgs:add_org(name)
  132. if self.id == nil then
  133. modpol.ocutil.log('Error: add_org can only be called by another org')
  134. return false
  135. end
  136. if modpol.ocutil.str_empty(name) then
  137. modpol.ocutil.log('Error: org name is required')
  138. return false
  139. end
  140. if modpol.orgs.get_org(name) then
  141. modpol.ocutil.log('Error: org name is already being used')
  142. return false
  143. end
  144. -- creating the child sub org
  145. modpol.orgs.count = modpol.orgs.count + 1
  146. local child_org = temp_org()
  147. child_org.id = modpol.orgs.count
  148. child_org.name = name
  149. child_org.parent = self.id
  150. setmetatable(child_org, modpol.orgs)
  151. -- adding child id to list of children
  152. table.insert(self.children, child_org.id)
  153. -- adding child to org list
  154. modpol.orgs.array[child_org.id] = child_org
  155. self:record('created sub org ' .. name, 'add_org')
  156. modpol.ocutil.log('Created sub org ' .. name)
  157. return child_org
  158. end
  159. -- ========================================
  160. -- recursively deletes an org and its suborgs
  161. -- leaves entry in modpol.orgs.array as a string "removed"
  162. -- note: "reason" param was removed, can be added back
  163. function modpol.orgs:delete()
  164. if self.id == 1 then
  165. modpol.ocutil.log('Error: cannot delete instance')
  166. return false
  167. end
  168. if #self.children > 0 then
  169. for i, child_id in pairs(self.children) do
  170. local child = modpol.orgs.get_org(child_id)
  171. print(child_id, child)
  172. child:delete()
  173. end
  174. end
  175. modpol.orgs.array[self.id] = 'removed'
  176. modpol.ocutil.log('Removed ' .. self.name .. ': ' .. self.id)
  177. self:record('Deleted ' .. self.name .. ' and all child orgs', 'del_org')
  178. end
  179. -- ===========================================
  180. -- internal function to get the index of a member name
  181. function modpol.orgs:get_member_index(member)
  182. for k, v in ipairs(self.members) do
  183. if v == member then
  184. return k
  185. end
  186. end
  187. end
  188. -- ===========================================
  189. -- adds a user to an org
  190. function modpol.orgs:add_member(user)
  191. -- trys to fill in empty spots first
  192. local empty_index = self:get_member_index('')
  193. if empty_index then
  194. self.members[empty_index] = user
  195. else
  196. -- adds to end if no empty spots
  197. table.insert(self.members, user)
  198. end
  199. self:record('Added member ' .. user, 'add_member')
  200. end
  201. -- =======================================
  202. -- removes a user from an org
  203. function modpol.orgs:remove_member(user)
  204. -- sets the array index to an empty string so that consecutive list is preserved
  205. -- empty spots will get filled in by new members
  206. user_index = self:get_member_index(user)
  207. if user_index then
  208. self.members[user_index] = ''
  209. end
  210. self:record('Removed member ' .. user, 'del_member')
  211. end
  212. -- ===========================================
  213. -- boolean check whether user is an org
  214. function modpol.orgs:has_member(user)
  215. user_index = self:get_member_index(user)
  216. if user_index then
  217. return true
  218. else
  219. return false
  220. end
  221. end
  222. -- ==================================
  223. -- returns a list of users in an org
  224. function modpol.orgs:list_member()
  225. local str
  226. for k, v in ipairs(self.members) do
  227. if str then
  228. str = str .. '\n' .. v
  229. else
  230. str = v
  231. end
  232. end
  233. return str
  234. end
  235. -- ===========================
  236. -- compares to requests to see if they are identical
  237. function modpol.orgs.comp_req(req1, req2)
  238. -- compares request type
  239. if req1.type ~= req2.type then
  240. return false
  241. else
  242. -- comparing parameters
  243. -- we can assume the number of params is the same as this is checked in the make_request func
  244. for k, v in ipairs(req1.params) do
  245. if v ~= req2.params[k] then
  246. return false
  247. end
  248. end
  249. end
  250. return true
  251. end
  252. -- ================================
  253. -- tries to make a request to the org
  254. function modpol.orgs:make_request(request)
  255. -- makes sure the request has the valid number of parameters
  256. local num_params = modpol.orgs.request_params[request.type]
  257. if num_params == nil then
  258. modpol.ocutil.log("Error: request type is invalid")
  259. return false
  260. end
  261. for k, v in ipairs(request.params) do
  262. num_params = num_params - 1
  263. end
  264. if num_params ~= 0 then
  265. modpol.ocutil.log("Error: request has invalid number of parameters")
  266. return false
  267. end
  268. -- checking to see if identical request already exists
  269. for k, v in ipairs(self.requests) do
  270. if self.comp_req(request, v) == true then
  271. modpol.ocutil.log("Error: request has already been made")
  272. return false
  273. end
  274. end
  275. -- use lazy deletion here, not very clean
  276. -- table.insert(self.requests, request)
  277. self.request_count = self.request_count + 1
  278. self.requests[self.request_count] = request
  279. return self.request_count
  280. end