base.lua 9.2 KB

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