decidim-voca 0.0.6 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +161 -161
  3. data/README.md +42 -133
  4. data/Rakefile +46 -39
  5. data/app/cells/decidim/footer_topics/show_overrides.erb +8 -0
  6. data/app/cells/decidim/meetings/dates_and_map/show_overrides.erb +23 -0
  7. data/app/commands/decidim/voca/admin/code_census/save_code_list.rb +34 -0
  8. data/app/commands/decidim/voca/code_census/confirm_code_authorization.rb +16 -0
  9. data/app/commands/decidim/voca/create_editor_file.rb +36 -0
  10. data/app/commands/decidim/voca/rack_attack_configurator.rb +244 -0
  11. data/app/commands/decidim/voca/sync_redis_routing.rb +120 -0
  12. data/app/commands/decidim/voca/user_fields_configurator.rb +40 -0
  13. data/app/controllers/decidim/voca/admin/application_controller.rb +4 -6
  14. data/app/controllers/decidim/voca/admin/code_census/code_lists_controller.rb +40 -0
  15. data/app/controllers/decidim/voca/application_controller.rb +4 -6
  16. data/app/controllers/decidim/voca/code_census/authorizations_controller.rb +53 -0
  17. data/app/controllers/decidim/voca/editor_files_controller.rb +47 -0
  18. data/app/controllers/decidim/voca/geolocation_controller.rb +32 -0
  19. data/app/forms/decidim/voca/admin/code_census/code_list_form.rb +21 -0
  20. data/app/forms/decidim/voca/admin/organization_config_form.rb +24 -0
  21. data/app/forms/decidim/voca/code_census/code_form.rb +41 -0
  22. data/app/forms/decidim/voca/editor_file_form.rb +18 -0
  23. data/app/jobs/decidim/voca/machine_translate_awesome_menu_label_job.rb +63 -0
  24. data/app/jobs/decidim/voca/machine_translate_component_setting_job.rb +58 -0
  25. data/app/jobs/decidim/voca/machine_translate_content_block_setting_job.rb +85 -0
  26. data/app/models/decidim/voca/conversation_uuid.rb +18 -0
  27. data/app/models/decidim/voca/editor_file.rb +18 -0
  28. data/app/models/decidim/voca/validation_code.rb +13 -0
  29. data/app/overrides/admin_add_machine_translation_flag.rb +40 -0
  30. data/app/overrides/insert_assets.rb +26 -0
  31. data/app/overrides/insert_weglot_scripts.rb +60 -0
  32. data/app/overrides/proposal_camera.rb +18 -0
  33. data/app/overrides/show_votes.rb +27 -0
  34. data/app/overrides/voca_footer.rb +52 -0
  35. data/app/packs/entrypoints/admin-decidim-voca.js +2 -0
  36. data/app/packs/entrypoints/admin-decidim-voca.scss +1 -0
  37. data/app/packs/entrypoints/decidim-voca-fixes.scss +1 -0
  38. data/app/packs/entrypoints/decidim-voca.js +1 -0
  39. data/app/packs/entrypoints/decidim-voca.scss +1 -0
  40. data/app/packs/images/voca-logo-primary-x3.png +0 -0
  41. data/app/packs/src/decidim/direct_uploads/upload_field.js +150 -0
  42. data/app/packs/src/decidim/voca/admin/voca.js +16 -0
  43. data/app/packs/src/decidim/voca/proposal_better_ux/use_camera_button.js +39 -0
  44. data/app/packs/src/decidim/voca/proposal_better_ux/utils/get_current_position.js +21 -0
  45. data/app/packs/src/decidim/voca/proposal_better_ux/utils/get_proposal_form.js +13 -0
  46. data/app/packs/src/decidim/voca/proposal_better_ux/utils/loading.js +23 -0
  47. data/app/packs/src/decidim/voca/proposal_better_ux/utils/locate_coordinates.js +23 -0
  48. data/app/packs/src/decidim/voca/proposal_custom_fields/custom_field_renderer_override.js +152 -0
  49. data/app/packs/src/decidim/voca/proposal_custom_fields/editor_budget_field.js +329 -0
  50. data/app/packs/src/decidim/voca/proposal_custom_fields/editor_upload_field.js +85 -0
  51. data/app/packs/src/decidim/voca/proposal_custom_fields/proposal_custom_fields.js +96 -0
  52. data/app/packs/src/decidim/voca/proposal_custom_fields/render_budget_field.js +76 -0
  53. data/app/packs/src/decidim/voca/proposal_fields/render_budget_field.js +76 -0
  54. data/app/packs/src/decidim/voca/questionnaire/scroll_questionnaire_steps_to_top.js +18 -0
  55. data/app/packs/src/decidim/voca/voca.js +4 -0
  56. data/app/packs/src/decidim/voca/weglot/index.js +13 -0
  57. data/app/packs/src/decidim/voca/weglot/start_weglot.js +112 -0
  58. data/app/packs/src/decidim/voca/weglot/switcher.js +92 -0
  59. data/app/packs/src/decidim/voca/weglot/wait_for_weglot.js +10 -0
  60. data/app/packs/stylesheets/decidim/voca/_custom_proposal_fields.scss +111 -0
  61. data/app/packs/stylesheets/decidim/voca/_decidim_voca.scss +5 -0
  62. data/app/packs/stylesheets/decidim/voca/_decidim_voca_fixes.scss +25 -0
  63. data/app/packs/stylesheets/decidim/voca/_primary_footer.scss +77 -0
  64. data/app/packs/stylesheets/decidim/voca/_proposal_camera.scss +14 -0
  65. data/app/packs/stylesheets/decidim/voca/_weglot.scss +61 -0
  66. data/app/packs/stylesheets/decidim/voca/admin/_decidim_voca.scss +5 -0
  67. data/app/packs/stylesheets/decidim/voca/admin/_full_machine_translation.scss +34 -0
  68. data/app/permissions/decidim/voca/admin/permissions.rb +31 -0
  69. data/app/permissions/decidim/voca/permissions.rb +17 -0
  70. data/app/views/decidim/voca/admin/code_census/authorizations/new.html.erb +33 -0
  71. data/app/views/decidim/voca/admin/code_census/code_lists/edit.html.erb +27 -0
  72. data/app/views/decidim/voca/code_census/authorizations/new.html.erb +24 -0
  73. data/app/views/decidim/voca/deepl/overrides/_admin_machine_translation_alert.html.erb +14 -0
  74. data/app/views/decidim/voca/rack_attack/_blocked.html.erb +151 -0
  75. data/app/views/decidim/voca/weglot/_js_configuration.html.erb +14 -0
  76. data/app/views/decidim/voca/weglot/_locale_switcher.html.erb +14 -0
  77. data/app/views/decidim/voca/weglot/_locale_switcher_mobile.erb +14 -0
  78. data/app/views/decidim/voca/weglot/_topbar_locale_switcher.html.erb +14 -0
  79. data/config/assets.rb +12 -0
  80. data/config/i18n-tasks.yml +14 -5
  81. data/config/locales/cs.yml +94 -0
  82. data/config/locales/custom_fields.cs.yml +30 -0
  83. data/config/locales/custom_fields.en.yml +41 -0
  84. data/config/locales/custom_fields.fr.yml +30 -0
  85. data/config/locales/custom_fields.it.yml +30 -0
  86. data/config/locales/en.yml +97 -4
  87. data/config/locales/fr.yml +102 -0
  88. data/config/locales/it.yml +94 -0
  89. data/lib/decidim/voca/awesome_menu_label_machine_translation.rb +73 -0
  90. data/lib/decidim/voca/awesome_menu_labels.rb +28 -0
  91. data/lib/decidim/voca/code_census/admin_engine.rb +22 -0
  92. data/lib/decidim/voca/code_census/engine.rb +29 -0
  93. data/lib/decidim/voca/code_census/workflow.rb +7 -0
  94. data/lib/decidim/voca/code_census.rb +4 -0
  95. data/lib/decidim/voca/component_setting_manifest.rb +27 -0
  96. data/lib/decidim/voca/component_setting_pending_locales.rb +33 -0
  97. data/lib/decidim/voca/component_translated_settings_machine_translation.rb +86 -0
  98. data/lib/decidim/voca/configuration.rb +23 -0
  99. data/lib/decidim/voca/content_block_setting_manifest.rb +67 -0
  100. data/lib/decidim/voca/content_block_translated_settings_machine_translation.rb +100 -0
  101. data/lib/decidim/voca/deepl/deepl_active_job_context.rb +27 -0
  102. data/lib/decidim/voca/deepl/deepl_context.rb +78 -0
  103. data/lib/decidim/voca/deepl/deepl_form_builder_overrides.rb +82 -0
  104. data/lib/decidim/voca/deepl/deepl_machine_translator.rb +61 -0
  105. data/lib/decidim/voca/deepl/deepl_middleware.rb +83 -0
  106. data/lib/decidim/voca/deepl/engine_config.rb +181 -0
  107. data/lib/decidim/voca/deepl/settings.rb +75 -0
  108. data/lib/decidim/voca/deepl/translation_bar_overrides.rb +20 -0
  109. data/lib/decidim/voca/deepl.rb +10 -0
  110. data/lib/decidim/voca/engine.rb +320 -11
  111. data/lib/decidim/voca/export/comment_serializer_localized_csv.rb +34 -0
  112. data/lib/decidim/voca/export/csv_with_locale_transformer.rb +53 -0
  113. data/lib/decidim/voca/export/proposal_serializer_localized_csv.rb +45 -0
  114. data/lib/decidim/voca/export/user_answers_serializer_localized_csv.rb +54 -0
  115. data/lib/decidim/voca/installation.rb +33 -0
  116. data/lib/decidim/voca/machine_translation/translate_string.rb +121 -0
  117. data/lib/decidim/voca/machine_translation_human_source.rb +29 -0
  118. data/lib/decidim/voca/machine_translation_resource_job_voca.rb +65 -0
  119. data/lib/decidim/voca/organization_voca_extensions.rb +31 -0
  120. data/lib/decidim/voca/overrides/csv_export_serializers.rb +51 -0
  121. data/lib/decidim/voca/overrides/meetings/dates_and_map_cell_overrides.rb +20 -0
  122. data/lib/decidim/voca/overrides/menu_breadcrumb_last_activity_cache.rb +31 -0
  123. data/lib/decidim/voca/overrides/mod_secure/conversation_controller_overrides.rb +19 -0
  124. data/lib/decidim/voca/overrides/mod_secure/conversation_sanitize.rb +29 -0
  125. data/lib/decidim/voca/overrides/mod_secure/conversation_uuid.rb +34 -0
  126. data/lib/decidim/voca/overrides/mod_secure/user_conversations_redirect_overrides.rb +53 -0
  127. data/lib/decidim/voca/overrides/mod_secure/user_profile_verification_override.rb +31 -0
  128. data/lib/decidim/voca/overrides/newsletter_helper_overrides.rb +30 -0
  129. data/lib/decidim/voca/overrides/resource_presenter_overrides.rb +30 -0
  130. data/lib/decidim/voca/overrides/sanitize_helper_overrides.rb +39 -0
  131. data/lib/decidim/voca/sync_locales/README.md +13 -0
  132. data/lib/decidim/voca/sync_locales/awesome_menu_label_sync.rb +89 -0
  133. data/lib/decidim/voca/sync_locales/command.rb +54 -0
  134. data/lib/decidim/voca/sync_locales/component_setting_sync.rb +72 -0
  135. data/lib/decidim/voca/sync_locales/content_block_setting_sync.rb +86 -0
  136. data/lib/decidim/voca/sync_locales/field_hash_normalizer.rb +98 -0
  137. data/lib/decidim/voca/sync_locales/locale_context.rb +589 -0
  138. data/lib/decidim/voca/sync_locales/machine_translation_enqueuer.rb +60 -0
  139. data/lib/decidim/voca/sync_locales/relation_lint_runner.rb +55 -0
  140. data/lib/decidim/voca/sync_locales/runner.rb +352 -0
  141. data/lib/decidim/voca/sync_locales.rb +11 -0
  142. data/lib/decidim/voca/update_column_without_callbacks.rb +21 -0
  143. data/lib/decidim/voca/version.rb +13 -3
  144. data/lib/decidim/voca.rb +40 -14
  145. data/lib/decidim-voca.rb +5 -0
  146. data/lib/tasks/decidim/voca/voca_anonymize.rake +41 -0
  147. data/lib/tasks/decidim/voca/voca_clean_machine_translations.rake +22 -0
  148. data/lib/tasks/decidim/voca/voca_enhanced_invalid_record.rake +141 -0
  149. data/lib/tasks/decidim/voca/voca_install_migration.rake +45 -0
  150. data/lib/tasks/decidim/voca/voca_relation_lint.rake +12 -0
  151. data/lib/tasks/decidim/voca/voca_sync_locales.rake +10 -0
  152. data/lib/tasks/decidim/voca/voca_upgrade.rake +13 -0
  153. data/lib/tasks/decidim/voca/voca_webpacker.rake +58 -0
  154. metadata +247 -55
  155. data/app/assets/config/voca_manifest.js +0 -0
  156. data/app/assets/images/decidim/voca/icon.svg +0 -1
  157. data/app/commands/decidim/voca/organization/command.rb +0 -16
  158. data/app/commands/decidim/voca/organization/organization_command.rb +0 -17
  159. data/app/commands/decidim/voca/organization/update_color_command.rb +0 -35
  160. data/app/commands/decidim/voca/organization/update_environment_command.rb +0 -23
  161. data/app/commands/decidim/voca/organization/update_feature_command.rb +0 -32
  162. data/app/commands/decidim/voca/organization/update_file_upload_command.rb +0 -45
  163. data/app/commands/decidim/voca/organization/update_locale_command.rb +0 -53
  164. data/app/commands/decidim/voca/organization/update_naming_command.rb +0 -30
  165. data/app/commands/decidim/voca/organization/update_permission_command.rb +0 -31
  166. data/app/commands/decidim/voca/organization/update_smtp_command.rb +0 -51
  167. data/app/jobs/decidim/voca/command_job.rb +0 -36
  168. data/app/jobs/decidim/voca/webhook_notifier_job.rb +0 -57
  169. data/app/rpc/decidim/voca/decidim_service_controller.rb +0 -68
  170. data/app/rpc/decidim/voca/rpc/enum_casting.rb +0 -58
  171. data/app/rpc/decidim/voca/rpc/get_settings.rb +0 -144
  172. data/app/rpc/decidim/voca/rpc/health.rb +0 -18
  173. data/app/rpc/decidim/voca/rpc/seed_admin.rb +0 -75
  174. data/app/rpc/decidim/voca/rpc/set_settings.rb +0 -56
  175. data/app/rpc/decidim/voca/rpc/setup_db.rb +0 -38
  176. data/config/environments/development.rb +0 -6
  177. data/config/spring.rb +0 -2
  178. data/lib/decidim/voca/admin.rb +0 -10
  179. data/lib/decidim/voca/admin_engine.rb +0 -27
  180. data/lib/decidim/voca/backup/app_backup.rb +0 -169
  181. data/lib/decidim/voca/backup/app_decrypt_backup_file.rb +0 -50
  182. data/lib/decidim/voca/backup/app_encrypt_backup_file.rb +0 -49
  183. data/lib/decidim/voca/backup/app_upload_to_s3.rb +0 -49
  184. data/lib/decidim/voca/middleware/dynamic_config_middleware.rb +0 -42
  185. data/lib/decidim/voca/rpc/decidim_pb.rb +0 -134
  186. data/lib/decidim/voca/rpc/decidim_services_pb.rb +0 -30
  187. data/lib/decidim/voca/test/factories.rb +0 -9
  188. data/lib/decidim/voca/updater.rb +0 -50
  189. data/lib/tasks/voca.rake +0 -88
