decidim-user_fields 0.0.2 → 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 (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +308 -49
  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/devise/invitations/edit/extra_user_fields_form.html.erb.deface +2 -2
  7. data/app/overrides/decidim/devise/omniauth_registrations/new/extra_user_fields.html.erb.deface +3 -0
  8. data/app/overrides/invitation_override.rb +31 -0
  9. data/app/packs/entrypoints/decidim_custom_user_fields.js +0 -0
  10. data/app/packs/stylesheets/decidim/custom_user_fields/_decidim_custom_user_fields.scss +48 -0
  11. data/app/views/decidim/custom_user_fields/_profile_form.html.erb +2 -2
  12. data/app/views/decidim/custom_user_fields/_registration_form.html.erb +2 -2
  13. data/app/views/decidim/custom_user_fields/_verification_form.erb +4 -0
  14. data/app/views/decidim/custom_user_fields/system/_customizations_tab.html.erb +15 -0
  15. data/config/assets.rb +9 -0
  16. data/config/i18n-tasks.yml +22 -0
  17. data/config/locales/decidim_custom_user_fields_scenarios.en.yml +50 -0
  18. data/config/locales/decidim_custom_user_fields_scenarios.fr.yml +48 -0
  19. data/config/locales/decidim_toggle.en.yml +13 -0
  20. data/config/locales/decidim_toggle.fr.yml +13 -0
  21. data/config/locales/decidim_user_fields.en.yml +25 -0
  22. data/config/locales/decidim_user_fields.fr.yml +25 -0
  23. data/lib/decidim/custom_user_fields/custom_user_fields.rb +10 -14
  24. data/lib/decidim/custom_user_fields/customization_field_naming.rb +17 -0
  25. data/lib/decidim/custom_user_fields/customizations/builder.rb +48 -0
  26. data/lib/decidim/custom_user_fields/customizations/customization.rb +29 -0
  27. data/lib/decidim/custom_user_fields/customizations.rb +42 -0
  28. data/lib/decidim/custom_user_fields/decidim_integrations.rb +52 -0
  29. data/lib/decidim/custom_user_fields/definition_guidance.rb +109 -0
  30. data/lib/decidim/custom_user_fields/dev/bootstrap.rb +102 -0
  31. data/lib/decidim/custom_user_fields/dev/local_oidc_seeder.rb +150 -0
  32. data/lib/decidim/custom_user_fields/dev/omniauth_providers_patcher.rb +68 -0
  33. data/lib/decidim/custom_user_fields/dev/openid_connect_setup.rb +110 -0
  34. data/lib/decidim/custom_user_fields/dev/scenario_customizations.rb +72 -0
  35. data/lib/decidim/custom_user_fields/engine.rb +59 -26
  36. data/lib/decidim/custom_user_fields/extended_data.rb +52 -0
  37. data/lib/decidim/custom_user_fields/field_definition.rb +28 -6
  38. data/lib/decidim/custom_user_fields/fields/boolean_field.rb +51 -0
  39. data/lib/decidim/custom_user_fields/fields/date_field.rb +80 -10
  40. data/lib/decidim/custom_user_fields/fields/dummy_field.rb +40 -0
  41. data/lib/decidim/custom_user_fields/fields/extra_field_ref_field.rb +118 -0
  42. data/lib/decidim/custom_user_fields/fields/generic_field.rb +99 -6
  43. data/lib/decidim/custom_user_fields/fields/text_area_field.rb +34 -9
  44. data/lib/decidim/custom_user_fields/fields/text_field.rb +47 -6
  45. data/lib/decidim/custom_user_fields/i18n/customization_keys.rb +55 -0
  46. data/lib/decidim/custom_user_fields/i18n/customization_keys_scanner.rb +80 -0
  47. data/lib/decidim/custom_user_fields/i18n/tasks.rb +7 -0
  48. data/lib/decidim/custom_user_fields/overrides/authorizations_controller.rb +18 -0
  49. data/lib/decidim/custom_user_fields/overrides/command.rb +23 -21
  50. data/lib/decidim/custom_user_fields/overrides/confirmations_controller.rb +15 -0
  51. data/lib/decidim/custom_user_fields/overrides/devise_authentication_methods.rb +68 -0
  52. data/lib/decidim/custom_user_fields/overrides/form_definition.rb +25 -8
  53. data/lib/decidim/custom_user_fields/overrides/invitations_controller.rb +109 -0
  54. data/lib/decidim/custom_user_fields/overrides/omniauth_command.rb +40 -0
  55. data/lib/decidim/custom_user_fields/overrides/omniauth_registrations_controller.rb +93 -0
  56. data/lib/decidim/custom_user_fields/overrides/user.rb +16 -0
  57. data/lib/decidim/custom_user_fields/registration_fields.rb +99 -0
  58. data/lib/decidim/custom_user_fields/update_customizations_config_command.rb +39 -0
  59. data/lib/decidim/custom_user_fields/upgrade/migrate_toggle_config.rb +74 -0
  60. data/lib/decidim/custom_user_fields/verifications/builder.rb +100 -0
  61. data/lib/decidim/custom_user_fields/verifications/verifications.rb +82 -0
  62. data/lib/decidim/custom_user_fields/version.rb +4 -2
  63. data/lib/decidim/user_fields.rb +46 -13
  64. data/lib/tasks/decidim/custom_user_fields/custom_user_fields_upgrade.rake +16 -0
  65. data/lib/tasks/decidim/custom_user_fields/custom_user_fields_webpacker.rake +59 -0
  66. data/lib/tasks/decidim/custom_user_fields/decidim_custom_user_fields_upgrade_tasks.rake +16 -0
  67. data/lib/tasks/decidim/custom_user_fields/dev/local_setup.rake +16 -0
  68. metadata +80 -15
  69. data/app/overrides/decidim/devise/invitations/edit/extra_user_fields.html.erb.deface +0 -3
@@ -1,38 +1,131 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Decidim
2
4
  module CustomUserFields
3
5
  module Fields
4
6
  class GenericField
5
- extend Forwardable
6
7
  include ActionView::Helpers::TagHelper
7
8
 
8
- def_delegators :@definition, :name, :type
9
+ delegate :name, :type, :handler_name, to: :definition
10
+
11
+ attr_reader :definition
12
+ attr_accessor :options
9
13
 
10
- attr_reader :definition, :options
11
14
  def initialize(definition, options)
12
15
  @definition = definition
13
16
  @options = options
14
17
  end
15
18
 
19
+ def validate(_value, _data, _errors); end
20
+
21
+ def sanitized_value(raw_value)
22
+ raw_value
23
+ end
24
+
25
+ def skip_hashing?
26
+ options[:skip_hashing].present?
27
+ end
28
+
29
+ # Key used in user.extended_data (overridden by ExtraFieldRefField).
30
+ def storage_name
31
+ name
32
+ end
33
+
34
+ def ui_options
35
+ options[:ui] || {}
36
+ end
37
+
16
38
  def configure_form(_form)
17
39
  raise Error, "Configure Form is not implemented for #{definition.type} type"
18
40
  end
19
41
 
42
+ def configure_settings(_settings)
43
+ raise Error, "Configure Settings is not implemented for #{definition.type} type"
44
+ end
45
+
20
46
  def map_model(_form, _model_data)
21
47
  raise Error, "Map Model is not implemented for #{definition.type} type"
22
48
  end
23
49
 
24
- def form_tag(_form)
50
+ def form_tag(_form, _custom_label = nil)
25
51
  raise Error, "Form Tag is not implemented for #{definition.type} type"
26
52
  end
27
53
 
28
54
  def class_name
29
- class_specifier = name.to_s.underscore
30
- "field field--#{type} field--#{class_specifier}"
55
+ @class_name ||= [
56
+ "field",
57
+ class_modifer(type),
58
+ class_modifer(name.to_s.underscore)
59
+ ].join(" ")
60
+ end
61
+
62
+ def label_class_name
63
+ @label_class_name ||= [
64
+ "field_label",
65
+ class_modifer(type, "field_label"),
66
+ class_modifer(name.to_s.underscore, "field_label")
67
+ ].join(" ")
68
+ end
69
+
70
+ def class_modifer(modifier, block = "field")
71
+ "#{block}--#{modifier}"
72
+ end
73
+
74
+ def label_exists?(label)
75
+ ::I18n.exists?(i18n_handler_label(label))
76
+ end
77
+
78
+ def label(label)
79
+ i18n_identifier = i18n_handler_label(label)
80
+ unless ::I18n.exists?(i18n_identifier)
81
+ Rails.logger.error("Missing #{i18n_handler_label(label)}")
82
+ return i18n_identifier
83
+ end
84
+
85
+ ::I18n.t(
86
+ i18n_identifier,
87
+ default: i18n_identifier
88
+ )
31
89
  end
32
90
 
33
91
  def required?
34
92
  options[:required].present? && options[:required]
35
93
  end
94
+
95
+ def apply_form_validations(form, validations)
96
+ validations = validations.dup
97
+ if validations.has_key?(:presence)
98
+ validations.delete(:presence) if validations[:presence] == false
99
+ elsif required?
100
+ validations[:presence] = true
101
+ end
102
+ return if validations.blank?
103
+
104
+ field_name = name
105
+ validation_options = validations.dup
106
+ if registration_form?(form)
107
+ validation_options[:if] = lambda { |record|
108
+ record.active_custom_field_names.include?(field_name)
109
+ }
110
+ end
111
+ form.validates(name, **validation_options)
112
+ end
113
+
114
+ def registration_form?(form)
115
+ form.included_modules.include?(Decidim::CustomUserFields::FormDefinition)
116
+ end
117
+
118
+ def i18n_context
119
+ @i18n_context ||= "decidim.custom_user_fields.#{handler_name}"
120
+ end
121
+
122
+ attr_writer :i18n_context
123
+
124
+ private
125
+
126
+ def i18n_handler_label(label)
127
+ "#{i18n_context}.#{name}.#{label}"
128
+ end
36
129
  end
37
130
  end
38
131
  end
@@ -1,16 +1,41 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Decidim
2
4
  module CustomUserFields
3
5
  module Fields
4
6
  class TextAreaField < GenericField
5
7
  def configure_form(form)
6
8
  form.attribute(name, String)
7
- form.validates(name, presence: required?)
9
+ validations = {
10
+ presence: required? && {
11
+ message: label(:required)
12
+ }
13
+ }
8
14
  if options[:min].present? || options[:max].present?
9
- min_max_options = {}
15
+ min_max_options = {
16
+ wrong_length: proc do
17
+ label(:bad_length)
18
+ end,
19
+ too_long: proc do
20
+ label(:too_long)
21
+ end,
22
+ too_short: proc do
23
+ label(:too_short)
24
+ end
25
+ }
10
26
  min_max_options[:minimum] = options[:min].to_i if options[:min].present?
11
27
  min_max_options[:maximum] = options[:max].to_i if options[:max].present?
12
- form.validates(name, length: min_max_options, allow_blank: !required?)
28
+ validations[:length] = min_max_options
29
+ validations[:allow_blank] = !required?
13
30
  end
31
+ apply_form_validations(form, validations)
32
+ end
33
+
34
+ def sanitized_value(value)
35
+ stripped_value = (value || "").strip
36
+ return stripped_value if stripped_value.present?
37
+
38
+ nil
14
39
  end
15
40
 
16
41
  def map_model(form, data)
@@ -19,13 +44,13 @@ module Decidim
19
44
 
20
45
  def form_tag(form_tag)
21
46
  field_options = {
22
- rows: options[:row] || 2
23
- }
24
- content_tag(
25
- :div,
26
- form_tag.text_area(name, **field_options),
47
+ rows: ui_options[:row] || 2,
48
+ label: label(:label),
49
+ help_text: label_exists?(:help_text) && label(:help_text),
50
+ label_options: { class: label_class_name },
27
51
  class: class_name
28
- )
52
+ }
53
+ form_tag.text_area(name, **field_options)
29
54
  end
