base.lua 9.0 KB

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