base.lua 8.2 KB

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