base.lua 9.0 KB

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