base.lua 9.0 KB

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