2
0

orgs.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. modpol.orgs =
  2. {
  3. count = 1,
  4. array = {}
  5. }
  6. -- sets modpol.orgs as it's own fallback
  7. modpol.orgs.__index = modpol.orgs
  8. -- ==================================================
  9. -- returns org when given its id or name
  10. function modpol.orgs.get_org(arg)
  11. if type(arg) == 'string' then
  12. for id, org in ipairs(modpol.orgs.array) do
  13. if org.name == arg then
  14. return org
  15. end
  16. end
  17. elseif type(arg) == 'number' then
  18. return modpol.orgs.array[arg]
  19. end
  20. return nil
  21. end
  22. -- ===============================================
  23. -- returns a string list of all orgs
  24. function modpol.orgs.list_all()
  25. local str
  26. for k, v in ipairs(modpol.orgs.array) do
  27. if type(v) == 'table' then
  28. if str then
  29. str = str .. '\n' .. v.name
  30. else
  31. str = v.name
  32. end
  33. end
  34. end
  35. return str
  36. end
  37. -- ===========================================
  38. -- deletes all orgs except for the instance
  39. function modpol.orgs.reset()
  40. for id, org in ipairs(modpol.orgs.array) do
  41. if id > 1 then
  42. modpol.orgs.array[id] = nil
  43. end
  44. end
  45. end
  46. -- ===================================================
  47. -- initializes the instance (root org)
  48. -- can only be run once, as only one instance can exist
  49. function modpol.orgs.init_instance()
  50. local error_msg
  51. if modpol.orgs.array[1] then
  52. error_msg = 'Error: instance has already been initialized'
  53. modpol.ocutil.log(error_msg)
  54. return false, error_msg
  55. end
  56. local instance = {
  57. id = 1,
  58. name = "instance",
  59. policies = {},
  60. members = {},
  61. ledger = {},
  62. parent = nil,
  63. children = {},
  64. properties = {},
  65. old_names = {}
  66. }
  67. setmetatable(instance, modpol.orgs)
  68. -- adding instance to org list
  69. modpol.orgs.array[1] = instance
  70. return instance
  71. end
  72. -- FUNCTIONS BEYOND HERE OPERATE ON ORG OBJECTS
  73. -- =======================================================
  74. -- records a log message to the modpol ledger
  75. function modpol.orgs:record(msg, entry_type)
  76. local entry = {
  77. timestamp = '',
  78. entry_type = nil,
  79. action_msg = '',
  80. org_name = '',
  81. org_id = nil,
  82. }
  83. if type(msg) == 'string' and not(modpol.ocutil.str_empty(msg)) then
  84. entry.action_msg = msg
  85. else
  86. print('Error: msg must be a non empty string')
  87. return false
  88. end
  89. if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then
  90. entry.entry_type = entry_type
  91. else
  92. print('Error: entry_type must be a non empty string')
  93. return false
  94. end
  95. entry.timestamp = os.time()
  96. entry.org_id = self.id
  97. entry.org_name = self.name
  98. table.insert(modpol.ledger, entry)
  99. modpol.store_data()
  100. end
  101. -- ==================================================
  102. -- adds a new sub org to the org it is called on
  103. -- ex: instance:add_org('town hall')
  104. function modpol.orgs:add_org(name)
  105. if self.id == nil then
  106. error_msg = 'Error: add_org can only be called by another org'
  107. modpol.ocutil.log(error_msg)
  108. return false, error_msg
  109. end
  110. if modpol.ocutil.str_empty(name) then
  111. error_msg = 'Error: org name is required'
  112. modpol.ocutil.log(error_msg)
  113. return false, error_msg
  114. end
  115. -- creating the child sub org
  116. modpol.orgs.count = modpol.orgs.count + 1
  117. local child_org = {
  118. id = modpol.orgs.count,
  119. name = name,
  120. policies = {},
  121. members = {},
  122. parent = self.id,
  123. children = {},
  124. }
  125. setmetatable(child_org, modpol.orgs)
  126. -- adding child id to list of children
  127. table.insert(self.children, child_org.id)
  128. -- adding child to org list
  129. modpol.orgs.array[child_org.id] = child_org
  130. return child_org
  131. end
  132. -- ========================================
  133. -- recursively deletes an org and its suborgs
  134. -- leaves entry in modpol.orgs.array as a string "removed"
  135. -- note: "reason" param was removed, can be added back
  136. function modpol.orgs:delete()
  137. if self.id == 1 then
  138. return false, 'Error: cannot delete instance'
  139. end
  140. if #self.children > 0 then
  141. for i, child_id in pairs(self.children) do
  142. local child = modpol.orgs.get_org(child_id)
  143. print(child_id, child)
  144. child:delete()
  145. end
  146. end
  147. modpol.orgs.array[self.id] = 'removed'
  148. print('Removed ' .. self.name .. ': ' .. self.id)
  149. end
  150. -- ===========================================
  151. -- internal function to get the index of a member name
  152. function modpol.orgs:get_member_index(member)
  153. for k, v in ipairs(self.members) do
  154. if v == member then
  155. return k
  156. end
  157. end
  158. end
  159. -- ===========================================
  160. -- adds a user to an org
  161. function modpol.orgs:add_member(user)
  162. -- trys to fill in empty spots first
  163. empty_index = self:get_member_index('')
  164. if empty_index then
  165. self.members[empty_index] = user
  166. else
  167. -- adds to end if no empty spots
  168. table.insert(self.members, user)
  169. end
  170. end
  171. -- =======================================
  172. -- removes a user from an org
  173. function modpol.orgs:remove_member(user)
  174. -- sets the array index to an empty string so that consecutive list is preserved
  175. -- empty spots will get filled in by new members
  176. user_index = self:get_member_index(user)
  177. if user_index then
  178. self.members[user_index] = ''
  179. end
  180. end
  181. -- ===========================================
  182. -- boolean check whether user is an org
  183. function modpol.orgs:has_member(user)
  184. user_index = self:get_member_index(user)
  185. if user_index then
  186. return true
  187. else
  188. return false
  189. end
  190. end
  191. -- ==================================
  192. -- returns a list of users in an org
  193. function modpol.orgs:list_member()
  194. local str
  195. for k, v in ipairs(self.members) do
  196. if str then
  197. str = str .. '\n' .. v
  198. else
  199. str = v
  200. end
  201. end
  202. return str
  203. end