change_policy.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. --- change_policy
  2. -- @module change_policy
  3. local change_policy = {
  4. name = "Change policy",
  5. slug = "change_policy",
  6. desc = "Change a policy in a module",
  7. hide = false;
  8. }
  9. change_policy.data = {
  10. result = nil
  11. }
  12. change_policy.config = {
  13. approval = "none"
  14. }
  15. --- Change modules initiate
  16. -- @function change_policy:initiate
  17. -- @param result Callback if this module is embedded in other modules
  18. function change_policy:initiate(result)
  19. -- prepare module options
  20. local available_modules = {}
  21. for k,org_mod in pairs(modpol.modules) do
  22. if not org_mod.hide and
  23. self.org.policies[k] then
  24. available_modules[org_mod.slug] = modpol.util.copy_table(org_mod)
  25. end end
  26. local modules_list = {}
  27. local modules_count = 0
  28. for k,v in pairs(available_modules) do
  29. table.insert(modules_list,v.slug)
  30. modules_count = modules_count + 1
  31. end
  32. -- abort if no modules to remove
  33. if modules_count == 0 then
  34. modpol.interactions.message(
  35. self.initiator, "Org has no modules")
  36. modpol.interactions.org_dashboard(
  37. self.initiator, self.org.id)
  38. if self.data.result then self.data.result() end
  39. self.org:delete_process(self.id)
  40. return
  41. end
  42. table.sort(modules_list)
  43. -- now ask which to remove
  44. modpol.interactions.dropdown_query(
  45. self.initiator, "Choose a module to change policies for:",
  46. modules_list,
  47. function(mod_choice)
  48. local this_module = modpol.modules[mod_choice]
  49. local module_policies = this_module.config
  50. local policy_list = {}
  51. for k,v in pairs(module_policies) do
  52. table.insert(policy_list, k)
  53. end
  54. if #policy_list == 0 then
  55. -- No policies; abort
  56. modpol.interactions.message(
  57. self.initiator, "Module has no policy options")
  58. modpol.interactions.org_dashboard(
  59. self.initiator, self.org.id)
  60. if self.data.result then self.data.result() end
  61. self.org:delete_process(self.id)
  62. return
  63. end
  64. modpol.interactions.dropdown_query(
  65. self.initiator, "Choose a policy to change:",
  66. policy_list,
  67. function(policy_choice)
  68. modpol.interactions.text_query(
  69. self.initiator,
  70. "Current " .. policy_choice .. " value: " ..
  71. tostring(modpol.modules[mod_choice][policy_choice])
  72. .. "\nChange value to (be careful!):",
  73. function(policy_input)
  74. self:approve_change(
  75. mod_choice,
  76. policy_choice,
  77. policy_input)
  78. end
  79. )
  80. end
  81. )
  82. end
  83. )
  84. end
  85. --- Propose a change.
  86. -- Type "add" or "remove"
  87. -- @function change_policy:approve_change
  88. -- @param module (string) slug of module
  89. -- @param policy (string) policy slug
  90. -- @param input (string) input content
  91. function change_policy:approve_change(module_slug, policy, input)
  92. -- NEED TO ADD APPROVAL CODE for consent, etc.
  93. modpol.interactions.message(
  94. self.initiator,
  95. "Updating " .. policy .. " policy on module " ..
  96. module_slug .. " with: " .. input)
  97. self.org.policies[module_slug][policy] = input
  98. modpol.interactions.org_dashboard(
  99. self.initiator, self.org.id)
  100. if self.data.result then self.data.result() end
  101. self.org:delete_process(self.id)
  102. return
  103. end
  104. modpol.modules.change_policy = change_policy