base.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.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 = "instance"
  88. setmetatable(instance, modpol.orgs)
  89. -- adding instance to org list
  90. modpol.orgs.array[1] = instance
  91. modpol.ocutil.log('Initialized the instance org')
  92. modpol.orgs:record('Initialized the instance 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. -- ex: instance:add_org('town hall')
  128. function modpol.orgs:add_org(name, user)
  129. if self.id == nil then
  130. modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> add_org can only be called by another org')
  131. return false
  132. end
  133. if modpol.ocutil.str_empty(name) then
  134. modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> org name is required')
  135. return false
  136. end
  137. if modpol.orgs.get_org(name) then
  138. modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> org name is already being used')
  139. return false
  140. end
  141. -- creating the child sub org
  142. modpol.orgs.count = modpol.orgs.count + 1
  143. local child_org = temp_org()
  144. child_org.id = modpol.orgs.count
  145. child_org.name = name
  146. child_org.parent = self.id
  147. setmetatable(child_org, modpol.orgs)
  148. -- adding child id to list of children
  149. table.insert(self.children, child_org.id)
  150. -- adding child to org list
  151. modpol.orgs.array[child_org.id] = child_org
  152. -- adding creator of org as the first member
  153. child_org:add_member(user)
  154. self:record('created sub org ' .. name, 'add_org')
  155. modpol.ocutil.log('Created ' .. name .. ' (suborg of ' .. self.name .. ')')
  156. return child_org
  157. end
  158. -- ========================================
  159. -- recursively deletes an org and its suborgs
  160. -- leaves entry in modpol.orgs.array as a string "removed"
  161. -- note: "reason" param was removed, can be added back
  162. function modpol.orgs:delete()
  163. if self.id == 1 then
  164. modpol.ocutil.log('Error in ' .. self.name .. ':delete -> cannot delete instance')
  165. return false
  166. end
  167. if #self.children > 0 then
  168. for i, child_id in pairs(self.children) do
  169. local child = modpol.orgs.get_org(child_id)
  170. modpol.ocutil.log(child_id, child)
  171. child:delete()
  172. end
  173. end
  174. modpol.orgs.array[self.id] = 'removed'
  175. modpol.ocutil.log('Deleted org ' .. self.name .. ': ' .. self.id)
  176. self:record('Deleted ' .. self.name .. ' and all child orgs', 'del_org')
  177. end
  178. -- ===========================================
  179. -- internal function to get the index of a member name
  180. function modpol.orgs:get_member_index(member)
  181. for k, v in ipairs(self.members) do
  182. if v == member then
  183. return k
  184. end
  185. end
  186. end
  187. -- ===========================================
  188. -- adds a user to an org
  189. function modpol.orgs:add_member(user)
  190. for id, name in ipairs(self.members) do
  191. if user == name then
  192. modpol.ocutil.log('Error in ' .. self.name .. ':add_member -> user already in org')
  193. return false
  194. end
  195. end
  196. -- trys to fill in empty spots first
  197. local empty_index = self:get_member_index('')
  198. if empty_index then
  199. self.members[empty_index] = user
  200. else
  201. -- adds to end if no empty spots
  202. table.insert(self.members, user)
  203. end
  204. modpol.ocutil.log('Added member ' .. user .. ' to ' .. self.name)
  205. self:record('Added member ' .. user, 'add_member')
  206. end
  207. -- =======================================
  208. -- removes a user from an org
  209. function modpol.orgs:remove_member(user)
  210. -- sets the array index to an empty string so that consecutive list is preserved
  211. -- empty spots will get filled in by new members
  212. local user_index = self:get_member_index(user)
  213. if user_index then
  214. self.members[user_index] = ''
  215. else
  216. modpol.ocutil.log('Error in ' .. self.name .. ':remove_member -> user not in org')
  217. end
  218. modpol.ocutil.log('Removed member ' .. user .. ' from ' .. self.name)
  219. self:record('Removed member ' .. user, 'del_member')
  220. end
  221. -- ===========================================
  222. -- boolean check whether user is an org
  223. function modpol.orgs:has_member(user)
  224. local user_index = self:get_member_index(user)
  225. if user_index then
  226. return true
  227. else
  228. return false
  229. end
  230. end
  231. -- ==================================
  232. -- returns a list of users in an org
  233. function modpol.orgs:list_member()
  234. local str
  235. for k, v in ipairs(self.members) do
  236. -- checking to see if member name is valid
  237. if str and str ~= '' then
  238. str = str .. '\n' .. v
  239. else
  240. str = v
  241. end
  242. end
  243. return str
  244. end
  245. -- ==============================
  246. -- because member list uses lazy deletion, using #org.members will not show an accurate number
  247. function modpol.orgs:get_member_count()
  248. local count = 0
  249. for k, v in ipairs(self.members) do
  250. -- the empty string represents a deleted member in the members list
  251. if v ~= '' then
  252. count = count + 1
  253. end
  254. end
  255. return count
  256. end
  257. -- ====================================
  258. -- adds a new policy to the policy table
  259. -- must define the policy type, process associated with it, and whether the request must be made by an org member
  260. function modpol.orgs:set_policy(policy_type, process_type, must_be_member)
  261. local new_policy = {
  262. process_type = process_type,
  263. must_be_member = must_be_member
  264. }
  265. self.policies[policy_type] = new_policy
  266. modpol.ocutil.log('Added policy for ' .. policy_type .. ' in ' .. self.name)
  267. end