@@ -0,0 +1,244 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ ##
6
+ # Configures Rack::Attack for Decidim Voca
7
+ # If you want to test it in development, create a file tmp/caching-dev.txt
8
+ class RackAttackConfigurator < Decidim::Command
9
+ def call
10
+ return unless defined?(Rack::Attack)
11
+
12
+ Rack::Attack.clear_configuration
13
+ enable_rack_attack!
14
+ return broadcast(:ok) unless rack_attack_enabled?
15
+
16
+ check_preconditions!
17
+ setup_safelist!
18
+ protect_system!
19
+ protect_fail2ban!
20
+ protect_conversations!
21
+ protect_signup!
22
+ protect_signin!
23
+ protect_password_reset!
24
+ protect_api!
25
+ protect_comments!
26
+ configure_throttles!
27
+ configure_allow2ban!
28
+ broadcast(:ok)
29
+ rescue StandardError => e
30
+ broadcast(:invalid, e.message)
31
+ end
32
+
33
+ private
34
+
35
+ def check_preconditions!
36
+ preconditions_met = [
37
+ Rails.application.config.action_controller.perform_caching == true,
38
+ Rails.application.config.cache_store.present?,
39
+ Rails.application.config.cache_store != :null_store
40
+ ].all?
41
+ return if preconditions_met
42
+
43
+ raise "Rack::Attack preconditions not met. Rack::Attack will not work."
44
+ end
45
+
46
+ def ban_minutes
47
+ @ban_minutes ||= config[:ban_minutes].present? && config[:ban_minutes].to_i.positive? ? config[:ban_minutes].to_i : 0
48
+ end
49
+
50
+ def configure_throttles!
51
+ return if ban_minutes.zero?
52
+
53
+ Rack::Attack.throttled_response_retry_after_header = true
54
+ Rack::Attack.throttled_response = lambda do |env|
55
+ match_data = env["rack.attack.match_data"]
56
+ now = match_data[:epoch_time]
57
+
58
+ headers = {
59
+ "RateLimit-Limit" => match_data[:limit].to_s,
60
+ "RateLimit-Remaining" => "0",
61
+ "RateLimit-Reset" => (now + ban_minutes.minutes.to_i).to_s
62
+ }
63
+
64
+ [429, headers, ["Try again later\n"]]
65
+ end
66
+ end
67
+
68
+ def configure_allow2ban!
69
+ return if ban_minutes.zero?
70
+
71
+ Rack::Attack.blocklisted_responder = lambda do |rack_request|
72
+ request = ActionDispatch::Request.new(rack_request.env)
73
+ controller = ApplicationController.new
74
+ controller.request = request
75
+ controller.response = ActionDispatch::Response.new
76
+ rendered_content = I18n.with_locale(I18n.default_locale) do
77
+ controller.render_to_string(
78
+ partial: "decidim/voca/rack_attack/blocked",
79
+ layout: false
80
+ )
81
+ end
82
+ [403, {}, [rendered_content]]
83
+ end
84
+ end
85
+
86
+ def config
87
+ @config ||= Decidim::Voca.configuration.rack_attack
88
+ end
89
+
90
+ ##
91
+ # Throttle API requests
92
+ # max 300 requests per minute per IP
93
+ def protect_api!
94
+ return unless config[:api_per_minute].positive?
95
+
96
+ Rack::Attack.throttle("api", limit: config[:api_per_minute], period: 1.minute) do |request|
97
+ request.ip if request.post? && request.path.start_with?("/api")
98
+ end
99
+ end
100
+
101
+ ##
102
+ # Ban on too many signup requests
103
+ # max 10 requests per minute per IP
104
+ def protect_signup!
105
+ return unless config[:post_signup_per_minute].positive?
106
+
107
+ Rack::Attack.blocklist("post signup allow2ban") do |request|
108
+ Rack::Attack::Allow2Ban.filter(request.ip, maxretry: config[:post_signup_per_minute], findtime: 1.minute, bantime: ban_minutes.minutes.to_i) do |_req|
109
+ request.ip if request.post? && request.path.start_with?("/users")
110
+ end
111
+ end
112
+ end
113
+
114
+ ##
115
+ # Ban on too many signin requests
116
+ # max 30 requests per minute per IP
117
+ def protect_signin!
118
+ return unless config[:post_signin_per_minute].positive?
119
+
120
+ Rack::Attack.blocklist("post signin allow2ban") do |request|
121
+ Rack::Attack::Allow2Ban.filter(request.ip, maxretry: config[:post_signin_per_minute], findtime: 1.minute, bantime: ban_minutes.minutes.to_i) do |_req|
122
+ # Resistent to ip changes: you can not signin more than x per minute per email
123
+ request.params["user"][:email].to_s.downcase.gsub(/\s+/, "") if request.post? && request.path.start_with?("/users/sign_in")
124
+ end
125
+ end
126
+ end
127
+
128
+ ##
129
+ # Ban on too many password reset requests
130
+ # max 5 requests per minute per IP
131
+ def protect_password_reset!
132
+ return unless config[:post_password_reset_per_minute].positive?
133
+
134
+ Rack::Attack.blocklist("post password reset allow2ban") do |request|
135
+ Rack::Attack::Allow2Ban.filter(request.ip, maxretry: config[:post_password_reset_per_minute], findtime: 1.minute, bantime: ban_minutes.minutes.to_i) do |_req|
136
+ # Resistent to ip changes: you can not reset password more than x per minute per email
137
+ request.params["user"][:email].to_s.downcase.gsub(/\s+/, "") if request.post? && request.path.start_with?("/users/password")
138
+ end
139
+ end
140
+ end
141
+
142
+ ##
143
+ # Ban on too many comment creation/edition requests
144
+ # max 10 requests per minute per IP
145
+ def protect_comments!
146
+ return unless config[:post_comments_per_minute].positive?
147
+
148
+ Rack::Attack.blocklist("post comments allow2ban") do |request|
149
+ Rack::Attack::Allow2Ban.filter(request.ip, maxretry: config[:post_comments_per_minute], findtime: 1.minute, bantime: ban_minutes.minutes.to_i) do |_req|
150
+ request.ip if (request.post? || request.put?) && request.path.start_with?("/comments")
151
+ end
152
+ end
153
+ end
154
+
155
+ ##
156
+ # Add a throtlle over /conversations routes
157
+ # on POST&PUT requests: max 10 requests per minute per IP
158
+ # on GET requests: max 100 requests per minute per IP
159
+ def protect_conversations!
160
+ protect_conversations_post!
161
+ protect_conversations_get!
162
+ end
163
+
164
+ def protect_conversations_post!
165
+ if config[:post_conversations_per_minute].positive?
166
+ Rack::Attack.blocklist("post conversations allow2ban") do |request|
167
+ Rack::Attack::Allow2Ban.filter(request.ip, maxretry: config[:post_conversations_per_minute], findtime: 1.minute, bantime: ban_minutes.minutes.to_i) do |_req|
168
+ request.ip if (request.post? || request.put?) && request.path.start_with?("/conversations")
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ def protect_conversations_get!
175
+ if config[:get_conversations_per_minute].positive?
176
+ Rack::Attack.blocklist("get conversations allow2ban") do |request|
177
+ Rack::Attack::Allow2Ban.filter(request.ip, maxretry: config[:get_conversations_per_minute], findtime: 1.minute, bantime: ban_minutes.minutes.to_i) do |_req|
178
+ request.ip if request.get? && request.path.start_with?("/conversations")
179
+ end
180
+ end
181
+ end
182
+ end
183
+
184
+ ##
185
+ # Define a list of IPS that are not protected by Rack::Attack
186
+ def setup_safelist!
187
+ return if safelist_ips.empty?
188
+
189
+ Rack::Attack.safelist_ip(white_ip)
190
+ end
191
+
192
+ ##
193
+ # Block all access to system, but the Decidim.system_accesslist_ips
194
+ def protect_system!
195
+ return unless protect_system?
196
+
197
+ Rack::Attack.blocklist("block all access to system") do |request|
198
+ # Requests are blocked if the return value is truthy
199
+ if request.path.start_with?("/system")
200
+ !(Decidim.system_accesslist_ips.any? &&
201
+ Decidim.system_accesslist_ips.map { |ip_address| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }.any?)
202
+ end
203
+ end
204
+ end
205
+
206
+ ##
207
+ # Enable/Disabled Rack::Attack
208
+ def enable_rack_attack!
209
+ Rack::Attack.enabled = rack_attack_enabled?
210
+ end
211
+
212
+ ##
213
+ # Block all access to strange routes
214
+ # fail2ban strategies
215
+ def protect_fail2ban!
216
+ Rack::Attack.blocklist("fail2ban") do |req|
217
+ # `filter` returns truthy value if request fails, or if it's from a previously banned IP
218
+ # so the request is blocked
219
+ Rack::Attack::Fail2Ban.filter("pentesters-#{req.ip}", maxretry: 3, findtime: 10.minutes, bantime: 1.hour.to_i) do
220
+ # The count for the IP is incremented if the return value is truthy
221
+ CGI.unescape(req.query_string) =~ %r{/etc/passwd} ||
222
+ forbidden_paths.any? { |path| req.path.include?(path) }
223
+ end
224
+ end
225
+ end
226
+
227
+ def rack_attack_enabled?
228
+ config[:enabled]
229
+ end
230
+
231
+ def safelist_ips
232
+ @safelist_ips ||= ENV.fetch("SAFELIST_IPS", "").split(",").map(&:strip)
233
+ end
234
+
235
+ def protect_system?
236
+ config[:block_system]
237
+ end
238
+
239
+ def forbidden_paths
240
+ config[:fail2ban_paths]
241
+ end
242
+ end
243
+ end
244
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ # This command get all the available host and subdomains of all organizations
6
+ # and sync them to the redis database, following Traefik routing format.
7
+ # As we can have N servers, we need to sync the routing using unique keys (UUID)
8
+ # to avoid conflicts.
9
+ class SyncRedisRouting < Command
10
+ attr_reader :organization
11
+
12
+ def initialize(organization)
13
+ @organization = organization
14
+ end
15
+
16
+ def call
17
+ return broadcast(:ok) unless redis?
18
+
19
+ ensure_voca_external_id!(organization)
20
+ upsert_routing(organization)
21
+ upsert_common_routing!
22
+ broadcast(:ok)
23
+ end
24
+
25
+ private
26
+
27
+ def redis?
28
+ raw_connection_url = ENV.fetch("TRAEFIK_REDIS_URL", "redis://traefik-db:6379/1")
29
+ connection_url = begin
30
+ URI.parse(raw_connection_url)
31
+ rescue URI::InvalidURIError
32
+ ""
33
+ end
34
+
35
+ connection_url.present? && connection_url.host.present? && Redis.new(url: connection_url.to_s).ping == "PONG"
36
+ rescue Redis::CannotConnectError => e
37
+ Rails.logger.error("Can't connect to Redis: #{e.message}")
38
+ false
39
+ end
40
+
41
+ def ensure_voca_external_id!(organization)
42
+ organization.voca_external_id || organization.create_voca_external_id!
43
+ end
44
+
45
+ def traefik_kv(organization)
46
+ # Will connect router Host(`host`) || HOST(`subdomains`)
47
+ # The service will point to the private ip exposed by Environment Variable `MASTER_IP`
48
+ # SEE https://doc.traefik.io/traefik/reference/routing-configuration/other-providers/kv/
49
+ external_id = organization.voca_external_id
50
+ hosts = [organization.host, *organization.secondary_hosts].select do |host|
51
+ URI.parse("http://#{host}").host.present?
52
+ end
53
+ kv = {
54
+ "traefik/http/routers/#{external_id}/rule" => hosts.map { |host| "Host(`#{host}`)" }.join(" || "),
55
+ "traefik/http/routers/#{external_id}/entrypoints/0" => "websecure",
56
+ "traefik/http/routers/#{external_id}/service" => "service-#{service_id}",
57
+ "traefik/http/routers/#{external_id}/priority" => "100"
58
+ }
59
+ # Skip cert resolver for localhost domains - Traefik will use defaultGeneratedCert
60
+ kv["traefik/http/routers/#{external_id}/tls/certresolver"] = traefik_cert_resolver unless hosts.any? { |host| host.end_with?(".localhost") || host == "localhost" }
61
+ kv
62
+ end
63
+
64
+ def service_id
65
+ @service_id ||= ENV.fetch("MASTER_ID")
66
+ end
67
+
68
+ def service_url
69
+ @service_url ||= ENV.fetch("MASTER_IP")
70
+ end
71
+
72
+ def upsert_routing(organization)
73
+ traefik_kv(organization).each do |key, value|
74
+ redis.set(key, value)
75
+ end
76
+ end
77
+
78
+ def upsert_common_routing!
79
+ url = "#{traefik_service_protocol}://#{service_url}:#{traefik_service_port}"
80
+ redis.set("traefik/http/services/service-#{service_id}/loadbalancer/servers/0/url", url)
81
+ redis.set("traefik/http/services/service-#{service_id}/loadbalancer/healthcheck/path", traefik_service_healthcheck_path)
82
+ redis.set("traefik/http/services/service-#{service_id}/loadbalancer/healthcheck/interval", traefik_service_healthcheck_interval)
83
+ redis.set("traefik/http/services/service-#{service_id}/loadbalancer/healthcheck/timeout", traefik_service_healthcheck_timeout)
84
+ redis.set("traefik/http/services/service-#{service_id}/loadbalancer/healthcheck/port", traefik_service_healthcheck_port)
85
+ end
86
+
87
+ def traefik_service_healthcheck_port
88
+ @traefik_service_healthcheck_port ||= ENV.fetch("TRAEFIK_SERVICE_HEALTHCHECK_PORT", "8080")
89
+ end
90
+
91
+ def traefik_service_healthcheck_path
92
+ @traefik_service_healthcheck_path ||= ENV.fetch("TRAEFIK_SERVICE_HEALTHCHECK_PATH", "/health/live")
93
+ end
94
+
95
+ def redis
96
+ @redis ||= Redis.new(url: ENV.fetch("TRAEFIK_REDIS_URL", "redis://localhost:6379/1"))
97
+ end
98
+
99
+ def traefik_service_protocol
100
+ @traefik_service_protocol ||= ENV.fetch("TRAEFIK_SERVICE_PROTOCOL", "http")
101
+ end
102
+
103
+ def traefik_service_port
104
+ @traefik_service_port ||= ENV.fetch("TRAEFIK_SERVICE_PORT", "8080")
105
+ end
106
+
107
+ def traefik_service_healthcheck_interval
108
+ @traefik_service_healthcheck_interval ||= ENV.fetch("TRAEFIK_SERVICE_HEALTHCHECK_INTERVAL", "60s")
109
+ end
110
+
111
+ def traefik_service_healthcheck_timeout
112
+ @traefik_service_healthcheck_timeout ||= ENV.fetch("TRAEFIK_SERVICE_HEALTHCHECK_TIMEOUT", "10s")
113
+ end
114
+
115
+ def traefik_cert_resolver
116
+ @traefik_cert_resolver ||= ENV.fetch("TRAEFIK_CERT_RESOLVER", "letsencrypt")
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ class UserFieldsConfigurator < Decidim::Command
6
+ def call
7
+ return unless user_fields_installed?
8
+
9
+ Rails.application.config.after_initialize do
10
+ configure_fullname_and_birthdate_authorization_handler!
11
+ configure_name_authorization_handler!
12
+ end
13
+ end
14
+
15
+ def configure_fullname_and_birthdate_authorization_handler!
16
+ Decidim::CustomUserFields::Verifications.register("FULLNAME_AND_BIRTHDATE") do |config|
17
+ config.add_field :first_name, type: :text, required: true, skip_hashing: true
18
+ config.add_field :last_name, type: :text, required: true, skip_hashing: true
19
+ config.add_field :birthdate, type: :date, required: true, not_after: 13.years.ago.to_date.iso8601, skip_hashing: true
20
+ config.ephemerable!
21
+ config.renewable!(1.day)
22
+ end
23
+ end
24
+
25
+ def configure_name_authorization_handler!
26
+ Decidim::CustomUserFields::Verifications.register("FIRSTNAME") do |config|
27
+ config.add_field :name, type: :text, required: true, skip_hashing: true
28
+ config.ephemerable!
29
+ config.renewable!(1.day)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def user_fields_installed?
36
+ Gem.loaded_specs.has_key?("decidim-user_fields")
37
+ end
38
+ end
39
+ end
40
+ end
@@ -3,12 +3,10 @@
3
3
  module Decidim
