decidim-user_fields 0.0.1 → 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 (70) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +320 -55
  3. data/Rakefile +253 -0
  4. data/app/forms/decidim/custom_user_fields/admin/customizations_config_form.rb +67 -0
  5. data/app/forms/decidim/custom_user_fields/verifications/verification_form.rb +85 -0
  6. data/app/overrides/decidim/admin/officializations/index/_export_users_dropdown.html.erb.deface +1 -3
  7. data/app/overrides/decidim/devise/invitations/edit/extra_user_fields_form.html.erb.deface +2 -2
  8. data/app/overrides/decidim/devise/omniauth_registrations/new/extra_user_fields.html.erb.deface +3 -0
  9. data/app/overrides/invitation_override.rb +31 -0
  10. data/app/packs/entrypoints/decidim_custom_user_fields.js +0 -0
  11. data/app/packs/stylesheets/decidim/custom_user_fields/_decidim_custom_user_fields.scss +48 -0
  12. data/app/views/decidim/custom_user_fields/_profile_form.html.erb +2 -2
  13. data/app/views/decidim/custom_user_fields/_registration_form.html.erb +2 -2
  14. data/app/views/decidim/custom_user_fields/_verification_form.erb +4 -0
  15. data/app/views/decidim/custom_user_fields/system/_customizations_tab.html.erb +15 -0
  16. data/config/assets.rb +9 -0
  17. data/config/i18n-tasks.yml +22 -0
  18. data/config/locales/decidim_custom_user_fields_scenarios.en.yml +50 -0
  19. data/config/locales/decidim_custom_user_fields_scenarios.fr.yml +48 -0
  20. data/config/locales/decidim_toggle.en.yml +13 -0
  21. data/config/locales/decidim_toggle.fr.yml +13 -0
  22. data/config/locales/decidim_user_fields.en.yml +25 -0
  23. data/config/locales/decidim_user_fields.fr.yml +25 -0
  24. data/lib/decidim/custom_user_fields/custom_user_fields.rb +10 -14
  25. data/lib/decidim/custom_user_fields/customization_field_naming.rb +17 -0
  26. data/lib/decidim/custom_user_fields/customizations/builder.rb +48 -0
  27. data/lib/decidim/custom_user_fields/customizations/customization.rb +29 -0
  28. data/lib/decidim/custom_user_fields/customizations.rb +42 -0
  29. data/lib/decidim/custom_user_fields/decidim_integrations.rb +52 -0
  30. data/lib/decidim/custom_user_fields/definition_guidance.rb +109 -0
  31. data/lib/decidim/custom_user_fields/dev/bootstrap.rb +102 -0
  32. data/lib/decidim/custom_user_fields/dev/local_oidc_seeder.rb +150 -0
  33. data/lib/decidim/custom_user_fields/dev/omniauth_providers_patcher.rb +68 -0
  34. data/lib/decidim/custom_user_fields/dev/openid_connect_setup.rb +110 -0
  35. data/lib/decidim/custom_user_fields/dev/scenario_customizations.rb +72 -0
  36. data/lib/decidim/custom_user_fields/engine.rb +59 -26
  37. data/lib/decidim/custom_user_fields/extended_data.rb +52 -0
  38. data/lib/decidim/custom_user_fields/field_definition.rb +28 -6
  39. data/lib/decidim/custom_user_fields/fields/boolean_field.rb +51 -0
  40. data/lib/decidim/custom_user_fields/fields/date_field.rb +91 -22
  41. data/lib/decidim/custom_user_fields/fields/dummy_field.rb +40 -0
  42. data/lib/decidim/custom_user_fields/fields/extra_field_ref_field.rb +118 -0
  43. data/lib/decidim/custom_user_fields/fields/generic_field.rb +99 -6
  44. data/lib/decidim/custom_user_fields/fields/text_area_field.rb +51 -27
  45. data/lib/decidim/custom_user_fields/fields/text_field.rb +47 -6
  46. data/lib/decidim/custom_user_fields/i18n/customization_keys.rb +55 -0
  47. data/lib/decidim/custom_user_fields/i18n/customization_keys_scanner.rb +80 -0
  48. data/lib/decidim/custom_user_fields/i18n/tasks.rb +7 -0
  49. data/lib/decidim/custom_user_fields/overrides/authorizations_controller.rb +18 -0
  50. data/lib/decidim/custom_user_fields/overrides/command.rb +23 -21
  51. data/lib/decidim/custom_user_fields/overrides/confirmations_controller.rb +15 -0
  52. data/lib/decidim/custom_user_fields/overrides/devise_authentication_methods.rb +68 -0
  53. data/lib/decidim/custom_user_fields/overrides/form_definition.rb +27 -8
  54. data/lib/decidim/custom_user_fields/overrides/invitations_controller.rb +109 -0
  55. data/lib/decidim/custom_user_fields/overrides/omniauth_command.rb +40 -0
  56. data/lib/decidim/custom_user_fields/overrides/omniauth_registrations_controller.rb +93 -0
  57. data/lib/decidim/custom_user_fields/overrides/user.rb +16 -0
  58. data/lib/decidim/custom_user_fields/registration_fields.rb +99 -0
  59. data/lib/decidim/custom_user_fields/update_customizations_config_command.rb +39 -0
  60. data/lib/decidim/custom_user_fields/upgrade/migrate_toggle_config.rb +74 -0
  61. data/lib/decidim/custom_user_fields/verifications/builder.rb +100 -0
  62. data/lib/decidim/custom_user_fields/verifications/verifications.rb +82 -0
  63. data/lib/decidim/custom_user_fields/version.rb +4 -2
  64. data/lib/decidim/user_fields.rb +46 -13
  65. data/lib/tasks/decidim/custom_user_fields/custom_user_fields_upgrade.rake +16 -0
  66. data/lib/tasks/decidim/custom_user_fields/custom_user_fields_webpacker.rake +59 -0
  67. data/lib/tasks/decidim/custom_user_fields/decidim_custom_user_fields_upgrade_tasks.rake +16 -0
  68. data/lib/tasks/decidim/custom_user_fields/dev/local_setup.rake +16 -0
  69. metadata +81 -16
  70. data/app/overrides/decidim/devise/invitations/edit/extra_user_fields.html.erb.deface +0 -3
