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,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module FirstSignInPrompt
6
+ # Decidim 0.32+ post-login prompt -- NB will be overridden by stored onboarding
7
+ def after_sign_in_path_for(user)
8
+ if user.present? && user.blocked?
9
+ check_user_block_status(user)
10
+ else
11
+ prompt_first_login_path(user) || super
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ # Not all Devise controllers include Decidim::DeviseAuthenticationMethods
18
+ def pending_redirect?(user)
19
+ store_location_for(user, stored_location_for(user))
20
+ end
21
+
22
+ def first_verifiable_sign_in?(user)
23
+ return true if user.sign_in_count == 1
24
+ return false unless user.confirmed?
25
+
26
+ user.last_sign_in_at.present? && user.last_sign_in_at < user.confirmed_at
27
+ end
28
+
29
+ def prompt_first_login_path(user)
30
+ return unless first_login_prompt?(user)
31
+
32
+ first_login_authorization_path(pending_customization_handlers(user))
33
+ end
34
+
35
+ def first_login_prompt?(user)
36
+ return false unless RegistrationFields.prompt_authorization_on_first_login?(current_organization)
37
+ return false unless eligible_first_sign_in?(user)
38
+ return false unless user.verifiable?
39
+ return false if current_organization.available_authorizations.blank?
40
+ return false if pending_redirect?(user)
41
+ return false if pending_onboarding_action?(user)
42
+
43
+ true
44
+ end
45
+
46
+ def eligible_first_sign_in?(user)
47
+ user.is_a?(Decidim::User) && first_verifiable_sign_in?(user) && !user.admin?
48
+ end
49
+
50
+ def first_login_authorization_path(handlers)
51
+ case handlers.size
52
+ when 1
53
+ decidim_verifications.new_authorization_path(handler: handlers.first, redirect_url: decidim.account_path)
54
+ when 2.. then decidim_verifications.authorizations_path
55
+ end
56
+ end
57
+
58
+ def pending_customization_handlers(user)
59
+ enabled = Verifications.enabled_customization_names(current_organization)
60
+ candidates = enabled.flat_map { |name| Verifications.workflow_handlers_for(name) }.uniq
61
+ org_enabled = current_organization.available_authorizations.map(&:to_s)
62
+ granted = Decidim::Verifications::Authorizations.new(organization: current_organization, user:).pluck(:name)
63
+
64
+ (candidates & org_enabled) - granted
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,23 +1,42 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/concern'
3
+ require "active_support/concern"
4
4
 
5
5
  module Decidim
6
6
  module CustomUserFields
7
- # Extra user fields definitions for forms
8
- module FormsDefinition
7
+ module FormDefinition
9
8
  extend ActiveSupport::Concern
10
9
 
11
- included do |inst|
10
+ included do
12
11
  include ::Decidim::CustomUserFields::ApplicationHelper
13
- Decidim::CustomUserFields.custom_fields.each do |field_def|
14
- field_def.configure_form(inst)
12
+
13
+ FormDefinition.setup_form_class(self)
14
+ end
15
+
16
+ class << self
17
+ def setup_form_class(form_class)
18
+ RegistrationFields.all_registration_fields.each do |field_def|
19
+ field_def.configure_form(form_class)
20
+ end
15
21
  end
16
22
  end
17
23
 
24
+ def active_custom_field_names
25
+ org = try(:current_organization)
26
+ return [] unless org
27
+
28
+ RegistrationFields.active_registration_fields(org).map(&:name)
29
+ end
30
+
18
31
  def map_model(model)
19
- extended_data = model.extended_data.with_indifferent_access
20
- field_def.map_model(self, model)
32
+ extended_data = (model.extended_data || {}).with_indifferent_access
33
+ org = try(:current_organization) || model.try(:organization)
34
+ active_names = org ? RegistrationFields.active_registration_fields(org).map(&:name) : []
35
+ RegistrationFields.all_registration_fields.each do |field_def|
36
+ next unless active_names.include?(field_def.name)
37
+
38
+ field_def.map_model(self, extended_data)
39
+ end
21
40
  end
22
41
  end
23
42
  end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module InvitationAcceptExtendedData