4
4
  module Voca
5
5
  module Admin
6
- # This controller is the abstract class from which all other controllers of
7
- # this engine inherit.
8
- #
9
- # Note that it inherits from `Decidim::Admin::Components::BaseController`, which
10
- # override its layout and provide all kinds of useful methods.
11
- class ApplicationController < Decidim::Admin::Components::BaseController
6
+ class ApplicationController < Decidim::Admin::ApplicationController
7
+ def permission_class_chain
8
+ [::Decidim::Voca::Admin::Permissions] + super
9
+ end
12
10
  end
13
11
  end
14
12
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module Admin
6
+ module CodeCensus
7
+ class CodeListsController < Decidim::Admin::ApplicationController
8
+ layout "decidim/admin/users"
9
+ helper_method :existing_codes
10
+
11
+ def edit
12
+ @form = form(CodeListForm).from_params(codes_text: existing_codes.join("\n"))
13
+ end
14
+
15
+ def update
16
+ @form = form(CodeListForm).from_params(params)
17
+
18
+ SaveCodeList.call(@form, current_organization, current_user) do
19
+ on(:ok) do
20
+ flash[:notice] = I18n.t("decidim.voca.admin.code_census.code_lists.update.success")
21
+ redirect_to edit_code_list_path
22
+ end
23
+
24
+ on(:invalid) do
25
+ flash.now[:alert] = I18n.t("decidim.voca.admin.code_census.code_lists.update.error")
26
+ render :edit, status: :unprocessable_content
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def existing_codes
34
+ Decidim::Voca::ValidationCode.where(organization: current_organization).pluck(:code)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -2,12 +2,10 @@
2
2
 
