decidim-apartment 0.2.0

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 (202) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE-AGPLv3.txt +661 -0
  3. data/README.md +167 -0
  4. data/Rakefile +405 -0
  5. data/app/commands/decidim/apartment/create_tenant_command.rb +94 -0
  6. data/app/commands/decidim/apartment/noop_settings_tab_command.rb +13 -0
  7. data/app/commands/decidim/apartment/performance/create_seed_tentant.rb +194 -0
  8. data/app/controllers/decidim/apartment/tenants_controller.rb +29 -0
  9. data/app/forms/decidim/apartment/admin/organization_settings_form.rb +44 -0
  10. data/app/forms/decidim/apartment/tenant_form.rb +35 -0
  11. data/app/helpers/decidim/apartment/dashboard_helper.rb +17 -0
  12. data/app/models/decidim/apartment/distribution_host.rb +21 -0
  13. data/app/models/decidim/apartment/distribution_key.rb +91 -0
  14. data/app/overrides/apartment_edit_organization.rb +21 -0
  15. data/app/overrides/apartment_system_asset.rb +10 -0
  16. data/app/packs/entrypoints/apartment_system_styles.scss +1 -0
  17. data/app/packs/stylesheets/decidim/apartment/system/_apartment_system_styles.scss +95 -0
  18. data/app/services/active_storage/service/apartment_disk_storage_service.rb +14 -0
  19. data/app/services/active_storage/service/apartment_s3_storage_service.rb +24 -0
  20. data/app/views/decidim/apartment/dashboard/_apartment.html.erb +55 -0
  21. data/app/views/decidim/apartment/dashboard/show.html.erb +3 -0
  22. data/app/views/decidim/apartment/organizations/_organizations_list.html.erb +45 -0
  23. data/app/views/decidim/apartment/organizations/index.html.erb +9 -0
  24. data/app/views/decidim/apartment/organizations/new.html.erb +11 -0
  25. data/config/assets.rb +15 -0
  26. data/config/locales/decidim_apartment.en.yml +58 -0
  27. data/config/locales/decidim_apartment_toggle.en.yml +15 -0
  28. data/db/migrate/20250814075441_add_apartment_distribution_key.rb +15 -0
  29. data/db/migrate/20260907100000_add_apartment_distribution_hosts.rb +32 -0
  30. data/lib/decidim/apartment/active_storage_wrap.rb +51 -0
  31. data/lib/decidim/apartment/allowed_hosts.rb +78 -0
  32. data/lib/decidim/apartment/cache_namespace.rb +38 -0
  33. data/lib/decidim/apartment/config_registry.rb +102 -0
  34. data/lib/decidim/apartment/configuration.rb +11 -0
  35. data/lib/decidim/apartment/convert/attachment_integrity.rb +130 -0
  36. data/lib/decidim/apartment/convert/constraint_graph_pruner.rb +183 -0
  37. data/lib/decidim/apartment/convert/errors.rb +9 -0
  38. data/lib/decidim/apartment/convert/host_gate.rb +76 -0
  39. data/lib/decidim/apartment/convert/org_distribution_report.rb +106 -0
  40. data/lib/decidim/apartment/convert/org_metrics.rb +164 -0
  41. data/lib/decidim/apartment/convert/organization_converter.rb +154 -0
  42. data/lib/decidim/apartment/convert/organization_pruner.rb +210 -0
  43. data/lib/decidim/apartment/convert/organization_resolver.rb +386 -0
  44. data/lib/decidim/apartment/convert/paper_trail_report.rb +127 -0
  45. data/lib/decidim/apartment/convert/preflight.rb +140 -0
  46. data/lib/decidim/apartment/convert/schema_cloner.rb +157 -0
  47. data/lib/decidim/apartment/convert/storage_preconditions.rb +43 -0
  48. data/lib/decidim/apartment/convert/storage_relocator.rb +118 -0
  49. data/lib/decidim/apartment/convert/table_registry.rb +109 -0
  50. data/lib/decidim/apartment/convert.rb +20 -0
  51. data/lib/decidim/apartment/demo/blob.rb +31 -0
  52. data/lib/decidim/apartment/demo/city.rb +107 -0
  53. data/lib/decidim/apartment/demo/education.rb +83 -0
  54. data/lib/decidim/apartment/demo/foss.rb +153 -0
  55. data/lib/decidim/apartment/demo/homepage.rb +39 -0
  56. data/lib/decidim/apartment/demo/localized.rb +27 -0
  57. data/lib/decidim/apartment/demo/provision.rb +107 -0
  58. data/lib/decidim/apartment/demo/runner.rb +66 -0
  59. data/lib/decidim/apartment/demo/system_admin.rb +24 -0
  60. data/lib/decidim/apartment/demo.rb +19 -0
  61. data/lib/decidim/apartment/elevator.rb +20 -0
  62. data/lib/decidim/apartment/engine.rb +140 -0
  63. data/lib/decidim/apartment/excluded_model_pin.rb +40 -0
  64. data/lib/decidim/apartment/host_registry.rb +71 -0
  65. data/lib/decidim/apartment/import/confirm_prompt.rb +46 -0
  66. data/lib/decidim/apartment/import/database_restorer.rb +105 -0
  67. data/lib/decidim/apartment/import/dump_host_extractor.rb +67 -0
  68. data/lib/decidim/apartment/import/dump_preview.rb +96 -0
  69. data/lib/decidim/apartment/import/dump_transformer.rb +87 -0
  70. data/lib/decidim/apartment/import/errors.rb +9 -0
  71. data/lib/decidim/apartment/import/provision_tenant.rb +50 -0
  72. data/lib/decidim/apartment/import/schema_auditor.rb +118 -0
  73. data/lib/decidim/apartment/import/storage_migrator.rb +71 -0
  74. data/lib/decidim/apartment/import/tenant_context.rb +226 -0
  75. data/lib/decidim/apartment/import/tenant_importer.rb +73 -0
  76. data/lib/decidim/apartment/import/verifier.rb +58 -0
  77. data/lib/decidim/apartment/import.rb +15 -0
  78. data/lib/decidim/apartment/ops/attribute_cryptor.rb +68 -0
  79. data/lib/decidim/apartment/ops/ciphertext_rewriter.rb +230 -0
  80. data/lib/decidim/apartment/ops/encrypted_attribute_catalog.rb +106 -0
  81. data/lib/decidim/apartment/ops/encrypted_attribute_registry.rb +74 -0
  82. data/lib/decidim/apartment/ops/postgres_permissions.rb +179 -0
  83. data/lib/decidim/apartment/ops/secret_key_preflight.rb +94 -0
  84. data/lib/decidim/apartment/ops/secret_key_rotation.rb +226 -0
  85. data/lib/decidim/apartment/ops/storage_transfer.rb +188 -0
  86. data/lib/decidim/apartment/overrides/active_job_with_tenant.rb +38 -0
  87. data/lib/decidim/apartment/overrides/core_seeds.rb +31 -0
  88. data/lib/decidim/apartment/overrides/create_organization_command.rb +69 -0
  89. data/lib/decidim/apartment/overrides/dashboard_controller.rb +30 -0
  90. data/lib/decidim/apartment/overrides/decidim_seed.rb +16 -0
  91. data/lib/decidim/apartment/overrides/organizations_controller_overrides.rb +75 -0
  92. data/lib/decidim/apartment/overrides/postgresql_enable_extension.rb +15 -0
  93. data/lib/decidim/apartment/overrides/postgresql_schema_adapter.rb +34 -0
  94. data/lib/decidim/apartment/overrides/postgresql_schema_import.rb +35 -0
  95. data/lib/decidim/apartment/overrides/postgresql_schema_search_path.rb +15 -0
  96. data/lib/decidim/apartment/overrides/postgresql_schema_statements.rb +17 -0
  97. data/lib/decidim/apartment/overrides/seeds.rb +52 -0
  98. data/lib/decidim/apartment/overrides/tenant_init_without_database.rb +19 -0
  99. data/lib/decidim/apartment/overrides/update_organization_command.rb +38 -0
  100. data/lib/decidim/apartment/postgres_extensions.rb +198 -0
  101. data/lib/decidim/apartment/rake_replace.rb +21 -0
  102. data/lib/decidim/apartment/schema_search_path.rb +43 -0
  103. data/lib/decidim/apartment/system_organization.rb +67 -0
  104. data/lib/decidim/apartment/test/factories.rb +11 -0
  105. data/lib/decidim/apartment/test/seeds/association_1.jpg +0 -0
  106. data/lib/decidim/apartment/test/seeds/association_2.jpeg +0 -0
  107. data/lib/decidim/apartment/test/seeds/association_3.jpg +0 -0
  108. data/lib/decidim/apartment/test/seeds/association_4.jpg +0 -0
  109. data/lib/decidim/apartment/test/seeds/bg_Dusty Grass.png +0 -0
  110. data/lib/decidim/apartment/test/seeds/bg_Hyper Blue.png +0 -0
  111. data/lib/decidim/apartment/test/seeds/bg_Moonwalker.png +0 -0
  112. data/lib/decidim/apartment/test/seeds/bg_Neon Green.png +0 -0
  113. data/lib/decidim/apartment/test/seeds/bg_Visual Blue.png +0 -0
  114. data/lib/decidim/apartment/test/seeds/bg_hinehouse.png +0 -0
  115. data/lib/decidim/apartment/test/seeds/city_1.jpg +0 -0
  116. data/lib/decidim/apartment/test/seeds/city_2.jpg +0 -0
  117. data/lib/decidim/apartment/test/seeds/city_3.jpg +0 -0
  118. data/lib/decidim/apartment/test/seeds/city_4.webp +0 -0
  119. data/lib/decidim/apartment/test/seeds/city_5.jpg +0 -0
  120. data/lib/decidim/apartment/test/seeds/debian.jpeg +0 -0
  121. data/lib/decidim/apartment/test/seeds/democratic_school_1.jpg +0 -0
  122. data/lib/decidim/apartment/test/seeds/drupal.jpg +0 -0
  123. data/lib/decidim/apartment/test/seeds/foss.jpg +0 -0
  124. data/lib/decidim/apartment/test/seeds/immich.jpg +0 -0
  125. data/lib/decidim/apartment/test/seeds/kid_1.jpg +0 -0
  126. data/lib/decidim/apartment/test/seeds/kid_2.jpg +0 -0
  127. data/lib/decidim/apartment/test/seeds/montessori_1.jpg +0 -0
  128. data/lib/decidim/apartment/test/seeds/montessori_2.jpg +0 -0
  129. data/lib/decidim/apartment/test/seeds/neighboor_1.jpg +0 -0
  130. data/lib/decidim/apartment/test/seeds/neighbor_2.jpg +0 -0
  131. data/lib/decidim/apartment/test/seeds/neighbor_3.jpg +0 -0
  132. data/lib/decidim/apartment/test/seeds/pattern_cubes-3_4_2_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(47, 81_, 61_, 1.00).png +0 -0
  133. data/lib/decidim/apartment/test/seeds/pattern_cubes-3_4_2_0-0_55_1__hsla(240, 7_, 18_, 1.00)_hsla(47, 81_, 61_, 1.00)(1).png +0 -0
  134. data/lib/decidim/apartment/test/seeds/pattern_cubes-3_4_2_0-0_55_1__hsla(240, 7_, 18_, 1.00)_hsla(47, 81_, 61_, 1.00).png +0 -0
  135. data/lib/decidim/apartment/test/seeds/pattern_diamonds-1_1_10_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(174, 100_, 29_, 1.00).png +0 -0
  136. data/lib/decidim/apartment/test/seeds/pattern_diamonds-1_1_2_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(174, 100_, 29_, 1.00).png +0 -0
  137. data/lib/decidim/apartment/test/seeds/pattern_diamonds-1_5_10_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(174, 100_, 29_, 1.00).png +0 -0
  138. data/lib/decidim/apartment/test/seeds/pattern_diamonds-1_5_2_0-0_0_1__hsla(240, 7_, 18_, 1.00)_hsla(174, 100_, 29_, 1.00).png +0 -0
  139. data/lib/decidim/apartment/test/seeds/pattern_new_01.png +0 -0
  140. data/lib/decidim/apartment/test/seeds/pattern_new_02.png +0 -0
  141. data/lib/decidim/apartment/test/seeds/pattern_new_03.png +0 -0
  142. data/lib/decidim/apartment/test/seeds/pattern_squares_01.png +0 -0
  143. data/lib/decidim/apartment/test/seeds/pattern_squares_02.png +0 -0
  144. data/lib/decidim/apartment/test/seeds/pattern_squares_03.png +0 -0
  145. data/lib/decidim/apartment/test/seeds/pattern_squares_04.png +0 -0
  146. data/lib/decidim/apartment/test/seeds/pattern_squares_05.png +0 -0
  147. data/lib/decidim/apartment/test/seeds/pattern_squares_06.png +0 -0
  148. data/lib/decidim/apartment/test/seeds/pattern_squares_07.png +0 -0
  149. data/lib/decidim/apartment/test/seeds/pattern_squares_08.png +0 -0
  150. data/lib/decidim/apartment/test/seeds/pattern_squares_09.png +0 -0
  151. data/lib/decidim/apartment/test/seeds/pattern_squares_10.png +0 -0
  152. data/lib/decidim/apartment/test/seeds/pattern_squares_11.png +0 -0
  153. data/lib/decidim/apartment/test/seeds/pattern_squares_12.png +0 -0
  154. data/lib/decidim/apartment/test/seeds/pattern_squares_13.png +0 -0
  155. data/lib/decidim/apartment/test/seeds/pattern_squares_14.png +0 -0
  156. data/lib/decidim/apartment/test/seeds/pattern_squares_15.png +0 -0
  157. data/lib/decidim/apartment/test/seeds/pattern_squares_16.png +0 -0
  158. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_01.png +0 -0
  159. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_02.png +0 -0
  160. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_03.png +0 -0
  161. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_04.png +0 -0
  162. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_05.png +0 -0
  163. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_06.png +0 -0
  164. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_07.png +0 -0
  165. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_08.png +0 -0
  166. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_09.png +0 -0
  167. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_10.png +0 -0
  168. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_11.png +0 -0
  169. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_12.png +0 -0
  170. data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_13.png +0 -0
  171. data/lib/decidim/apartment/test/seeds/rails.jpg +0 -0
  172. data/lib/decidim/apartment/test/seeds/school_1.jpg +0 -0
  173. data/lib/decidim/apartment/test/seeds/school_3.jpg +0 -0
  174. data/lib/decidim/apartment/test/seeds/school_4.jpg +0 -0
  175. data/lib/decidim/apartment/test/seeds/school_5.jpg +0 -0
  176. data/lib/decidim/apartment/test/seeds/silverstripe.jpg +0 -0
  177. data/lib/decidim/apartment/test/seeds/street_1.jpg +0 -0
  178. data/lib/decidim/apartment/test/seeds/street_2.jpg +0 -0
  179. data/lib/decidim/apartment/test/seeds/street_3.jpg +0 -0
  180. data/lib/decidim/apartment/test/seeds/urban_agri_1.webp +0 -0
  181. data/lib/decidim/apartment/test/seeds/urban_agri_2.jpg +0 -0
  182. data/lib/decidim/apartment/test/seeds/urban_agri_3.jpg +0 -0
  183. data/lib/decidim/apartment/test/seeds/urban_agri_4.jpg +0 -0
  184. data/lib/decidim/apartment/test/seeds/world-map-colors.png +0 -0
  185. data/lib/decidim/apartment/version.rb +44 -0
  186. data/lib/decidim/apartment.rb +69 -0
  187. data/lib/tasks/decidim_apartment_check_install.rake +58 -0
  188. data/lib/tasks/decidim_apartment_convert.rake +60 -0
  189. data/lib/tasks/decidim_apartment_gem_manager.rake +62 -0
  190. data/lib/tasks/decidim_apartment_import.rake +39 -0
  191. data/lib/tasks/decidim_apartment_install_extension.rake +49 -0
  192. data/lib/tasks/decidim_apartment_performance_seed.rake +12 -0
  193. data/lib/tasks/decidim_apartment_rotate.rake +17 -0
  194. data/lib/tasks/decidim_apartment_storage.rake +27 -0
  195. data/lib/tasks/decidim_download_your_data_tasks.rake +121 -0
  196. data/lib/tasks/decidim_mailers.rake +38 -0
  197. data/lib/tasks/decidim_meetings.rake +30 -0
  198. data/lib/tasks/decidim_metrics_tasks.rake +29 -0
  199. data/lib/tasks/decidim_open_data_tasks.rake +24 -0
  200. data/lib/tasks/decidim_participatory_processes.rake +20 -0
  201. data/lib/tasks/decidim_reminders_tasks.rake +24 -0
  202. metadata +361 -0
