change_policy.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = false
  11. }
  12. change_policy.config = {
  13. approval_module = false
  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 self.org.policies[k] then
  23. available_modules[org_mod.slug] = modpol.util.copy_table(org_mod)
  24. end end
  25. local modules_list = {}
  26. local modules_count = 0
  27. for k,v in pairs(available_modules) do
  28. table.insert(modules_list,v.slug)
  29. modules_count = modules_count + 1
  30. end
  31. -- abort if no modules to remove
  32. if modules_count == 0 then
  33. modpol.interactions.message(
  34. self.initiator, "Org has no modules")
  35. modpol.interactions.org_dashboard(
  36. self.initiator, self.org.id)
  37. if self.data.result then self.data.result() end
  38. self.org:delete_process(self.id)
  39. return
  40. end
  41. table.sort(modules_list)
  42. -- now ask which to remove
  43. modpol.interactions.dropdown_query(
  44. self.initiator, "Choose a module to change policies for:",
  45. modules_list,
  46. function(mod_choice)
  47. local this_module = modpol.modules[mod_choice]
  48. local module_policies = this_module.config
  49. local policy_list = {}
  50. for k,v in pairs(module_policies) do
  51. table.insert(policy_list, k)
  52. end
  53. if #policy_list == 0 then
  54. -- No policies; abort
  55. modpol.interactions.message(
  56. self.initiator, "Module has no policy options")
  57. modpol.interactions.org_dashboard(
  58. self.initiator, self.org.id)
  59. if self.data.result then self.data.result() end
  60. self.org:delete_process(self.id)
  61. return
  62. end
  63. local readable_value =
  64. tostring(modpol.modules[mod_choice][policy_choice])
  65. if readable_value == "nil" then
  66. readable_value = "none"
  67. end
  68. modpol.interactions.dropdown_query(
  69. self.initiator, "Choose a policy to change:",
  70. policy_list,
  71. function(policy_choice)
  72. modpol.interactions.text_query(
  73. self.initiator,
  74. "Current " .. policy_choice .. " value: "
  75. .. readable_value
  76. .. "\nChange value to (or leave blank):",
  77. function(policy_input)
  78. if policy_input == "" then
  79. policy_input = false
  80. end
  81. self:approve_change(
  82. mod_choice,
  83. policy_choice,
  84. policy_input)
  85. end
  86. )
  87. end
  88. )
  89. end
  90. )
  91. end
  92. --- Propose a change.
  93. -- Type "add" or "remove"
  94. -- @function change_policy:approve_change
  95. -- @param module (string) slug of module
  96. -- @param policy (string) policy slug
  97. -- @param input (string) input content
  98. function change_policy:approve_change(module_slug, policy, input)
  99. self.org:call_module(
  100. self.config.approval_module,
  101. self.initiator,
  102. {prompt = "Update " .. policy .. " policy on module " ..
  103. module_slug .. " with: " .. input .. " ?"},
  104. function()
  105. modpol.interactions.message(
  106. self.initiator,
  107. "Updating " .. policy .. " policy on module " ..
  108. module_slug .. " with: " .. input)
  109. self.org.policies[module_slug][policy] = input
  110. modpol.interactions.org_dashboard(
  111. self.initiator, self.org.id)
  112. if self.data.result then self.data.result() end
  113. self.org:delete_process(self.id)
  114. end,
  115. self.id
  116. )
  117. end
  118. modpol.modules.change_policy = change_policy