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.
- checksums.yaml +7 -0
- data/LICENSE-AGPLv3.txt +661 -0
- data/README.md +167 -0
- data/Rakefile +405 -0
- data/app/commands/decidim/apartment/create_tenant_command.rb +94 -0
- data/app/commands/decidim/apartment/noop_settings_tab_command.rb +13 -0
- data/app/commands/decidim/apartment/performance/create_seed_tentant.rb +194 -0
- data/app/controllers/decidim/apartment/tenants_controller.rb +29 -0
- data/app/forms/decidim/apartment/admin/organization_settings_form.rb +44 -0
- data/app/forms/decidim/apartment/tenant_form.rb +35 -0
- data/app/helpers/decidim/apartment/dashboard_helper.rb +17 -0
- data/app/models/decidim/apartment/distribution_host.rb +21 -0
- data/app/models/decidim/apartment/distribution_key.rb +91 -0
- data/app/overrides/apartment_edit_organization.rb +21 -0
- data/app/overrides/apartment_system_asset.rb +10 -0
- data/app/packs/entrypoints/apartment_system_styles.scss +1 -0
- data/app/packs/stylesheets/decidim/apartment/system/_apartment_system_styles.scss +95 -0
- data/app/services/active_storage/service/apartment_disk_storage_service.rb +14 -0
- data/app/services/active_storage/service/apartment_s3_storage_service.rb +24 -0
- data/app/views/decidim/apartment/dashboard/_apartment.html.erb +55 -0
- data/app/views/decidim/apartment/dashboard/show.html.erb +3 -0
- data/app/views/decidim/apartment/organizations/_organizations_list.html.erb +45 -0
- data/app/views/decidim/apartment/organizations/index.html.erb +9 -0
- data/app/views/decidim/apartment/organizations/new.html.erb +11 -0
- data/config/assets.rb +15 -0
- data/config/locales/decidim_apartment.en.yml +58 -0
- data/config/locales/decidim_apartment_toggle.en.yml +15 -0
- data/db/migrate/20250814075441_add_apartment_distribution_key.rb +15 -0
- data/db/migrate/20260907100000_add_apartment_distribution_hosts.rb +32 -0
- data/lib/decidim/apartment/active_storage_wrap.rb +51 -0
- data/lib/decidim/apartment/allowed_hosts.rb +78 -0
- data/lib/decidim/apartment/cache_namespace.rb +38 -0
- data/lib/decidim/apartment/config_registry.rb +102 -0
- data/lib/decidim/apartment/configuration.rb +11 -0
- data/lib/decidim/apartment/convert/attachment_integrity.rb +130 -0
- data/lib/decidim/apartment/convert/constraint_graph_pruner.rb +183 -0
- data/lib/decidim/apartment/convert/errors.rb +9 -0
- data/lib/decidim/apartment/convert/host_gate.rb +76 -0
- data/lib/decidim/apartment/convert/org_distribution_report.rb +106 -0
- data/lib/decidim/apartment/convert/org_metrics.rb +164 -0
- data/lib/decidim/apartment/convert/organization_converter.rb +154 -0
- data/lib/decidim/apartment/convert/organization_pruner.rb +210 -0
- data/lib/decidim/apartment/convert/organization_resolver.rb +386 -0
- data/lib/decidim/apartment/convert/paper_trail_report.rb +127 -0
- data/lib/decidim/apartment/convert/preflight.rb +140 -0
- data/lib/decidim/apartment/convert/schema_cloner.rb +157 -0
- data/lib/decidim/apartment/convert/storage_preconditions.rb +43 -0
- data/lib/decidim/apartment/convert/storage_relocator.rb +118 -0
- data/lib/decidim/apartment/convert/table_registry.rb +109 -0
- data/lib/decidim/apartment/convert.rb +20 -0
- data/lib/decidim/apartment/demo/blob.rb +31 -0
- data/lib/decidim/apartment/demo/city.rb +107 -0
- data/lib/decidim/apartment/demo/education.rb +83 -0
- data/lib/decidim/apartment/demo/foss.rb +153 -0
- data/lib/decidim/apartment/demo/homepage.rb +39 -0
- data/lib/decidim/apartment/demo/localized.rb +27 -0
- data/lib/decidim/apartment/demo/provision.rb +107 -0
- data/lib/decidim/apartment/demo/runner.rb +66 -0
- data/lib/decidim/apartment/demo/system_admin.rb +24 -0
- data/lib/decidim/apartment/demo.rb +19 -0
- data/lib/decidim/apartment/elevator.rb +20 -0
- data/lib/decidim/apartment/engine.rb +140 -0
- data/lib/decidim/apartment/excluded_model_pin.rb +40 -0
- data/lib/decidim/apartment/host_registry.rb +71 -0
- data/lib/decidim/apartment/import/confirm_prompt.rb +46 -0
- data/lib/decidim/apartment/import/database_restorer.rb +105 -0
- data/lib/decidim/apartment/import/dump_host_extractor.rb +67 -0
- data/lib/decidim/apartment/import/dump_preview.rb +96 -0
- data/lib/decidim/apartment/import/dump_transformer.rb +87 -0
- data/lib/decidim/apartment/import/errors.rb +9 -0
- data/lib/decidim/apartment/import/provision_tenant.rb +50 -0
- data/lib/decidim/apartment/import/schema_auditor.rb +118 -0
- data/lib/decidim/apartment/import/storage_migrator.rb +71 -0
- data/lib/decidim/apartment/import/tenant_context.rb +226 -0
- data/lib/decidim/apartment/import/tenant_importer.rb +73 -0
- data/lib/decidim/apartment/import/verifier.rb +58 -0
- data/lib/decidim/apartment/import.rb +15 -0
- data/lib/decidim/apartment/ops/attribute_cryptor.rb +68 -0
- data/lib/decidim/apartment/ops/ciphertext_rewriter.rb +230 -0
- data/lib/decidim/apartment/ops/encrypted_attribute_catalog.rb +106 -0
- data/lib/decidim/apartment/ops/encrypted_attribute_registry.rb +74 -0
- data/lib/decidim/apartment/ops/postgres_permissions.rb +179 -0
- data/lib/decidim/apartment/ops/secret_key_preflight.rb +94 -0
- data/lib/decidim/apartment/ops/secret_key_rotation.rb +226 -0
- data/lib/decidim/apartment/ops/storage_transfer.rb +188 -0
- data/lib/decidim/apartment/overrides/active_job_with_tenant.rb +38 -0
- data/lib/decidim/apartment/overrides/core_seeds.rb +31 -0
- data/lib/decidim/apartment/overrides/create_organization_command.rb +69 -0
- data/lib/decidim/apartment/overrides/dashboard_controller.rb +30 -0
- data/lib/decidim/apartment/overrides/decidim_seed.rb +16 -0
- data/lib/decidim/apartment/overrides/organizations_controller_overrides.rb +75 -0
- data/lib/decidim/apartment/overrides/postgresql_enable_extension.rb +15 -0
- data/lib/decidim/apartment/overrides/postgresql_schema_adapter.rb +34 -0
- data/lib/decidim/apartment/overrides/postgresql_schema_import.rb +35 -0
- data/lib/decidim/apartment/overrides/postgresql_schema_search_path.rb +15 -0
- data/lib/decidim/apartment/overrides/postgresql_schema_statements.rb +17 -0
- data/lib/decidim/apartment/overrides/seeds.rb +52 -0
- data/lib/decidim/apartment/overrides/tenant_init_without_database.rb +19 -0
- data/lib/decidim/apartment/overrides/update_organization_command.rb +38 -0
- data/lib/decidim/apartment/postgres_extensions.rb +198 -0
- data/lib/decidim/apartment/rake_replace.rb +21 -0
- data/lib/decidim/apartment/schema_search_path.rb +43 -0
- data/lib/decidim/apartment/system_organization.rb +67 -0
- data/lib/decidim/apartment/test/factories.rb +11 -0
- data/lib/decidim/apartment/test/seeds/association_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/association_2.jpeg +0 -0
- data/lib/decidim/apartment/test/seeds/association_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/association_4.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Dusty Grass.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Hyper Blue.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Moonwalker.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Neon Green.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_Visual Blue.png +0 -0
- data/lib/decidim/apartment/test/seeds/bg_hinehouse.png +0 -0
- data/lib/decidim/apartment/test/seeds/city_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/city_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/city_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/city_4.webp +0 -0
- data/lib/decidim/apartment/test/seeds/city_5.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/debian.jpeg +0 -0
- data/lib/decidim/apartment/test/seeds/democratic_school_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/drupal.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/foss.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/immich.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/kid_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/kid_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/montessori_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/montessori_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/neighboor_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/neighbor_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/neighbor_3.jpg +0 -0
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- data/lib/decidim/apartment/test/seeds/pattern_new_01.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_new_02.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_new_03.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_01.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_02.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_03.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_04.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_05.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_06.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_07.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_08.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_09.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_10.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_11.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_12.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_13.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_14.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_15.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_16.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_01.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_02.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_03.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_04.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_05.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_06.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_07.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_08.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_09.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_10.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_11.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_12.png +0 -0
- data/lib/decidim/apartment/test/seeds/pattern_squares_and_circles_13.png +0 -0
- data/lib/decidim/apartment/test/seeds/rails.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/school_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/school_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/school_4.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/school_5.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/silverstripe.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/street_1.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/street_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/street_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/urban_agri_1.webp +0 -0
- data/lib/decidim/apartment/test/seeds/urban_agri_2.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/urban_agri_3.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/urban_agri_4.jpg +0 -0
- data/lib/decidim/apartment/test/seeds/world-map-colors.png +0 -0
- data/lib/decidim/apartment/version.rb +44 -0
- data/lib/decidim/apartment.rb +69 -0
- data/lib/tasks/decidim_apartment_check_install.rake +58 -0
- data/lib/tasks/decidim_apartment_convert.rake +60 -0
- data/lib/tasks/decidim_apartment_gem_manager.rake +62 -0
- data/lib/tasks/decidim_apartment_import.rake +39 -0
- data/lib/tasks/decidim_apartment_install_extension.rake +49 -0
- data/lib/tasks/decidim_apartment_performance_seed.rake +12 -0
- data/lib/tasks/decidim_apartment_rotate.rake +17 -0
- data/lib/tasks/decidim_apartment_storage.rake +27 -0
- data/lib/tasks/decidim_download_your_data_tasks.rake +121 -0
- data/lib/tasks/decidim_mailers.rake +38 -0
- data/lib/tasks/decidim_meetings.rake +30 -0
- data/lib/tasks/decidim_metrics_tasks.rake +29 -0
- data/lib/tasks/decidim_open_data_tasks.rake +24 -0
- data/lib/tasks/decidim_participatory_processes.rake +20 -0
- data/lib/tasks/decidim_reminders_tasks.rake +24 -0
- metadata +361 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Overrides
|
|
6
|
+
##
|
|
7
|
+
# ros-apartment clones public via pg_dump with search_path = tenant, public only.
|
|
8
|
+
# Extensions live in public (persistent_schemas) — keep public on the clone path.
|
|
9
|
+
# Prepended onto Apartment::Adapters::PostgresqlSchemaFromSqlAdapter (class defines
|
|
10
|
+
# patch_search_path itself, so include would not override).
|
|
11
|
+
module PostgresqlSchemaAdapter
|
|
12
|
+
# PostgreSQL 16+ pg_dump emits psql meta-commands that are not valid SQL.
|
|
13
|
+
PG16_DUMP_BLACKLISTED_STATEMENTS = [
|
|
14
|
+
/\A\\restrict\b/i,
|
|
15
|
+
/\A\\unrestrict\b/i
|
|
16
|
+
].freeze
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def patch_search_path(sql)
|
|
21
|
+
schemas = ["\"#{current}\"", default_tenant, *::Apartment.persistent_schemas].uniq.join(", ")
|
|
22
|
+
search_path = "SET search_path = #{schemas};"
|
|
23
|
+
blacklist = self.class::PSQL_DUMP_BLACKLISTED_STATEMENTS + PG16_DUMP_BLACKLISTED_STATEMENTS
|
|
24
|
+
|
|
25
|
+
swap_schema_qualifier(sql)
|
|
26
|
+
.split("\n")
|
|
27
|
+
.select { |line| check_input_against_regexps(line, blacklist).empty? }
|
|
28
|
+
.prepend(search_path)
|
|
29
|
+
.join("\n")
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Overrides
|
|
6
|
+
# schema.rb uses create_table force: :cascade. Unqualified DROP TABLE walks
|
|
7
|
+
# search_path and removes public tables when the tenant copy does not exist
|
|
8
|
+
# yet. Qualify drops to the tenant while the schema file loads.
|
|
9
|
+
# Types still resolve via persistent public on search_path.
|
|
10
|
+
# Prepended onto Apartment::Adapters::PostgresqlSchemaAdapter (non-SQL path).
|
|
11
|
+
module PostgresqlSchemaImport
|
|
12
|
+
THREAD_KEY = :decidim_apartment_schema_import_tenant
|
|
13
|
+
|
|
14
|
+
def import_database_schema
|
|
15
|
+
Thread.current[THREAD_KEY] = current.to_s
|
|
16
|
+
super
|
|
17
|
+
ensure
|
|
18
|
+
Thread.current[THREAD_KEY] = nil
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Prepended onto ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.
|
|
23
|
+
module PostgresqlDropTableTenantScoped
|
|
24
|
+
def drop_table(table_name, **options)
|
|
25
|
+
tenant = Thread.current[Decidim::Apartment::Overrides::PostgresqlSchemaImport::THREAD_KEY]
|
|
26
|
+
if tenant
|
|
27
|
+
bare = table_name.to_s.delete('"').split(".").last
|
|
28
|
+
table_name = "#{tenant}.#{bare}"
|
|
29
|
+
end
|
|
30
|
+
super
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Overrides
|
|
6
|
+
# Rails db:migrate:status (and other tasks) open a temporary pool.
|
|
7
|
+
# after_initialize ensure! never sees those connections; $user is unquoted SET.
|
|
8
|
+
module PostgresqlSchemaSearchPath
|
|
9
|
+
def schema_search_path=(schema_search_path)
|
|
10
|
+
super(Decidim::Apartment::SchemaSearchPath.sanitize(schema_search_path))
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Overrides
|
|
6
|
+
# Rails dumps create_table "public.foo" when multiple PG schemas exist.
|
|
7
|
+
# AR validate_table_length! counted the qualifier (rails/rails#57093).
|
|
8
|
+
# ponytail: drop when Rails includes #57096 (schema-stripped length check)
|
|
9
|
+
module PostgresqlSchemaStatements
|
|
10
|
+
def validate_table_length!(table_name)
|
|
11
|
+
_schema, table_name = extract_schema_qualified_name(table_name)
|
|
12
|
+
super
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Overrides
|
|
6
|
+
module Seeds
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
def create_tenant!
|
|
11
|
+
host = ENV.fetch("DECIDIM_HOST", "localhost")
|
|
12
|
+
tenant_form = Decidim::Apartment::TenantForm.new({ host: host })
|
|
13
|
+
raise tenant_form.errors.full_messages.join(", ") unless tenant_form.valid?
|
|
14
|
+
|
|
15
|
+
Decidim::Apartment::CreateTenantCommand.call(tenant_form) do
|
|
16
|
+
on(:ok) do |distribution_key|
|
|
17
|
+
distribution_key
|
|
18
|
+
end
|
|
19
|
+
on(:invalid) do
|
|
20
|
+
raise "Failed to create tenant with host: #{host}"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def tenant
|
|
26
|
+
::Apartment::Tenant.switch("public") do
|
|
27
|
+
Decidim::Apartment::DistributionKey.first
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def tenant?
|
|
32
|
+
::Apartment::Tenant.switch("public") do
|
|
33
|
+
Decidim::Apartment::DistributionKey.exists?
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def ensure_tenant_switched!
|
|
38
|
+
create_tenant! unless tenant?
|
|
39
|
+
raise "Failed to find a tenant" unless tenant?
|
|
40
|
+
|
|
41
|
+
tenant.switch! if ::Apartment::Tenant.current != tenant.key
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def organization
|
|
45
|
+
ensure_tenant_switched!
|
|
46
|
+
Decidim::Organization.first
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Overrides
|
|
6
|
+
# Host to_prepare often calls Tenant.init. That connects to the app DB.
|
|
7
|
+
# db:create / db:drop boot environment first — the DB is missing.
|
|
8
|
+
# db:migrate on an empty DB boots before public tables exist.
|
|
9
|
+
# ponytail: adapter is built lazily on first real use
|
|
10
|
+
module TenantInitWithoutDatabase
|
|
11
|
+
def init
|
|
12
|
+
super
|
|
13
|
+
rescue ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
|
|
14
|
+
nil
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
module Overrides
|
|
6
|
+
##
|
|
7
|
+
# Overrides UpdateOrganizationCommand to handle tenant switch correctly
|
|
8
|
+
module UpdateOrganizationCommand
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
alias_method :apartment_origin_call, :call
|
|
13
|
+
def call
|
|
14
|
+
switch_tenant do
|
|
15
|
+
apartment_origin_call
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def switch_tenant(&block)
|
|
22
|
+
current_organization = organization
|
|
23
|
+
match_tenant = ::Decidim::Apartment::DistributionKey.for_host(
|
|
24
|
+
current_organization.host
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
match_tenant.switch(&block)
|
|
28
|
+
|
|
29
|
+
match_tenant.sync_hosts!(
|
|
30
|
+
primary_host: form.host,
|
|
31
|
+
secondary_hosts: form.secondary_hosts
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
# Installs PostgreSQL extensions in public. Moves relocatable leftovers out
|
|
6
|
+
# of shared_extensions; aborts if a PostGIS-family extension sits elsewhere.
|
|
7
|
+
class PostgresExtensions
|
|
8
|
+
SCHEMA = "public"
|
|
9
|
+
LEGACY_SCHEMA = "shared_extensions"
|
|
10
|
+
REQUIRED = %w(hstore ltree pg_trgm plpgsql fuzzystrmatch).freeze
|
|
11
|
+
OPTIONAL = %w(postgis).freeze
|
|
12
|
+
POSTGIS_FAMILY = %w(postgis postgis_raster postgis_sfcgal postgis_topology).freeze
|
|
13
|
+
ALLOWED_POSTGIS_SCHEMAS = %w(public pg_catalog).freeze
|
|
14
|
+
|
|
15
|
+
def self.prepare!(connection = ActiveRecord::Base.connection)
|
|
16
|
+
new(connection).prepare!
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def initialize(connection = ActiveRecord::Base.connection)
|
|
20
|
+
@connection = connection
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def prepare!
|
|
24
|
+
assert_postgis_in_public!
|
|
25
|
+
relocate_legacy!
|
|
26
|
+
apartment_install_extensions
|
|
27
|
+
drop_empty_legacy_schema!
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def ready?
|
|
31
|
+
missing(REQUIRED).empty? && legacy_names.empty?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def enable!(name)
|
|
35
|
+
ident = name.to_s.split(".").last
|
|
36
|
+
assert_postgis_in_public!
|
|
37
|
+
relocate_named!(ident) if in_legacy?(ident)
|
|
38
|
+
install_extension(ident)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
attr_reader :connection
|
|
44
|
+
|
|
45
|
+
def quoted_schema
|
|
46
|
+
connection.quote_table_name(SCHEMA)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def quoted_legacy_schema
|
|
50
|
+
connection.quote_table_name(LEGACY_SCHEMA)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def assert_postgis_in_public!
|
|
54
|
+
POSTGIS_FAMILY.each do |name|
|
|
55
|
+
schema = extension_schema(name)
|
|
56
|
+
next if schema.nil? || ALLOWED_POSTGIS_SCHEMAS.include?(schema)
|
|
57
|
+
|
|
58
|
+
raise ActiveRecord::StatementInvalid,
|
|
59
|
+
"Decidim Apartment: extension #{name.inspect} is in schema " \
|
|
60
|
+
"#{schema.inspect}; PostGIS must live in public " \
|
|
61
|
+
"(no supported SET SCHEMA path)."
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def relocate_legacy!
|
|
66
|
+
legacy_names.each { |name| relocate_named!(name) }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def relocate_named!(name)
|
|
70
|
+
connection.execute(
|
|
71
|
+
"ALTER EXTENSION #{connection.quote_table_name(name)} SET SCHEMA #{quoted_schema}"
|
|
72
|
+
)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def legacy_names
|
|
76
|
+
connection.select_values(<<~SQL.squish)
|
|
77
|
+
SELECT e.extname
|
|
78
|
+
FROM pg_extension e
|
|
79
|
+
JOIN pg_namespace n ON n.oid = e.extnamespace
|
|
80
|
+
WHERE n.nspname = #{connection.quote(LEGACY_SCHEMA)}
|
|
81
|
+
ORDER BY e.extname
|
|
82
|
+
SQL
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def in_legacy?(name)
|
|
86
|
+
legacy_names.include?(name)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def extension_schema(name)
|
|
90
|
+
connection.select_value(<<~SQL.squish)
|
|
91
|
+
SELECT n.nspname
|
|
92
|
+
FROM pg_extension e
|
|
93
|
+
JOIN pg_namespace n ON n.oid = e.extnamespace
|
|
94
|
+
WHERE e.extname = #{connection.quote(name)}
|
|
95
|
+
SQL
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def exists?(name)
|
|
99
|
+
connection.select_value(<<~SQL.squish).present?
|
|
100
|
+
SELECT 1 FROM pg_extension WHERE extname = #{connection.quote(name)}
|
|
101
|
+
SQL
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def missing(names)
|
|
105
|
+
names.reject { |name| exists?(name) }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def apartment_install_extensions
|
|
109
|
+
install_missing(REQUIRED)
|
|
110
|
+
install_available(OPTIONAL)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def install_missing(names)
|
|
114
|
+
missing(names).each { |name| install_extension(name) }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def install_available(names)
|
|
118
|
+
names.each { |name| install_extension(name) if available?(name) }
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def install_extension(name)
|
|
122
|
+
return if exists?(name)
|
|
123
|
+
return skip_unprivileged(name) unless can_create_extension?(name)
|
|
124
|
+
|
|
125
|
+
create_extension(name)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def can_create_extension?(name)
|
|
129
|
+
trusted_extension?(name) || superuser? || rds_superuser?
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def skip_unprivileged(name)
|
|
133
|
+
warn(<<~MSG.squish)
|
|
134
|
+
Decidim Apartment: skipping CREATE EXTENSION #{name}
|
|
135
|
+
(insufficient privilege; user must be a superuser).
|
|
136
|
+
MSG
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def trusted_extension?(name)
|
|
140
|
+
boolean_sql(connection.select_value(<<~SQL.squish))
|
|
141
|
+
SELECT trusted FROM pg_available_extensions
|
|
142
|
+
WHERE name = #{connection.quote(name)}
|
|
143
|
+
SQL
|
|
144
|
+
rescue ActiveRecord::StatementInvalid
|
|
145
|
+
false
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def superuser?
|
|
149
|
+
connection.select_value("SHOW is_superuser").to_s == "on"
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def rds_superuser?
|
|
153
|
+
boolean_sql(connection.select_value(<<~SQL.squish))
|
|
154
|
+
SELECT pg_has_role(current_user, 'rds_superuser', 'member')
|
|
155
|
+
SQL
|
|
156
|
+
rescue ActiveRecord::StatementInvalid
|
|
157
|
+
false
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def boolean_sql(value)
|
|
161
|
+
ActiveRecord::Type::Boolean.new.cast(value) == true
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def available?(name)
|
|
165
|
+
connection.select_value(<<~SQL.squish).present?
|
|
166
|
+
SELECT 1 FROM pg_available_extensions WHERE name = #{connection.quote(name)}
|
|
167
|
+
SQL
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def create_extension(name)
|
|
171
|
+
connection.execute(
|
|
172
|
+
"CREATE EXTENSION IF NOT EXISTS #{connection.quote_table_name(name)} SCHEMA #{quoted_schema}"
|
|
173
|
+
)
|
|
174
|
+
rescue ActiveRecord::StatementInvalid => e
|
|
175
|
+
raise unless privilege_error?(e)
|
|
176
|
+
|
|
177
|
+
skip_unprivileged(name)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def privilege_error?(error)
|
|
181
|
+
error.message.match?(/insufficient privilege|permission denied|must be superuser/i)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def drop_empty_legacy_schema!
|
|
185
|
+
return unless legacy_schema_exists?
|
|
186
|
+
return unless legacy_names.empty?
|
|
187
|
+
|
|
188
|
+
connection.execute("DROP SCHEMA #{quoted_legacy_schema}")
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def legacy_schema_exists?
|
|
192
|
+
connection.select_value(<<~SQL.squish).present?
|
|
193
|
+
SELECT 1 FROM pg_namespace WHERE nspname = #{connection.quote(LEGACY_SCHEMA)}
|
|
194
|
+
SQL
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
# Drop core rake actions so they do not run in public before the tenant loop.
|
|
6
|
+
module RakeReplace
|
|
7
|
+
def self.call(name, with:)
|
|
8
|
+
return unless Rake::Task.task_defined?(name)
|
|
9
|
+
|
|
10
|
+
Rake::Task[name].tap do |task|
|
|
11
|
+
task.clear_actions
|
|
12
|
+
task.enhance do |_, args|
|
|
13
|
+
child = Rake::Task[with]
|
|
14
|
+
child.reenable
|
|
15
|
+
child.invoke(*args.to_a)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
##
|
|
6
|
+
# Ensures +public+ without host database.yml edits.
|
|
7
|
+
class SchemaSearchPath
|
|
8
|
+
REQUIRED = %w(public).freeze
|
|
9
|
+
# PostgreSQL current-user schema token; unquoted `$user` breaks SET search_path.
|
|
10
|
+
USER_SCHEMA = "$user"
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
def ensure!
|
|
14
|
+
apply_to_connection!
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def schema_names(path)
|
|
18
|
+
path.to_s.delete('"').split(",").map(&:strip).compact_blank
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def sanitize(path)
|
|
22
|
+
return path.to_s if hyphenated_tenant_path?(path)
|
|
23
|
+
|
|
24
|
+
((schema_names(path) - [USER_SCHEMA]) | REQUIRED).join(",")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def hyphenated_tenant_path?(path)
|
|
28
|
+
schema_names(path).any? { |name| name.include?("-") }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.apply_to_connection!
|
|
33
|
+
conn = ActiveRecord::Base.connection
|
|
34
|
+
sanitized = sanitize(conn.schema_search_path)
|
|
35
|
+
return if schema_names(conn.schema_search_path) == schema_names(sanitized)
|
|
36
|
+
|
|
37
|
+
conn.schema_search_path = sanitized
|
|
38
|
+
rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError, PG::Error
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Apartment
|
|
5
|
+
# Publishes the tenant organization onto the request for system edit/update.
|
|
6
|
+
# Elevator keeps /system on public; this runs after CurrentOrganization.
|
|
7
|
+
class SystemOrganization
|
|
8
|
+
EDIT_PATH = %r{\A/system/organizations/([^/]+)/edit\z}
|
|
9
|
+
UPDATE_PATH = %r{\A/system/organizations/([^/]+)\z}
|
|
10
|
+
EDIT_METHODS = %w(GET HEAD).freeze
|
|
11
|
+
UPDATE_METHODS = %w(PATCH PUT POST).freeze
|
|
12
|
+
|
|
13
|
+
def initialize(app)
|
|
14
|
+
@app = app
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call(env)
|
|
18
|
+
publish!(env)
|
|
19
|
+
@app.call(env)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def publish!(env)
|
|
25
|
+
request = ActionDispatch::Request.new(env)
|
|
26
|
+
id = path_id(request)
|
|
27
|
+
return unless id
|
|
28
|
+
|
|
29
|
+
organization = load_organization(id, request.params["tenant"])
|
|
30
|
+
env["decidim.current_organization"] = organization if organization
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def path_id(request)
|
|
34
|
+
path = request.path
|
|
35
|
+
method = request.request_method
|
|
36
|
+
return EDIT_PATH.match(path)&.[](1) if EDIT_METHODS.include?(method)
|
|
37
|
+
return UPDATE_PATH.match(path)&.[](1) if UPDATE_METHODS.include?(method)
|
|
38
|
+
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def load_organization(id, tenant)
|
|
43
|
+
key = DistributionKey.for_system(id: id, tenant: tenant)
|
|
44
|
+
return unless key
|
|
45
|
+
|
|
46
|
+
warm_organization(key)
|
|
47
|
+
rescue ::Apartment::TenantNotFound
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def warm_organization(key)
|
|
52
|
+
key.switch do
|
|
53
|
+
organization = Decidim::Organization.first
|
|
54
|
+
warm_deepl!(organization)
|
|
55
|
+
organization
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def warm_deepl!(organization)
|
|
60
|
+
return if organization.blank?
|
|
61
|
+
return unless organization.respond_to?(:deepl_enabled?)
|
|
62
|
+
|
|
63
|
+
organization.deepl_enabled?
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "decidim/core/test/factories"
|
|
4
|
+
|
|
5
|
+
# frozen_string_literal: true
|
|
6
|
+
|
|
7
|
+
FactoryBot.define do
|
|
8
|
+
factory :distribution_key, class: "Decidim::Apartment::DistributionKey" do
|
|
9
|
+
host { Faker::Internet.domain_name }
|
|
10
|
+
end
|
|
11
|
+
end
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|