3
3
  module Decidim
4
4
  module Voca
5
- # This controller is the abstract class from which all other controllers of
6
- # this engine inherit.
7
- #
8
- # Note that it inherits from `Decidim::Components::BaseController`, which
9
- # override its layout and provide all kinds of useful methods.
10
- class ApplicationController < Decidim::Components::BaseController
5
+ class ApplicationController < Decidim::ApplicationController
6
+ def permission_class_chain
7
+ [::Decidim::Voca::Permissions] + super
8
+ end
11
9
  end
12
10
  end
13
11
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module CodeCensus
6
+ class AuthorizationsController < Decidim::ApplicationController
7
+ include Decidim::FormFactory
8
+ include Decidim::Verifications::Renewable
9
+
10
+ helper_method :authorization
11
+ before_action :load_authorization
12
+ layout "layouts/decidim/authorizations"
13
+
14
+ def new
15
+ @form = form(CodeForm).from_params(user: current_user)
16
+ end
17
+
18
+ def create
19
+ @form = form(CodeForm).from_params(code_form_params.merge(user: current_user))
20
+
21
+ ConfirmCodeAuthorization.call(@authorization, @form, session) do
22
+ on(:ok) do
23
+ flash[:notice] = t("authorizations.create.success", scope: "decidim.voca.code_census")
24
+ redirect_to decidim_verifications.authorizations_path
25
+ end
26
+
27
+ on(:invalid) do
28
+ flash.now[:alert] = t("authorizations.create.error", scope: "decidim.voca.code_census")
29
+ render :new, status: :unprocessable_content
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def authorization
37
+ @authorization ||= AuthorizationPresenter.new(@authorization)
38
+ end
39
+
40
+ def load_authorization
41
+ @authorization = Decidim::Authorization.find_or_initialize_by(
42
+ user: current_user,
43
+ name: "code_census"
44
+ )
45
+ end
46
+
47
+ def code_form_params
48
+ params.expect(code: [:code])
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ # This controller handles file uploads for the hacked Quill editor
6
+ class EditorFilesController < ::Decidim::DecidimAwesome::ApplicationController
7
+ include ::Decidim::FormFactory
8
+ include ::Decidim::DecidimAwesome::NeedsAwesomeConfig
9
+
10
+ # overwrite original rescue_from to ensure we print messages from ajax methods (update)
11
+ rescue_from Decidim::ActionForbidden, with: :ajax_user_has_no_permission
12
+
13
+ def create
14
+ @form = form(EditorFileForm).from_params(form_values)
15
+ CreateEditorFile.call(@form) do
16
+ on(:ok) do |file|
17
+ url = file.attached_uploader(:file).url(host: current_organization.host)
18
+ url = "#{request.base_url}#{url}" unless url&.start_with?("http")
19
+ render json: { url:, message: I18n.t("editor_files.create.success", scope: "decidim.voca") }
20
+ end
21
+
22
+ on(:invalid) do |_message|
23
+ render json: { message: I18n.t("editor_files.create.error", scope: "decidim.voca") }, status: :unprocessable_content
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ # Rescue ajax calls and print the update.js view which prints the info on the message ajax form
31
+ # Only if the request is AJAX, otherwise behave as Decidim standards
32
+ def ajax_user_has_no_permission
33
+ return user_has_no_permission unless request.xhr?
34
+
35
+ render json: { message: I18n.t("actions.unauthorized", scope: "decidim.core") }, status: :unprocessable_content
36
+ end
37
+
38
+ def form_values
39
+ {
40
+ file: params[:file],
41
+ author_id: current_user.id,
42
+ path: request.referer
43
+ }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ class GeolocationController < Decidim::Voca::ApplicationController
6
+ # overwrite original rescue_from to ensure we print messages from ajax methods (update)
7
+ rescue_from Decidim::ActionForbidden, with: :ajax_user_has_no_permission
8
+
9
+ def locate
10
+ enforce_permission_to :locate, :geolocation
11
+
12
+ unless Decidim::Map.configured?
13
+ return render(json: { message: I18n.t("unconfigured", scope: "decidim.application.geocoding"), found: false }, status: :unprocessable_content)
14
+ end
15
+
16
+ geocoder = Decidim::Map.utility(:geocoding, organization: current_organization)
17
+ address = geocoder.address([params[:latitude], params[:longitude]])
18
+ render json: { address:, found: address.present? }
19
+ end
20
+
21
+ private
22
+
23
+ # Rescue ajax calls and print the update.js view which prints the info on the message ajax form
24
+ # Only if the request is AJAX, otherwise behave as Decidim standards
25
+ def ajax_user_has_no_permission
26
+ return user_has_no_permission unless request.xhr?
27
+
28
+ render json: { message: I18n.t("actions.unauthorized", scope: "decidim.core") }, status: :unprocessable_content
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module Admin
6
+ module CodeCensus
7
+ class CodeListForm < Form
8
+ mimic :code_list
9
+
10
+ attribute :codes_text, String
11
+
12
+ validates :codes_text, presence: true
13
+
14
+ def codes
15
+ codes_text.to_s.split(/\r?\n/).map(&:strip).compact_blank.uniq
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Voca
5
+ module Admin
6
+ class OrganizationConfigForm < Decidim::Form
7
+ include Decidim::Toggle::ModuleConfigForm
8
+ include Decidim::Toggle::ExposeAttributesToJs
9
+
10
+ self.module_config_name = Decidim::Voca::MODULE_NAME
11
+
12
+ mimic :organization
13
+
14
+ attribute :deepl_enabled, Boolean, default: true
15
+ attribute :minimalistic_deepl_enabled, Boolean, default: true
16
+ attribute :weglot_enabled, Boolean, default: false
17
+ attribute :weglot_api_key, String, default: ::Decidim::Env.new("WEGLOT_API_KEY", "").to_s
18
+ attribute :weglot_cache, Boolean, default: false
19
+
20
+ expose_to_javascript :deepl_enabled, :minimalistic_deepl_enabled, :weglot_enabled, :weglot_cache
21
+ end
22
+ end
23
+ end
24
+ end