@@ -0,0 +1,13 @@
1
+ en:
2
+ decidim_toggle:
3
+ system:
4
+ custom_user_fields:
5
+ first_login_mode: "First login mode"
6
+ enabled_customization: "Customization"
7
+ none: "None"
8
+ birthdate_age_gates_enabled: "Birthdate age gates"
9
+ location_validation_enabled: "Location validation"
10
+ association_enabled: "Association representative"
11
+ helptext:
12
+ first_login_mode: "Choose whether participants are prompted to verify after their first sign-in. Authorizations remain available when actions require them."
13
+ enabled_customization: "Enable only one customization at a time to avoid conflicts. Choose none to keep extra fields and bundled authorizations off."
@@ -0,0 +1,13 @@
1
+ fr:
2
+ decidim_toggle:
3
+ system:
4
+ custom_user_fields:
5
+ first_login_mode: "Mode première connexion"
6
+ enabled_customization: "Personnalisation"
7
+ none: "Aucune"
8
+ birthdate_age_gates_enabled: "Contrôles d'âge par date de naissance"
9
+ location_validation_enabled: "Validation de localisation"
10
+ association_enabled: "Représentant d'association"
11
+ helptext:
12
+ first_login_mode: "Choisir si les participants sont invités à se vérifier après leur première connexion. Les autorisations restent disponibles lorsque les actions les exigent."
13
+ enabled_customization: "Activer une seule personnalisation à la fois pour éviter les conflits. Choisir aucune pour laisser les champs extra et les autorisations groupées désactivés."
@@ -0,0 +1,25 @@
1
+ en:
2
+ decidim:
3
+ custom_user_fields:
4
+ system:
5
+ customizations_tab: "User fields"
6
+ first_login_mode:
7
+ prompt_authorization: "Prompt authorization"
8
+ none: "None"
9
+ customizations:
10
+ incompatible_authorizations: "Incompatible with enabled authorizations: %{authorizations}"
11
+ unknown_customization: "Unknown customization: %{name}"
12
+ date:
13
+ required: "This date is required"
14
+ bad_format: "This date is badly formatted"
15
+ bad_not_before: "This date is invalid"
16
+ bad_not_after: "This date is invalid"
17
+ bad_date: "This date is invalid"
18
+ textarea:
19
+ required: "This text is required"
20
+ bad_length: "This text is invalid"
21
+ too_long: "Must be less than %{count} characters"
22
+ too_short: "Must be more than %{count} characters"
23
+ text:
24
+ bad_values: "This value is not allowed"
25
+ bad_format: "Please check this format again"
@@ -0,0 +1,25 @@
1
+ fr:
2
+ decidim:
3
+ custom_user_fields:
4
+ system:
5
+ customizations_tab: "Champs utilisateur"
6
+ first_login_mode:
7
+ prompt_authorization: "Demander une autorisation"
8
+ none: "Aucun"
9
+ customizations:
10
+ incompatible_authorizations: "Incompatible avec les autorisations activées : %{authorizations}"
11
+ unknown_customization: "Personnalisation inconnue : %{name}"
12
+ date:
13
+ required: "Cette date est obligatoire"
14
+ bad_format: "Cette date est mal formatée"
15
+ bad_not_before: "Cette date est invalide"
16
+ bad_not_after: "Cette date est invalide"
17
+ bad_date: "Cette date est invalide"
18
+ textarea:
19
+ required: "Ce texte est obligatoire"
20
+ bad_length: "Ce texte est invalide"
21
+ too_long: "Doit être moins de %{count} caractères"
22
+ too_short: "Doit être plus de %{count} caractères"
23
+ text:
24
+ bad_values: "Cette valeur n'est pas autorisée"
25
+ bad_format: "Vérifiez à nouveau ce format"
@@ -1,27 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/custom_user_fields/definition_guidance"
4
+
1
5
  module Decidim
