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,45 @@
1
+ <table>
2
+ <thead>
3
+ <tr>
4
+ <th><%= t("models.apartment/distribution_key.fields.created_at") %></th>
5
+ <th><%= t("models.apartment/distribution_key.fields.key") %></th>
6
+ <th><%= t("models.apartment/distribution_key.fields.user_count") %></th>
7
+ <th><%= t("models.apartment/distribution_key.fields.admin_count") %></th>
8
+ <th><%= t("models.apartment/distribution_key.fields.available_locales") %></th>
9
+ <th><%= t("models.apartment/distribution_key.fields.default_locale") %></th>
10
+ <th><%= t("models.apartment/distribution_key.fields.host") %></th>
11
+ <th><%= t(".actions.title") %></th>
12
+ </tr>
13
+ </thead>
14
+ <tbody>
15
+ <% tenants.each do |tenant| %>
16
+ <% stats = stats_for(tenant.organization) %>
17
+ <tr>
18
+ <td>
19
+ <%= l tenant.created_at, format: :short %>
20
+ </td>
21
+ <td>
22
+ <%= link_to tenant.key, edit_organization_path(id: 1, tenant: tenant.key) %><br>
23
+ </td>
24
+ <td>
25
+ <%= stats.participants %>
26
+ </td>
27
+ <td>
28
+ <%= stats.general_administrators %>
29
+ </td>
30
+ <td>
31
+ <%= tenant.organization.available_locales.join(", ") %>
32
+ </td>
33
+ <td>
34
+ <%= tenant.organization.default_locale %>
35
+ </td>
36
+ <td>
37
+ <%= link_to tenant.host, edit_organization_path(id: 1, tenant: tenant.key) %><br>
38
+ </td>
39
+ <td>
40
+ <%= link_to t("actions.edit", scope: "decidim.system"), edit_organization_path(id: 1, tenant: tenant.key) %>
41
+ </td>
42
+ </tr>
43
+ <% end %>
44
+ </tbody>
45
+ </table>
@@ -0,0 +1,9 @@
1
+ <% add_decidim_page_title(t(".title")) %>
2
+
3
+ <% provide :title do %>
4
+ <h1 class="h1"><%= t ".title" %></h1>
5
+ <% end %>
6
+
7
+ <%= link_to t(".actions.new_organization"), [:new, :organization], class: "button button__sm md:button__lg button__primary" %>
8
+
9
+ <%= render partial: "decidim/apartment/organizations/organizations_list", locals: { tenants: @tenants } %>
@@ -0,0 +1,11 @@
1
+
2
+ <div class="new_tenant">
3
+ <div class="new_tenant__intro">
4
+ <div class="h3"><%= t("decidim.apartment.tenant_form.intro.title") %></div>
5
+ <%= t("decidim.apartment.tenant_form.intro.text_html") %>
6
+ </div>
7
+ <%= form_for @tenant_form, url: decidim_system.apartment_tenants_path, method: :post, html: { class: "new_tenant__form" } do |f| %>
8
+ <%= f.text_field :host, label: t("decidim.apartment.tenant_form.host.label") %>
9
+ <%= f.submit t("decidim.apartment.tenant_form.submit.create"), class: "button__sm md:button__lg button__primary" %>
10
+ <% end %>
11
+ </div>
data/config/assets.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ base_path = File.expand_path("..", __dir__)
4
+
5
+ # 0.32 renamed the registrar; earlier releases still expose Decidim::Webpacker.
6
+ assets = if Gem.loaded_specs.fetch("decidim-core").version >= Gem::Version.new("0.32.0")
7
+ Decidim::Shakapacker
8
+ else
9
+ Decidim::Webpacker
10
+ end
11
+
12
+ assets.register_path("#{base_path}/app/packs", prepend: true)
13
+ assets.register_entrypoints(
14
+ apartment_system_styles: "#{base_path}/app/packs/entrypoints/apartment_system_styles.scss"
15
+ )
@@ -0,0 +1,58 @@
1
+ en:
2
+ models:
3
+ apartment/distribution_key:
4
+ fields:
5
+ key: "Distribution Key"
6
+ created_at: "Created at"
7
+ host: "Host"
8
+ user_count: "User Count"
9
+ admin_count: "Admin Count"
10
+ available_locales: "Available Locales"
11
+ default_locale: "Default Locale"
12
+ activemodel:
13
+ errors:
14
+ models:
15
+ tenant:
16
+ attributes:
17
+ host:
18
+ bad_format: "Host must be a valid hostname (no port, no special characters)"
19
+ decidim:
20
+ apartment:
21
+ dashboard:
22
+ show:
23
+ title: "Tenants"
24
+ participants: "Participants"
25
+ general_administrators: "General Administrators"
26
+ no_tenants: "No tenants found"
27
+ postgres_schema: "Postgres Schema"
28
+ name: "Name"
29
+ host: "Host"
30
+ default_locale: "Default Locale"
31
+ available_locales: "Available Locales"
32
+ edit: "Edit"
33
+ create: "Create Tenant"
34
+ create_org: "Create First Organization"
35
+ no_organization: "No organization found in this tenant, please create one first"
36
+ organizations:
37
+ errors:
38
+ not_found: "Tenant not found"
39
+ no_organization: "No organization found in this tenant, please create one first"
40
+ index:
41
+ title: "Organizations"
42
+ actions:
43
+ new_organization: "New organization"
44
+ organizations_list:
45
+ actions:
46
+ title: "Actions"
47
+
48
+ tenant_form:
49
+ intro:
50
+ title: "Create a new tenant"
51
+ text_html: |
52
+ You are creating a new tenant, that will be host on its own space in our databases.<br />
53
+ To create this, please provide first the host for the new tenant
54
+ host:
55
+ label: "Host for the new tenant"
56
+ submit:
57
+ create: "Create tenant"
58
+ success: "Tenant created successfully"
@@ -0,0 +1,15 @@
1
+ en:
2
+ decidim_toggle:
3
+ system:
4
+ decidim_apartment:
5
+ tab: Apartment
6
+ not_applicable: "—"
7
+ status_callout_html: |
8
+ <dl>
9
+ <dt>Distribution key</dt>
10
+ <dd>%{distribution_key}</dd>
11
+ <dt>Storage adapter</dt>
12
+ <dd>%{storage_adapter}</dd>
13
+ <dt>Active Job adapter</dt>
14
+ <dd>%{active_job_adapter}</dd>
15
+ </dl>
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Add a uniq key per organization
5
+ # as decidim_organizations does not belongs to us, we create
6
+ # an external table, and will join over it
7
+ class AddApartmentDistributionKey < ActiveRecord::Migration[7.0]
8
+ def change
9
+ create_table :apartment_distribution_keys do |t|
10
+ t.string :host, null: false, index: { unique: true }
11
+ t.uuid :key, null: false, default: -> { "gen_random_uuid()" }, index: { unique: true }
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Public registry of primary + secondary hosts for Elevator lookup.
5
+ # Keeps aliases out of per-request tenant schema scans.
6
+ class AddApartmentDistributionHosts < ActiveRecord::Migration[7.0]
7
+ def up
8
+ create_table :apartment_distribution_hosts do |t|
9
+ t.references :apartment_distribution_key,
10
+ null: false,
11
+ foreign_key: { to_table: :apartment_distribution_keys },
12
+ index: { name: "index_apartment_distribution_hosts_on_key_id" }
13
+ t.string :host, null: false
14
+ t.string :kind, null: false, default: "primary"
15
+ t.timestamps
16
+ end
17
+
18
+ add_index :apartment_distribution_hosts, :host, unique: true
19
+
20
+ execute <<~SQL.squish
21
+ INSERT INTO apartment_distribution_hosts
22
+ (apartment_distribution_key_id, host, kind, created_at, updated_at)
23
+ SELECT id, host, 'primary', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
24
+ FROM apartment_distribution_keys
25
+ ON CONFLICT (host) DO NOTHING
26
+ SQL
27
+ end
28
+
29
+ def down
30
+ drop_table :apartment_distribution_hosts
31
+ end
32
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Maps stock Disk/S3 Active Storage services to apartment-prefixed services.
7
+ class ActiveStorageWrap
8
+ MAP = {
9
+ "Disk" => "ApartmentDiskStorage",
10
+ "S3" => "ApartmentS3Storage"
11
+ }.freeze
12
+
13
+ class << self
14
+ def wrap!
15
+ configs = Rails.application.config.active_storage.service_configurations
16
+ return if configs.blank?
17
+
18
+ changed = false
19
+ configs.each do |name, config|
20
+ next if name.to_s == "test"
21
+ next unless config.is_a?(Hash)
22
+
23
+ mapped = MAP[config["service"].to_s]
24
+ next unless mapped
25
+
26
+ config["service"] = mapped
27
+ changed = true
28
+ end
29
+ return unless changed
30
+
31
+ ensure_services_load_path!
32
+ rebuild_registry!(configs)
33
+ end
34
+
35
+ private
36
+
37
+ def ensure_services_load_path!
38
+ path = Decidim::Apartment::Engine.root.join("app/services").to_s
39
+ $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
40
+ end
41
+
42
+ def rebuild_registry!(configs)
43
+ return unless defined?(ActiveStorage::Blob)
44
+ return unless ActiveStorage::Blob.respond_to?(:services=)
45
+
46
+ ActiveStorage::Blob.services = ActiveStorage::Service::Registry.new(configs)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Boot-safe Rails +config.hosts+ population from organization hosts.
7
+ class AllowedHosts
8
+ ALLOW_ALL = [/.*/].freeze
9
+
10
+ class << self
11
+ def apply!(app)
12
+ new(app).apply!
13
+ end
14
+ end
15
+
16
+ def initialize(app)
17
+ @app = app
18
+ end
19
+
20
+ def apply!
21
+ hosts = collect_hosts
22
+ if hosts.empty?
23
+ @app.config.hosts = ALLOW_ALL.dup
24
+ else
25
+ @app.config.hosts.concat(hosts)
26
+ end
27
+ rescue StandardError
28
+ @app.config.hosts = ALLOW_ALL.dup
29
+ end
30
+
31
+ private
32
+
33
+ def collect_hosts
34
+ return [] unless database_reachable?
35
+ return [] unless organizations_table?
36
+
37
+ (hosts_from_public + hosts_from_tenants).map(&:to_s).compact_blank.uniq
38
+ end
39
+
40
+ def database_reachable?
41
+ ActiveRecord::Base.connection.verify!
42
+ true
43
+ rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError, PG::Error
44
+ false
45
+ end
46
+
47
+ def organizations_table?
48
+ ActiveRecord::Base.connection.table_exists?("decidim_organizations")
49
+ end
50
+
51
+ def hosts_from_public
52
+ ::Apartment::Tenant.switch("public") { pluck_org_hosts }
53
+ end
54
+
55
+ def hosts_from_tenants
56
+ return [] unless distribution_keys_table?
57
+
58
+ ::Apartment::Tenant.switch("public") do
59
+ DistributionKey.find_each.flat_map do |key|
60
+ key.switch { pluck_org_hosts }
61
+ rescue ::Apartment::TenantNotFound
62
+ []
63
+ end
64
+ end
65
+ end
66
+
67
+ def distribution_keys_table?
68
+ ActiveRecord::Base.connection.table_exists?("apartment_distribution_keys")
69
+ end
70
+
71
+ def pluck_org_hosts
72
+ Decidim::Organization.find_each.flat_map do |org|
73
+ [org.host, *Array(org.secondary_hosts)]
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Appends current tenant to the Rails cache namespace (keeps MASTER_ID etc.).
7
+ class CacheNamespace
8
+ class << self
9
+ def wrap!
10
+ store = Rails.application.config.cache_store
11
+ return if store.blank? || store == :null_store
12
+
13
+ config = Array(store)
14
+ return if config.first == :null_store
15
+
16
+ options = config.find { |item| item.is_a?(Hash) }
17
+ unless options
18
+ options = {}
19
+ config << options
20
+ end
21
+
22
+ previous = options[:namespace]
23
+ options[:namespace] = build_namespace(previous)
24
+ Rails.application.config.cache_store = config.size == 1 ? config.first : config
25
+ end
26
+
27
+ private
28
+
29
+ def build_namespace(previous)
30
+ proc do
31
+ base = previous.respond_to?(:call) ? previous.call : previous
32
+ [base, ::Apartment::Tenant.current].compact_blank.join("/")
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ ##
6
+ # Collects ros-apartment settings from decidim-apartment defaults, optional
7
+ # modules (geo), and host-app registrations. Applied in +configure_apartment+
8
+ # and re-applied on +to_prepare+/+after_initialize+, including table_name
9
+ # pins so late-loaded gems (GoodJob) cannot write into tenant schemas.
10
+ class ConfigRegistry
11
+ DEFAULT_PERSISTENT_SCHEMAS = %w(public).freeze
12
+ DEFAULT_EXCLUDED_MODELS = %w(
13
+ Decidim::Apartment::DistributionKey
14
+ Decidim::Apartment::DistributionHost
15
+ ).freeze
16
+ EXTENSION_PG_TYPES = %w(ltree citext).freeze
17
+ GEO_PG_TYPES = %w(geometry geography).freeze
18
+ SESSION_STORE_MODEL = "ActiveRecord::SessionStore::Session"
19
+ GOOD_JOB_MODELS = %w(
20
+ GoodJob::Job
21
+ GoodJob::Execution
22
+ GoodJob::BatchRecord
23
+ GoodJob::Process
24
+ GoodJob::Setting
25
+ ).freeze
26
+
27
+ def initialize
28
+ @persistent_schemas = Set.new
29
+ @excluded_models = Set.new
30
+ @pg_excluded_names = Set.new
31
+ end
32
+
33
+ def register
34
+ yield self
35
+ end
36
+
37
+ def add_persistent_schema(*names)
38
+ @persistent_schemas.merge(names.flatten.map(&:to_s))
39
+ end
40
+
41
+ def add_excluded_model(*names)
42
+ @excluded_models.merge(names.flatten.map(&:to_s))
43
+ end
44
+
45
+ def add_pg_excluded_name(*names)
46
+ @pg_excluded_names.merge(names.flatten.map(&:to_s))
47
+ end
48
+
49
+ def apply!(apartment_config = ::Apartment)
50
+ apartment_config.persistent_schemas = persistent_schemas
51
+ apartment_config.excluded_models = excluded_models
52
+ apartment_config.pg_excluded_names = pg_excluded_names
53
+ pin_excluded_tables!(apartment_config)
54
+ end
55
+
56
+ def persistent_schemas
57
+ (DEFAULT_PERSISTENT_SCHEMAS + @persistent_schemas.to_a).uniq
58
+ end
59
+
60
+ def excluded_models
61
+ (DEFAULT_EXCLUDED_MODELS + @excluded_models.to_a + session_store_models + good_job_models).uniq
62
+ end
63
+
64
+ def pg_excluded_names
65
+ return [] unless Decidim::Apartment.use_sql?
66
+
67
+ (EXTENSION_PG_TYPES + geo_pg_types + @pg_excluded_names.to_a).uniq
68
+ end
69
+
70
+ private
71
+
72
+ def geo_pg_types
73
+ Decidim::Apartment.use_sql? ? GEO_PG_TYPES : []
74
+ end
75
+
76
+ def session_store_models
77
+ session_store_loaded? ? [SESSION_STORE_MODEL] : []
78
+ end
79
+
80
+ def session_store_loaded?
81
+ Decidim::Toggle.gem_present?("activerecord-session_store")
82
+ end
83
+
84
+ def good_job_models
85
+ good_job_loaded? ? GOOD_JOB_MODELS : []
86
+ end
87
+
88
+ def good_job_loaded?
89
+ Decidim::Apartment.good_job_available?
90
+ end
91
+
92
+ def pin_excluded_tables!(apartment_config)
93
+ ExcludedModelPin.call(excluded_models, schema: pin_schema(apartment_config))
94
+ end
95
+
96
+ def pin_schema(apartment_config)
97
+ tenant = apartment_config.respond_to?(:default_tenant) ? apartment_config.default_tenant : nil
98
+ tenant.presence || "public"
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/ordered_options"
4
+
5
+ module Decidim
6
+ module Apartment
7
+ ##
8
+ # Class holding configuration for apartment module.
9
+ Configuration = ActiveSupport::OrderedOptions
10
+ end
11
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module Apartment
5
+ module Convert
6
+ ##
7
+ # Detects shared ActiveStorage blobs and verifies attachment integrity after prune.
8
+ class AttachmentIntegrity
9
+ class << self
10
+ def assert_no_shared_blobs!(schema: "public")
11
+ new(schema: schema).assert_no_shared_blobs!
12
+ end
13
+
14
+ def verify_tenant!(schema:, keep_org_id:)
15
+ new(schema: schema, keep_org_id: keep_org_id).verify_tenant!
16
+ end
17
+ end
18
+
19
+ def initialize(schema:, keep_org_id: nil)
20
+ @schema = schema
21
+ @keep_org_id = keep_org_id&.to_i
22
+ @connection = ActiveRecord::Base.connection
23
+ end
24
+
25
+ def assert_no_shared_blobs!
26
+ return unless table_exists?("active_storage_blobs") && table_exists?("active_storage_attachments")
27
+
28
+ shared = []
29
+ ::Apartment::Tenant.switch(@schema) do
30
+ return unless defined?(ActiveStorage::Blob)
31
+
32
+ ActiveStorage::Blob.unscoped.find_each do |blob|
33
+ org_ids = blob.attachments.filter_map do |attachment|
34
+ OrganizationResolver.resolve(attachment)&.id
35
+ end.uniq
36
+ next if org_ids.size < 2
37
+
38
+ shared << { key: blob.key, org_ids: org_ids }
39
+ end
40
+ end
41
+
42
+ return if shared.empty?
43
+
44
+ details = shared.first(10).map { |row| "#{row[:key]}→orgs #{row[:org_ids].join("/")}" }.join("; ")
45
+ raise ConvertError,
46
+ "ActiveStorage blobs shared across organizations (#{shared.size}): #{details}"
47
+ end
48
+
49
+ def verify_tenant!
50
+ counts = { orphan_attachments: 0, missing_blobs: 0, foreign_attachments: 0 }
51
+
52
+ ::Apartment::Tenant.switch(@schema) do
53
+ audit_active_storage_attachments!(counts)
54
+ verify_decidim_attachments! if table_exists?("decidim_attachments")
55
+ end
56
+
57
+ assert_no_shared_blobs!
58
+ raise_attachment_errors!(counts)
59
+ end
60
+
61
+ private
62
+
63
+ def audit_active_storage_attachments!(counts)
64
+ return unless defined?(ActiveStorage::Attachment) && table_exists?("active_storage_attachments")
65
+
66
+ ActiveStorage::Attachment.unscoped.find_each do |attachment|
67
+ classify_attachment!(attachment, counts)
68
+ end
69
+ end
70
+
71
+ def classify_attachment!(attachment, counts)
72
+ blob = safe_association(attachment, :blob)
73
+ if blob.nil?
74
+ counts[:missing_blobs] += 1
75
+ return
76
+ end
77
+
78
+ record = safe_association(attachment, :record)
79
+ if record.nil?
80
+ counts[:orphan_attachments] += 1
81
+ return
82
+ end
83
+
84
+ org = OrganizationResolver.resolve(attachment)
85
+ counts[:foreign_attachments] += 1 if @keep_org_id && org && org.id != @keep_org_id
86
+ end
87
+
88
+ def safe_association(record, name)
89
+ record.public_send(name)
90
+ rescue StandardError
91
+ nil
92
+ end
93
+
94
+ def raise_attachment_errors!(counts)
95
+ errors = []
96
+ errors << "#{counts[:missing_blobs]} attachment(s) missing blob" if counts[:missing_blobs].positive?
97
+ errors << "#{counts[:orphan_attachments]} attachment(s) missing record" if counts[:orphan_attachments].positive?
98
+ errors << "#{counts[:foreign_attachments]} attachment(s) resolve to other org" if counts[:foreign_attachments].positive?
99
+ return if errors.empty?
100
+
101
+ raise ConvertError, "attachment integrity failed: #{errors.join("; ")}"
102
+ end
103
+
104
+ def verify_decidim_attachments!
105
+ model = TableRegistry.ar_model_for("decidim_attachments")
106
+ return unless model
107
+
108
+ foreign = 0
109
+ model.unscoped.find_each do |record|
110
+ org = OrganizationResolver.resolve(record)
111
+ foreign += 1 if @keep_org_id && org && org.id != @keep_org_id
112
+ end
113
+ return if foreign.zero?
114
+
115
+ raise ConvertError, "decidim_attachments integrity failed: #{foreign} row(s) for other org"
116
+ end
117
+
118
+ def table_exists?(table)
119
+ @connection.select_value(<<~SQL.squish).present?
120
+ SELECT 1
121
+ FROM pg_tables
122
+ WHERE schemaname = #{@connection.quote(@schema)}
123
+ AND tablename = #{@connection.quote(table)}
124
+ LIMIT 1
125
+ SQL
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end