base.lua 9.5 KB

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