2
6
  module CustomUserFields
3
7
  include ActiveSupport::Configurable
4
8
 
9
+ DSL_METHODS = [:configure, :register_customization].freeze
10
+
5
11
  def self.configure
6
12
  yield self
7
13
  end
8
14
 
9
- ##
10
- # If users should receive emails on notification
11
- # by default
12
- config_accessor :default_email_on_notification do
13
- false
14
- end
15
-
16
- def self.custom_fields
17
- @custom_fields ||= []
18
- end
19
-
20
- def self.add_field(field_type, field_definition)
21
- custom_fields.push(FieldDefinition.new(field_type, field_definition))
22
- self
15
+ def self.register_customization(name, &)
16
+ Customizations.register(name, &)
23
17
  end
24
18
 
25
19
  class Error < StandardError; end
20
+
21
+ extend DefinitionMissing
26
22
  end
27
23
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module CustomizationFieldNaming
6
+ module_function
7
+
8
+ def prefixed(customization_name, field_name)
9
+ name = field_name.to_s
10
+ prefix = "#{customization_name}_"
11
+ return field_name.to_sym if name.start_with?(prefix)
12
+
13
+ :"#{prefix}#{name}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module Customizations
6
+ class Builder
7
+ include DefinitionMissing
8
+
9
+ DSL_METHODS = [:registration_fields, :authorization].freeze
10
+
11
+ def initialize(customization)
12
+ @customization = customization
13
+ end
14
+
15
+ def registration_fields(&)
16
+ field_builder = RegistrationFieldsBuilder.new(@customization)
17
+ yield field_builder
18
+ end
19
+
20
+ def authorization(name, &)
21
+ Decidim::CustomUserFields::Verifications.register(name, customization: @customization.name, &)
22
+ end
23
+ end
24
+
25
+ class RegistrationFieldsBuilder
26
+ include DefinitionMissing
27
+
28
+ DSL_METHODS = [:add_field].freeze
29
+
30
+ def initialize(customization)
31
+ @customization = customization
32
+ end
33
+
34
+ def add_field(field_name, field_definition)
35
+ if RegistrationFields.reserved_key?(field_name.to_s.to_sym)
36
+ raise Decidim::CustomUserFields::Error,
37
+ "Field name #{field_name} is reserved by decidim-core extended_data"
38
+ end
39
+ field_name = CustomizationFieldNaming.prefixed(@customization.name, field_name)
40
+
41
+ @customization.fields.push(
42
+ FieldDefinition.new(field_name, field_definition, "extended_data")
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module Customizations
6
+ class Customization
7
+ attr_reader :name, :fields, :workflow_handlers
8
+
9
+ def initialize(name)
10
+ @name = name.to_sym
11
+ @fields = []
12
+ @workflow_handlers = []
13
+ end
14
+
15
+ def label
16
+ ::I18n.t(
17
+ @name,
18
+ scope: "decidim.custom_user_fields.customizations",
19
+ default: @name.to_s.humanize
20
+ )
21
+ end
22
+
23
+ def register_handler!(handler_name)
24
+ @workflow_handlers << handler_name.to_s unless @workflow_handlers.include?(handler_name.to_s)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "decidim/custom_user_fields/customizations/customization"
4
+ require "decidim/custom_user_fields/customizations/builder"
5
+
6
+ module Decidim
7
+ module CustomUserFields
8
+ module Customizations
9
+ class << self
10
+ def register(name, &)
11
+ customization = Customization.new(name)
12
+ registry[name.to_sym] = customization
13
+ builder = Builder.new(customization)
14
+ yield builder
15
+ customization
16
+ end
17
+
18
+ def find(name)
19
+ return if name.blank?
20
+
21
+ registry[name.to_sym]
22
+ end
23
+
24
+ def all
25
+ registry.values
26
+ end
27
+
28
+ delegate :any?, to: :registry
29
+
30
+ def clear!
31
+ registry.clear
32
+ end
33
+
34
+ private
35
+
36
+ def registry
37
+ @registry ||= {}
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module DecidimIntegrations
6
+ module_function
7
+
8
+ def apply!
9
+ include_form(Decidim::RegistrationForm) do |klass|
10
+ next if klass.respond_to?(:require_password_on_accepting)
11
+
12
+ klass.define_singleton_method(:require_password_on_accepting) do
13
+ Decidim::User.require_password_on_accepting
14
+ end
15
+ end
16
+
17
+ include_form(Decidim::OmniauthRegistrationForm)
18
+ include_form(Decidim::AccountForm)
19
+ prepend_to(Decidim::CreateRegistration, Command)
20
+ prepend_to(Decidim::CreateOmniauthRegistration, OmniauthCommand)
21
+ prepend_to(Decidim::Devise::OmniauthRegistrationsController, OmniauthRegistrationsController)
22
+ prepend_to(Decidim::UpdateAccount, Command)
23
+ prepend_to(Decidim::Devise::InvitationsController, InvitationAcceptExtendedData)
24
+ prepend_to(Decidim::Devise::SessionsController, FirstSignInPrompt)
25
+ prepend_to(Decidim::Devise::OmniauthRegistrationsController, FirstSignInPrompt)
26
+ prepend_to(Decidim::Devise::RegistrationsController, FirstSignInPrompt)
27
+ prepend_to(Decidim::Devise::InvitationsController, FirstSignInPrompt)
28
+ prepend_to(Decidim::Devise::ConfirmationsController, ConfirmationsController)
29
+ prepend_to(Decidim::Devise::ConfirmationsController, FirstSignInPrompt)
30
+ prepend_to(Decidim::Verifications::AuthorizationsController, AuthorizationsController)
31
+ prepend_to(Decidim::User, UserExtendedDataGuard)
32
+ end
33
+
34
+ def include_form(klass)
35
+ include_module(klass, FormDefinition)
36
+ yield klass if block_given?
37
+ end
38
+
39
+ def prepend_to(klass, mod)
40
+ return if klass.included_modules.include?(mod)
41
+
42
+ klass.prepend(mod)
43
+ end
44
+
45
+ def include_module(klass, mod)
46
+ return if klass.included_modules.include?(mod)
47
+
48
+ klass.include(mod)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "did_you_mean"
4
+
5
+ module Decidim
6
+ module CustomUserFields
7
+ # Builds Decidim::CustomUserFields::Error messages for unknown or removed DSL calls.
8
+ module DefinitionGuidance
9
+ LEGACY = {
10
+ custom_fields:
11
+ "Use Customizations.all / register_customization instead of custom_fields.",
12
+ register_field_set:
13
+ "Use register_customization instead of register_field_set.",
14
+ add_field:
15
+ "Use register_customization { registration_fields { |set| set.add_field ... } } " \
16
+ "instead of configure { add_field }."
17
+ }.freeze
18
+
19
+ SUPPORTED_FIELD_TYPES = [:dummy, :text, :textarea, :date, :extra_field_ref, :boolean].freeze
20
+
21
+ module_function
22
+
23
+ def raise_unknown!(receiver, method_name, suggestions: [])
24
+ raise Error, unknown_message(receiver, method_name, suggestions)
25
+ end
26
+
27
+ def unknown_message(receiver, method_name, suggestions)
28
+ [
29
+ "#{label(receiver)}##{method_name} is not a valid CustomUserFields DSL call.",
30
+ caller_hint,
31
+ hint_for(receiver, method_name, suggestions)
32
+ ].compact.join(" ")
33
+ end
34
+
35
+ def unsupported_type_message(type)
36
+ hint = type_suggestion(type)
37
+ base = "field type #{type} is not supported"
38
+ return "#{base} (supported: #{SUPPORTED_FIELD_TYPES.join(", ")})" if hint.blank?
39
+
40
+ "#{base}. Did you mean? #{hint}"
41
+ end
42
+
43
+ def hint_for(receiver, method_name, suggestions)
44
+ legacy = legacy_hint(receiver, method_name)
45
+ return legacy if legacy
46
+
47
+ match = spell(method_name.to_s, suggestions.map(&:to_s))
48
+ match ? "Did you mean? #{match}" : nil
49
+ end
50
+
51
+ def legacy_hint(receiver, method_name)
52
+ key = method_name.to_sym
53
+ return LEGACY[key] if key != :add_field
54
+ return LEGACY[key] if module_receiver?(receiver)
55
+
56
+ nil
57
+ end
58
+
59
+ def type_suggestion(type)
60
+ spell(type.to_s, SUPPORTED_FIELD_TYPES.map(&:to_s))
61
+ end
62
+
63
+ def spell(input, dictionary)
64
+ DidYouMean::SpellChecker.new(dictionary:).correct(input).first
65
+ end
66
+
67
+ def module_receiver?(receiver)
68
+ receiver.equal?(Decidim::CustomUserFields)
69
+ end
70
+
71
+ def label(receiver)
72
+ receiver.is_a?(Module) ? receiver.name : receiver.class.name
73
+ end
74
+
75
+ def caller_hint
76
+ loc = caller_locations.find { |entry| !gem_path?(entry.path) }
77
+ return unless loc
78
+
79
+ "(called from #{loc.path}:#{loc.lineno})"
80
+ end
81
+
82
+ def gem_path?(path)
83
+ path.include?("custom_user_fields") || path.include?("definition_guidance")
84
+ end
85
+ end
86
+
87
+ # Raises DefinitionGuidance errors from method_missing on DSL receivers.
88
+ module DefinitionMissing
89
+ def method_missing(method_name, *_args, &)
90
+ DefinitionGuidance.raise_unknown!(
91
+ self,
92
+ method_name,
93
+ suggestions: dsl_method_names
94
+ )
95
+ end
96
+
97
+ def respond_to_missing?(_method_name, _include_private = false)
98
+ false
99
+ end
100
+
101
+ def dsl_method_names
102
+ owner = is_a?(Module) ? self : self.class
103
+ return [] unless owner.const_defined?(:DSL_METHODS, false)
104
+
105
+ owner::DSL_METHODS
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module Dev
6
+ # Minimal Decidim org + admin for local OIDC (no full Decidim.seed! demo data).
7
+ class Bootstrap
8
+ ADMIN_EMAIL = "admin@example.org"
9
+ ADMIN_PASSWORD = "decidim123456789"
10
+
11
+ def self.call(**)
12
+ new(**).call
13
+ end
14
+
15
+ def initialize(organization_host: ENV.fetch("DECIDIM_HOST", "localhost"))
16
+ @organization_host = organization_host
17
+ end
18
+
19
+ def call
20
+ organization = ensure_organization!
21
+ ensure_terms_of_service!(organization)
22
+ ensure_admin!(organization)
23
+ ensure_system_admin!
24
+ organization
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :organization_host
30
+
31
+ def ensure_organization!
32
+ Decidim::Organization.find_by(host: organization_host) || Decidim::Organization.create!(
33
+ name: localized("Custom User Fields Dev"),
34
+ host: organization_host,
35
+ secondary_hosts: %w(0.0.0.0 127.0.0.1),
36
+ default_locale: Decidim.default_locale,
37
+ available_locales: Decidim.available_locales,
38
+ reference_prefix: "dev",
39
+ description: localized("Local development organization"),
40
+ smtp_settings: {
41
+ from: "decidim-local <#{ADMIN_EMAIL}>",
42
+ from_email: ADMIN_EMAIL,
43
+ from_label: "decidim-local",
44
+ address: ENV.fetch("SMTP_ADDRESS", "mailcatcher"),
45
+ port: ENV.fetch("SMTP_PORT", "1025"),
46
+ user_name: ENV.fetch("SMTP_USERNAME", ""),
47
+ encrypted_password: Decidim::AttributeEncryptor.encrypt(ENV.fetch("SMTP_PASSWORD", ""))
48
+ },
49
+ colors: { primary: "#bf4044", secondary: "#09780e", tertiary: "#3584e4" },
50
+ available_authorizations: []
51
+ )
52
+ end
53
+
54
+ def ensure_terms_of_service!(organization)
55
+ return if Decidim::StaticPage.exists?(organization:, slug: "terms-of-service")
56
+
57
+ tos_page = Decidim::StaticPage.create!(
58
+ organization:,
59
+ slug: "terms-of-service",
60
+ title: localized("Terms of service"),
61
+ content: localized("Terms of service for local development."),
62
+ allow_public_access: true
63
+ )
64
+ organization.update!(tos_version: tos_page.updated_at)
65
+ end
66
+
67
+ def ensure_admin!(organization)
68
+ user = Decidim::User.find_or_initialize_by(email: ADMIN_EMAIL, organization:)
69
+ user.assign_attributes(
70
+ name: "Admin",
71
+ nickname: "admin",
72
+ password: ADMIN_PASSWORD,
73
+ password_confirmation: ADMIN_PASSWORD,
74
+ confirmed_at: Time.current,
75
+ admin: true,
76
+ admin_terms_accepted_at: Time.current,
77
+ tos_agreement: true,
78
+ accepted_tos_version: organization.tos_version,
79
+ newsletter_notifications_at: Time.current,
80
+ locale: organization.default_locale
81
+ )
82
+ user.save!
83
+ user
84
+ end
85
+
86
+ def ensure_system_admin!
87
+ user = Decidim::System::Admin.find_or_initialize_by(email: "system@example.org")
88
+ user.assign_attributes(
89
+ password: "decidim123456789",
90
+ password_confirmation: "decidim123456789"
91
+ )
92
+ user.save!
93
+ user
94
+ end
95
+
96
+ def localized(text)
97
+ Decidim.available_locales.index_with { |_locale| text }
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module Decidim
6
+ module CustomUserFields
7
+ module Dev
8
+ # Configures the demo organization for local OIDC + scenario customizations.
9
+ class LocalOidcSeeder
10
+ DEFAULT_CUSTOMIZATION = :association
11
+
12
+ def self.call(**)
13
+ new(**).call
14
+ end
15
+
16
+ def initialize(
17
+ oidc_env_path: default_oidc_env_path,
18
+ customization: ENV.fetch("DEV_CUSTOMIZATION", DEFAULT_CUSTOMIZATION).to_sym,
19
+ organization_host: ENV.fetch("DECIDIM_HOST", "localhost")
20
+ )
21
+ @oidc_env_path = Pathname.new(oidc_env_path)
22
+ @customization = customization
23
+ @organization_host = organization_host
24
+ end
25
+
26
+ def call
27
+ register_scenarios!
28
+ organization = ensure_organization!
29
+ apply_oidc_settings!(organization)
30
+ enable_customization!(organization)
31
+ enable_authorizations!(organization)
32
+ print_summary(organization)
33
+ organization
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :oidc_env_path, :customization, :organization_host
39
+
40
+ def default_oidc_env_path
41
+ module_root = File.expand_path("../../../..", __dir__)
42
+ Pathname.new(module_root).join("docker/zitadel/oidc.env")
43
+ end
44
+
45
+ def register_scenarios!
46
+ return unless defined?(Decidim::CustomUserFields::ScenarioCustomizations)
47
+
48
+ Decidim::CustomUserFields::ScenarioCustomizations.register!
49
+ end
50
+
51
+ def ensure_organization!
52
+ Decidim::Organization.find_by(host: organization_host) || Bootstrap.call(organization_host:)
53
+ end
54
+
55
+ def apply_oidc_settings!(organization)
56
+ env = load_oidc_env
57
+ settings = organization.omniauth_settings || {}
58
+ public_issuer = env.fetch("OIDC_ISSUER")
59
+ internal_issuer = env.fetch("OIDC_ISSUER_INTERNAL", public_issuer)
60
+ redirect_uri = env.fetch("OIDC_REDIRECT_URI")
61
+ client_id = env.fetch("OIDC_CLIENT_ID")
62
+ client_secret = env.fetch("OIDC_CLIENT_SECRET")
63
+ URI.parse(internal_issuer)
64
+ public_uri = URI.parse(public_issuer)
65
+
66
+ settings.merge!(
67
+ "omniauth_settings_openid_connect_enabled" => true,
68
+ "omniauth_settings_openid_connect_name" => "Zitadel",
69
+ "omniauth_settings_openid_connect_scope" => "openid email profile",
70
+ "omniauth_settings_openid_connect_issuer" => public_issuer,
71
+ "omniauth_settings_openid_connect_uid_field" => "sub",
72
+ "omniauth_settings_openid_connect_response_type" => "code",
73
+ "omniauth_settings_openid_connect_discovery" => false,
74
+ "omniauth_settings_openid_connect_icon" => "phone-line",
75
+ "omniauth_settings_openid_connect_client_options__host" => public_uri.host,
76
+ "omniauth_settings_openid_connect_client_options__port" => public_uri.port.to_s,
77
+ "omniauth_settings_openid_connect_client_options__scheme" => public_uri.scheme,
78
+ "omniauth_settings_openid_connect_client_options__identifier" => client_id,
79
+ "omniauth_settings_openid_connect_client_options__secret" => client_secret,
80
+ "omniauth_settings_openid_connect_client_options__redirect_uri" => redirect_uri,
81
+ "omniauth_settings_openid_connect_client_options__authorization_endpoint" =>
82
+ "#{public_issuer}/oauth/v2/authorize",
83
+ "omniauth_settings_openid_connect_client_options__token_endpoint" =>
84
+ "#{internal_issuer}/oauth/v2/token",
85
+ "omniauth_settings_openid_connect_client_options__userinfo_endpoint" =>
86
+ "#{internal_issuer}/oidc/v1/userinfo",
87
+ "omniauth_settings_openid_connect_client_options__jwks_uri" =>
88
+ "#{internal_issuer}/oauth/v2/keys",
89
+ "omniauth_settings_openid_connect_client_options__end_session_endpoint" =>
90
+ "#{public_issuer}/oidc/v1/end_session"
91
+ )
92
+
93
+ organization.update!(omniauth_settings: encrypt_omniauth_settings(settings))
94
+ end
95
+
96
+ def encrypt_omniauth_settings(settings)
97
+ settings.transform_values do |value|
98
+ Decidim::OmniauthProvider.value_defined?(value) ? Decidim::AttributeEncryptor.encrypt(value) : value
99
+ end
100
+ end
101
+
102
+ def enable_customization!(organization)
103
+ Decidim::Toggle.save_config!(
104
+ organization,
105
+ :custom_user_fields,
106
+ { "enabled_customization" => customization.to_s },
107
+ merge: false
108
+ )
109
+ end
110
+
111
+ def enable_authorizations!(organization)
112
+ handlers = Decidim::CustomUserFields::Verifications.workflow_handlers_for(customization)
113
+ return if handlers.empty?
114
+
115
+ current = Array(organization.available_authorizations)
116
+ organization.update!(
117
+ available_authorizations: (current + handlers).uniq
118
+ )
119
+ end
120
+
121
+ def load_oidc_env
122
+ raise "Missing #{oidc_env_path}. Run: ./bin/dev-oidc-up" unless oidc_env_path.file?
123
+
124
+ env = oidc_env_path.each_line.with_object({}) do |line, memo|
125
+ next if line.strip.empty? || line.start_with?("#")
126
+
127
+ key, value = line.split("=", 2)
128
+ memo[key] = value.to_s.strip
129
+ end
130
+ %w(OIDC_ISSUER OIDC_REDIRECT_URI OIDC_CLIENT_ID OIDC_CLIENT_SECRET).each do |key|
131
+ raise "Missing #{key} in #{oidc_env_path}" if env[key].blank?
132
+ end
133
+ env
134
+ end
135
+
136
+ def print_summary(organization)
137
+ Rails.logger.debug { <<~MSG }
138
+
139
+ decidim_custom_user_fields:dev:seed complete
140
+ Organization: #{organization.name} (#{organization.host})
141
+ Customization: #{customization} enabled
142
+ OIDC provider: Zitadel (openid_connect)
143
+ Login: http://localhost:3000/users/sign_in
144
+
145
+ MSG
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end