base.lua 9.4 KB

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