30
55
  end
31
56
  end
@@ -1,21 +1,62 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Decidim
2
4
  module CustomUserFields
3
5
  module Fields
4
6
  class TextField < GenericField
5
7
  def configure_form(form)
6
8
  form.attribute(name, String)
7
- form.validates(name, presence: required?)
9
+ validations = {}
10
+ validations[:presence] = true if required?
11
+ if options[:values_in]
12
+ validations[:inclusion] = {
13
+ in: options[:values_in],
14
+ message: proc do |_, _|
15
+ label(:bad_values)
16
+ end
17
+ }
18
+ end
19
+
20
+ if options[:format]
21
+ validations[:format] = {
22
+ with: options[:format],
23
+ message: proc do |_, _|
24
+ label(:bad_format)
25
+ end
26
+ }
27
+ end
28
+
29
+ apply_form_validations(form, validations)
8
30
  end
9
31
 
10
32
  def map_model(form, data)
11
- form[name] = data[name] if data[name].present?
33
+ form[name] = data[name].strip if data[name].present?
34
+ end
35
+
36
+ def validate(value, _data, errors)
37
+ if required? && value.blank?
38
+ errors.add(name, label(:required))
39
+ elsif options[:values_in] && value.present? && options[:values_in].exclude?(value)
40
+ errors.add(name, label(:bad_values))
41
+ elsif options[:format] && value.present? && value !~ options[:format]
42
+ errors.add(name, label(:bad_format))
43
+ end
44
+ end
45
+
46
+ def sanitized_value(value)
47
+ stripped_value = (value || "").strip
48
+ return stripped_value if stripped_value.present?
49
+
50
+ nil
12
51
  end
