2
0

base.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. add_org={process_type='consent', must_be_member=false},
  14. delete={process_type='consent', must_be_member=false},
  15. add_member={process_type='consent', must_be_member=false},
  16. remove_member={process_type='consent', must_be_member=false}
  17. },
  18. processes = {},
  19. -- requests = {},
  20. pending = {},
  21. members = {},
  22. parent = nil,
  23. children = {}
  24. }
  25. end
  26. -- ==================================================
  27. -- returns org when given its id or name
  28. function modpol.orgs.get_org(arg)
  29. if type(arg) == 'string' then
  30. for id, org in ipairs(modpol.orgs.array) do
  31. if org.name == arg then
  32. return org
  33. end
  34. end
  35. elseif type(arg) == 'number' then
  36. return modpol.orgs.array[arg]
  37. end
  38. return nil
  39. end
  40. -- ===============================================
  41. -- returns a table list of all org names
  42. function modpol.orgs.list_all()
  43. local org_table
  44. for k, v in ipairs(modpol.orgs.array) do
  45. if type(v) == 'table' then
  46. if org_table then
  47. table.insert(org_table, v.name)
  48. else
  49. org_table = {v.name}
  50. end
  51. end
  52. end
  53. return org_table
  54. end
  55. -- Function: modpol.orgs.user_orgs(user)
  56. -- input: user (string)
  57. -- output: 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. -- ===========================================
  70. -- deletes all orgs except for the instance
  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. -- ===================================================
  83. -- initializes the instance (root org)
  84. -- can only be run once, as only one instance can exist
  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. -- =======================================================
  103. -- records a log message to the modpol ledger
  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. -- ==================================================
  132. -- adds a new sub org to the org it is called on
  133. -- input: name (string), user (string)
  134. -- ex: instance:add_org('town hall')
  135. function modpol.orgs:add_org(name, user)
  136. if self.id == nil then
  137. modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> add_org can only be called by another org')
  138. return false
  139. end
  140. if modpol.ocutil.str_empty(name) then
  141. modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> org name is required')
  142. return false
  143. end
  144. if modpol.orgs.get_org(name) then
  145. modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> org name is already being used')
  146. return false
  147. end
  148. -- creating the child sub org
  149. modpol.orgs.count = modpol.orgs.count + 1
  150. local child_org = temp_org()
  151. child_org.id = modpol.orgs.count
  152. child_org.name = name
  153. child_org.parent = self.id
  154. child_org.processes = self.processes
  155. setmetatable(child_org, modpol.orgs)
  156. -- adding child id to list of children
  157. table.insert(self.children, child_org.id)
  158. -- adding child to org list
  159. modpol.orgs.array[child_org.id] = child_org
  160. -- adding creator of org as the first member
  161. child_org:add_member(user)
  162. self:record('created sub org ' .. name, 'add_org')
  163. modpol.ocutil.log('Created ' .. name .. ' (suborg of ' .. self.name .. ')')
  164. return child_org
  165. end
  166. -- ========================================
  167. -- recursively deletes an org and its suborgs
  168. -- leaves entry in modpol.orgs.array as a string "removed"
  169. -- note: "reason" param was removed, can be added back
  170. function modpol.orgs:delete()
  171. if self.id == 1 then
  172. modpol.ocutil.log('Error in ' .. self.name .. ':delete -> cannot delete instance')
  173. return false
  174. end
  175. if #self.children > 0 then
  176. for i, child_id in pairs(self.children) do
  177. local child = modpol.orgs.get_org(child_id)
  178. modpol.ocutil.log(child_id, child)
  179. child:delete()
  180. end
  181. end
  182. modpol.orgs.array[self.id] = 'removed'
  183. modpol.ocutil.log('Deleted org ' .. self.name .. ': ' .. self.id)
  184. self:record('Deleted ' .. self.name .. ' and all child orgs', 'del_org')
  185. end
  186. -- ===========================================
  187. -- internal function to get the index of a member name
  188. function modpol.orgs:get_member_index(member)
  189. for k, v in ipairs(self.members) do
  190. if v == member then
  191. return k
  192. end
  193. end
  194. end
  195. -- ===========================================
  196. -- adds a user to an org
  197. function modpol.orgs:add_member(user)
  198. for id, name in ipairs(self.members) do
  199. if user == name then
  200. modpol.ocutil.log('Error in ' .. self.name .. ':add_member -> user already in org')
  201. return false
  202. end
  203. end
  204. -- trys to fill in empty spots first
  205. local empty_index = self:get_member_index('')
  206. if empty_index then
  207. self.members[empty_index] = user
  208. else
  209. -- adds to end if no empty spots
  210. table.insert(self.members, user)
  211. end
  212. modpol.ocutil.log('Added member ' .. user .. ' to ' .. self.name)
  213. self:record('Added member ' .. user, 'add_member')
  214. end
  215. -- =======================================
  216. -- removes a user from an org
  217. function modpol.orgs:remove_member(user)
  218. -- sets the array index to an empty string so that consecutive list is preserved
  219. -- empty spots will get filled in by new members
  220. local user_index = self:get_member_index(user)
  221. if user_index then
  222. self.members[user_index] = ''
  223. else
  224. modpol.ocutil.log('Error in ' .. self.name .. ':remove_member -> user not in org')
  225. end
  226. modpol.ocutil.log('Removed member ' .. user .. ' from ' .. self.name)
  227. self:record('Removed member ' .. user, 'del_member')
  228. end
  229. -- ===========================================
  230. -- boolean check whether user is an org
  231. function modpol.orgs:has_member(user)
  232. local user_index = self:get_member_index(user)
  233. if user_index then
  234. return true
  235. else
  236. return false
  237. end
  238. end
  239. -- ==================================
  240. -- Function: modpol.orgs:list_members()
  241. -- output: a table of the names (string) of members
  242. function modpol.orgs:list_members()
  243. local members = {}
  244. for k, v in ipairs(self.members) do
  245. table.insert(members, v)
  246. end
  247. return members
  248. end
  249. -- ==============================
  250. -- because member list uses lazy deletion, using #org.members will not show an accurate number
  251. function modpol.orgs:get_member_count()
  252. local count = 0
  253. for k, v in ipairs(self.members) do
  254. -- the empty string represents a deleted member in the members list
  255. if v ~= '' then
  256. count = count + 1
  257. end
  258. end
  259. return count
  260. end
  261. -- ====================================
  262. -- adds a new policy to the policy table
  263. -- must define the policy type, process associated with it, and whether the request must be made by an org member
  264. function modpol.orgs:set_policy(policy_type, process_type, must_be_member)
  265. local new_policy = {
  266. process_type = process_type,
  267. must_be_member = must_be_member
  268. }
  269. self.policies[policy_type] = new_policy
  270. modpol.ocutil.log('Added policy for ' .. policy_type .. ' in ' .. self.name)
  271. self:record('Added policy for ' .. policy_type, 'set_policy')
  272. end