@@ -0,0 +1,194 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Performance
6
+ class CreateSeedTentant < ::Decidim::Command
7
+ attr_reader :user_count, :tenant_count, :host
8
+
9
+ def initialize(user_count, tenant_count, host)
10
+ @user_count = user_count
11
+ @tenant_count = tenant_count
12
+ @host = host
13
+ end
14
+
15
+ def call
16
+ create_organizations!
17
+ create_processes!
18
+ create_proposals!
19
+ end
20
+
21
+ def create_proposals!
22
+ Decidim::Apartment::DistributionKey.all.map do |key|
23
+ key.switch do
24
+ Decidim::Organization.first
25
+ participatory_process = Decidim::ParticipatoryProcess.first
26
+ component = Decidim::Component.create!(
27
+ name: "Proposals",
28
+ manifest_name: "proposals",
29
+ participatory_space: participatory_process,
30
+ published_at: Time.current,
31
+ settings: {}
32
+ )
33
+
34
+ settings = {
35
+ votes_hidden: false,
36
+ votes_blocked: false,
37
+ votes_enabled: true,
38
+ announcement_en: nil,
39
+ comments_blocked: false,
40
+ creation_enabled: true,
41
+ answers_with_costs: false,
42
+ automatic_hashtags: false,
43
+ default_sort_order: nil,
44
+ comments_enabled: true,
45
+ vote_limit: 0
46
+ }
47
+ component.default_step_settings = settings
48
+ component.step_settings = participatory_process.steps.to_h { |step| [step.id, settings] }
49
+ component.save!
50
+ 5.times.each do |i|
51
+ Rails.logger.debug { "Creating proposals for tenant #{key.host}, user #{i + 1}/#{user_count / 2}" }
52
+
53
+ params = {
54
+ title: Decidim.available_locales.index_with { |locale| "Proposing random ideas is fun, as soon as you vote form me #{locale} ##{i}" },
55
+ body: Decidim.available_locales.index_with do |locale|
56
+ <<~BODY
57
+ Load testing is important #{i},
58
+ we are doing scenario testing, with an arrival rate of 5 users per seconds the first minutes,
59
+ then 15users for 2minutes #{Date.current.to_time.strftime("%Y-%m-%d %H:%M:%S")}.
60
+ This is a proposal in #{locale} language.
61
+ It is a random proposal created for load testing.
62
+ It is not a real proposal.
63
+ BODY
64
+ end,
65
+ component:,
66
+ published_at: Time.current
67
+ }
68
+ proposal = Decidim::Proposals::Proposal.new(params)
69
+ proposal.save(validate: false)
70
+ proposal.coauthorships.create!(author: pick_user, decidim_author_type: "Decidim::UserBaseEntity")
71
+ proposal.save!
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def pick_user
78
+ Decidim::User.where(admin: false).sample
79
+ end
80
+
81
+ def create_processes!
82
+ Decidim::Apartment::DistributionKey.all.map do |key|
83
+ key.switch do
84
+ Rails.logger.debug { "Creating process for tenant #{key.host}" }
85
+ organization = Decidim::Organization.first
86
+ params = {
87
+ title: Decidim.available_locales.index_with { |locale| "Load testing: is decidim-apartment fast enough? #{locale}" },
88
+ slug: "decidim-apartment",
89
+ subtitle: Decidim.available_locales.index_with { |locale| "Load testing: is decidim-apartment fast enough? #{locale}" },
90
+ short_description: Decidim.available_locales.index_with { |locale| "Load testing: is decidim-apartment fast enough? #{locale}" },
91
+ description: Decidim.available_locales.index_with { |locale| "Load testing: is decidim-apartment fast enough? #{locale}" },
92
+ organization:,
93
+ start_date: Date.current,
94
+ end_date: 2.months.from_now,
95
+ participatory_process_group: nil,
96
+ scope: Decidim::Scope.all.sample,
97
+ published_at: Time.current
98
+ }
99
+
100
+ process = Decidim::ParticipatoryProcess.create!(params)
101
+ process.add_to_index_as_search_resource
102
+ Decidim::ContentBlocksCreator.new(process).create_default!
103
+ Decidim::ParticipatoryProcessStep.create!(
104
+ title: Decidim.available_locales.index_with { |locale| "Proposing random ideas is fun, as soon as you vote form me #{locale}" },
105
+ description: Decidim.available_locales.index_with { |locale| "Load testing: is decidim-apartment fast enough? #{locale}" },
106
+ participatory_process: process
107
+ )
108
+ process
109
+ end
110
+ end
111
+ end
112
+
113
+ def create_organizations!
114
+ tenant_count.times.each do |i|
115
+ Rails.logger.debug { "Creating organization #{i + 1}/#{tenant_count}" }
116
+ name = "Performance test #{i}"
117
+ organization_host = "perf-#{i}.#{host}"
118
+ form_params = {
119
+ name: Decidim.available_locales.index_with { |locale| "#{name} #{locale}" },
120
+ host: organization_host,
121
+ available_locales: Decidim.available_locales,
122
+ default_locale: Decidim.default_locale,
123
+ reference_prefix: name.parameterize.upcase,
124
+ time_zone: "UTC",
125
+ smtp_settings: {},
126
+ file_upload_settings: {},
127
+ force_users_to_authenticate_before_access_organization: false,
128
+ available_authorizations: [],
129
+ users_registration_mode: "enabled",
130
+ organization_admin_email: "admin@example.org",
131
+ organization_admin_name: "John Doe",
132
+ content_type_allowlist: %w(image/jpeg image/gif image/png)
133
+ }
134
+ form = Decidim::System::RegisterOrganizationForm.new(form_params)
135
+
136
+ raise "Can not create organization. Errors: #{form.errors.full_messages}" unless form.valid?
137
+
138
+ result = Decidim::System::CreateOrganization.call(form)
139
+ raise "Can not create organization. Errors: #{form.errors.full_messages}" unless result.has_key? :ok
140
+
141
+ ::Decidim::Apartment::DistributionKey.for_host(organization_host).switch do
142
+ organization = Decidim::Organization.first
143
+ organization.file_upload_settings = Decidim::OrganizationSettings.default(:upload)
144
+ organization.colors = {
145
+ primary: "#14342B",
146
+ secondary: "#006482",
147
+ tertiary: "#F7E733"
148
+ }.compact_blank
149
+ organization.save!
150
+ author = Decidim::User.find_by(admin: true, organization:)
151
+ author.update(nickname: "john_doe", password: "Administrator123456789!", password_confirmation: "Administrator123456789!")
152
+ author.skip_confirmation!
153
+ author.save!
154
+ create_users!(organization)
155
+ end
156
+ end
157
+ end
158
+
159
+ def create_users!(organization)
160
+ user_count.times.each do |index|
161
+ Rails.logger.info("Creating fake user #{index + 1}/#{user_count}")
162
+ retry_count = 0
163
+ Rails.logger.debug { "create fake user #{index + 1}/#{user_count}" }
164
+ begin
165
+ user = Decidim::User.create!(
166
+ email: "user#{index + 1}@example.org",
167
+ password: "PlatformParticipant123456789",
168
+ password_confirmation: "PlatformParticipant123456789",
169
+ organization: organization,
170
+ confirmed_at: Time.current,
171
+ personal_url: "https://example.org/user#{index + 1}",
172
+ nickname: "user_#{index + 1}",
173
+ name: "User #{index + 1}",
174
+ about: "Performance test user #{index + 1}",
175
+ extended_data: { demo: true },
176
+ tos_agreement: true,
177
+ accepted_tos_version: 1.day.from_now
178
+ )
179
+ user.skip_confirmation!
180
+ user.save!
181
+ user.reload
182
+ rescue StandardError => e
183
+ Rails.logger.error("Error creating fake user #{i + 1}: #{e.message}")
184
+ Rails.logger.error(e.backtrace.join("\n"))
185
+ retry_count += 1
186
+ retry if retry_count < 10
187
+ raise e
188
+ end
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Controller that handle tenant creation.
7
+ # For Decidim::System::OrganizationController, see overides in
8
+ # lib/decidim/apartment/overrides/organizations_controller_overrides.rb
9
+ class TenantsController < ::Decidim::System::ApplicationController
10
+ delegate :root_path, :destroy_admin_session_path, to: :decidim_system
11
+ helper_method :root_path, :destroy_admin_session_path
12
+ helper Decidim::OmniauthHelper
13
+
14
+ def create
15
+ @tenant_form = Decidim::Apartment::TenantForm.from_params(params.require(:tenant))
16
+ Decidim::Apartment::CreateTenantCommand.call(@tenant_form) do
17
+ on(:ok) do |tenant_key|
18
+ redirect_to decidim_system.new_organization_path(tenant: tenant_key),
19
+ notice: I18n.t("decidim.apartment.tenant_form.submit.success")
20
+ end
21
+ on(:invalid) do
22
+ # Pass request to the Decidim::System::OrganizationsController#new action
23
+ render "decidim/apartment/organizations/new"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Admin
6
+ class OrganizationSettingsForm < Decidim::Form
7
+ include Decidim::Toggle::TabForm
8
+
9
+ mimic :organization
10
+
11
+ info :status_callout_html
12
+
13
+ def self.from_model(organization)
14
+ new.with_context(current_organization: organization)
15
+ end
16
+
17
+ def status_callout_html
18
+ I18n.t(
19
+ "decidim_toggle.system.decidim_apartment.status_callout_html",
20
+ distribution_key: distribution_key_label,
21
+ storage_adapter: storage_adapter_name,
22
+ active_job_adapter: active_job_adapter_name
23
+ )
24
+ end
25
+
26
+ private
27
+
28
+ def distribution_key_label
29
+ key = Decidim::Apartment::DistributionKey.for_host(current_organization.host)&.key
30
+ key.presence || I18n.t("decidim_toggle.system.decidim_apartment.not_applicable")
31
+ end
32
+
33
+ def storage_adapter_name
34
+ ActiveStorage::Blob.service.class.name
35
+ end
36
+
37
+ def active_job_adapter_name
38
+ adapter = Rails.application.config.active_job.queue_adapter
39
+ adapter.is_a?(Symbol) ? adapter.to_s : adapter.class.name
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Plain Ruby Object to handle tenant validation before creation.
7
+ class TenantForm < ::Decidim::Form
8
+ attribute :host, String
9
+
10
+ validates :host, presence: true
11
+
12
+ validate :unique_host
13
+ validate :valid_host
14
+
15
+ private
16
+
17
+ # Host must be a valid hostname
18
+ def valid_host
19
+ return if host.blank?
20
+
21
+ # No consecutive dots
22
+ errors.add(:host, :invalid) if host.include?("..")
23
+ errors.add(:host, :bad_format) unless host.match?(/^[a-zA-Z0-9.-]+$/)
24
+ # Must start and end with alphanumeric
25
+ errors.add(:host, :invalid) unless host.match?(/^[a-zA-Z0-9].*[a-zA-Z0-9]$/)
26
+ end
27
+
28
+ def unique_host
29
+ match = Decidim::Apartment::DistributionKey.for_host(host)
30
+
31
+ errors.add(:host, :taken) if match && match.organization?
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module DashboardHelper
6
+ def stats_for(organization)
7
+ return OpenStruct.new(participants: "-", general_administrators: "-") unless organization
8
+
9
+ OpenStruct.new(
10
+ participants: Decidim::User.where(organization: organization, blocked_at: nil, locked_at: nil,
11
+ admin: false).where("confirmed_at IS NULL AND last_sign_in_at IS NULL").count,
12
+ general_administrators: Decidim::User.where(organization: organization, blocked_at: nil, locked_at: nil, admin: true).count
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Host alias row in +public+. Elevator resolves here without scanning tenants.
7
+ class DistributionHost < ApplicationRecord
8
+ self.table_name = "apartment_distribution_hosts"
9
+
10
+ KINDS = %w(primary secondary).freeze
11
+
12
+ belongs_to :distribution_key,
13
+ class_name: "Decidim::Apartment::DistributionKey",
14
+ foreign_key: :apartment_distribution_key_id,
15
+ inverse_of: :distribution_hosts
16
+
17
+ validates :host, presence: true, uniqueness: true
18
+ validates :kind, inclusion: { in: KINDS }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Model used to store tenant essentials information.
7
+ # This model will be populated only in "public" schema.
8
+ class DistributionKey < ApplicationRecord
9
+ self.table_name = "apartment_distribution_keys"
10
+ before_create :generate_key
11
+
12
+ has_many :distribution_hosts,
13
+ class_name: "Decidim::Apartment::DistributionHost",
14
+ foreign_key: :apartment_distribution_key_id,
15
+ dependent: :destroy,
16
+ inverse_of: :distribution_key
17
+
18
+ # First db:migrate boots before this table exists.
19
+ def self.tenant_keys
20
+ pluck(:key)
21
+ rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished, ActiveRecord::StatementInvalid
22
+ []
23
+ end
24
+
25
+ def self.for_host(host)
26
+ ::Apartment::Tenant.switch("public") do
27
+ row = DistributionHost.find_by(host: host)
28
+ return row.distribution_key if row
29
+
30
+ where(host: host).first
31
+ end
32
+ end
33
+
34
+ def self.collection
35
+ ::Apartment::Tenant.switch("public") do
36
+ all
37
+ end
38
+ end
39
+
40
+ # System organization edit/update: ?tenant= key, else distribution-key id.
41
+ def self.for_system(id:, tenant:)
42
+ return find_by(key: tenant) if tenant.present?
43
+ return find_by(id: id) if id.present?
44
+
45
+ nil
46
+ end
47
+
48
+ def switch(&)
49
+ ::Apartment::Tenant.switch(key, &)
50
+ end
51
+
52
+ def switch!(&)
53
+ ::Apartment::Tenant.switch!(key, &)
54
+ end
55
+
56
+ def tenant_name
57
+ key
58
+ end
59
+
60
+ def organization
61
+ switch do
62
+ Decidim::Organization.first
63
+ end
64
+ rescue ::Apartment::TenantNotFound
65
+ nil
66
+ end
67
+
68
+ def organization?
69
+ switch do
70
+ Decidim::Organization.exists?
71
+ end
72
+ rescue ::Apartment::TenantNotFound
73
+ false
74
+ end
75
+
76
+ def sync_hosts!(primary_host: host, secondary_hosts: [])
77
+ HostRegistry.sync!(
78
+ self,
79
+ primary_host: primary_host,
80
+ secondary_hosts: secondary_hosts
81
+ )
82
+ end
83
+
84
+ private
85
+
86
+ def generate_key
87
+ self.key = SecureRandom.uuid
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ Deface::Override.new(
4
+ virtual_path: "decidim/system/organizations/edit",
5
+ name: "apartment_edit_organization",
6
+ insert_after: "erb[loud]:contains('text_field :host')",
7
+ text: "<%= hidden_field_tag :tenant, ::Apartment::Tenant.current %>"
8
+ )
9
+
10
+ Deface::Override.new(
11
+ virtual_path: "decidim/system/organizations/edit",
12
+ name: "apartment_resend_invitation_organization",
13
+ replace: "erb[loud]:contains('resend_invitation')",
14
+ text: <<~ERB
15
+ <%= link_to t(".resend_invitation"),
16
+ resend_invitation_organization_path(@organization, tenant: ::Apartment::Tenant.current),
17
+ method: :post,
18
+ class: "button button__sm md:button__lg button__transparent-secondary",
19
+ data: { confirm: t(".confirm_resend_invitation") } %>
20
+ ERB
21
+ )
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ Deface::Override.new(
4
+ virtual_path: "layouts/decidim/system/_header",
5
+ name: "add_apartment_system_assets",
6
+ insert_after: "erb[loud]:contains('decidim_system_overrides')",
7
+ text: <<-ERB
8
+ <%= append_stylesheet_pack_tag "apartment_system_styles" %>
9
+ ERB
10
+ )
@@ -0,0 +1 @@
1
+ @use "stylesheets/decidim/apartment/system/apartment_system_styles";
@@ -0,0 +1,95 @@
1
+ /**
2
+ * CSS Files aditions for Decidim Apartment module.
3
+ * This file is loaded on every system page (see app/overrides/apartment_system_asset.rb).
4
+ * And imported through registered entrypoint (see config/assets.rb, app/packs/entrypoints/apartment_system_styles.scss).
5
+ **/
6
+
7
+ .org_new_form--hidden {
8
+ display: none !important;
9
+ }
10
+
11
+ .new_tenant {
12
+ padding: 1rem;
13
+ border: 1px solid #313131;
14
+ background: #ddd;
15
+ border-radius: 4px;
16
+ margin: 1rem 0;
17
+ &__intro {
18
+ margin-bottom: 20px;
19
+ }
20
+
21
+ &__form {
22
+ margin-bottom: 20px;
23
+ }
24
+ label {
25
+ display: block;
26
+ margin-bottom: 10px;
27
+ }
28
+ input#tenant_host {
29
+ display: block;
30
+ }
31
+ }
32
+
33
+ .tenant_dashboard {
34
+ margin-top: 2rem;
35
+ }
36
+ .tenant_cardlist {
37
+ display: flex;
38
+ flex-wrap: wrap;
39
+ gap: 1rem;
40
+ }
41
+ .tenant_card {
42
+ flex: 1;
43
+ max-width: 33.33%;
44
+ border: 1px solid #313131;
45
+ border-radius: 4px;
46
+ padding: 1rem;
47
+ margin: 1rem 0;
48
+ display: flex;
49
+ flex-direction: column;
50
+ &__title {
51
+ font-size: 1.2rem;
52
+ font-weight: bold;
53
+ }
54
+ &__body {
55
+ flex: 1;
56
+ padding: 1rem 0 0.5rem 0;
57
+
58
+ dd {
59
+ margin-top: 0.25rem;
60
+ }
61
+ code {
62
+ background: #f0f0f0;
63
+ padding: 0.25rem;
64
+ border-radius: 4px;
65
+ font-family: monospace;
66
+ font-size: 0.8rem;
67
+ color: #313131;
68
+ white-space: nowrap;
69
+ overflow: hidden;
70
+ text-overflow: ellipsis;
71
+ }
72
+ a {
73
+ color: blue;
74
+ &:hover {
75
+ text-decoration: underline;
76
+ }
77
+ }
78
+ }
79
+ &__warning {
80
+ background: var(--warning);
81
+ padding: 1rem 0.5rem;
82
+ border-radius: 4px;
83
+ color: black;
84
+ text-align: center;
85
+ margin: 1rem 0;
86
+ }
87
+
88
+ &__actions {
89
+ flex: 0;
90
+ display: flex;
91
+ gap: 1rem;
92
+ justify-content: flex-end;
93
+ align-items: center;
94
+ }
95
+ }
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_storage/service/disk_service"
4
+ module ActiveStorage
5
+ ##
6
+ # A disk storage service that upload files to the tenant's folder.
7
+ class Service::ApartmentDiskStorageService < Service::DiskService
8
+ def path_for(key)
9
+ current_tenant = ::Apartment::Tenant.current
10
+ File.join root, current_tenant, folder_for(key), key
11
+ end
12
+ end
13
+ # rubocop:enable Style/ClassAndModuleChildren
14
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ if Gem.loaded_specs.has_key?("aws-sdk-s3")
4
+ require "active_storage/service/s3_service"
5
+ module ActiveStorage
6
+ ##
7
+ # A disk storage service that upload files to the tenant's folder.
8
+ class Service::ApartmentS3StorageService < Service::S3Service
9
+ private
10
+
11
+ def object_for(key)
12
+ current_tenant = ::Apartment::Tenant.current
13
+ bucket.object File.join(current_tenant, key)
14
+ end
15
+ end
16
+ end
17
+ else
18
+ # No AWS SDK, so we use the disk service
19
+ module ActiveStorage
20
+ class Service::ApartmentS3StorageService < Service::ApartmentDiskStorageService
21
+ end
22
+ end
23
+ end
24
+ # rubocop:enable Style/ClassAndModuleChildren
@@ -0,0 +1,55 @@
1
+ <% tenant_scope = "decidim.apartment.dashboard.show" %>
2
+ <div class="tenant_dashboard">
3
+ <h2 class="h3"><%= t("title", scope: tenant_scope) %></h2>
4
+ <%= link_to t("create", scope: tenant_scope), decidim_system.new_organization_path, class: "button md:button__lg button__primary" %>
5
+ <% if Decidim::Apartment::DistributionKey.count > 0 %>
6
+ <div class="tenant_cardlist">
7
+ <% Decidim::Apartment::DistributionKey.all.map do |key| %>
8
+ <div class="tenant_card">
9
+ <div class="tenant_card__title"><%= key.host %></div>
10
+ <% key.switch do %>
11
+ <div class="tenant_card__body">
12
+ <%
13
+ organization = key.organization
14
+ stats = stats_for(organization)
15
+ %>
16
+ <dl>
17
+ <dt><%= t("postgres_schema", scope: tenant_scope) %></dt>
18
+ <dd><code><%= key.key %></code></dd>
19
+ <% if key.organization? %>
20
+ <dt><%= t("name", scope: tenant_scope) %></dt>
21
+ <dd><%= organization.name[organization.default_locale] %></dd>
22
+ <dt><%= t("host", scope: tenant_scope) %></dt>
23
+ <dd>
24
+ <a href="https://<%= organization.host %>" target="_blank" data-external-link="false"><%= organization.host %></a>
25
+ </dd>
26
+ <dt><%= t("default_locale", scope: tenant_scope) %></dt>
27
+ <dd><%= organization.default_locale %></dd>
28
+ <dt><%= t("available_locales", scope: tenant_scope) %></dt>
29
+ <dd><%= organization.available_locales.join(", ") %></dd>
30
+ <dt><%= t("participants", scope: tenant_scope) %></dt>
31
+ <dd><%= stats.participants %></dd>
32
+ <dt><%= t("general_administrators", scope: tenant_scope) %></dt>
33
+ <dd><%= stats.general_administrators %></dd>
34
+ </dl>
35
+
36
+ <% end %>
37
+ </div>
38
+ <% unless organization %>
39
+ <div class="tenant_card__warning">
40
+ <%= t("no_organization", scope: tenant_scope) %>
41
+ </div>
42
+ <% end %>
43
+ <div class="tenant_card__actions">
44
+ <%= link_to organization ? t("edit", scope: tenant_scope) : t("create_org", scope: tenant_scope), decidim_system.edit_organization_path(id: 1, tenant: key.key), class: "button button__sm button__primary" %>
45
+ </div>
46
+ <% end %>
47
+ </div>
48
+ <% end %>
49
+ </div>
50
+ <% else %>
51
+ <div class="tenant_cardlist tenant_cardlist--empty">
52
+ <%= t("no_tenants", scope: tenant_scope) %>
53
+ </div>
54
+ <% end %>
55
+ </div>
@@ -0,0 +1,3 @@
1
+ <%# System dashboard without Deface: keep checks, list apartment tenants. %>
2
+ <%= render partial: "decidim/system/system_checks" %>
3
+ <%= render partial: "decidim/apartment/dashboard/apartment" %>