6
+ def accept_resource
7
+ custom_params = extract_custom_field_params
8
+ if (validation_errors = validate_custom_params(custom_params)).any?
9
+ return invalid_invitation_resource(validation_errors)
10
+ end
11
+
12
+ resource = super
13
+ return resource unless resource.valid? && resource.invitation_accepted?
14
+
15
+ persist_invitation_extended_data!(resource, custom_params)
16
+ resource
17
+ end
18
+
19
+ protected
20
+
21
+ def configure_permitted_parameters
22
+ devise_parameter_sanitizer.permit(
23
+ :accept_invitation,
24
+ keys: [:nickname, :password, :password_confirmation, :tos_agreement, :newsletter_notifications]
25
+ )
26
+ end
27
+
28
+ private
29
+
30
+ def extract_custom_field_params
31
+ return {} unless params[:user]
32
+
33
+ user_params = params[:user].to_unsafe_h.with_indifferent_access if params[:user].respond_to?(:to_unsafe_h)
34
+ user_params ||= params[:user].to_h.with_indifferent_access
35
+ custom_keys = RegistrationFields.all_registration_fields.map { |field| field.name.to_s }
36
+ extracted = user_params.slice(*custom_keys)
37
+ custom_keys.each do |key|
38
+ params[:user].delete(key) if params[:user].respond_to?(:delete)
39
+ params[:user].delete(key.to_sym) if params[:user].respond_to?(:delete)
40
+ end
41
+ extracted
42
+ end
43
+
44
+ def validate_custom_params(custom_params)
45
+ organization = invitation_organization
46
+ return ActiveModel::Errors.new(Object.new) if custom_params.blank? || organization.blank?
47
+
48
+ ExtendedData.validate_params(organization, custom_params)
49
+ end
50
+
51
+ def invalid_invitation_resource(errors)
52
+ resource = resource_class.find_by_invitation_token(invitation_token, true)
53
+ errors.each { |error| resource.errors.add(error.attribute, error.message) }
54
+ assign_invitation_form_with_errors!(resource, errors)
55
+ resource
56
+ end
57
+
58
+ def invitation_token
59
+ params.dig(:user, :invitation_token) || params[:invitation_token]
60
+ end
61
+
62
+ def invitation_organization
63
+ resource_class.find_by_invitation_token(invitation_token, true)&.organization
64
+ end
65
+
66
+ def persist_invitation_extended_data!(resource, custom_params)
67
+ success, errors = ExtendedData.merge_into(resource, resource.organization, custom_params)
68
+ return if success
69
+
70
+ errors.each do |error|
71
+ resource.errors.add(error.attribute, error.message)
72
+ end
73
+ assign_invitation_form_with_errors!(resource, errors)
74
+ end
75
+
76
+ def assign_invitation_form_with_errors!(resource, errors)
77
+ @form = invitation_registration_form(resource)
78
+ merge_invitation_params_into_form!(@form)
79
+ copy_errors_to_form!(@form, errors)
80
+ end
81
+
82
+ def invitation_registration_form(resource)
83
+ Decidim::RegistrationForm.from_model(resource).with_context(
84
+ current_organization: invitation_organization,
85
+ current_user: try(:current_user),
86
+ invitation_token:
87
+ )
88
+ end
89
+
90
+ def merge_invitation_params_into_form!(form)
91
+ return unless params[:user]
92
+
93
+ user_params = params[:user].to_unsafe_h.with_indifferent_access if params[:user].respond_to?(:to_unsafe_h)
94
+ user_params ||= params[:user].to_h.with_indifferent_access
95
+
96
+ user_params.each do |key, value|
97
+ setter = :"#{key}="
98
+ form.public_send(setter, value) if form.respond_to?(setter)
99
+ end
100
+ end
101
+
102
+ def copy_errors_to_form!(form, errors)
103
+ errors.each do |error|
104
+ form.errors.add(error.attribute, error.message)
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Decidim
6
+ module CustomUserFields
7
+ module OmniauthCommand
8
+ extend ActiveSupport::Concern
9
+
10
+ private
11
+
12
+ def create_or_find_user
13
+ super
14
+ persist_omniauth_extended_data!
15
+ end
16
+
17
+ def persist_omniauth_extended_data!
18
+ return unless @user
19
+
20
+ params = omniauth_extended_data
21
+ return if params.blank?
22
+
23
+ success, errors = ExtendedData.merge_into(@user, form.current_organization, params)
24
+ return if success
25
+
26
+ errors.each { |error| @user.errors.add(error.attribute, error.message) }
27
+ raise ActiveRecord::RecordInvalid, @user
28
+ end
29
+
30
+ def omniauth_extended_data
31
+ org = form.try(:current_organization)
32
+ custom_data = {}
33
+ RegistrationFields.active_registration_fields(org).each do |field_def|
34
+ custom_data[field_def.name] = form[field_def.name]
35
+ end
36
+ custom_data
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Decidim
6
+ module CustomUserFields
7
+ module OmniauthRegistrationsController
8
+ extend ActiveSupport::Concern
9
+
10
+ private
11
+
12
+ def verified_email
13
+ @verified_email ||= begin
14
+ email = oauth_verified_email
15
+ email ||= trusted_form_email_for_openid_connect
16
+ session[:verified_email] = email if email.present?
17
+ email.presence || session[:verified_email]
18
+ end
19
+ end
20
+
21
+ def user_params_from_oauth_hash
22
+ return nil if oauth_data.empty?
23
+
24
+ uid = resolve_omniauth_uid
25
+ return nil if uid.blank?
26
+
27
+ info = oauth_info_with_indifferent_access
28
+
29
+ {
30
+ provider: oauth_data[:provider],
31
+ uid:,
32
+ name: info[:name],
33
+ nickname: info[:nickname],
34
+ oauth_signature: OmniauthRegistrationForm.create_signature(oauth_data[:provider], uid),
35
+ avatar_url: info[:image],
36
+ raw_data: oauth_hash
37
+ }
38
+ end
39
+
40
+ def oauth_verified_email
41
+ info = oauth_info_with_indifferent_access
42
+ email = info[:email].presence || raw_omniauth_info[:email].presence
43
+ return if email.blank?
44
+ return email if openid_connect_provider?
45
+ return email unless info.has_key?(:email_verified)
46
+
47
+ ActiveModel::Type::Boolean.new.cast(info[:email_verified]) ? email : nil
48
+ end
49
+
50
+ def trusted_form_email_for_openid_connect
51
+ return unless Rails.env.development?
52
+ return unless openid_connect_registration?
53
+
54
+ @form&.email.presence || params.dig(:user, :email).presence
55
+ end
56
+
57
+ def openid_connect_registration?
58
+ openid_connect_provider? && oauth_identity_present?
59
+ end
60
+
61
+ def openid_connect_provider?
62
+ provider = oauth_data[:provider].presence || params.dig(:user, :provider)
63
+ provider.to_s == "openid_connect"
64
+ end
65
+
66
+ def oauth_identity_present?
67
+ resolve_omniauth_uid.present?
68
+ end
69
+
70
+ def resolve_omniauth_uid
71
+ uid = oauth_data[:uid]
72
+ return uid.to_s if uid.present?
73
+
74
+ raw = raw_omniauth_info
75
+ raw[:sub].presence || raw[:id].presence || raw[:uid].presence || raw[:nickname].presence ||
76
+ oauth_info_with_indifferent_access[:nickname].presence ||
77
+ oauth_info_with_indifferent_access[:email].presence
78
+ end
79
+
80
+ def oauth_info_with_indifferent_access
81
+ info = oauth_data[:info] || {}
82
+ info = info.with_indifferent_access if info.respond_to?(:with_indifferent_access)
83
+ info
84
+ end
85
+
86
+ def raw_omniauth_info
87
+ raw = oauth_hash.dig(:extra, :raw_info) || {}
88
+ raw = raw.with_indifferent_access if raw.respond_to?(:with_indifferent_access)
89
+ raw
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Decidim
6
+ module CustomUserFields
7
+ module UserExtendedDataGuard
8
+ extend ActiveSupport::Concern
9
+
10
+ # Make User::exphemeral nil-safe, required after bringing ephemeral users into core (see https://github.com/decidim/decidim/pull/13579)
11
+ def ephemeral?
12
+ (extended_data || {})["ephemeral"]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module RegistrationFields
6
+ RESERVED_EXTENDED_DATA_KEYS = %w(
7
+ nickname
8
+ name
9
+ personal_url
10
+ about
11
+ extended_data
12
+ newsletter_notifications_at
13
+ accepted_tos_version
14
+ locale
15
+ newsletter_notifications
16
+ managed
17
+ deleted_at
18
+ blocked
19
+ confirmed_at
20
+ officialized_at
21
+ ).freeze
22
+
23
+ FIRST_LOGIN_MODES = %w(prompt_authorization none).freeze
24
+ DEFAULT_FIRST_LOGIN_MODE = "prompt_authorization"
25
+
26
+ class << self
27
+ def reserved_key?(name)
28
+ RESERVED_EXTENDED_DATA_KEYS.include?(name.to_s)
29
+ end
30
+
31
+ def first_login_mode(organization)
32
+ value = raw_toggle_config(organization)[:first_login_mode].presence
33
+ return DEFAULT_FIRST_LOGIN_MODE if value.blank? || FIRST_LOGIN_MODES.exclude?(value.to_s)
34
+
35
+ value.to_s
36
+ end
37
+
38
+ def prompt_authorization_on_first_login?(organization)
39
+ first_login_mode(organization) == DEFAULT_FIRST_LOGIN_MODE
40
+ end
41
+
42
+ def enabled_customization_names(organization)
43
+ config = raw_toggle_config(organization)
44
+ from_enabled_customization_key(config) || from_legacy_enabled_flags(config)
45
+ end
46
+
47
+ def active_registration_fields(organization)
48
+ enabled_customization_names(organization).flat_map do |name|
49
+ Customizations.find(name)&.fields || []
50
+ end.uniq(&:name)
51
+ end
52
+
53
+ def all_registration_fields
54
+ Customizations.all.flat_map(&:fields).uniq(&:name)
55
+ end
56
+
57
+ private
58
+
59
+ def raw_toggle_config(organization)
60
+ Decidim::Toggle::OrganizationModuleConfig.find_by(
61
+ decidim_organization_id: organization.id,
62
+ module_name: "custom_user_fields"
63
+ )&.config&.with_indifferent_access || {}
64
+ end
65
+
66
+ def from_enabled_customization_key(config)
67
+ return unless config.has_key?(:enabled_customization)
68
+
69
+ name = config[:enabled_customization].to_s
70
+ Customizations.find(name) ? [name] : []
71
+ end
72
+
73
+ def from_legacy_enabled_flags(config)
74
+ enabled = enabled_from_customization_flags(config)
75
+ return enabled.first(1) if enabled.any?
76
+ return [] if legacy_config_superseded?(config)
77
+
78
+ Array(config[:active_field_set].presence&.to_s)
79
+ end
80
+
81
+ def customization_enabled?(config, name)
82
+ ActiveModel::Type::Boolean.new.cast(
83
+ config[:"#{name}_enabled"] || config["#{name}_enabled"]
84
+ )
85
+ end
86
+
87
+ def enabled_from_customization_flags(config)
88
+ Customizations.all.filter_map do |customization|
89
+ customization.name.to_s if customization_enabled?(config, customization.name)
90
+ end
91
+ end
92
+
93
+ def legacy_config_superseded?(config)
94
+ config.keys.any? { |key| key.to_s.end_with?("_enabled") }
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ class UpdateCustomizationsConfigCommand < Decidim::Command
6
+ def initialize(organization, form)
7
+ @organization = organization
8
+ @form = form
9
+ end
10
+
11
+ def call
12
+ return broadcast(:invalid) if form.class.module_config_name.blank?
13
+ return broadcast(:invalid) if form.invalid?
14
+
15
+ persist_and_prune!
16
+ broadcast(:ok)
17
+ rescue ActiveRecord::RecordInvalid
18
+ broadcast(:invalid)
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :organization, :form
24
+
25
+ def persist_and_prune!
26
+ Decidim::Toggle.save_config!(organization, form.class.module_config_name, form.to_h)
27
+ prune_disabled_authorization_handlers!
28
+ end
29
+
30
+ def prune_disabled_authorization_handlers!
31
+ current = organization.read_attribute(:available_authorizations)
32
+ pruned = Verifications.prune_disabled_handlers(current, organization)
33
+ return if pruned == current
34
+
35
+ organization.update!(available_authorizations: pruned)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module Upgrade
6
+ # One-time migration: decidim-toggle config `active_field_set` → `enabled_customization`.
7
+ class MigrateToggleConfig
8
+ MODULE_NAME = "custom_user_fields"
9
+ LEGACY_KEY = "active_field_set"
10
+
11
+ class << self
12
+ def run(dry_run: false)
13
+ new(dry_run:).run
14
+ end
15
+ end
16
+
17
+ def initialize(dry_run: false)
18
+ @dry_run = dry_run
19
+ @migrated = 0
20
+ @skipped = 0
21
+ end
22
+
23
+ def run
24
+ scope.find_each { |record| migrate_record!(record) }
25
+ summary
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :dry_run, :migrated, :skipped
31
+
32
+ def summary
33
+ { migrated:, skipped: }
34
+ end
35
+
36
+ def scope
37
+ Decidim::Toggle::OrganizationModuleConfig.where(module_name: MODULE_NAME)
38
+ end
39
+
40
+ def migrate_record!(record)
41
+ config = (record.config || {}).with_indifferent_access
42
+ legacy_name = config[LEGACY_KEY]
43
+ return increment_skipped if legacy_name.blank?
44
+
45
+ persist_migrated!(record, config, legacy_name)
46
+ @migrated += 1
47
+ end
48
+
49
+ def increment_skipped
50
+ @skipped += 1
51
+ end
52
+
53
+ def persist_migrated!(record, config, legacy_name)
54
+ next_config = config.except(LEGACY_KEY)
55
+ next_config[:enabled_customization] ||= legacy_name.to_s
56
+ return log(record, legacy_name) if dry_run
57
+
58
+ record.update!(config: stringify_config(next_config))
59
+ end
60
+
61
+ def stringify_config(config)
62
+ config.to_h.stringify_keys
63
+ end
64
+
65
+ def log(record, legacy_name)
66
+ Rails.logger.debug do
67
+ "[dry-run] org #{record.decidim_organization_id}: " \
68
+ "#{LEGACY_KEY}=#{legacy_name} → enabled_customization=#{legacy_name}"
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module Verifications
6
+ class Builder
7
+ include DefinitionMissing
8
+
9
+ DSL_METHODS = [
10
+ :add_field,
11
+ :renewable!,
12
+ :renewable?,
13
+ :ephemerable!,
14
+ :ephemerable?,
15
+ :register_workflow!
16
+ ].freeze
17
+
18
+ attr_reader :name, :customization
19
+ attr_accessor :fields, :renewable, :time_between_renewals
20
+
21
+ def initialize(name, customization: nil)
22
+ @name = name.to_s
23
+ @customization = customization&.to_sym
24
+ @fields = []
25
+ @ephemerable = false
26
+ @renewable = false
27
+ @time_between_renewals = nil
28
+ end
29
+
30
+ def renewable!(time_between_renewals = 1.day)
31
+ @renewable = true
32
+ @time_between_renewals = time_between_renewals
33
+ end
34
+
35
+ def renewable?
36
+ @renewable
37
+ end
38
+
39
+ def add_field(field_name, field_definition)
40
+ field_def = FieldDefinition.new(field_name, field_definition, handler_name)
41
+ field_def.field.customization_name = @customization if field_def.type == :extra_field_ref
42
+ fields.push(field_def)
43
+ end
44
+
45
+ def ephemerable!
46
+ @ephemerable = true
47
+ end
48
+
49
+ def ephemerable?
50
+ @ephemerable
51
+ end
52
+
53
+ def handler_name
54
+ klass_name.underscore
55
+ end
56
+
57
+ def klass_name
58
+ name.camelize.to_s
59
+ end
60
+
61
+ def register_workflow!
62
+ validate_extra_field_ref_requirements!
63
+ return if workflow_already_registered?
64
+
65
+ Decidim::Verifications.register_workflow(handler_name.to_sym) do |workflow|
66
+ workflow.form = "Decidim::CustomUserFields::Verifications::#{klass_name}"
67
+ klass = Decidim::CustomUserFields::Verifications.create_verification_class(klass_name)
68
+ workflow.ephemerable = ephemerable? if workflow.respond_to?(:ephemerable=)
69
+ workflow.renewable = renewable? if workflow.respond_to?(:renewable=)
70
+ workflow.time_between_renewals = time_between_renewals unless time_between_renewals.nil?
71
+ workflow.metadata_cell = "decidim/verifications/authorization_metadata"
72
+
73
+ klass.decidim_custom_fields = fields
74
+ fields.map do |field|
75
+ field.configure_form(klass)
76
+ end
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ def workflow_already_registered?
83
+ Decidim::Verifications.find_workflow_manifest(handler_name).present?
84
+ end
85
+
86
+ def validate_extra_field_ref_requirements!
87
+ uses_extra_field_ref = fields.any? { |field| field.type == :extra_field_ref }
88
+ if uses_extra_field_ref && customization.nil?
89
+ raise Decidim::CustomUserFields::Error,
90
+ "authorization #{name} must be registered inside a customization when using extra_field_ref fields"
91
+ end
92
+
93
+ fields.each do |field|
94
+ field.field.validate_customization_reference! if field.type == :extra_field_ref
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end