13
52
 
14
53
  def form_tag(form_tag)
15
- content_tag(
16
- :div,
17
- form_tag.text_field(name),
18
- class: class_name
54
+ form_tag.text_field(
55
+ name,
56
+ label: label(:label),
57
+ help_text: label_exists?(:help_text) && label(:help_text),
58
+ class: class_name,
59
+ label_options: { class: label_class_name }
19
60
  )
20
61
  end
21
62
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module I18n
6
+ # Required translation keys derived from {Customizations} registry.
7
+ # Used by RSpec and the i18n-tasks custom scanner.
8
+ module CustomizationKeys
9
+ module_function
10
+
11
+ def required_keys
12
+ Decidim::CustomUserFields::Customizations.all.flat_map do |customization|
13
+ keys_for(customization)
14
+ end.uniq.sort
15
+ end
16
+
17
+ def missing(locale: :en)
18
+ required_keys.reject { |key| ::I18n.exists?(key, locale) }
19
+ end
20
+
21
+ def keys_for(customization)
22
+ name = customization.name
23
+ keys = [
24
+ "decidim_toggle.system.custom_user_fields.#{name}_enabled",
25
+ "decidim.custom_user_fields.customizations.#{name}"
26
+ ]
27
+
28
+ customization.fields.each do |field|
29
+ keys << "decidim.custom_user_fields.extended_data.#{field.name}.label"
30
+ end
31
+
32
+ customization.workflow_handlers.each do |handler|
33
+ keys << "decidim.authorization_handlers.#{handler}.name"
34
+ keys << "decidim.authorization_handlers.#{handler}.explanation"
35
+ auth_fields_for(handler).each do |field|
36
+ keys << "decidim.custom_user_fields.#{handler}.#{field.name}.label"
37
+ end
38
+ end
39
+
40
+ keys
41
+ end
42
+
43
+ def auth_fields_for(handler)
44
+ klass_name = handler.to_s.camelize
45
+ return [] unless Decidim::CustomUserFields::Verifications.const_defined?(klass_name, false)
46
+
47
+ klass = Decidim::CustomUserFields::Verifications.const_get(klass_name, false)
48
+ Array(klass.try(:decidim_custom_fields))
49
+ rescue NameError
50
+ []
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "i18n/tasks"
4
+ require "i18n/tasks/scanners/scanner"
5
+ require "i18n/tasks/scanners/results/key_occurrences"
6
+ require "i18n/tasks/scanners/results/occurrence"
7
+ require "decidim/custom_user_fields/i18n/customization_keys"
8
+
9
+ module Decidim
10
+ module CustomUserFields
11
+ module I18n
12
+ # Reports {CustomizationKeys.required_keys} as used for i18n-tasks.
13
+ class CustomizationKeysScanner < ::I18n::Tasks::Scanners::Scanner
14
+ def initialize(config: {}, **_kwargs)
15
+ super()
16
+ @config = config
17
+ @registry_ready = false
18
+ end
19
+
20
+ def keys
21
+ ensure_registry_loaded!
22
+ return [] unless @registry_ready
23
+
24
+ CustomizationKeys.required_keys.map do |key|
25
+ ::I18n::Tasks::Scanners::Results::KeyOccurrences.new(
26
+ key:,
27
+ occurrences: [synthetic_occurrence(key)]
28
+ )
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :config
35
+
36
+ def ensure_registry_loaded!
37
+ if defined?(::Rails) && ::Rails.application &&
38
+ defined?(Decidim::CustomUserFields::Customizations)
39
+ @registry_ready = true
40
+ return
41
+ end
42
+
43
+ gem_root = ENV.fetch("ENGINE_ROOT") do
44
+ File.expand_path("../../../..", __dir__)
45
+ end
46
+ environment = File.join(gem_root, "spec/decidim_dummy_app/config/environment.rb")
47
+ unless File.exist?(environment)
48
+ warn(
49
+ "[decidim-user_fields] CustomizationKeysScanner: could not load " \
50
+ "#{environment}; registry keys skipped."
51
+ )
52
+ @registry_ready = false
53
+ return
54
+ end
55
+
56
+ require environment
57
+ Decidim::CustomUserFields::ScenarioCustomizations.register! if defined?(Decidim::CustomUserFields::ScenarioCustomizations)
58
+ @registry_ready = defined?(Decidim::CustomUserFields::Customizations)
59
+ rescue LoadError, StandardError => e
60
+ warn(
61
+ "[decidim-user_fields] CustomizationKeysScanner: failed to boot Rails " \
62
+ "(#{e.class}: #{e.message}); registry keys skipped."
63
+ )
64
+ @registry_ready = false
65
+ end
66
+
67
+ def synthetic_occurrence(key)
68
+ ::I18n::Tasks::Scanners::Results::Occurrence.new(
69
+ path: "lib/decidim/custom_user_fields/i18n/customization_keys.rb",
70
+ pos: 0,
71
+ line_num: 1,
72
+ line_pos: 1,
73
+ line: "# registry:#{key}",
74
+ raw_key: key
75
+ )
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Host apps: add to config/i18n-tasks.yml —
4
+ # <% require "decidim/custom_user_fields/i18n/tasks" %>
5
+ require "decidim/custom_user_fields/i18n/customization_keys_scanner"
6
+
7
+ I18n::Tasks.add_scanner "Decidim::CustomUserFields::I18n::CustomizationKeysScanner"
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module AuthorizationsController
6
+ def onboarding_pending
7
+ return super if pending_onboarding_action?(current_user)
8
+
9
+ unless RegistrationFields.prompt_authorization_on_first_login?(current_organization)
10
+ redirect_to decidim.account_path
11
+ return
12
+ end
13
+
14
+ super
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,6 @@
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
@@ -12,37 +12,39 @@ module Decidim
12
12
 
13
13
  def create_user
14
14
  user_payload = {
15
- email: @form.email,
16
- name: @form.name,
17
- nickname: @form.nickname,
18
- password: @form.password,
19
- password_confirmation: @form.password_confirmation,
20
- organization: @form.current_organization,
21
- tos_agreement: @form.tos_agreement,
22
- newsletter_notifications_at: @form.newsletter_at,
23
- email_on_notification: Decidim::CustomUserFields.default_email_on_notification,
24
- accepted_tos_version: @form.current_organization.tos_version,
25
- locale: @form.current_locale,
26
- extended_data: extended_data
15
+ email: form.email,
16
+ name: form.name,
17
+ nickname: form.nickname,
18
+ password: form.password,
19
+ password_updated_at: Time.current,
20
+ organization: form.current_organization,
21
+ tos_agreement: form.tos_agreement,
22
+ newsletter_notifications_at: form.newsletter_at,
23
+ accepted_tos_version: form.current_organization.tos_version,
24
+ locale: form.current_locale,
25
+ extended_data:
27
26
  }
28
27
  @user = User.create!(user_payload)
29
28
  end
30
29
 
31
30
  def update_personal_data
32
- @user.name = @form.name
33
- @user.nickname = @form.nickname
34
- @user.email = @form.email
35
- @user.personal_url = @form.personal_url
36
- @user.about = @form.about
37
- @user.extended_data = extended_data
31
+ current_user.locale = @form.locale
32
+ current_user.name = @form.name
33
+ current_user.nickname = @form.nickname
34
+ current_user.email = @form.email
35
+ current_user.personal_url = @form.personal_url
36
+ current_user.about = @form.about
37
+ current_user.extended_data = extended_data
38
38
  end
39
39
 
40
40
  def extended_data
41
+ org = @form.try(:current_organization)
41
42
  custom_data = {}
42
- Decidim::CustomUserFields.custom_fields.each do |field_def|
43
+ RegistrationFields.active_registration_fields(org).each do |field_def|
43
44
  custom_data[field_def.name] = @form[field_def.name]
44
45
  end
45
- @extended_data ||= (@user&.extended_data || {}).merge(custom_data)
46
+ source_user = try(:current_user) || @user
47
+ @extended_data ||= (source_user&.extended_data || {}).merge(custom_data)
46
48
  end
47
49
  end
48
50
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ module CustomUserFields
5
+ module ConfirmationsController
6
+ def after_confirmation_path_for(resource_name, resource)
7
+ sign_in(resource)
8
+ store_onboarding_cookie_data!(resource)
9
+ return decidim_verifications.onboarding_pending_authorizations_path if pending_onboarding_action?(resource)
10
+
11
+ prompt_first_login_path(resource) || super
12
+ end
13
+ end
14
+ end
15
+ end
@@ -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,40 @@
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
- Decidim::CustomUserFields.custom_fields.each do |field_def|
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
+
21
38
  field_def.map_model(self, extended_data)
22
39
  end
23
40
  end